/**
 * handles Photo and Video roatator module
 * @param sandbox
 */
Cobalt.Website.VehicleDetail.PhotoModule = function(sandbox)
{

    var _instance = null;
    var _dataManager = null;
	var _popupInitiated;
	
    function getImageRotator() {
        if (_instance === null)
        {
            _instance = Cobalt.Website.VehicleDetail.ThumbsRotatorModule();
        }
        return _instance;
    }
	
	//Handle thumbnail image click
	function handleThumbnailClick(e){
		getImageRotator().stopRotatorAutoPlay();
		getImageRotator().updateThumbElement(e.target);
		getImageRotator().updateVehicleDetailImage();
		sandbox.raise(null, Cobalt.Website.Common.Events.PhotoThumbChanged, null);
	}
	
    function getDataManager()
    {
        if (_dataManager === null)
        {
            _dataManager = Cobalt.Website.Data.VehicleDetailDataManager;
        }

        return _dataManager;
    }	

    function getPhotoData()
    {
        if (getDataManager())
        {
            return getDataManager().getAllPhotos();
        }
        return null;
    }
	
    function getIsAutoPlayPhoto()
    {
        if (getDataManager())
        {
            return getDataManager().isAutoPlayPhoto();
        }
        return null;
    }
	
	function removeAllThumbSelect(e)
	{
		getImageRotator().stopRotatorAutoPlay();
		getImageRotator().removeAllThumbSelect();
	}
	
	function handlePopupModuleClick()
	{
		_popupInitiated = true;
		removeAllThumbSelect();
		sandbox.listen(null, Cobalt.Website.Common.Events.RenderingCompleted, selectFirstThumb);
	}
	
	function selectFirstThumb()
	{
		if(!_popupInitiated) return;
		var thumb = sandbox.dom(getImageRotator().getThumbElements()[0]).getDomElements();
		sandbox.raise(thumb,"click",null);
		_popupInitiated = false;
	}
	
    return {
        
		init:function(){
            var thumbElements = sandbox.dom(this.getThumbElementsSelector()).getDomElements();
            var detailImageElement = sandbox.dom(this.getVehicleDetailImageContainerSelector()).getDomElements();
			var imageDisclaimerElement = sandbox.dom(this.getVehicleDetailDisclaimerContainerSelector()).getDomElements();
            
            getImageRotator().init({
                speed:6000,
                data:getPhotoData(),
                thumbElements: thumbElements,
                detailPlaceHolderElement: detailImageElement,
				detailDisclaimerElement: imageDisclaimerElement,
                sandbox: sandbox
            });

            if (getIsAutoPlayPhoto())
            {
			    getImageRotator().startRotatorAutoPlay();
            }
			sandbox.dom(this.getThumbElementsSelector()).listenLive("click", handleThumbnailClick);
			sandbox.listen(null, Cobalt.Website.Common.Events.VideoThumbChanged, removeAllThumbSelect);
			sandbox.listen(null, Cobalt.Website.Common.Events.PopupClicked, handlePopupModuleClick);
        },

        destroy:function(){
            //TODO: handle destroy
        },

        getThumbElementsSelector:function()
        {
            return ".photo-thumbs img";
        },

        getVehicleDetailImageContainerSelector:function()
        {
            return "#media_placeholder";
        },
		
		getVehicleDetailDisclaimerContainerSelector:function()
        {
            return ".media .disclaimer";
        }
    };
};

