/**
 * responsible for handling popup overlays for photo, video, etc.
 * @param {Cobalt.Core.Sandbox} sandbox
 */

Cobalt.Website.Common.MediaPopupModule = function(sandbox)
{
	return {
		component: null,
		componentId: null,
		vehicleId: null,
		componentName: null,
		moduleName: null,
		popupEvent: null,
		photoPopupEvent: null,
		videoPhotoPopupEvent: null,
		destroyPopupEvent: null,
		responseData: null,
		pixelData: null,
		currentPanel: null,
		tabData: null,
		photoData: null,
		videoData: null,
		initialLoad: true,
		panels: [
			"media_popup_all_photos", 
			"media_popup_dealer_photo", 
			"media_popup_manu_photo", 
			"media_popup_video_photo"
		],
		
		init: function(data)
		{
			this.resetData();
			
			this.component = data.component;
			this.moduleName = data.moduleName;
			this.popupEvent = data.popupEvent;
			this.photoPopupEvent = data.photoPopupEvent;
			this.videoPhotoPopupEvent = data.videoPhotoPopupEvent;
			this.pixelData = data.pixelData;
		},
		
		resetData: function()
		{
			this.component = null;
			this.componentId = null;
			this.vehicleId = null;
			this.componentName = null;
			this.moduleName = null;
			this.popupEvent = null;
			this.photoPopupEvent = null;
			this.videoPhotoPopupEvent = null;
			this.destroyPopupEvent = null;
			this.popupEvent = null;
			this.responseData = null;
			this.pixelData = null;
			this.currentPanel = null;
			this.tabData = null;
			this.photoData = null;
			this.videoData = null;
			this.initialLoad = true;	
		},
		
		getComponent: function()
		{
			return this.component;
		},
		
		getModuleName: function()
		{
			return this.moduleName;
		},
		
		getComponentId: function()
		{
			if(!this.componentId){
				this.componentId = sandbox.dom(this.getComponent().currentTarget).attr("id");
			}
			return this.componentId;
		},
		
		getVehicleId: function()
		{
			if(!this.vehicleId){
				var componentId = this.getComponentId();
				this.vehicleId = componentId.split("_")[3];
			}
			return this.vehicleId;
		},
		
		getComponentName: function()
		{
			if(!this.componentName){
				var componentId = this.getComponentId();
				this.componentName = componentId.split("_")[2];
			}
			return this.componentName;
		},		
		
		getPopupEvent: function()
		{
			if(!this.popupEvent){
				this.popupEvent = Cobalt.Website.Common.Events.PopupClicked;
			}
			return this.popupEvent;
		},
		
		getPhotoPopupEvent: function()
		{
			if(!this.photoPopupEvent){
				this.photoPopupEvent = Cobalt.Website.Common.Events.PhotoTabClicked;
			}
			return this.photoPopupEvent;
		},

		getVideoPopupEvent: function()
		{
			if(!this.videoPopupEvent){
				this.videoPopupEvent = Cobalt.Website.Common.Events.VideoTabClicked;
			}
			return this.videoPopupEvent;
		},
		
		getDestroyPopupEvent: function()
		{
			if(!this.destroyPopupEvent){
				this.destroyPopupEvent = Cobalt.Website.Common.Events.DestroyPopup;
			}
			return this.destroyPopupEvent;
		},		
		
		popupRaiseEvent: function()
		{
			var data = this.getRequestData();
			var event = this.getPopupEvent();
			sandbox.raise(null, event, data);
		},
		
		popupRaiseDestroyEvent: function()
		{
			var data = this.getResponseData();
			var event = this.getDestroyPopupEvent();
			sandbox.raise(null, event, data);
		},

		getRequestData: function()
		{
			var moduleName = this.getModuleName();
			var componentId = this.getComponentId();
			return {
				componentId:componentId, 
				sourceModule:moduleName
			}
		},
		
		setResponseData: function(data)
		{
			this.responseData = data.customData;
			this.responseData.pixelData = this.pixelData;
		},
		
		getResponseData: function(data)
		{
			return this.responseData;
		},

		setTabData: function(data)
		{
			this.tabData = data;
		},
		
		getTabData: function()
		{
			return this.tabData;
		},

		getPhotoData: function()
		{
			if(!this.photoData){
				this.photoData = ("photoJSON" in window) ? photoJSON.data : null;
			}
			return this.photoData;
		},
		
		getVideoData: function()
		{
			if(!this.videoData){
				this.videoData = ("videoJSON" in window) ? videoJSON.data : null;
			}
			return this.videoData;			
		},
		
		setCurrentPanel: function(data)
		{
			this.currentPanel = data;
		},
		
		getCurrentPanel: function()
		{
			return this.currentPanel;
		},
		
		getPanels: function()
		{
			return this.panels;
		},
		
		createTabbedMediaModule: function()
		{
			var customData = this.getResponseData();
			var componentName = this.getComponentName();
			var data = this.getPhotoData();
			var oThis = this;
			if (data.showTabs)
			{
                var mediaTabs = sandbox.dom("#media_popup").tabs({
				
					show: function(event, ui)
					{					
						oThis.setTabData({ tab: ui.tab, panel: ui.panel, index: ui.index });
						customData.tabData = oThis.getTabData();						
						customData.initialLoad = oThis.initialLoad;						
						oThis.setCurrentPanel(ui.panel.id);						
						if(!oThis.initialLoad)
						{
							oThis.raiseMediaPopupClicked();
						}						
					},
					
					select: function(event, ui)
					{
						oThis.popupRaiseDestroyEvent();				
					}
					
				});

				if(componentName === "video")
				{				
					var tabLength = sandbox.dom(mediaTabs).tabs('length');
					sandbox.dom(mediaTabs).tabs('select',tabLength-1);					
				}
				
				this.raiseMediaPopupClicked();
				this.initialLoad = false;
				return true;				
				
			}
			
			var panelId = this.getEvent();
			this.setCurrentPanel(panelId);
			
			var panelElement = sandbox.dom("[id='"+panelId+"']").getDomElements();
			
			this.setTabData({ panel: panelElement });
			customData.tabData = this.getTabData();
			
			customData.initialLoad = this.initialLoad;
			this.raiseMediaPopupClicked();
			return true;
			
		},		
		
		initializePopupTabs: function(data)
		{
			this.setResponseData(data);
			this.createTabbedMediaModule();
			return true;
		},
		
		raiseMediaPopupClicked: function()
		{
			var photoData = this.getPhotoData();
			var videoData = this.getVideoData();
			var customData = this.getResponseData();
			var panelId = this.getCurrentPanel();
			var panel = this.getPanels();
			switch (panelId)
			{
				case panel[0]:
					customData.data = photoData;
					customData.items = customData.data.allPhotos;
					sandbox.raise(null, Cobalt.Website.Common.Events.PhotoTabClicked, customData);	
					this.showDisclaimer(true);
					return true;				
				case panel[1]:
					customData.data = photoData;
					customData.items = customData.data.dealerPhotos;
					sandbox.raise(null, Cobalt.Website.Common.Events.PhotoTabClicked, customData);
					this.showDisclaimer(false);
					return true;
				case panel[2]:
					customData.data = photoData;
					customData.items = customData.data.manufacturerPhotos;
					sandbox.raise(null, Cobalt.Website.Common.Events.PhotoTabClicked, customData);
					this.showDisclaimer(true);
					return true;
				case panel[3]:
					customData.data = videoData;
					customData.liquidus = photoData.liquidusVideo;
					if(videoData.videos){
					customData.enhancedDmiVideo = videoData.videos.enhancedDmiVideo;
					}
					customData.items = customData.data.videos;
					sandbox.raise(null, Cobalt.Website.Common.Events.VideoTabClicked, customData);
					this.showDisclaimer(false);
					return true;
				default:
					throw new Error("MediaModule => unknown Tab to Render;");
			}
		},
		
		getEvent: function()
		{
			var data = this.getPhotoData();
			switch (data.mediaTypeToRenderWhenNoTabs)
			{
				case "allPhotos":
					return this.panels[0];
				case "dealerPhoto":
					return this.panels[1];
				case "manufacturerPhoto":
					return this.panels[2];
				case "video":
					return this.panels[3];
				default:
					throw new Error("MediaModule => unknown Tab to Render;");
			}
		},

		showDisclaimer: function(isShown)
		{
			var visibility = (isShown) ? "visible" : "hidden";
			sandbox.dom('div.media_popup_disclaimer').addStyle({visibility:visibility});
		}
		
	}
};
