
/**

 * responsible for handling popup overlays for photo, video, etc.

 * @param {Cobalt.Core.Sandbox} sandbox

 */
Cobalt.Website.Results.DealerInfoModule = function (sandbox) {

	var moduleName = "Cobalt.Website.Results.DealerInfoModule";
	var _vehicleId = null;
	
    //inherit from PopupModule
	var _super = Cobalt.Website.Common.PopupModule(sandbox);
	
	_super.calculatePopupPosition = calculatePopupPosition;
	
	_super.showPopupBackground = showPopupBackground;
	
	_super.getPopupType = function() 
	{
		return moduleName;
	};
	
	/**
     * override super calculatePopupPosition
     * @param e
     */
	function calculatePopupPosition(e) 
	{
		if (e.customData.componentId) {
			var position = sandbox.dom("#" + e.customData.componentId).offset();
			position.top = position.top-130;
			return position;
		}
		return false;
	}
	
	/**
     * overrde showPopupBackground method
     */
	function showPopupBackground() 
	{
		return null;
	}
	
	function getVehicleDataManager() 
	{
		if (!Cobalt.Website.Results.VehicleDataManager) {
			return new Error("no vehicleDataManager instance");
		}
		return Cobalt.Website.Results.VehicleDataManager;
	}
	
	var firePixelTagForDealerInfo = function(elementId) 
	{
		var vehicleId = getVehicleId();
		var vinValue = getVehicleDataManager().getVINByVehicleId(vehicleId);
		var formModule = "DealerInfo";
		sandbox.firePixelTag({
								eventName: "com.cobaltgroup.ws.action.click.dealerInfo", 
								vin: vinValue,
								pageLabel: formModule + "_vehicleSearchResults", pageLayout:formModule,
								pageName: formModule + "_vehicleSearchResults"
							});
	};
	
	var handleOnclick = function (e) {
		var componentId = sandbox.dom(e.currentTarget).attr("id");
		setVehicleId(componentId);
		var mediaPopupComponentData = getMediaComponentId(e);
		firePixelTagForDealerInfo(componentId);
		sandbox.raise(null, Cobalt.Website.Common.Events.PopupClicked, mediaPopupComponentData);
		return false;
	};
	
	function setVehicleId(componentId)
	{
		_vehicleId = componentId.split("_")[3];
	};
	
	function getVehicleId()
	{
		return _vehicleId;
	};
	
	function getMediaComponentId(component) 
	{
		var compId = sandbox.dom(component.currentTarget).attr("id");
		var parentModule = getParentModule();
		return {
				componentId:compId, 
				sourceModule:moduleName,
				parentModule:parentModule
			};
	};
	
	function getParentModule()
	{
		var selector = "result_item_"+getVehicleId();
		return sandbox.getElementById(selector);
	};
	
	return {
		init: function() 
		{
			_super.init();
			var jquerySelector = sandbox.getUIElementSelectorsForModule(Cobalt.Website.Results.ResultsApplication.Modules.DealerInfoModule);
			for (var i = 0; i < jquerySelector.length; ++i) {
				sandbox.dom(jquerySelector[i]).listenLive("click", handleOnclick);
			}
		}, 
		destroy:function() 
		{
			_super.destroy();
		}
	};
};


