/**************************************
**** PRODUCT OVERWRITE FUNCTIONS *********
**************************************/
MessageController.prototype.getBestRelativePopupPosition = function(boundingBox, containerBoundingBox, popupWidth, popupHeight, heightAdjustment) {
	
	//Distance of the popup from it's reference element
	var distanceOfReference = 0;	
		
	var popupLeft = 0;
	var popupTop = 0;
	
	
	var scrollTop = parseInt(document.documentElement.scrollTop) * 2; 
  	var scrollLeft = parseInt(document.documentElement.scrollLeft) * 2; 
  
	//TODO: Modify getStyle so that even when the pageLayout is contained in something else, we can see the REAL offset.
  	var pageLayoutOffsetY = parseInt(getStyle(document.getElementById("pageLayout"), "top"));
  	var pageLayoutOffsetX = parseInt(getStyle(document.getElementById("pageLayout"), "left"));

	var containerLeft = parseInt(containerBoundingBox[this.X1]) - pageLayoutOffsetX;
	var containerTop = parseInt(containerBoundingBox[this.Y1]) - pageLayoutOffsetY;
	var containerWidth = parseInt(containerBoundingBox[this.X2]) - containerBoundingBox[this.X1];
	var containerHeight = parseInt(containerBoundingBox[this.Y2]) - containerBoundingBox[this.Y1];
	
	var refLeft = parseInt(boundingBox[this.X1]) + containerLeft;
	var refTop = parseInt(boundingBox[this.Y1]) + containerTop;
	var refWidth = parseInt(boundingBox[this.X2]) - boundingBox[this.X1];
	var refHeight = parseInt(boundingBox[this.Y2]) - boundingBox[this.Y1];
	
	var leftPosition = parseInt(refLeft) - parseInt(distanceOfReference) - parseInt(popupWidth) + parseInt(scrollLeft);
	var rightPosition = parseInt(refLeft) + parseInt(refWidth) + parseInt(distanceOfReference) + parseInt(scrollLeft);
	
	var topPosition = parseInt(refTop) + parseInt(refHeight) - parseInt(popupHeight) + parseInt(scrollTop) - parseInt(heightAdjustment);
	var bottomPosition = parseInt(refTop) + parseInt(scrollTop) + parseInt(heightAdjustment);
	
	var isLeft = true;
	var isTop = true;
	
	var positionInfo = new Array();
	
	if (refTop > (containerHeight - refTop - refHeight)){
		positionInfo["top"] = topPosition;
		positionInfo["isTop"] = true;
	}
	else{
		positionInfo["top"] = bottomPosition;
		positionInfo["isTop"] = false;
	}
	
	if (refLeft > (containerWidth - refLeft - refWidth)){
		positionInfo["left"] = leftPosition;
		positionInfo["isLeft"] = true;
	}
	else{
		positionInfo["left"] = rightPosition;
		positionInfo["isLeft"] = false;
	}	
		
	return positionInfo;
}


