/*jslint white: false, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: true, bitwise: true, newcap: true, indent: 4 */
/*global Cobalt, CobaltPixelTag, ContextManager */

(function($)
{	
	"use strict";
	
	var DOR_URL = "https://dealeronlineretailing.com/GMOR/init.action", 
		DOR_LINK_OUT_LABEL = "gmbuyitnow",
		DOR_PAGE_LABEL = "linkout_gmbuyitnow_",	
		DOR_PAGE_LAYOUT = "linkout_gmbuyitnow",
		DOR_PAGE_NAME = "gmbuyitnow",
		DEALER_CODES = 
		{
			"DEFAULT": ">.<", 
		        "gmps-al": "G1AL54006201",
			"gmps-patobrien": "G1PA70X25100",
			"gmps-rydell": "G1RY112700SW",
			"gmps-friendly-fridley": "G1RY11HW7501",
			"gmps-capitol-sc": "G1S0NCNR1111",
			"gmps-patobrien-oh": "G1PA70PR3880",
			"gmps-pat-obrien-oh": "G1PA70BR2810",
			"gmps-pat-obrien": "G1PA70LA4545",
			"gmps-st-claire-cad": "G1S0NCSC3737",
			"gmps-classic": "G1CL55HW1101",
			"gmps-classic-grapevine": "G1CL55HW1101",
			"gmps-frank-kent-cadillac": "G1FRNK3800SB",
			"gmps-vistaridge": "G1CL55NI2700",
			"gmps-classic-ga": "G1C55CRD7700",
			"gmps-carlblack-kennesaw": "G1C4BKRB1110",
			"gmps-carlblack-fl": "G1C4BKC11500",
			"gmps-ballweg-chevy": "G1B11WTD3605",
			"gmps-watertown": "G1IG3RSTK725",
			"gmps-danbury": "G1IG3RF3DL84",
			"gmps-abel": "G1IG3RF3DL85",
			"gmps-berger": "G1B3R6252528",
			"gmps-kool-gr": "G1K00L3770PL",
			"gmps-todd-wenzel-mi": "G1W3NZ3L3156",
			"gmps-seif": "G1S31PH632M4",
			"gmps-denooyer": "G1D3N00Y3R58",
			"gmps-cole-krum": "G1C0L3343PR4",
			"gmps-midway": "G1M1DW38112S",
			"gmps-tapper-mi": "G1T4PP429SK4",
			"gmps-robert-denooyer": "G1R08D600E8S"
		};
		
	
	/**
	 * Finds the parameters needed for the DOR popup.
	 * 
	 * @param (jQuery) The link element
	 * @returns (object) A map of parameters 
	 */
	function gatherParameters(link)
	{
		var parameters = {};
		
		// Gather the parameters for the new window.  If new parameters 
		// are needed, all you need to do is add them here:
		parameters.vin = link.attr("id");
		parameters.refer = window.location.href;
		parameters.grti = DEALER_CODES[ContextManager.webId] || DEALER_CODES.DEFAULT;
		
		return parameters;
	}
	
	
	/**
	 * Creates a URL with an arbitrary number of parameters.
	 * 
	 * @param url (string) The base URL
	 * @param parameters (object) A map of parameters
	 * @returns (string) The parameterized URL
	 */
	function buildUrl(url, parameters)
	{
		var params = [],
			key,
			value,
			result;
		
		// Build the parameterized URL:
		parameters = parameters || {};
		for (key in parameters) 
		{
			if (parameters.hasOwnProperty(key))
			{
				value = encodeURIComponent(parameters[key]);
				params.push(key + "=" + value);
			}
		}
		result = url + "?" + params.join("&");
		
		return result;
	}
	
	
	/**
	 * Fires a DOR pixel tag.
	 */
	function firePixelTag()
	{
		var pixelTag,
			data = {};	
		
		data.pageLayout = DOR_PAGE_LAYOUT;
		data.pageName = DOR_PAGE_NAME;
		data.pageLabel = DOR_PAGE_LABEL + ContextManager.pageName;
		data.linkoutLabel = DOR_LINK_OUT_LABEL;
		data.paramSet = ["linkoutLabel"];
		
		pixelTag = new CobaltPixelTag(data);
		pixelTag.writePixelTag(data);
	}
	
	
	// Finally, listen for the DOR link clicks:
	$(".spriteDor").live("click", function()
	{
		var link = $(this).parent(),
			parameters = gatherParameters(link),
			url;
		
		// Fire the DOR pixel tag:
		firePixelTag();
		
		// Build the URL and open it:
		url = buildUrl(DOR_URL, parameters);
		window.open(url, 'CbltDorWindow');
						
		return false;
	});

	
}(Cobalt.Core.JQueryFactory.getLatest()));

