/*
 * --------------------------------------------------------------------
 * 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 thumbs = $("#thumbs img");
	var imgHeight = 131;
	$(thumbs).slice(0,3).clone().appendTo("#thumbs");
	for (i=0; i<thumbs.length; i++)
	{
		$(thumbs[i]).addClass("thumb-"+i);
		$(images[i]).addClass("image-"+i);
	}
	var tmp_block_next=0;
	$("#next").click(setblock);
	show(index);
	tmp_interval=setTimeout(sift, 8000);
	
	function sift()
	{
		clearTimeout(tmp_interval);
		if (index<(thumbs.length-1)){index+=1 ; }
		else {index=0}
		show (index);
		tmp_interval=setTimeout(sift, 8000);
	}
	
	function setblock(){
		if(tmp_block_next==0){
			sift();
			tmp_block_next=1;
			t=setTimeout(unsetblock, 1000);
		}
	}
	
	
	function unsetblock(){
		tmp_block_next=0;
	}
	
	function show(num)
	{
		$(images).fadeOut(400);
		$(".image-"+num).stop(true,true).fadeIn(400);
		var scrollPos = (num+1)*imgHeight;
		$("#thumbs").stop().animate({scrollTop: scrollPos}, 400);
//		console.log(scrollPos, "img.image-"+num);
	}
});
