﻿var PoseraCarousel = {
	init: function(params) {
		PoseraCarousel.parameters = params;

		//optional params
		if (!PoseraCarousel.parameters.timerInterval) {
			PoseraCarousel.timerInterval = 5000;
		}else{
			PoseraCarousel.timerInterval = PoseraCarousel.parameters.timerInterval;
		};
		
		//timer 
		PoseraCarousel.timerID = 0;			
		PoseraCarousel.tStart = null;
		PoseraCarousel.currentLinkIndex = null;
		
		//go
		PoseraCarousel.build();
		//PoseraCarousel.toggleImage(0);
		PoseraCarousel.timerStart();
	},//end init
	
	build: function(){
		//removing fallback elements from the control
		$('div#' + PoseraCarousel.parameters.containerElement).empty();
		
		//binding timerStart and timerStop events on control mouseX events
		$('div#' + PoseraCarousel.parameters.containerElement).mouseover( function() { PoseraCarousel.timerStop(); } );
		$('div#' + PoseraCarousel.parameters.containerElement).mouseout( function() { PoseraCarousel.timerStart(); } );
					
		//appending <ul> element that will contain links
		if (PoseraCarousel.parameters.displayLinks == null || PoseraCarousel.parameters.displayLinks == true) {    		
    		$('<div class="clear"></div><ul id="carouselLinks"></ul>').insertAfter('div#' + PoseraCarousel.parameters.containerElement);
        };
        
		for (i=0; i<PoseraCarousel.parameters.images.length; i+=1) {
			//setting currentIndex if none is set
			if (!PoseraCarousel.currentLinkIndex) { PoseraCarousel.currentLinkIndex = i };
			
			if (PoseraCarousel.parameters.displayLinks == null || PoseraCarousel.parameters.displayLinks == true){
			    PoseraCarousel.addLink(i, PoseraCarousel.parameters.images[i]);			
			};
			
			PoseraCarousel.addImage(i, PoseraCarousel.parameters.images[i]);	
		};
		
		//binding timerStart and timerStop events on control mouseX events (UL)
		$('ul#carouselLinks').mouseover( function() { PoseraCarousel.timerStop(); } );
		$('ul#carouselLinks').mouseout( function() { PoseraCarousel.timerStart(); } );
	},//end build
	
	addLink: function(index, image){	
	    var cssClass = '';
	    if(index == 0) {cssClass="selected";};
		$('<li id="item_'+index+'" class="'+ cssClass +'"><a href="#" onClick="PoseraCarousel.toggleImage(' + index + '); return false;">' + (index + 1) + '</a></li>').appendTo('ul#carouselLinks');	
	},//end addLinks
	
	addImage: function(index, image){
		var imgCSSClass;			
		if(index == 0) { imgCSSClass = 'visible'; }else{ imgCSSClass = 'hidden'; };
		$('<a href="' +  image.href + '" id="carousel_link_' + index + '" onclick="' + image.onclick + '"><img id="image_' + index + '" src="' + image.src + '" alt="' + image.caption + '" class="' + imgCSSClass +'" /></a>').appendTo('div#' + PoseraCarousel.parameters.containerElement);
	},//end addImages
	
	toggleImage: function(index) {
    	$("ul#carouselLinks li").removeClass('selected');
		$("div#carousel img:visible").fadeOut("fast",
			function(){
				$("div#carousel img#image_" + index ).fadeIn("slow");
				$("ul#carouselLinks li#item_" + index ).addClass('selected');
			}
		);
	},//end toggleImage
	
	timerStart: function() {
		PoseraCarousel.tStart = new Date();
   		PoseraCarousel.timerID  = setTimeout("PoseraCarousel.timerUpdate()", PoseraCarousel.timerInterval);			
	},//end timerStart
	
	timerStop: function() {
		if(PoseraCarousel.timerID) {
	  		clearTimeout(PoseraCarousel.timerID);
	  		PoseraCarousel.timerID  = 0;
   		};
   		tStart = null;
	},//end timerStop
	
	timerReset: function() {
		PoseraCarousel.tStart = null;	
	},//end timerReset
			
	timerUpdate: function(){
		if(PoseraCarousel.timerID) { clearTimeout(PoseraCarousel.timerID); };
	   	if(!PoseraCarousel.tStart) { PoseraCarousel.tStart = new Date(); };

		//exec
		PoseraCarousel.toggleImage(PoseraCarousel.currentLinkIndex);		
		
		//updating 	
		if (PoseraCarousel.currentLinkIndex + 1 > PoseraCarousel.parameters.images.length - 1) {
			PoseraCarousel.currentLinkIndex = 0;
		}else{
			PoseraCarousel.currentLinkIndex += 1;
		};		
   
	   	PoseraCarousel.timerID = setTimeout("PoseraCarousel.timerUpdate()", PoseraCarousel.timerInterval);			
	}//end timerupdate
};


