var MESSAGEBOX_BUTTON_NONE 			= 0;
var MESSAGEBOX_BUTTON_YES 			= 1;
var MESSAGEBOX_BUTTON_NO 			= 2;
var MESSAGEBOX_BUTTON_OK 			= 4;
var MESSAGEBOX_BUTTON_CANCEL 		= 8;
var MESSAGEBOX_BUTTON_CLOSE			= 16;

var MESSAGEBOX_BUTTON_USR1			= 32;
var MESSAGEBOX_BUTTON_USR2			= 64;

var MESSAGEBOX_BUTTONS_YESNO		= 3;
var MESSAGEBOX_BUTTONS_YESNOCANCEL	= 11;
var MESSAGEBOX_BUTTONS_OKCANCEL		= 12;
var MESSAGEBOX_BUTTONS_USER12		= 96;

var MESSAGEBOX_TYPE_FREE			= 0;
var MESSAGEBOX_TYPE_MODAL			= 1;

var MESSAGEBOX_MESSAGES = null;

/**
 * call-up-function to generate messagebox
 * @param string strBoxTitle box-title
 * @param string strBoxContent box-content
 * @param integer intBoxButtons bitmask of buttons
 * @param integer intBoxWidth the width of the container
 * @param integer intBoxHeight the height of the container
 * @param string strBoxClass css-classname or null if no additional class used
 * @param string strBoxCallback callback-function-name or null if no callback used
 */
function MessageBox(strBoxTitle, strBoxContent, intBoxButtons, intBoxModal, intBoxWidth, intBoxHeight, strBoxClass, strBoxCallback,objButtonLinks)
{
	var m = new MsgBox();
	m.show(strBoxTitle, strBoxContent, intBoxButtons, intBoxModal, intBoxWidth, intBoxHeight, strBoxClass, strBoxCallback,objButtonLinks);
}

/**
 * construct of a messagebox
 * prepares container and set defaults
 */
function MsgBox()
{
	this.containerID 	= 'infoBox';
	this.containerClass = 'infoBoxContainer';
	this.width = 400;
	this.height= 300;
	this.callback = null;
	this.frame = this.createNode("div");
	this.frame.className = "infoBoxContainer";
	//this.frame.style.width = this.width+'px';
	//this.frame.style.height = this.height+'px';
	this.frame.style.zIndex = '10';
	this.frame.callback = null;
	this.frame.innerHTML="";
	this.frame.style.display = 'none';
	/**
	 * hides the container
	 * @param integer intBoxButton the pressed button-id
	 */
	this.frame.hide = function(intBoxButton){
		
		this.style.display = 'none';
		
		if( typeof(this.backpanel) != "undefined" && this.backpanel!=null){
			this.backpanel.style.display = 'none';
			this.backpanel = null;
		}
		if( typeof(intBoxButton) == "undefined" || intBoxButton==null){
			intBoxButton = MESSAGEBOX_BUTTON_NONE;
		}
		
		if(document.all){
			this.objIframe.style.display	=	"none";
			this.objIframe = null;
		}
		
		if(this.callback != null){
			eval(this.callback+'('+intBoxButton+');');
		}
	};
}


/**
 * creates a single node
 * @param string elmName the element-type-name
 * @param string elmContent the content of node or null if empty
 */
MsgBox.prototype.createNode = function(elmName, elmContent)
{
	var elm = document.createElement(elmName);
	if( typeof(elmContent)!="undefined" && elmContent!=null){
		elm.innerHTML=elmContent;
	}
	return elm;
}

/**
 * generates a single button
 * @param node parentNode the button-container-node
 * @param integer intBoxButtonID current button-type-id
 * @param string strBoxButtonTitle button-caption
 */
MsgBox.prototype.createButtonNode = function(parentNode, intBoxButtonID, strBoxButtonTitle,strButtonLink)
{
	
	var tmpNode = this.createNode("div");
	tmpNode.className = "buttonBox";
	var a = this.createNode("a", strBoxButtonTitle);
	if( typeof(strButtonLink) == "undefined" || strButtonLink==null){
		strButtonLink = "javascript:void(0);";
		a.onclick = function() {this.mainNode.hide(this.eventValue);}
	}
	a.href=strButtonLink;
	a.eventValue = intBoxButtonID;
	a.mainNode = this.frame;
	tmpNode.appendChild(a);
	parentNode.appendChild(tmpNode);
}


/**
 * generates buttons
 * @param integer intBoxButtons bitmask of buttons
 * @return node button-container-node
 */
