Cobalt.Website.VehicleDetail.ThumbsRotatorModule = function()
{
    return {
		_detailPlaceHolderElement: null,
		_speed:6000,
		_timerId: null,
		_data: null,
		_currentIndex:0,
		_thumbElements: null,
		_thumbElementsLength: -1,
		_detailDisclaimerElement: null,
		_tooltipClass: "thumbs-tooltip",

		init:function(options)
		{
			if (options === undefined || !options)
			{
				throw new Error("ThumbsRotator => init no options");
			}

			this._sandbox = options.sandbox;
			this._thumbElements = options.thumbElements;
			this._speed = options.speed;
			this._data = options.data;
			this._detailPlaceHolderElement = options.detailPlaceHolderElement;
			this._detailDisclaimerElement = options.detailDisclaimerElement;
		},

		getThumbElements: function()
		{
			return this._thumbElements;
		},
		
		getThumbElementsLength: function()
		{
			if (this._thumbElementsLength <= 0)
			{
				this._thumbElementsLength = this._sandbox.dom(this.getThumbElements()).length();
			}

			return this._thumbElementsLength;
		},

		/*Check to verify more than 1 thumb image*/
		isThereMoreThanOneThumb: function()
		{
			return this.getThumbElementsLength() > 1;
		},	

		/*Set data*/
		setData: function(data)
		{
			this._data = data;
		},

		/*Get data*/
		getData: function()
		{
			return this._data;
		},

		/*Image Rotator*/
		rotate: function()
		{
			this.selectNextThumb();
			this.updateVehicleDetailImage();
		},

		/*Auto run image rotator*/
		startRotatorAutoPlay: function()
		{
			if(this.isThereMoreThanOneThumb())
			{
				this.selectFirstThumb();
				var _this = this;
				this._timerId = setInterval(function(){_this.rotate()}, this._speed);
			}
		},

		selectFirstThumb:function()
		{
			this._currentIndex = 0;
			this.selectThumb(-1, this._currentIndex);
		},

		selectNextThumb:function()
		{
			this._currentIndex = ++this._currentIndex % this.getThumbElementsLength();
			var prevIndex = this._currentIndex === 0 ? this.getThumbElementsLength()-1 : this._currentIndex -1;
			this.selectThumb(prevIndex, this._currentIndex);
		},

		selectThumb:function(preIndex, thumbIndex)
		{
			this._sandbox.dom(this.getThumbElements()[preIndex]).removeClass('selected');
			this._sandbox.dom(this.getThumbElements()[thumbIndex]).addClass('selected');
		},
		
		removeAllThumbSelect:function()
		{
			this._sandbox.dom(this.getThumbElements()).removeClass('selected');
		},
		
		getCurrentIndex:function(){
			return this._currentIndex;
		},

		/*Stop image rotator auto run*/
		stopRotatorAutoPlay: function()
		{
			clearInterval(this._timerId);
		},

		/*Update rotator Image*/
		updateVehicleDetailImage: function()
		{
			this.loadVehicleDetailImage(this._currentIndex);
		},

		updateThumbElement:function(thumbElement)
		{
			
			var clickedThumbnailIndex = this.getThumbIndexOf(thumbElement);
			this.selectThumb(this._currentIndex, clickedThumbnailIndex);
			this._currentIndex = clickedThumbnailIndex;
		},

		loadVehicleDetailImage:function(index)
		{
			if(this.getData())
			{
				var sandbox = this._sandbox;
				this.updateDisclaimer();
				var url = this.getData()[index].full;
				sandbox.raise(null, Cobalt.Website.Common.Events.LoadingIndicatorStart, {parentModule:this._detailPlaceHolderElement});
				this.imageLoader(this._detailPlaceHolderElement, url, function(){
					sandbox.raise(null, Cobalt.Website.Common.Events.LoadingIndicatorStop, null);
				});
			}
		},
		
		updateDisclaimer: function()
		{
			var disclaimer = (this.getData().disclaimer) ? this.getData().disclaimer : "&nbsp;";
			this._sandbox.dom(this._detailDisclaimerElement).html(disclaimer);	
		},
		
		getThumbIndexOf:function(thumbElement)
		{
			return this._sandbox.dom(this.getThumbElements()).index(thumbElement);
		},

		getThumbElementParent:function(){
			return this._sandbox.dom(this.getThumbElements()).parent();
		},
		
		showTooltip:function(thumbElement, displayData)
		{
			this.renderToolTip(thumbElement, displayData);
		},
		
		hideTooltip:function()
		{
			var parentNode = this.getThumbElementParent();		
			var tooltipElement = this.getTooltipElement();
			this._sandbox.dom(tooltipElement).remove();
		},
		
		renderToolTip:function(thumbElement, displayData)
		{
			var parentNode = this.getThumbElementParent();
			var elementPosition = this._sandbox.dom(thumbElement).position();
			var htmlContent = "<div class=\"" + this.getTooltipClass()+ "\">" + displayData + "</div>";
			this._sandbox.dom(parentNode).append(htmlContent);
			var tooltipElement = this.getTooltipElement();
			this._sandbox.dom(tooltipElement).addStyle({"left":elementPosition.left,"top":elementPosition.top});
			this._sandbox.dom(tooltipElement).show(200);
		},
		
		getTooltipElement:function(){
			return this._sandbox.getElementsByAttribute("class",this._tooltipClass);
		},
		
		getTooltipClass:function(){
			return this._tooltipClass;
		},
		
		imageLoader:function(imageContainer, newImageUrl, callback)
		{
			var sandbox = this._sandbox;
			var newImg = new Image();
			sandbox.dom(newImg).attr("src", newImageUrl);
			sandbox.dom(newImg).attr("alt", this.getData().alt);
			sandbox.dom(newImg).attr("title", this.getData().alt);//This is for firefox
            
			sandbox.dom(newImg).load(function(e)
			{
				if (imageContainer)
				{
                    sandbox.dom(imageContainer).empty();
					sandbox.dom(imageContainer).append(this);
				}
				
				if (typeof callback ===  "function"){ callback(); }
			});
			
			sandbox.dom(newImg).each(function()    //ie doesn't fire load if img is loaded from cahce
			{
				if(this.complete) sandbox.raise(this,"load",null,null);
			});
			
			sandbox.dom(newImg).error(function (e) {
				var error = e;
			});
		}
	
	}
};
