
var Carousel = function($){


	var setLeftRightClickEvent = function(){
		$('#carousel-right-toggle, #carousel-left-toggle').click(function(e) {
				direction = $(this).attr("name");		
				changeFeature(direction);
				//disable the autoslide on click
				stopAutoSlide();
				e.preventDefault();
			});
	}
	
	var removeLeftRightClickEvent = function(){
		$('#carousel-right-toggle, #carousel-left-toggle').unbind("click");
	}
	
	var changeFeature = function(seek){
	
		removeLeftRightClickEvent();	
			
		var elemMargin = parseInt($('#carousel-image-container').css('margin-left'));		
		var activeSlide = $("#carousel-image-container li.active");
		var activeSlideIndex = $("#carousel-image-container li").index(activeSlide);
		var marginIncrement =  Carousel.panelSize;

		if(seek == "right"){
			
			seek = activeSlideIndex + 1;		
			if(seek == Carousel.totalSlides){
				seek = 0;
				marginIncrement = -(Carousel.panelSize*(Carousel.totalSlides-1));
			} else {
				marginIncrement = Carousel.panelSize;
			}
		} else if(seek == "left"){
			marginIncrement =  -Carousel.panelSize;
			seek = activeSlideIndex - 1;
		} else {
			marginIncrement = ((seek+1) - (activeSlideIndex+1)) * Carousel.panelSize;
		}

		var marginSlide = elemMargin-marginIncrement;
		//hide the left/right nav accordingly
		var display = (seek == 0) ? $('#carousel-left-toggle').addClass("hidden") :  $('#carousel-left-toggle').removeClass("hidden");
		var display = (seek == (Carousel.totalSlides-1)) ? $('#carousel-right-toggle').addClass("hidden") :  $('#carousel-right-toggle').removeClass("hidden");
		
		//hide the text layer
		//slide-copy-wrap
		upcomingSlideCopy = $("#carousel-image-container li").eq(seek).find("div img");	
		
		if(!$.browser.msie)				
				upcomingSlideCopy.fadeOut("fast");
	
		$("#carousel-active li a").removeClass("active");
		$("#carousel-active li a").eq(seek).addClass("active");
				
		setLearnMore(seek);
		
		$("#carousel-image-container").animate({marginLeft: marginSlide+'px'}, Carousel.slideSpeed, 'easeInOutExpo', function(){
			$("#carousel-image-container li").removeClass("active");
			$("#carousel-image-container li").eq(seek).addClass("active");
			
			if(!$.browser.msie)
				upcomingSlideCopy.fadeIn("fast");
				
				setLeftRightClickEvent();
		});
		
	};
	
	var setLearnMore = function(activeSlideIndex){
		activeSlideHref = $("#carousel-image-container li").eq(activeSlideIndex).find("a").attr("href");
		$("#carousel-learnmore").attr("href", activeSlideHref);
	}
	
	var generateActiveIndicator = function(){
		
		var li = "";
		for (x=0; x<Carousel.totalSlides; x++){
		    li += "<li><a href=\"#\"><img src=\"/images/global960/homepage/splash_content/blank.gif\"></a></li>";
		}
		$("#carousel-active").append(li);
		$("#carousel-active li").click(function(e){
			seekTo = $(this).index();
			changeFeature(seekTo);
			stopAutoSlide();
			e.preventDefault();
		});
		$("#carousel-active li a").eq(0).addClass("active");
	}
			
	var autoSlide = function() {
		changeFeature("right");
		this.timer = setTimeout(autoSlide, Carousel.autoSlidePause);
	};
	
	var stopAutoSlide = function(){
		clearTimeout(this.timer);
	};
	
	return {
		
		timer: null,
		totalSlides: null,
		panelSize: 960,
		activeSlide: 1,
		autoSlidePause: 5000,
		slideSpeed: 500,			
		init: function(){	
					
			this.totalSlides = $('#carousel-image-container li').size();
			
			//set the first slide as active
			$("#carousel-image-container li").eq(0).addClass("active");
			
			$('#carousel-left-toggle').fadeOut("fast");
			
			generateActiveIndicator();
			//set the width of the container based on the number of slides
			$("#carousel-image-container").css("width", (this.panelSize * this.totalSlides));
			
			//set the value of the first learn more link
			setLearnMore(0);
			
			setLeftRightClickEvent();			
	
			timer = setTimeout(autoSlide, this.autoSlidePause);
		}
		//end init		
	};
}(jQuery);

