

// All supported symbolic targets:
GmdsCmsLinks.BUILD_YOUR_OWN = "byo"; //vehicleSpecific
GmdsCmsLinks.DEALER_LOCATOR = "lad";
GmdsCmsLinks.MODEL_OVERVIEW = "mov"; //vehicleSpecific
GmdsCmsLinks.REQUEST_A_BROCHURE = "rab";
GmdsCmsLinks.REQUEST_A_QUOTE = "raq";
GmdsCmsLinks.REQUEST_A_TEST_DRIVE = "rtd";
GmdsCmsLinks.REQUEST_NEWSLETTER = "rnl";
GmdsCmsLinks.SEARCH_INVENTORY ="lnv";
GmdsCmsLinks.COMPETITIVE_FEATURE_COMPARISON ="cfc"; //vehicleSpecific
GmdsCmsLinks.COMPETITIVE_PHOTO_COMPARISON ="cpc"; //vehicleSpecific
GmdsCmsLinks.ESTIMATE_PAYMENTS ="epm";
GmdsCmsLinks.VIEW_CURRENT_OFFERS ="vco";

function GmdsCmsLinksUtil(){

	/*function to add a params to an url*/
	this.appendParams = function (/*String*/ url, /*String*/ params) {
		if (url && params && params.length > 0) {
			var qmIdx = url.indexOf("?");
			var hshIdx = url.indexOf("#");
			if (qmIdx == -1 && hshIdx == -1) {
				
				url = url + "?" + params;
			} else if (qmIdx != -1 && hshIdx < qmIdx) {
				
				url = url + "&amp;" + params;
			} else if (qmIdx == -1 && hshIdx != -1) {
				
				url = url.substring(0, hshIdx) + "?" + params + url.substring(hshIdx, url.length);
			} else {
				
				url = url.substring(0, hshIdx) + "&" + params + url.substring(hshIdx, url.length);
			}
		}
		return url;
	};
}



