Cobalt.Website.VehicleDetail.VideoThumbsModule = function(sandbox)
{

    var _instance = null;
    var _dataManager = null;
	var _isPlaying;
	
    function getVideoRotator() {
		if (_instance === null)
        {
            _instance = Cobalt.Website.VehicleDetail.ThumbsRotatorModule();
        }
        return _instance;
    };
	
    function getDataManager()
    {
        if (_dataManager === null)
        {
            _dataManager = Cobalt.Website.Data.VehicleDetailDataManager;
        }
        return _dataManager;
    };
	
	function getVideoData()
    {
        if (getDataManager())
        {
            return getDataManager().getVideos();
        }
        return null;
    };
	
	function getVideoDataByIndex(index){
		return getVideoData().videos[index];
	};	function getDmiVideoData(){		return getVideoData().videos.enhancedDmiVideo;	}
	
	function getIsAutoPlayVideo(){
        if (getDataManager())
        {
            return getDataManager().isAutoPlayVideo();
        }
        return false;	
	};

    function getIsShowVideo()
    {
        if (getDataManager())
        {
            return getDataManager().isShowVideo();
        }
        return false;

    };
	
	function getTooltipData(index){
		var data = getVideoDataByIndex(index);
		return {
			typeDesc: data.videoTypeDesc,
			shortDesc: data.videoShortDesc,
			duration: data.videoDuration
		}
	};	
	
	//Handle thumbnail image click
	function handleThumbnailClick(e){
		getVideoRotator().updateThumbElement(e.target);
		var index = getVideoRotator().getCurrentIndex();
		var data = getVideoDataByIndex(index);				var enhancedVideoData = getDmiVideoData() || {};
		setIsVideoPlaying(true);
		sandbox.raise(null, Cobalt.Website.Common.Events.VideoThumbChanged, {enhancedVideoData:enhancedVideoData,data: data, index: index});
	};

	function showTooltip(e){
		var index = getVideoRotator().getThumbIndexOf(e.target);
		var htmlData = generateTooltipData(index);
		getVideoRotator().showTooltip(e.target, htmlData);	
	};
	
	function hideTooltip(){
		getVideoRotator().hideTooltip();
	};
	
	function generateTooltipData(index){
		var tooltipData = getTooltipData(index);
		var htmlData = [];
		htmlData.push("<div class='inner-tooltip'>");
		htmlData.push("<h3 class='tooltip-typedesc'>");
		htmlData.push(tooltipData.typeDesc);
		htmlData.push("</h3>");
		htmlData.push("<p class='tooltip-shortdesc'>");
		htmlData.push(tooltipData.shortDesc);
		htmlData.push("</p>");
		htmlData.push("<p class='tooltip-duration'>");
		htmlData.push(tooltipData.duration);
		htmlData.push("</p>");
		htmlData.push("</div>");
		return htmlData.join("");
	};	
	
	function removeAllThumbSelect(e)
	{
		setIsVideoPlaying(false);
		getVideoRotator().removeAllThumbSelect();
	};
	
	function renderOrDestroyVideoOnTabChange(e)
	{
		var destroyObject = e.customData.destroyObject;
		var isPlaying = getIsVideoPlaying();
		
		if(!isPlaying) return;
		
		if(destroyObject)
		{
			sandbox.dom("#media_placeholder object").remove();
			return;
		}
		
		if(!destroyObject)
		{
			checkConfigAndAutoPlayVideo();
			return;
		}
	};
	
	function setIsVideoPlaying(data)
	{
		_isPlaying = data;
	};
	
	function getIsVideoPlaying()
	{
		return _isPlaying;
	};
	
	function checkConfigAndAutoPlayVideo()
	{
		if (getIsAutoPlayVideo() || getIsShowVideo())
		{
			var index = getVideoRotator().getCurrentIndex();
			var data = getVideoDataByIndex(index);
			getVideoRotator().selectThumb(0, index);						var enhancedVideoData = getDmiVideoData() || {};
			setIsVideoPlaying(true);
			sandbox.raise(null, Cobalt.Website.Common.Events.VideoOnPageLoad, {enhancedVideoData:enhancedVideoData,data: data, index: 0});
			return;
		}	
	};
	
	function getThumbElementsSelector()
	{
		return ".video-thumbs img";
	};

    return {
        init: function()
        {
            var thumbElements = sandbox.dom(this.getThumbElementsSelector()).getDomElements();

            getVideoRotator().init({
                thumbElements: thumbElements,
                sandbox: sandbox
            });

            sandbox.listen(thumbElements, "click", handleThumbnailClick);
            sandbox.listen(thumbElements, "mouseover", showTooltip);
            sandbox.listen(thumbElements, "mouseout", hideTooltip);
			sandbox.listen(null, Cobalt.Website.Common.Events.PhotoThumbChanged, removeAllThumbSelect);
			sandbox.listen(null, Cobalt.Website.Common.Events.PhotoTabClicked, removeAllThumbSelect);
			sandbox.listen(null, Cobalt.Website.Common.Events.TabsChanged, renderOrDestroyVideoOnTabChange);
			
			checkConfigAndAutoPlayVideo();

        },

        destroy:function()
        {
        },

        getThumbElementsSelector:function()
        {
            return getThumbElementsSelector();
        }

    };

};
