/*
 * --------------------------------------------------------------------
 * Simple Scroller
 * by Siddharth S, www.ssiddharth.com, hello@ssiddharth.com
 * Version: 1.0, 05.10.2009 	
 * --------------------------------------------------------------------
 */

function onDocReadyFunction() 
{	 
	var index = 0;
	var images = jQuery("#gallery img");
   	var thumbs = jQuery.makeArray(jQuery("#thumbs img"));
	var imgHeight = jQuery(thumbs).attr("height");
	var navig = jQuery("#navig a");
    var links = jQuery("#thumbs li");
    var thumbsLiToCopy = jQuery("#thumbs ul li").clone();
    
	for (i=0; i<thumbs.length; i++)
	{
		jQuery(thumbs[i]).addClass("thumb-"+i);
		jQuery(images[i]).addClass("image-"+i);
        jQuery(links[i]).addClass("link");  //images
        jQuery(navig[i]).attr('id', i); //pageable
    }
	
	jQuery("#next").click(shift);
	show(index);
    var t = setTimeout(shift, 5000);
    
    jQuery("#navig a").click(function() {
        shift1(parseInt(jQuery(this).attr('id')));
    });

	function shift()
	{ 
		clearTimeout(t);
		index+=1 ;
        if (index >= images.length) {
            index = 0;
        }
		show (index);
        t = setTimeout(shift, 5000);
	}
    
    function shift1(num)
	{
        clearTimeout(t);
        index = num;
        show (index);
        t = setTimeout(shift, 5000);
    }

	
	function show(num)
	{
		jQuery(images).fadeOut(400);
		jQuery(".image-"+num).stop().fadeIn(400);
        
        theCloneLis = thumbsLiToCopy.clone();
        theNewLis = new Array();
        for (i=0; i<5; i++) {
            theShiftedIdx = (i + (num)) % thumbs.length;
            theNewLis.unshift(jQuery(theCloneLis[theShiftedIdx]).attr("class", "").clone());
        } 
        jQuery("#thumbs").replaceWith('<div id="thumbs" class="list"></div>');
        jQuery("<ul></ul>").appendTo(jQuery("#thumbs"));
        jQuery(theNewLis).appendTo(jQuery("#thumbs>ul"));
        theThumbs = jQuery("#thumbs img");
        
        //If height is zero then determine height
        if (imgHeight == 0) {
            imgHeight = jQuery(theThumbs).attr("height");
        }
        var scrollPos = imgHeight * (2);
        jQuery("#thumbs").animate({scrollTop: scrollPos}, 0); 
        scrollPos = imgHeight;
        jQuery("#thumbs").animate({scrollTop: scrollPos}, 400); 
        
        for (i=0; i<navig.length; i++)
        {
            jQuery("#navig #" +i).attr('class', 'non');
            
        }
        jQuery("#navig #"+(num)).addClass("active");        
        
        jQuery(theThumbs[3]).animate({opacity: 0.3}, 400);
        for (i=0; i<theThumbs.length; i++) {
            jQuery(theThumbs[i]).attr('class', 'thumb-'+i);
        }

        jQuery(theThumbs[3]).attr('class', 'active');
        jQuery(theThumbs[3]).animate({opacity: 1}, 400);
	}


}

jQuery.noConflict();
jQuery(document).ready(function() {
    setTimeout("onDocReadyFunction()", 300);
});