MsgBox.prototype.createButtonNodes = function(intBoxButtons,objButtonLinks)
{
	var buttonlink = null;
	var parentNode = this.createNode("div");
	parentNode.className = "infoBoxButtons";
	//parentNode.style.position = "absolute";
	//parentNode.style.bottom = '20px';
	if( typeof(objButtonLinks) == "undefined" || objButtonLinks==null){
		objButtonLinks = {};
	}
	
	if((intBoxButtons&MESSAGEBOX_BUTTON_YES)==MESSAGEBOX_BUTTON_YES){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_YES) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_YES!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_YES;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_YES, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_YES,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_NO)==MESSAGEBOX_BUTTON_NO){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_NO) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_NO!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_NO;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_NO, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_NO,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_OK)==MESSAGEBOX_BUTTON_OK){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_OK) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_OK!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_OK;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_OK, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_OK,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_CANCEL)==MESSAGEBOX_BUTTON_CANCEL){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_CANCEL) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_CANCEL!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_CANCEL;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_CANCEL, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_CANCEL,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_CLOSE)==MESSAGEBOX_BUTTON_CLOSE){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_CLOSE) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_CLOSE!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_CLOSE;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_CLOSE, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_CLOSE,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_USR1)==MESSAGEBOX_BUTTON_USR1){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_USR1) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_USR1!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_USR1;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_USR1, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_USR1,buttonlink);
	}
	if((intBoxButtons&MESSAGEBOX_BUTTON_USR2)==MESSAGEBOX_BUTTON_USR2){
		if( typeof(objButtonLinks.MESSAGEBOX_BUTTON_USR2) != "undefined" && objButtonLinks.MESSAGEBOX_BUTTON_USR2!=null){
			buttonlink=objButtonLinks.MESSAGEBOX_BUTTON_USR2;
		}else{
			buttonlink=null;
		}
		this.createButtonNode(parentNode,MESSAGEBOX_BUTTON_USR2, MESSAGEBOX_MESSAGES.MESSAGEBOX_BUTTON_USR2,buttonlink);
	}
	
	
	
	return parentNode;
}


/**
 * prepares and displays the box
 * @param string strBoxTitle box-title
 * @param string strBoxContent box-content
 * @param integer intBoxButtons bitmask of buttons
 * @param integer intBoxWidth the width of the container
 * @param integer intBoxHeight the height of the container
 * @param string strBoxClass css-classname or null if no additional class used
 * @param string strBoxCallback callback-function-name or null if no callback used
 */
MsgBox.prototype.show = function(strBoxTitle, strBoxContent, intBoxButtons, intBoxModal, intBoxWidth, intBoxHeight, strBoxClass, strBoxCallback, objButtonLinks)
{
	if( typeof(intBoxModal) == "undefined" || intBoxModal==null){
		intBoxModal = MESSAGEBOX_TYPE_FREE;
	}
	if(intBoxModal==MESSAGEBOX_TYPE_MODAL){
		this.showModalPanel();
	}
	if( typeof(intBoxWidth) == "undefined" || intBoxWidth==null){
		intBoxWidth = this.width;
	}
	if( typeof(intBoxHeight) == "undefined" || intBoxHeight==null){
		//intBoxHeight = this.height;
		intBoxHeight = null;
	}
	if( typeof(strBoxClass) == "undefined" || strBoxClass==null){
		strBoxClass = "";
	}else{
		strBoxClass = " "+strBoxClass;
	}
	if( typeof(strBoxCallback) == "undefined"){
		strBoxCallback = null;
	}
	this.callback = strBoxCallback;
	var posStyle = "";
	var coords   = getWindowCenterPosition();
	var posX = coords['posx'];
	var posY = coords['posy'];
	if(posX != undefined && posY != undefined){
		posX = (posX-intBoxWidth/2);
		posY = (posY-intBoxHeight/2);
	}

	var node = this.createNode("h2");
	node.innerHTML = strBoxTitle;  
	
	this.frame.appendChild(node);
	node = this.createNode("p"); 
	node.innerHTML = strBoxContent;
	//node.innerHTML = "<p>hallo du</p>";
	this.frame.appendChild(node);
	
	node 	 = this.createButtonNodes(intBoxButtons,objButtonLinks); 
	this.frame.appendChild(node);

	this.frame.callback 		= strBoxCallback;
	this.frame.style.left 		= posX+"px";
	this.frame.style.top  		= posY+"px";
	this.frame.className 		= this.containerClass+" "+strBoxClass;
	this.frame.style.width		= intBoxWidth+"px";
	if(intBoxHeight!=null){
		this.frame.style.height		= intBoxHeight+"px";
		
		
	}
	this.frame.style.display 	= 'block';
	this.frame.style.zIndex 	= '102';

	if(document.all){
		node = this.createNode("iframe"); 
		node.className = this.containerClass;
		node.style.position = "absolute";
		node.style.zIndex = '101';
		node.style.left = (posX+4)+"px";
		node.style.top  = (posY+4)+"px";
		
		node.style.width	=	(intBoxWidth-8)+"px";
		if(intBoxHeight!=null){
			node.style.height	=	(intBoxHeight-12)+"px";
		}
		node.style.display	=	"block";
		node.style.borderColor = "FFFFFF";
		node.style.borderWidth = "0";
		document.body.appendChild(node);
		
		this.frame.objIframe = node;
	}
	
	document.body.appendChild(this.frame);
}


/**
 * display the floatbox for modal calls
 */
MsgBox.prototype.showModalPanel = function()
{
	var coords   = getWindowSize();
	var node = this.createNode("div");
	node.className = "infoBoxBackPanel";
	node.style.zIndex = '100';
	if(document.all){
		node.style.position= 'absolute';
	}
	node.style.height = coords['zht'];
	node.style.width  = coords['zwd'];
	this.frame.backpanel = node;
	document.body.appendChild(node);
	
}