
var FortJS = {};

FortJS.Carousel = Class.create({
	
	init: function(options)
	{
		this.car_options = $.extend(
		{
			container: 								"#quicklinks_carousel", 
			btnWest: 								".quicklinks_previous_btn", 
			btnEast: 								".quicklinks_next_btn", 
			itemContainerDiv:						".quicklinks_holder", 
			itemContainerTag:						".quicklinks_holder ul", 
			
			atEnd: 									'loop',		// stop | reset | reverse | loop
			pauseOnHover: 							true, 		// pauses the animation at the moment a mouse hovers over
			direction: 								'west',	 	// west|east [default = west] initial direction of movement
			scrollingAllowed:						true,
			animationSpeedMs:						800
			
		}, arguments[0] || {});
		
		this.car_container = $(this.car_options.container);
		this.car_btnWest = $(this.car_options.btnWest);
		this.car_btnEast = $(this.car_options.btnEast);
		this.car_itemContainerDiv = $(this.car_options.itemContainerDiv);
		this.car_itemContainerTag = $(this.car_options.itemContainerTag);
		this.car_scrollElements = $(this.car_itemContainerTag).children("li");
		
		if (this.car_options.atEnd != 'loop')
			alert ('atEnd: variation not implemented');
		
		//this.car_imageElements = $(this.car_itemContainerTag).find('img');
	
		this.car_visibleWidth = this.car_itemContainerDiv.width();
		this.car_elementsWidth = this.car_GetWidthOfScrollElements();
		this.car_animationTime = this.car_options.animationSpeedMs 
			* this.car_scrollElements.length / this.car_elementsWidth;
		this.car_scrollElementsAsHtml = $(this.car_itemContainerTag).html();
		
		//alert (this.car_elementsWidth + ':' + this.car_visibleWidth);
		if (this.car_elementsWidth < this.car_visibleWidth)
		{
			// If too short to scroll, disable scrolling
			this.car_options.scrollingAllowed = false;
		}
		else
		{
			// Else add 2 x clones of the scroll items
			$(this.car_itemContainerTag).append(this.car_scrollElementsAsHtml);
			$(this.car_itemContainerTag).append(this.car_scrollElementsAsHtml);
			$(this.car_itemContainerTag).width(this.car_elementsWidth * 3);
		}
		
		if (this.car_options.pauseOnHover)
		{
			$('.quicklinks_holder img').hover(
					function () { $(cal_Carousel.car_itemContainerTag).pause(); }, 
					function () { $(cal_Carousel.car_itemContainerTag).resume(); });
		}
		
		$(this.car_btnWest).click( this.car_btnWest_Clicked );
		$(this.car_btnEast).click( this.car_btnEast_Clicked );

		$(this.car_btnWest).hover( this.car_btnWest_Clicked );
		$(this.car_btnEast).hover( this.car_btnEast_Clicked );
		
		// Start Animation
		this.car_ResetToStart();
		this.car_Begin();
	},
	
	car_GetWidthOfScrollElements: function()
	{
		var iWidth = 0;
		
		$(this.car_scrollElements).each( 
				function (index, item) 
				{ 
					iWidth += $(item).width() + $(item).margin().left + $(item).margin().right;
				} );
		
		return iWidth;
	},
	
	car_ResetToStart: function()
	{
		$(this.car_itemContainerTag).css('left', '-' + this.car_elementsWidth + 'px');
	},
	
	car_Begin: function()
	{
		if (this.car_options.scrollingAllowed)
		{
			if (this.car_options.direction == "west")
				this.car_MoveToPosition(this.car_elementsWidth * -2);
			else if (this.car_options.direction == "east")
				this.car_MoveToPosition(0);
		}
	},
	
	car_MoveToPosition: function(newLeftValue)
	{
		var iTime = 
			Math.abs($(this.car_itemContainerTag).position().left - newLeftValue);
			
		this.car_isAnimating = true;
		$(this.car_itemContainerTag).animate(
			{ left: newLeftValue }, 
			this.car_animationTime * iTime, 
			'linear', 
			function () { cal_Carousel.car_Complete(); });
	},
	
	car_Complete: function()
	{
		this.car_ResetToStart();
		this.car_isAnimating = false;
		
		switch (this.car_options.atEnd)
		{
			case 'loop':
				break;
		}

		this.car_Begin();
	},
	
	car_btnWest_Clicked: function()
	{
		if (cal_Carousel.car_options.direction == "west")
			return;

		if (cal_Carousel.car_isAnimating)
		{
			$(cal_Carousel.car_itemContainerTag).stop();

			var iLeft = $(cal_Carousel.car_itemContainerTag).position().left;
			iLeft -= cal_Carousel.car_elementsWidth;
			
			$(cal_Carousel.car_itemContainerTag).css('left', iLeft + 'px');
		}
		
		cal_Carousel.car_options.direction = "west";
		cal_Carousel.car_Begin();
	},
	
	car_btnEast_Clicked: function()
	{
		if (cal_Carousel.car_options.direction == "east")
			return;

		if (cal_Carousel.car_isAnimating)
		{
			$(cal_Carousel.car_itemContainerTag).stop();

			var iLeft = $(cal_Carousel.car_itemContainerTag).position().left;
			iLeft += cal_Carousel.car_elementsWidth;
			
			$(cal_Carousel.car_itemContainerTag).css('left', iLeft + 'px');
		}
		
		cal_Carousel.car_options.direction = "east";
		cal_Carousel.car_Begin();
	}

});