MessageController.prototype.displayMessage = function(params) {
	
	if (typeof(params) == "string"){
		if (params == ""){
			return;
		} else {
			var temp = params;
			params = new Array();
			params["message"] = temp;
		}
	}
	
	params["templateId"] = (params["templateId"] != undefined ? params["templateId"] : this.messageAttributes["templateId"]);
	params["heightAdjustment"] = 0;
	
	var messageWidth = undefined;
	var messageHeight = undefined;
	
	//Detect callout or message width and height from the template
	if (document.getElementById(params["templateId"]) != null){
		messageWidth = (!isNaN(parseInt(document.getElementById(params["templateId"]).style.width)) ? parseInt(document.getElementById(params["templateId"]).style.width) : this.messageAttributes["width"]);
		messageHeight = (!isNaN(parseInt(document.getElementById(params["templateId"]).style.height)) ? parseInt(document.getElementById(params["templateId"]).style.height) : this.messageAttributes["height"]);
		if (document.getElementById(params["templateId"]).heightAdjustment != undefined){
			params["heightAdjustment"] = parseInt(document.getElementById(params["templateId"]).heightAdjustment);
		}
	}
	else{
		messageWidth = this.messageAttributes["width"];
		messageHeight = this.messageAttributes["height"];
	}
	
	params["left"] = (params["left"] != undefined ? params["left"] : this.messageAttributes["left"]);
	params["top"] = (params["top"] != undefined ? params["top"] : this.messageAttributes["top"]);
	params["width"] = (params["width"] != undefined ? params["width"] : messageWidth);
	params["height"] = (params["height"] != undefined ? params["height"] : messageHeight);
	params["duration"] = (params["duration"] != undefined ? params["duration"] : this.messageAttributes["duration"]);
	params["elementReference"] = (params["elementReference"] != undefined ? params["elementReference"] : this.calloutAttributes["elementReference"]);
	params["containerSpace"] = (params["containerSpace"] != undefined ? params["containerSpace"] : this.messageAttributes["containerSpace"]);
	params["isClosableOnClick"] = (params["isClosableOnClick"] != undefined ? params["isClosableOnClick"] : this.messageAttributes["isClosableOnClick"]);
	params["isForcedTopLeft"] = (params["isForcedTopLeft"] != undefined ? params["isForcedTopLeft"] : this.messageAttributes["isForcedTopLeft"]);
	
	//4 valeurs des backgrounds, remplacer dans params
	params["popupImageTopRight"] = (params["popupImageTopRight"] != undefined ? params["popupImageTopRight"] : this.calloutAttributes["popupImageTopRight"]);
	params["popupImageTopLeft"] = (params["popupImageTopLeft"] != undefined ? params["popupImageTopLeft"] : this.calloutAttributes["popupImageTopLeft"]);
	params["popupImageBottomRight"] = (params["popupImageBottomRight"] != undefined ? params["popupImageBottomRight"] : this.calloutAttributes["popupImageBottomRight"]);
	params["popupImageBottomLeft"] = (params["popupImageBottomLeft"] != undefined ? params["popupImageBottomLeft"] : this.calloutAttributes["popupImageBottomLeft"]);
	
	// Move all attributes from the attribute sub-array to root
	params = MapUtils.putAll(params, params["attributes"]);
	params["attributes"] = undefined;
	
	// Lookup for a different message in resources using the code
	if (params["code"] != undefined && resource.get(code) != "") {
		message = resource.get(code);
	} else {
		if (params["message"] == undefined) {
			message = this.messageAttributes["message"];
		} else {
			message = params["message"];
		}
	}
	
	// Look for a message object with the same messageObjectId, and hide it
	for (var messageId in this.messageObjectMap) {
		if (messageId == params["messageObjectId"]) {
			this.hide(messageId);
			break;
		}
	}


	var newMessage = this.initMessageObject(params);
	params["messageObjectId"] = newMessage.id;
		
	if ( newMessage.elementId != undefined ) {

		params["message"] = message;
		params["background_url"] = newMessage.background_url;
		
		newMessage.setContent(params["templateId"] != undefined ? params["templateId"] : this.defaultTemplate, params);
/*		
		if (document.getElementById(params["templateId"]) != undefined){
			newMessage.setContent(ParserUtils.parseTemplate(params["templateId"], params));
		}
		else{
			newMessage.setContent(ParserUtils.parseHtml(this.defaultTemplate, params));
		}
*/		
		// Fades in the message
		if (typeof(Anim) != "undefined") {
			Anim.setOpacity(document.getElementById(newMessage.elementId), 0);
			newMessage.container.style.display = "block";
			Anim.fadeIn(newMessage.elementId);
			newMessage.startThread();
			newMessage.isVisible = true;
			
		} else {
			document.getElementById(newMessage.elementId).style.display = "block";
		}
		
		return newMessage.id;
	}
}


