if (DHL == undefined) var DHL = {};
if (DHL.UTIL == undefined) DHL.UTIL = {};
DHL.UTIL.MESSAGEBOX = function (objDocument) {

	this.strMessage = "";
	this.strTitle = "";
	this.arrButton = "";
	this.objDocument = objDocument;
	
	this.arrListener = new Object();
	this.arrListener[DHL.UTIL.MESSAGEBOX.EVENT_SHOW] = new Array();
	this.arrListener[DHL.UTIL.MESSAGEBOX.EVENT_HIDE] = new Array();
	this.arrListener[DHL.UTIL.MESSAGEBOX.EVENT_RESPONSE] = new Array();
	
	this.objBckDiv = null;
	this.objAlertDiv = null;
	this.intWidth = 361;
	this.intHeight = 100;
	this.objTemplate = null;
	this.objBoxTemplate = null;
	this.blnClientResponse = false;
	this.intBoxStyle = 1;
	this.checkInterval = null;
	
	this.blnUnclosableBackground = false;
	
	// Style information
	this.strBackgroundColor = "black";
	this.intBackgroundOpacity = 45;
	this.strAlertBorderColor = "white";
	this.strAlertTitleColor = "black";
	this.strAlertMessageColor = "black";
	
	this.arrOtherInstance = new Array();
} 

PUBLIC = DHL.UTIL.MESSAGEBOX.prototype;
PRIVATE = DHL.UTIL.MESSAGEBOX.prototype;
STATIC = DHL.UTIL.MESSAGEBOX;

DHL.UTIL.MESSAGEBOX.BOXSTYLE_OK = 1;
DHL.UTIL.MESSAGEBOX.BOXSTYLE_OKCANCEL = 2;
DHL.UTIL.MESSAGEBOX.BOXSTYLE_NOBUTTON = 3;
DHL.UTIL.MESSAGEBOX.BOXSTYLE_VOTECANCEL = 4;
// DHL.UTIL.MESSAGEBOX.BOXSTYLE_LOGIN_SIGNUP = 5;


DHL.UTIL.MESSAGEBOX.EVENT_SHOW = 'show';
DHL.UTIL.MESSAGEBOX.EVENT_HIDE = 'hide';
DHL.UTIL.MESSAGEBOX.EVENT_RESPONSE = 'response';

PRIVATE._setOpacity = function(obj, intOpacity) {
	obj.style.opacity = intOpacity / 100; 
   	obj.style.MozOpacity = intOpacity / 100; 
   	obj.style.KhtmlOpacity = intOpacity / 100; 
   	if (intOpacity < 100) {
   		obj.style.filter = "alpha(opacity=" + intOpacity + ")";
   	}
}

PRIVATE._getBrowser = function() {
	// Code From http://www.quirksmode.org/js/detect.html
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{string: navigator.userAgent,subString: "Chrome",identity: "Chrome"},
			{string: navigator.userAgent,subString: "OmniWeb",versionSearch: "OmniWeb/",identity: "OmniWeb"},
			{string: navigator.vendor,subString: "Apple",identity: "Safari",versionSearch: "Version"},
			{prop: window.opera,identity: "Opera"},
			{string: navigator.vendor,subString: "iCab",identity: "iCab"},
			{string: navigator.vendor,subString: "KDE",identity: "Konqueror"},
			{string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},
			{string: navigator.vendor,subString: "Camino",identity: "Camino"},
			{string: navigator.userAgent,subString: "Netscape",identity: "Netscape"},
			{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"},
			{string: navigator.userAgent,subString: "Gecko",identity: "Mozilla",versionSearch: "rv"},
			{string: navigator.userAgent,subString: "Mozilla",identity: "Netscape",versionSearch: "Mozilla"}
		],
		dataOS : [
			{string: navigator.platform,subString: "Win",identity: "Windows"},
			{string: navigator.platform,subString: "Mac",identity: "Mac"},
			{string: navigator.userAgent,subString: "iPhone",identity: "iPhone/iPod"},
			{string: navigator.platform,subString: "Linux",identity: "Linux"}
		]
	
	};
	BrowserDetect.init();
	return BrowserDetect;
}

