$(document).ready(function(){	
	preloadImages();
	var liWidth = 0;
	var current = 0;

	$("#slider ul li").each(function(){		
		var width = $(this).find('img').attr("width");
		liWidth += parseInt( width ) + parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
	});

	$("#slider ul").css("width", liWidth);

	$("#slider ul li").hover( function(){
		$(this).animate({opacity: 0.4}, 500);
		$("#slider").stop();
	}, function(){
		$(this).animate({opacity: 1}, 500);
		scrollIt(liWidth);
	});

	scrollIt(liWidth);
	changeIt();
	
	function scrollIt(width)
	{
		$("#slider").animate({scrollLeft: width}, 200000, "", function(){
			scrollIt(-width);
		});	
	}

	function preloadImages()
	{
		for(var i = 0; i < bigPhoto.length; i++)
		{			
			var preImage = new Image();
			preImage.src = bigPhoto[i]['path'];
		}
		
		for(var i = 0; i < smallPhoto.length; i++)
		{
			var preImage = new Image();
			preImage.src = smallPhoto[i];
		}		
	}	
	
	function changeIt()
	{
		if( bigPhoto.length < 1 ) return;
		var currentImg = bigPhoto[current]['path'];

		var nextImg = (current == (bigPhoto.length - 1)) ? bigPhoto[0]['path'] : bigPhoto[current + 1]['path'];
		var nextHref = (current == (bigPhoto.length - 1)) ? bigPhoto[0]['href'] : bigPhoto[current + 1]['href'];
		
		$("#bigPhotoImgTop").attr("src", currentImg).fadeIn(1000, function(){		
			$("#bigPhotoImgBottom").attr("src", nextImg);
						
			$("#bigPhotoImgTop").attr("src", currentImg).fadeOut(3000, function(){
				$("#bigPhotoImgTopLink").attr("href", nextHref);
		        if( current == (bigPhoto.length - 1) ){
		            current = 0;
		        }else{
		            current++;
		        }						
				setTimeout(changeIt, 4000);  						
			}) 				
		});		
	}
});
