/*
 * --------------------------------------------------------------------
 * Simple Scroller
 * by Siddharth S, www.ssiddharth.com, hello@ssiddharth.com
 * Version: 1.0, 05.10.2009 	
 * --------------------------------------------------------------------
 */

$(document).ready(function() 
{	 
	var index = 0;
	var images = $("#gallery img");
	var countImages = $("#thumbs").find('img').size()
	var thumbs = $("#thumbs img");
	var imgHeight = $(thumbs).attr("height");
	
	/* solo se ho meno di tre immagini ne appendo altre */
//	if(countImages <= 3 ){
	 $(thumbs).slice(0,countImages).clone().appendTo("#thumbs");
//	}
	
	for (i=0; i<thumbs.length; i++){
		$(thumbs[i]).addClass("thumb-"+i);
		$(images[i]).addClass("image-"+i);
	}
	
	
	$("#next").click(sift);
	show(index);
	setInterval(sift, 8000);
	
	function sift()
	{
		if (index<(thumbs.length-1)){index+=1 ; }
		else {index=0}
		show (index);
	}
	
	function show(num)
	{
		$(images).fadeOut(400);
		$(".image-"+num).stop().fadeIn(400);
		var scrollPos = (num+1)*imgHeight;
		$("#thumbs").stop().animate({scrollTop: scrollPos}, 400)/*.removeAttr('class').addClass('scr'+ num)*/;		
		$("#container_header").removeAttr('class').addClass('scr'+ num);		
		/*console.log(scrollPos, "img.image-" + num);*/
	}
	
  $("#thumbs").find('img').each(function(index){
  /*console.log($("#thumbs").find('img').size());*/
	 $(this).click(function(){
	   show(index % countImages );
	 })
	});
	
});

	/*
$(".thumb-0").click(sift);
	show(index);
	setInterval(sift, 8000);
	
	function sift()
	{
		if (index<(thumbs.length-1)){index+=1 ; }
		else {index=0}
		show (index);
	}
*/