PRIVATE._getDefaultTemplate = function(objContainer) {
	var objTemplate = this.objDocument.createElement("div");
	var objTitle = this.objDocument.createElement("p");
	var objMessage = this.objDocument.createElement("p");
	var objClose = this.objDocument.createElement("a");
	var objButtonContainer = this.objDocument.createElement('div');
	objContainer.appendChild(objTemplate);
	objTitle.id = "title"
	objTitle.style.padding="0";
	objTitle.style.margin="0";
	
	objMessage.id = "description"
	objMessage.style.padding="0";
	objMessage.style.margin="0";
	
	objTemplate.style.position = "relative";
	objTemplate.style.minWidth = this.intWidth + "px";
	objTemplate.style.minHeight = this.intHeight + "px";
	objTemplate.style.backgroundColor = "white";
	
	objTemplate.style.padding = "0";
	objTemplate.style.margin = "0";
	
	objTemplate.style.textAlign = "center";
	objTemplate.appendChild(objTitle);
	objTemplate.appendChild(objClose);
	objTemplate.appendChild(objMessage);
	objTemplate.appendChild(objButtonContainer);

	objClose.id ="close";
	objClose.style.position = "absolute";
	objClose.style.right = "2px";
	objClose.style.bottom = "2px";
	objClose.href="#";
	objClose.innerHTML = "close";
	
	objButtonContainer.id="buttonPlaceHolder";
	
	
	var me = this;
	objClose.onclick=function() {
		me.hide();
		return false;
	}
	
	return objTemplate;
}


PUBLIC._createButton = function(buttonText, returnValue ) {
	var objButton = this.objDocument.createElement("input");
	objButton.type = "button";
	objButton.value = buttonText;
	objButton.style.textAlign = 'center';
	objButton.style.margin = '10px';
	var me = this;
	objButton.onclick = function() {
		me.blnClientResponse = returnValue;
		me.notify(DHL.UTIL.MESSAGEBOX.EVENT_RESPONSE);
		me.hide();
	}	
	return objButton;
}


PUBLIC._addButton = function() {
	var objContainer = this.objDocument.createElement("div");
	var cnt = 0;
	for (index in this.arrButton)
    {
		var curButton = 'objButton'+index;
		if(this.arrButton[index] && this.arrButton[index] != ''){
			var curButton = this._createButton(this.arrButton[index]['buttonText'],this.arrButton[index]['returnValue']);
			objContainer.insertBefore(curButton, objContainer.firstChild);
		}
	cnt++;
    }
	if(cnt < 1){
		var curButton = 'objButton'+'DEFAULT';
		var curButton = this._createButton(' OK ', true);
		objContainer.insertBefore(curButton, objContainer.firstChild);
	}
	return objContainer;
}



/**
 * This method was pulled from windowsize.js
 * 
 */
PRIVATE._getWindowSize = function() {
	var intWidth = 0, intHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    // Non-IE
    intWidth = this.objDocument.window.innerWidth;
    intHeight = this.objDocument.window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    // IE 6+ in 'standards compliant mode'
    intWidth = this.objDocument.documentElement.clientWidth;
    intHeight = this.objDocument.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    // IE 4 compatible
    intWidth = this.objDocument.body.clientWidth;
    intHeight = this.objDocument.body.clientHeight;
  }
  
	arrReturn = new Object();
	arrReturn['width'] = intWidth;
	arrReturn['height'] = intHeight;
	return arrReturn;
}

