var sliderIndex = 0;
var sliderCount = 0;
var slideTime = 5000;
$(document).ready(
		function() {
			// init the array.

			var sUrl = window.location.protocol + '//' + sBasehref + '/';
			$('#slider').html('').css( {
				position : 'relative',
				width : '325px',
				height : '325px',
				float : 'right',
				marginBottom : '25px'
			});

			$.getJSON(sUrl + 'getslides.php', function(data) {
				/**
				 * Create html objects.
				 */
				$.each(data.data, function(i, obj) {
					var img = document.createElement('img');
					$(img).attr('src', sUrl + 'upload/slider/' + obj.slider_img)
							.attr('alt', obj.slider_title)
							.attr('width', '325')
							.attr('height', '325');

					/**
					 * Display first item
					 */
					if (i != 0) {
						$(img).css( {
							opacity : 0.0
						});
					}
					var anchor = document.createElement('a');
					
					if (obj.slider_url != null) {
						$(anchor).attr('href', obj.slider_url);
					}
					
					$(anchor).css( {
						position : 'absolute'
					});
					
					if (i != 0) {
						$(anchor).css( {
							display : 'none'
						});
					}
					
					$(img).mouseover(function(){
						window.status = obj.slider_title;
					});
					
					anchor.appendChild(img);

					$('#slider').get(0).appendChild(anchor);
					sliderCount++;
				});
				setInterval('slide()', slideTime);
			});
		});

function slide() {
	// Set the opacity of all images to 0
	// $('#slider a').css({opacity: 0.0});
	// Get the first image and display it (set it to full opacity)
	// $('#slider a:first').css({opacity: 1.0});
	var history = sliderIndex;
	$('#slider img:eq(' + sliderIndex + ')').animate( {
		opacity : 0.0
	}, 600, function() {
		$('#slider a:eq(' + history + ')').css( {
			display : 'none'
		});
	});
	var nextIndex = (sliderIndex + 1);
	if (nextIndex == sliderCount) {
		sliderIndex = -1;
		nextIndex = 0;
	}
	$('#slider a:eq(' + nextIndex + ')').css( {
		display : 'block'
	})
	$('#slider img:eq(' + nextIndex + ')').animate( {
		opacity : 1.0
	}, 600);

	sliderIndex++;
}

