

// 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";

Array.prototype.contains = function(object){
	for (var i=0; i<this.length; i++) {
  		if (this[i] === object) return true;
	}		
	return false;
} 

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":"",
				"isPopup":false},"rab":{
			"url":"/tools/opel-request-brochure.html",
				"isPopup":false},"raq":{
			"url":"/tools/opel-request-quote.html",
				"isPopup":false},"rtd":{
			"url":"/tools/opel-request-test-drive.html",
				"isPopup":false},"rnl":{
			"url":"/tools/opel-request-newsletter.html",
				"isPopup":false},"lnv":{
			"url":"/content/opel/europe/romania/nscwebsite/ro/index.html",
				"isPopup":false},"vco":{
			"url":"",
				"isPopup":false},"epm":{
			"url":"",
				"isPopup":false}
	};
	
	/* object which contains the data of all vehicle specific links*/
	var vehicleLinkdata = {
			"0P/0P 68":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/new-astra-hatchback/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/new-astra-hatchback/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"01/01 05":{
				"mov":{
						"url":"/autovehicule/showroom/furgoane/vivaro/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/furgoane/vivaro/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0D 25/0D 25":{
				"mov":{
						"url":"/autovehicule/showroom/furgoane/combo-van/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/furgoane/combo-van/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0Z/0Z 75":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/meriva-ng/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/meriva-ng/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0Q/0Q 75":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/zafira/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/zafira/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0D 06/0D 06":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/combo/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/combo/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"01/01 06":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/vivaro/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/vivaro/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0AV/0AV08":{
				"mov":{
						"url":"/autovehicule/showroom/furgoane/corsa-van/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/furgoane/corsa-van/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"01/01 05":{
				"mov":{
						"url":"/autovehicule/showroom/furgoane/vivaro/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/furgoane/vivaro/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0A/0A 08":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/corsa-3-door/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/corsa-3-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0A/0A 68":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/corsa-5-door/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/corsa-5-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0I/0I 35":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/insignia-sports-tourer/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/insignia-sports-tourer/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0I/0I 68":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/insignia-5-door/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/insignia-5-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0I/0I 69":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/insignia-4-door/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/insignia-4-door/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0TC/0T 48":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/gama-astra/astra-hatchback.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/astra-hatchback/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0TC/0T 35":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/astra-station-wagon/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/astra-station-wagon/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0TC/0T 69":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/astra-saloon/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/astra-saloon/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0L/0L 26":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/antara/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/antara/configurator.html","isPopup":false},"cfc":{
						"url":"","isPopup":false},"cpc":{
						"url":"","isPopup":false}},
			"0T/0T 08":{
				"mov":{
						"url":"/autovehicule/showroom/autoturisme/astra-gtc/index.html","isPopup":false},"byo":{
						"url":"/autovehicule/showroom/autoturisme/astra-gtc/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.contains(symbolicName);
		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;
	};
})();