PRIVATE._createBox = function() {
	blnReturn = false;
	if(this.objDocument) {
		if(this.objBckDiv == null && this.objAlertDiv == null) {

			this.objAlertDiv = this.objDocument.createElement("div");
			this.objBckDiv = this.objDocument.createElement("div");

			this.objDocument.body.insertBefore(this.objBckDiv, this.objDocument.body.firstChild);
			this.objDocument.body.insertBefore(this.objAlertDiv, this.objDocument.body.firstChild);

			// ////////////////////////////////////
			// Background Style Definition
			this.objBckDiv.style.display = "block";
			this.objBckDiv.style.position = "fixed";
			this.objBckDiv.style.top = "0px";
			this.objBckDiv.style.left = "0px";
			this.objBckDiv.style.backgroundColor = this.strBackgroundColor;
			this.objBckDiv.style.width="100%";
			this.objBckDiv.style.height="100%";

			this.objBckDiv.style.padding = 0;
			this.objBckDiv.style.margin = 0;
			this.objBckDiv.style.zIndex = 99;
			this._setOpacity(this.objBckDiv, this.intBackgroundOpacity);

			var me = this;
			if(!this.blnUnclosableBackground) {
				this.objBckDiv.onclick = function() {
					me.hide();
				}
			}

			 // //////////////////////////////////////////
			 // Display the login box
			 this.objAlertDiv.id ="msgBoxContainer";
			 this.objAlertDiv.style.display = "block"
			 this.objAlertDiv.style.position = "fixed";
			 this.objAlertDiv.style.top = "50%";
			 this.objAlertDiv.style.left = "50%";
			 this.objAlertDiv.style.marginRight = "50%";
			 this.objAlertDiv.style.marginTop = "-" + (Math.round(this.intHeight / 2)) + "px";
			 this.objAlertDiv.style.marginLeft = "-" + (Math.round(this.intWidth / 2)) + "px";
			 this.objAlertDiv.style.zIndex = 10;
			 this.objAlertDiv.align="center";
			 this.objAlertDiv.vAlign="middle";
			 this.objAlertDiv.style.zIndex = 100;
			 //this._setOpacity(this.objAlertDiv, 100);

			 if(this.objBoxTemplate && this.objBoxTemplate.addTemplate) {
			 	this.objTemplate = this.objBoxTemplate.addTemplate(this.objDocument, this.objAlertDiv);
			 	this.objTemplate.style.width = this.intWidth + "px";
			 	this.objTemplate.style.height = this.intHeight + "px";
			 }
			 else {
			 	this.objTemplate = this._getDefaultTemplate(this.objAlertDiv);
			 }

			 var objBrowser = this._getBrowser();
			 if(objBrowser.browser == "Explorer" && objBrowser.version == 6) {
			 	this.objBckDiv.style.position="absolute";
			 	this.objBckDiv.style.top = "0";
			 	this.objBckDiv.style.left = "0";

			 	this.objAlertDiv.style.position = "absolute";

			 	var arrSize = this._getWindowSize();

			 	this.objBckDiv.style.width = (arrSize['width']+20) + "px";
				this.objBckDiv.style.height = (arrSize['height']+15) + "px";

			 	var me = this;
			 	this.checkInterval = window.setInterval(
			 		function() {
			 			me.move_box();
			 		}
			 	, 10);
			 }
		}

		 // add Button
		 var arrTemplate = this.objTemplate.getElementsByTagName('*');
		 if(arrTemplate["close"]) {
			 arrTemplate["close"].style.color = this.strAlertTitleColor;
		 	 var me = this;
			 arrTemplate["close"].onclick = function() {
			 	me.hide();
			 }
		 }
		 
		 if(this.strTitle && arrTemplate["title"]) {
		 	arrTemplate["title"].innerHTML = this.strTitle;
		 }
		 if(this.strMessage && arrTemplate["description"]) {
		 	arrTemplate["description"].innerHTML = this.strMessage;
		 }
		 
		 if(arrTemplate["buttonPlaceHolder"]) {
		 	var objButtonContainer = this._addButton(this.arrButton);
			while(arrTemplate["buttonPlaceHolder"].firstChild) {
				arrTemplate["buttonPlaceHolder"].removeChild(arrTemplate["buttonPlaceHolder"].firstChild);
			}
			arrTemplate["buttonPlaceHolder"].appendChild(objButtonContainer);
		 }
		 
		 if(
		 	this.objTemplate.applyStyle &&
		 	this.objBckDiv
		 ) {
		 	this.objTemplate.applyStyle(this.objBckDiv);
		 }
		 
		blnReturn = true;
	}
	return blnReturn;
}


PUBLIC.setWidth = function(intWidth) {
	this.intWidth = intWidth;
}

PUBLIC.setHeight = function(intHeight) {
	this.intHeight = intHeight;
}

PUBLIC.addListener = function(strEvent, ptFunction) {
	this.arrListener[strEvent].push(ptFunction);
}

PUBLIC.addOtherInstance = function (objHtml) {
	this.arrOtherInstance.push(objHtml);
}
PRIVATE.hideOtherInstance = function () {
	for(var intI = 0; intI < this.arrOtherInstance.length; intI++) {
		this.arrOtherInstance[intI].hide();
	}
}

