
function GmdsShoppingLink(/*HTMLAnchorElement*/ anchorElem, /*String*/ key) {
	
	var params;
	var thisCarlineCode;
	var thisBodystyleCode;
	
	/**
	 * Sets the VehicleCodes for this shopping link
	 */
	this.setVehicleCodes = function(/*String*/ carlineCode, /*String*/ bodystyleCode) {
		if(!carlineCode){
			mrm.util.log("carlineCode is undefined");
			//Log.warn("carlineCode is undefined");
		}
		if(!bodystyleCode){
			mrm.util.log("bodystyleCode is undefined");
			//Log.warn("bodystyleCode is undefined");
		}
		thisCarlineCode = carlineCode;
		thisBodystyleCode = bodystyleCode;
	};
	
	/**
	 * Public method for setting the label of a shopping link.
	 */
	this.setLinkLabel = function(/*String*/ label) {
		var span = mrm.$('span', anchorElem);
		if(span.length > 0){
			span.empty();			
			span.append(document.createTextNode(label));
		} else {
			var a = mrm.$(anchorElem);
			a.empty();			
			a.append(document.createTextNode(label));
		}
	};
	
	/**
	 * This function is the onClick-handler for "shopping link".
	 */
	this.openLink = function(){
		getGmdsCmsLinks().openCmsPage(key, params, thisCarlineCode, thisBodystyleCode);	
	};
	
	/**
	 * Public method for adding a request-parameter to a shopping link.
	 */
	this.addParam = function(/*String*/ key, /*String*/ value) {
		var localParams = escape(key)+"="+escape(value);
		if(params && params.length > 0){
			params += "&"+localParams;
		} else{
			params = localParams;
		}
		//add params to href
		var url = mrm.$(anchorElem).attr("href");
		if(!url.indexOf("#")==0){
			mrm.$(anchorElem).attr("href",getGmdsCmsLinksUtil().appendParams(url, localParams));
		}
	};
}


function GmdsInAppContentDiv(/*HTMLDivElement*/ standbyContentDiv, /*String*/ divId) {
	
	//hash to store already instantiated GmdsShoppingLink-Objects
	var shoppingLink = {};
	var shoppingLinkKeys = [];
	var thisCarlineCode;
	var thisBodystyleCode;
	
	/**
	 * Returns the shopping link with the specified key, if it is available
	 * within this container.
	 */
	this.getShoppingLink = function(/*String*/ key) {
		if(shoppingLink[key]){
			return shoppingLink[key];
		} else{
			elems = mrm.$("div#"+divId+" div.shoppingLink."+key+" > a");
			if (elems && elems.length == 1) {
				var gmdsShoppingLink = new GmdsShoppingLink(elems[0], key);
				if(thisCarlineCode && thisBodystyleCode){
					gmdsShoppingLink.setVehicleCodes(thisCarlineCode, thisBodystyleCode);
				}
				shoppingLink[key] = gmdsShoppingLink;
				shoppingLinkKeys.push(key);
				return gmdsShoppingLink;
			} else {
				mrm.util.error("found "+elems.length+" shopping links with key "+key);
				//Log.error("found "+elems.length+" shopping links with key "+key);
			}
		}
	};
	
	/**
	 * Sets the vehicle codes into this container. This is required to make
	 * vehicle-dependent shopping links work if the current page does not have a
	 * baseball-card reference.
	 */
	this.setVehicleCodes = function(/*String*/ carlineCode, /*String*/ bodystyleCode) {
		if(!carlineCode){
			mrm.util.log("carlineCode is undefined");
			//Log.warn("carlineCode is undefined");
		}
		if(!bodystyleCode){
			mrm.util.log("bodystyleCode is undefined");
			//Log.warn("bodystyleCode is undefined");
		}
		thisCarlineCode = carlineCode;
		thisBodystyleCode = bodystyleCode;
		
		for (var i = 0; i <shoppingLinkKeys.length; i++){ 
			shoppingLink[shoppingLinkKeys[i]].setVehicleCodes(carlineCode, bodystyleCode);
		}
	};
}


function GmdsInAppContent() {
	
	//hash to store already instantiated GmdsInAppContentDiv-Objects
	var content = {};
	
	/**
	 * Returns the div container with the specific id
	 */
	this.getContent = function(/*String*/ divId) {
		if(content[divId]){
			return content[divId];
		} else{
			var elems = mrm.$("#"+divId);
			if (elems && elems.length == 1) {
				var gmdsInAppContentDiv = new GmdsInAppContentDiv(elems[0],divId);
				content[divId] = gmdsInAppContentDiv;
				return gmdsInAppContentDiv;
			}
		}
	};
}


(function() {
	var gmdsInAppContent = new GmdsInAppContent();
	window.getGmdsInAppContent = function () {
		return gmdsInAppContent;
	};
})();

