

// 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":"",
				"isPopup":false},"rab":{
			"url":"/tools/opel-request-brochure.html",
				"isPopup":false},"raq":{
			"url":"/tools/opel-contact-us.html",
				"isPopup":false},"rtd":{
			"url":"/tools/opel-contact-us.html",
				"isPopup":false},"rnl":{
			"url":"",
				"isPopup":false},"lnv":{
			"url":"/content/opel/europe/malta/nscwebsite/mt/index.html",
				"isPopup":false},"vco":{
			"url":"",
				"isPopup":false},"epm":{
			"url":"",
				"isPopup":false}
	};
	
	/* object which contains the data of all vehicle specific links*/
	var vehicleLinkdata = {
	};
	
	/**
	 * 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;
	};
})();