function GmdsCmsLinks() {

	/* defines which links refer to a specific vehicle */
	var vehicleSpecificLinks = [GmdsCmsLinks.BUILD_YOUR_OWN, GmdsCmsLinks.COMPETITIVE_FEATURE_COMPARISON, GmdsCmsLinks.COMPETITIVE_PHOTO_COMPARISON,
								GmdsCmsLinks.MODEL_OVERVIEW];
	
	/* object which contains the data of all non-vehicle links*/					
	var linkdata = {"lad":{
			"url":"/tools/vauxhall-locate-dealer.html",
				"isPopup":false},"rab":{
			"url":"/tools/vauxhall-request-brochure.html",
				"isPopup":false},"raq":{
			"url":"/tools/vauxhall-request-quote.html",
				"isPopup":false},"rtd":{
			"url":"/tools/vauxhall-request-test-drive.html",
				"isPopup":false},"rnl":{
			"url":"/tools/vauxhall-request-newsletter.html",
				"isPopup":false},"lnv":{
			"url":"/content/vauxhall/europe/united_kingdom/nscwebsite/uk/index.html",
				"isPopup":false},"vco":{
			"url":"",
				"isPopup":false},"epm":{
			"url":"",
				"isPopup":false}
	};
	
	/* object which contains the data of all vehicle specific links*/
	var vehicleLinkdata = {
			"9AV08/9AV08":{
				"mov":{
						"url":"/vehicles/vauxhall-range/vans/corsavan/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/vans/corsavan/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9D/9D 25":{
				"mov":{
						"url":"/vehicles/vauxhall-range/vans/combo-van/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/vans/combo-van/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"91/91A05":{
				"mov":{
						"url":"/vehicles/vauxhall-range/vans/vivaro/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/vans/vivaro/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"95/95H05":{
				"mov":{
						"url":"/vehicles/vauxhall-range/vans/movano-ng/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/vans/movano-ng/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9A/9A 08":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/corsa-3-door/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/corsa-3-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9A/9A 68":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/corsa-5-door/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/corsa-5-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9Z/9Z 75":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/new-meriva/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/new-meriva/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9C/9C 67":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/astra-twintop/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/astra-twintop/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9T/9T 08":{
				"mov":{
						"url":"/vehicles/opel_range/cars/astra-sports-tourer/index.html","isPopup":false},"byo":{
						"url":"/vehicles/opel_range/cars/astra-sports-tourer/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9B/9B 69":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/insignia-4-door/index.html.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/insignia-4-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9Q/9Q 75":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/zafira/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/zafira/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9B/9B 35":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/insignia-sports-tourer/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/insignia-sports-tourer/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9B/9B 68":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/insignia-5-door/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/insignia-5-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9L/9L 26":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/antara/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/antara/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9H/9H 68":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/agila/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/agila/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9P/9P 68":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/new-astra/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/new-astra/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9T/9T 08":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/astra-sport-hatch/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/astra-sport-hatch/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9T/9T 35":{
				"mov":{
						"url":"/vehicles/vauxhall-range/cars/astra-estate/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/cars/astra-estate/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"9T70/9T 70":{
				"mov":{
						"url":"/vehicles/vauxhall-range/vans/astravan/index.html","isPopup":false},"byo":{
						"url":"/vehicles/vauxhall-range/vans/astravan/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}}
	};
	
	/**
	 * Opens a CMS page. The page might be either opened instead of the 
	 * current page, or as a new pop-up page, depending on the configuration 
	 * of the page in the CMS.
	 * 
	 * @param symbolicName the symbolicName of the page that should be opened
	 * @param params a parameter string that may contain several request parameters separated by &amp;
	 * @param carlineCode the carline-code, only required if the page to be opened is vehicle-specific.
	 * @param bodystyleCode the bodystyle-code, only required of the page to be opened is vehicle-specific.
	 */
	this.openCmsPage = function(/*String*/ symbolicName, /*String*/ params, /*String*/ carlineCode, /*String*/ bodystyleCode, /*String*/ modelYear) {
		var myLinkdata;
		var isVehicleSpecific = (vehicleSpecificLinks.indexOf(symbolicName) >= 0);
		if (isVehicleSpecific) {
			if(modelYear && modelYear.length > 0){
				myLinkdata = vehicleLinkdata[modelYear + "/" + carlineCode + "/" + bodystyleCode];
			}else{
				myLinkdata = vehicleLinkdata[carlineCode + "/" + bodystyleCode];
			}
			if (!myLinkdata) {
				mrm.util.error("VehicleSpecific: myLinkdata is undefined");
				//Log.error("VehicleSpecific: myLinkdata is undefined");
				return;
			}
		} else {
			myLinkdata = linkdata;
		}
		
		var ld = myLinkdata[symbolicName];
		if (ld) {
			var url = getGmdsCmsLinksUtil().appendParams(ld.url, params);
			if (ld.isPopup) {
				var popupProps = "";
				popupProps += "height=" + ld.height;
				popupProps += ",width=" + ld.width;
				if (ld.scrollable) {
					popupProps += ",scrollbars=yes";
				} else {
					popupProps += ",scrollbars=no";
				}
				if (ld.resizable) {
					popupProps += ",resizable=yes";
				} else {
					popupProps += ",resizable=no";
				}
				window.open(url, "", popupProps);
			} else {
				document.location.href = url;
			}
		}else{
			mrm.util.error("wrong shopping link identifier: "+identifier);
			//Log.error("wrong shopping link identifier: "+identifier);
		}
	}; 
}

(function() {
	var gmdsCmsLinksUtil = new GmdsCmsLinksUtil();
	var gmdsCmsLinks = new GmdsCmsLinks();

	window.getGmdsCmsLinks = function () {
		return gmdsCmsLinks;
	};
	
	window.getGmdsCmsLinksUtil = function () {
		return gmdsCmsLinksUtil;
	};
})();