PRIVATE.hide = function() {
	if(this.objAlertDiv) {
		while (this.objAlertDiv.firstChild) {
  			this.objAlertDiv.removeChild(this.objAlertDiv.firstChild);
		}	
		this.objAlertDiv.parentNode.removeChild(this.objAlertDiv);
		this.objAlertDiv = null;
	}
	
	if(this.objBckDiv) {
		while (this.objBckDiv.firstChild) {
  			this.objBckDiv.removeChild(this.objBckDiv.firstChild);
		}
		this.objBckDiv.parentNode.removeChild(this.objBckDiv);
		this.objBckDiv = null;
	}
	
	this.notify(DHL.UTIL.MESSAGEBOX.EVENT_HIDE);
	if(this.checkInterval != null) {
		window.clearInterval(this.checkInterval);
		this.checkInterval = null;
	}
	return false;
}


PRIVATE.notify = function(strEvent) {
	for(var intI = 0; intI < this.arrListener[strEvent].length; intI++) {
		if(this.arrListener[strEvent][intI]) {
			this.arrListener[strEvent][intI](this);
		}
	}
}


/**
 * This method display a box with only an ok button
 * 
 * @param string
 *            strMessage
 * @return void
 */
PUBLIC.alert = function(strTitle,strMessage,arrButton) {
	if (this.arrOtherInstance && this.arrOtherInstance.length > 0) {
		this.hideOtherInstance();
	}
	this.strTitle = strTitle;
	this.strMessage = strMessage;
	this.arrButton = arrButton;
	 	this.intBoxStyle = DHL.UTIL.MESSAGEBOX.BOXSTYLE_OK;
	if(this._createBox()) {
		this.notify(DHL.UTIL.MESSAGEBOX.EVENT_SHOW);
	}
}

/*
 * This method will display the combo box with a ok and a cancel button @param
 * string strMessage @return boolean
 */
PUBLIC.confirm = function(strTitle, strMessage) {
	if (this.arrOtherInstance && this.arrOtherInstance.length > 0) {
		this.hideOtherInstance();
	}
	this.strTitle = strTitle;
	this.strMessage = strMessage;
	this.intBoxStyle = DHL.UTIL.MESSAGEBOX.BOXSTYLE_OKCANCEL;
	if(this._createBox()) {
		this.notify(DHL.UTIL.MESSAGEBOX.EVENT_SHOW);
	}
}

/**
 * This method will display the message box with no button
 * 
 * @param string strMessage
 * @return boolean
 */

PUBLIC.message = function(strTitle, strMessage) {
	if (this.arrOtherInstance && this.arrOtherInstance.length > 0) {
		this.hideOtherInstance();
	}
	this.strTitle = strTitle;
	this.strMessage = strMessage;
	this.intBoxStyle = DHL.UTIL.MESSAGEBOX.BOXSTYLE_NOBUTTON;
	var arrButton = this.arrButton;
	this.arrButton = [];
	if(this._createBox()) {
		this.notify(DHL.UTIL.MESSAGEBOX.EVENT_SHOW);
	}
	this.arrButton = arrButton;
}

PUBLIC.voteMessage = function(strTitle, strMessage) {
	if (this.arrOtherInstance && this.arrOtherInstance.length > 0) {
		this.hideOtherInstance();
	}
	this.strTitle = strTitle;
	this.strMessage = strMessage;
	this.intBoxStyle = DHL.UTIL.MESSAGEBOX.BOXSTYLE_VOTECANCEL;
	if(this._createBox()) {
		this.notify(DHL.UTIL.MESSAGEBOX.EVENT_SHOW);
	}
}


PUBLIC.getClientResponse = function() {
	return this.blnClientResponse;
}
PUBLIC.setBackgroundColor = function(strColor) {
	this.strBackgroundColor = strColor;
}
PUBLIC.setBackgroundOpacity = function(intOpacity) {
	this.intBackgroundOpacity = intOpacity;
}
PUBLIC.setBoxBorderColor = function(strColor) {
	this.strAlertBorderColor = strColor;
}
PUBLIC.setBoxTitleColor = function(strColor) {
	this.strAlertTitleColor = strColor;
}
PUBLIC.setBoxMessageColor = function(strColor) {
	this.strAlertMessageColor = strColor;
}
PUBLIC.setTemplate = function(objTemplate) {
	this.objBoxTemplate = objTemplate;
}

PUBLIC.lockBackground = function(blnLockBackground) {
	this.blnUnclosableBackground = blnLockBackground;
}

PRIVATE.move_box = function () {
	var offset = 0; // set offset (likely equal to your css top)
	// this.objBckDiv.style.top = (this.objDocument.documentElement.scrollTop +
	// offset) + 'px';
	this.objAlertDiv.style.top = (this.objDocument.documentElement.scrollTop + offset + 300) + 'px';
}