MessageController.prototype.initMessageObject = function(params) {
	
	var id = (params["messageObjectId"] != undefined ? params["messageObjectId"] : "messageObject" + this.messageObjectCount);
	
	var messageObj = new MessageObject(id, this.id);
	
	if (params["elementReference"] != undefined){
		messageObj.elementReference = params["elementReference"];
	}
	
	messageObj.duration = params["duration"];
	messageObj.width = params["width"];
	messageObj.height = params["height"];
	messageObj.isClosableOnClick = params["isClosableOnClick"];
	messageObj.heightAdjustment = params["heightAdjustment"] == undefined ? 0 : params["heightAdjustment"];
	
	var position = undefined;
		
	var backUrl = params["popupBackground"] == undefined ? Workspace.message.messageAttributes["popupBackground"] : params["popupBackground"];
		
	// Forced position
	if (params["isForcedTopLeft"] && (params["referenceBoundingBox"] == undefined && params["elementReference"] == undefined)){
		position = {"left":params["left"], "top":params["top"]};
		
	

	// Get best position based on a bounding box
	} else if (params["referenceBoundingBox"] != undefined) {
		
		position = this.getBestRelativePopupPosition(
			params["referenceBoundingBox"],
			getElementBoundingBox(document.getElementById(params["containerSpace"])),
			messageObj.width,
			messageObj.height,
			messageObj.heightAdjustment
		);			

		// TODO:Remove background handling			
		if (position["isLeft"]) {
			if (position["isTop"]) {
				backUrl = this.calloutAttributes["popupImageBottomRight"];
			} else {
				backUrl = this.calloutAttributes["popupImageTopRight"];
			}
		} else {
			if (position["isTop"]){
				backUrl = this.calloutAttributes["popupImageBottomLeft"];
			} else {
				backUrl = this.calloutAttributes["popupImageTopLeft"];
			}
		}
	
	// Get the best position based on the bounding box of the element in reference
	} else if (params["elementReference"] != undefined) {
		
		position = this.getBestRelativePopupPosition(
			getElementBoundingBox(document.getElementById(params["elementReference"])),
			getElementBoundingBox(document.getElementById(params["containerSpace"])),
			messageObj.width,
			messageObj.height,
			messageObj.heightAdjustment
		);

		// TODO:Remove background handling			
		if (position["isLeft"]) {
			if (position["isTop"]) {
				backUrl = params["popupImageBottomRight"];
			} else {
				backUrl = params["popupImageTopRight"];
			}
		} else {
			if (position["isTop"]){
				backUrl = params["popupImageBottomLeft"];
			} else {
				backUrl = params["popupImageTopLeft"];
			}
		}
	
	// Get the best position centered in the container space element
	} else if (document.getElementById(params["containerSpace"]) != undefined) {	
		position = this.getBestCenteredPopupPosition(getElementBoundingBox(document.getElementById(params["containerSpace"])));
	
	// Centered on page
	} else {
		position = {left:(Workspace.width-messageObj.width)/2, top:(Workspace.height-messageObj.height)/2};
	}
	
	messageObj.background_url = backUrl;
	messageObj.left = position["left"];
	messageObj.top = position["top"];
	
	messageObj.init();
	
	this.messageObjectCount = this.messageObjectCount + 1; 
	this.messageObjectMap[messageObj.id] = messageObj;

	return messageObj;
}

function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		// replace all "-x" by "X"
		while(strCssRule.indexOf("-")>=0){
			strCssRule1 = strCssRule.substring(0,strCssRule.indexOf("-"));
			strCssRule2 = strCssRule.substring(strCssRule.indexOf("-"));
			strCssRule2 = strCssRule2.substring(1); 
			strCssRule2 = strCssRule2.substring(0,1).toUpperCase()+strCssRule2.substring(1);
			strCssRule = strCssRule1+strCssRule2;
		}
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}