/**
 * Functions related to the homepage image slider
 */

//Create the ImageSlider namespace SLI
if (KM.SLI == null || typeof(KM.SLI) != "object") { KM.SLI = new Object();}

KM.SLI = {
		
	/**
	 * Fade to next image after a delay
	 */
    slideSwitch: function () {

		    var $active = $('#image-reel DIV.active');
					
		    if ( $active.length == 0 ) $active = $('#image-reel DIV:last');
					
		    var $next = $active.next().length ? $active.next()
		        : $('#image-reel DIV:first');
		
		    $active.addClass('last-active');
		
		    $next.css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 1000, function() {
		            $active.removeClass('active last-active');
		        });
	},
		
	/**
	* Initialize the image slider
	*/
	initSlides: function () {
		var playSlideshow =  setInterval( "KM.SLI.slideSwitch()", 3000 );
		var numDivs = $('#image-reel div').length;
		
		$('#slideshow').mouseover(function() { 
			clearInterval(playSlideshow);
			$('#slideshow .paging').show();
		});
		
		$('#slideshow').mouseout(function() { 
			playSlideshow =  setInterval( "KM.SLI.slideSwitch()", 3000 );
			$('#slideshow .paging').hide();
		});		

		$('#slideshow .paging a').click(function() { 	
			clearInterval(playSlideshow);
			var upcoming = $(this).attr('rel');
		    var current = $('#image-reel DIV.active');
			
		    current.addClass('last-active');
		    
			$("#image-reel > div.image:nth-child("+upcoming+")").css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 1000, function() {
		            current.removeClass('active last-active');
		        });
	        
	        return false;
		});	
	}
 
};

$(document).ready(function(){
	KM.SLI.initSlides();
});
