/*
 * --------------------------------------------------------------------
 * Simple Scroller
 * by Siddharth S, www.ssiddharth.com, hello@ssiddharth.com
 * Version: 1.0, 05.10.2009 	
 * --------------------------------------------------------------------
 */
 
(function ($) // Self-executing function
{
    $(function () // DOM-ready
    {
		//alert('');
		var index = 0;
		var images = jQuery("#gallery img");
		var thumbs = jQuery("#thumbs a");
		var imgHeight = jQuery(thumbs).attr("height");
		jQuery(thumbs).slice(0,3).clone().appendTo("#thumbs");
		for (i=0; i<thumbs.length; i++)
		{
			jQuery(thumbs[i]).addClass("thumb-"+i);
			jQuery(images[i]).addClass("image-"+i);
		}
		
		jQuery("#next").click(sift);
		jQuery("#prev").click(sift2);
		jQuery("#paus").click(pauseAll);
		jQuery("#play").click(resumeAll);
		show(index);
		var siftInterval = setInterval(sift, 8000);
		jQuery("#paus").show();
		jQuery("#play").hide();
		
		// pause rotator on hover
		images.bind('mouseover',function(){
			pauseSift();							 
		});
		images.bind('mouseout',function(){
			resumeSift();							 
		});
		thumbs.bind('mouseover',function(){
			pauseSift();							 
		});
		thumbs.bind('mouseout',function(){
			resumeSift();							 
		});
		
		function sift()
		{
			if (index<(thumbs.length-1)){index+=1; }
			else {index=0}
			show (index);
		}
		
		function sift2()
		{
			if(index > 0){
				if (index>(0)){index-=1; }
				else {index=thumbs.length}
				show (index);
			}
		}
		
		function show(num)
		{
			jQuery(images).fadeOut(400);
			jQuery(".image-"+num).stop().fadeIn(400);
			//console.log(imgHeight);
			var scrollPos = (num+1)*168;
			jQuery("#thumbs").stop().animate({scrollTop: scrollPos}, 400);		
			//console.log(scrollPos, "img.image-"+num);
		}
		
		function pauseSift(){
			clearInterval(siftInterval);
		}
		
		function resumeSift(){
			siftInterval = setInterval(sift, 8000);
		}
		
		function pauseAll(){
			images.unbind('mouseover');
			images.unbind('mouseout');
			thumbs.unbind('mouseover');
			thumbs.unbind('mouseout');
			clearInterval(siftInterval);
			jQuery("#paus").hide();
			jQuery("#play").show();
		}
		
		function resumeAll(){
			images.bind('mouseover',function(){
				pauseSift();							 
			});
			images.bind('mouseout',function(){
				resumeSift();							 
			});
			thumbs.bind('mouseover',function(){
				pauseSift();							 
			});
			thumbs.bind('mouseout',function(){
				resumeSift();							 
			});
			siftInterval = setInterval(sift, 8000);
			jQuery("#paus").show();
			jQuery("#play").hide();
		}
    });
})(jQuery);

 


