var msgbox = {
	loaded:false,

	init: function() {
	this.onclose=null;
	this.onok=null;
	this.msgcount=0;
	this.reference=null;
	this.loaded=true;
	this.dragmode=false;  //drag mode
	this.dragable=false; //drag flag
	
	this.config=msg_config;
	
	//cache images
	if(msg_config.imageCache!="") {this.cacheImages();};
	
	//create shadow box
	var element=document.createElement('DIV')
    document.body.appendChild(element);  
    element.id=msg_config.instanceName2;  

	
	var style2=msg_config.shadow[0];
	element.style.cssText+=style2.line1;
	element.style.cssText+=style2.line2;
	

    element.id=msg_config.instanceName2;  
	this.shadow=document.getElementById(msg_config.instanceName2); //screen shadow

	this.resize2();

    //create message holder
	var element=document.createElement('DIV')
    document.body.appendChild(element);  
    element.id=msg_config.instanceName;  

	this.shadowbox=document.getElementById(msg_config.instanceName); //contains messagebox

	
	//attach events	for shadowbox
	 if(window.addEventListener){ // Mozilla, Netscape, Firefox
	 	window.addEventListener('resize', msgbox.resize, false);
	 	window.addEventListener('error', msgbox.error, false);
	 	window.document.addEventListener('mousedown', msgbox.mousedown, false);
	 	window.document.addEventListener('mouseup', msgbox.mouseup, false);
	 	window.document.addEventListener('mousemove', msgbox.mousemove, false);
	 } else { // IE
	 	window.attachEvent('onresize',msgbox.resize);
	 	window.attachEvent('onerror',msgbox.error);
	 	window.document.attachEvent('onmousedown',msgbox.mousedown);
	 	window.document.attachEvent('onmouseup',msgbox.mouseup);
	 	window.document.attachEvent('onmousemove',msgbox.mousemove);
	 }
	},
	
	
	cacheImages:function () {
	//alert(this.cache);
	var icache=new Array();
	for(key in msg_config.images[0]) {
	 var pos=icache.length;
	 var icon=msg_config.imagePath + msg_config.images[0][key];
	 icache[pos]= new Image(5,5);
	 icache[pos].src=icon;
	}
	 return;
	},
	
	resize2:function() {
	var scnWid,scnHei;
	if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}

		
	this.shadow.style.width=scnWid+document.body.scrollLeft;
	this.shadow.style.height=scnHei+document.body.scrollTop;  

	return;
	},
	
	resize:function(e) {
	setTimeout(function() {
	msgbox.resize2();
	},200);
	},


	ajaxRequest:function () {
	   var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
	   if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		for (var i=0; i<activexmodes.length; i++){
			try { 
				return new ActiveXObject(activexmodes[i])
			}
		catch(e){
			//suppress error
			}
		}
	}
		else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return new XMLHttpRequest()
		else
	   return false;
	},
	
	replaceText:function(text) {
	 this.message=text;
	 this.textbox.innerHTML="<nobr>"+text+"</nobr>";
	},
	
	runEvent:function(handler) {
	var e=eval("this."+handler);
	if(typeof(e)=="function") {
	var ef=e+"";i=ef.indexOf("{");j=ef.lastIndexOf("}");ef=ef.substring(i+1,j);
	with(this) {try {eval(ef);} catch(err) {void(0);return false;}}
	}
	return true;
	},

	
	setText:function(text) {this.replaceText(text);},

	setIcon:function(button) {	
	 var images=msg_config.images[0];
	 try {
	 var icon=eval("images."+button);
	 } catch(exception) {
	 return false;
	 }
	 icon=msg_config.imagePath+icon; 
	 var temp=msg_config.iconWidth;
	 if(temp!="") {temp=" width='"+temp+"' ";}
	 this.message="<img "+temp+" align='absmiddle' src='"+icon+"'>&nbsp;&nbsp;"+this.message;	
	 this.replaceText(this.message);
	 return true;
	},
	
	setTitle:function(text) {
	 this.title=text;
	 this.titlebox.innerHTML="<nobr>"+text+"</nobr>";	
	},
	
	
	mousedown:function(e) {	
	//select mode
	if(!msgbox.dragmode) {msgbox.dragable=false;return;}
	
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
		
	//bubble
	if(targ.parentNode.tagName=="TD") {targ=targ.parentNode;}
	if(targ.id!="m_titlebox") {return;}

	//switch target
	this.mbox=document.getElementById("m_mbox");
	targ=this.mbox;
	
	this.dragable=true;
	this.pixelLeft=targ.offsetLeft
	this.pixelTop=targ.offsetTop
	this.clientX=e.clientX
	this.clientY=e.clientY
	},
	
	mouseup:function(e) {
	this.dragable=false; 
	},

	error:function(e) {
	//alert("Error detected!");
	//return true; 
	},
	
	mousemove:function(e) {
	var targ;
	if (!e) var e = window.event;

	//if button is not left in IE/NE return
	if((document.all&&e.button!=1)||(!document.all&&e.button!=0)||(!this.dragable)) {return;}
	
	//move it
	try {
	var cLeft=this.pixelLeft+e.clientX-this.clientX;
	var cTop=this.pixelTop+e.clientY-this.clientY;
	this.mbox.style.left=cLeft;
	this.mbox.style.top=cTop;
	} catch(err) {
	 this.dragable=false;
	 void(0);
	}
	
	},
	
	setActionText:function(text) {
	 this.action=text;
	 this.actionbtn.value=text;	
	},
	
	setCloseText:function(text) {
	 this.close=text;
	 this.closebtn.value=text;	
	},
	
	
	
	doModal:function() {this.shadow.style.display="block";},
	
	loadMedia:function(filename) {
	var i=filename.lastIndexOf(".");
	var ext=filename.substr(i+1,filename.length);
	switch(ext) {
	case "css":
	var fileref=document.createElement("link")
	fileref.setAttribute("rel", "stylesheet")
	fileref.setAttribute("type", "text/css")
	fileref.setAttribute("href", filename)
	break;
	case "js":
	var fileref=document.createElement('script')
	fileref.setAttribute("type","text/javascript")
	this.fileref=fileref;	
	fileref.setAttribute("src", filename);  
	break;
	default:
	return
	}
	//complete insertion
	document.getElementsByTagName("head")[0].appendChild(fileref);
	return;
	},


//load ajax	
	load:function() {
	var jfunc="";
	switch(this.load.arguments.length) {
	case 1:
	url=this.load.arguments[0];
	break;
	case 2:
	url=this.load.arguments[0];
	jfunc=this.load.arguments[1];
	break;
	default:
	return false;
	}
	
	//jfunc validate start
	if(jfunc!="") {
	try {eval(jfunc);this.jfunc=null;alert("The function "+jfunc+" already exist!");} catch(err) {
	if(err.code!=null) {alert("The function "+jfunc+" already exist!");this.jfunc=null;} else {
	this.jfunc=jfunc; //save for future reference
	}}}
	//jfunc validate stops
	
	if(url=="") {return false;}
	var s = url.split(",");

	url=s[0]; //default ajax page
	if(s.length>1) {
	 for(i=1;i<s.length;i++) {
	  this.loadMedia(s[i]);
	 }	
	}

	
	var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	if(this.msgcount==0) {this.show(this.config.ajaxLoading,"","","title=false|action=close");}
	this.mygetrequest=new this.ajaxRequest()
    this.mygetrequest.onreadystatechange=function(){
    if (msgbox.mygetrequest.readyState==4){
     if (msgbox.mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
      var data=msgbox.mygetrequest.responseText;
	  msgbox.replaceText(data);
	  msgbox.onload2();
	  
    }
	else{
	  msgbox.replaceText(msg_config.ajaxError);
	  msgbox.runEvent("onfail2");
	 }
	}
	}
	msgbox.mygetrequest.open("POST", url+bustcacheparameter, true);
	msgbox.mygetrequest.send(null);		
},
	
	jload:function(url) {
	var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	this.mygetrequest2=new this.ajaxRequest()
    this.mygetrequest2.onreadystatechange=function(){
    if (msgbox.mygetrequest2.readyState==4){
     if (msgbox.mygetrequest2.status==200 || window.location.href.indexOf("http")==-1){
      var data=msgbox.mygetrequest2.responseText;
	  msgbox.onjload2(data);
    }
	else{
	  msgbox.runEvent("onjsfail");
	 }
	}
	}
	msgbox.mygetrequest2.open("POST", url+bustcacheparameter, true);
	msgbox.mygetrequest2.send(null);		
	},
	
	onload2:function() {  //ajax load successful!
		//clear out abort
		this.onabort=null;
		this.runEvent("onload");

		//check for jfunc
		if(this.jfunc!=null) {
		 try {eval(this.jfunc);this.jfunc=null;} catch(err) {setTimeout("msgbox.loadFunc()",1000);}
		}
	},

	onjload2:function(jscript) {  //ajax load successful!
	    try {eval(jscript)} catch(err) {void(0);}
		if(this.onjsload!=null) {setTimeout(msgbox.onjsload,1);}	
	},
	
	loadFunc:function() {
	try {
	eval(this.jfunc);
	if(this.onjsload!=null) {setTimeout(msgbox.onjsload,1);}
	} catch(err) {
	 if(this.onjsfail!=null) {setTimeout(msgbox.onjsfail,1);}	
	}
	this.jfunc=null;
	return false;
	},

	onfail2:function() {

		if(this.onfail!=null) {setTimeout(msgbox.onfail,1);}			
	},
	
	recall:function() {
	 this.shadowbox.style.display="block";
	},

	move:function() {
	switch(this.move.arguments.length) {
	case 1:
	this.mbox.style.left=parseInt(this.move.arguments[0])+"px";
	break;
	case 2:
	this.mbox.style.left=parseInt(this.move.arguments[0])+"px";
	this.mbox.style.top=parseInt(this.move.arguments[1])+"px";
	break;
	}
	return;
	},

	moveTo:function() {
	switch(this.moveTo.arguments.length) {
	case 1:
	this.mbox.style.left=parseInt(this.moveTo.arguments[0])+"px";
	break;
	case 2:
	this.mbox.style.left=parseInt(this.moveTo.arguments[0])+"px";
	this.mbox.style.top=parseInt(this.moveTo.arguments[1])+"px";
	break;
	}
	return;
	},
	
	moveBy:function() {
	switch(this.moveBy.arguments.length) {
	case 1:
	this.moveLeft(this.moveBy.arguments[0],true);
	break;
	case 2:
	this.moveLeft(this.moveBy.arguments[0],true);
	this.moveTop(this.moveBy.arguments[1],true);
	break;
	default:
	alert("Invalid number of parameters.");
	}
	return;
	},


	moveTop:function() {
	switch(this.moveTop.arguments.length) {
	case 1:
	this.mbox.style.top=parseInt(this.moveTop.arguments[0])+"px";
	break;
	case 2:
	this.mbox.style.top=parseInt(this.mbox.offsetTop) + parseInt(this.moveTop.arguments[0])+"px";
	break;
	default:
	alert("Invalid params");
	break;
	}
	},

	moveLeft:function() {
	switch(this.moveLeft.arguments.length) {
	case 1:
	this.mbox.style.left=parseInt(this.moveLeft.arguments[0])+"px";
	break;
	case 2:
	this.mbox.style.left=parseInt(this.mbox.offsetLeft) + parseInt(this.moveLeft.arguments[0])+"px";
	break;
	default:
	alert("Invalid params");
	break;
	}
	},

	estyle:function() {
	with(this) {
	titlebox.style.cssText+=  ";background:#b58f45;color:#ffffe1;font:bold 12px tahoma;padding-top:5px;padding-bottom:5px;cursor:pointer;cursor:hand;";
	btnbox.style.cssText+= ";background:#cbb17d;";
	actionbtn.style.cssText+= ";background:#ffffe1;color:#996600;";
	dragmode=true;
	}	
	},
	
	show:function() {
	if(!this.loaded) {this.init();}
	//clear cache
	this.message="";
	this.button="";
	this.title="";
	this.options="";
	this.action="";
	this.close="";

	this.callback="msgbox.hide();"; //default callback	

	switch(this.show.arguments.length) {
	case 0:	
	//no parameters will recall
	this.recall();
	return false;
	case 1: //message only
	this.message=this.show.arguments[0];
	this.title=msg_config.defaultTitle;
	break;
	case 2: //message,button only
	this.message=this.show.arguments[0];
	this.button=this.show.arguments[1];
	this.title=msg_config.defaultTitle;
	break;
	case 3: //message,button,title only
	this.message=this.show.arguments[0];
	this.button=this.show.arguments[1];
	this.title=this.show.arguments[2];
	break;
	case 4: //message,button,title, options
	this.message=this.show.arguments[0];
	this.button=this.show.arguments[1];
	this.title=this.show.arguments[2];
	this.options=this.show.arguments[3];
	break;
	case 5: //message,button,title, options,callback
	this.message=this.show.arguments[0];
	this.button=this.show.arguments[1];
	this.title=this.show.arguments[2];
	this.options=this.show.arguments[3];
	this.callback=this.show.arguments[4];
	break;
	default:
	alert("Too many parameters");
	return false;
	}

	//fetch table
	element=document.getElementById(msg_config.instanceName);
	
	var table=msg_config.table[0];
	var style=msg_config.style[0];
	var images=msg_config.images[0];
	
	if(this.button!="") {
	try {
	var icon=eval("images."+this.button);
	} catch(exception) {
	var icon=null;
	}
	
	if(icon!=null) {
	icon=msg_config.imagePath+icon; 
	//this.message="<table><tr><td><img width=22 align='left' src='"+icon+"'></td><td>"+this.message+"</td></tr></table>";	
	var temp=msg_config.iconWidth;
	if(temp!="") {temp=" width='"+temp+"' ";}
	this.message="<img "+temp+" align='absmiddle' src='"+icon+"'>&nbsp;&nbsp;"+this.message;	
	}
	}

	var source=table.line1;
	source+=table.line2;
	source+=table.line3;
	source+=table.line4;

	//optimization	
	var key; var val;
	if(this.options!="") {
	var s = this.options.split("|");
	
	//opti-A
	for(var i=0;i<s.length;i++) {
	   key=s[i].split("=")[0];
	   val=s[i].split("=")[1];
	   switch(key) {
	   case "action":
	   this.action=val;
	   break;
	   case "close":
	   this.close=val;
	   break;
	   }
	 }	
	}
	
	
	if(this.action=="") {this.action=" OK ";}
	if(this.close=="") {this.close=" Cancel ";}
	
	//substitutions
	source=source.replace(/#title/,"<nobr>" + this.title + "</nobr>");
	source=source.replace(/#text/, "<nobr>"+this.message+"</nobr>");
	source=source.replace(/#action/, this.action);
	source=source.replace(/#close/, this.close);

	//render
	element.innerHTML=source;
	
	//fetch objects
	var tab=element.getElementsByTagName("TABLE");
	this.mbox=tab[0];
	var tds=element.getElementsByTagName("TD");
	var buttons=tds[2].getElementsByTagName("INPUT");
	
	//store references for future usage
	this.titlebox=tds[0]; 
	this.textbox=tds[1];
	this.btnbox=tds[2];
	this.actionbtn=buttons[0];
	this.closebtn=buttons[1];
	
	//create ids
	this.mbox.id="m_mbox"; 
	this.titlebox.id="m_titlebox"; this.titlebar=this.titlebox;
	this.textbox.id="m_textbox";
	this.btnbox.id="m_btnbox";
	this.actionbtn.id="m_actionbtn";
	this.closebtn.id="m_closebtn";
	

	//apply style rules
	this.mbox.style.cssText=style.cover;
	this.titlebox.style.cssText=style.title;
	this.textbox.style.cssText=style.text;
	this.btnbox.style.cssText=style.btn;
	this.actionbtn.style.cssText=style.action;
	this.closebtn.style.cssText=style.close;

	//attach callback functions
	this.actionbtn.onclick = function() {
	 try {eval(msgbox.callback);} catch(exception) {void(0);}	
	 if(msgbox.onok!=null) {setTimeout(msgbox.onok,1);}
	};

	this.closebtn.onclick = function() {msgbox.hide();};

	
	//optimization

	//opti-B
	if(this.options!="") {
	 for(var i=0;i<s.length;i++) {
	   key=s[i].split("=")[0];
	   val=s[i].split("=")[1];
	    switch(key) {
	    case "type":
	     if(val=="OKOnly") {this.closebtn.style.display="none";this.btnbox.style.textAlign="center";}
	     else if(val=="OKCancel") {void(0);}
	     break;
	    }
	 }	
	}
	
	//default button: ok
	if(this.options.indexOf('type=')==-1) {this.closebtn.style.display="none";this.btnbox.style.textAlign="center";}
	
	//optimize width
	if(parseInt(tab[0].offsetWidth)<parseInt(msg_config.minWidth)) {tab[0].style.width=msg_config.minWidth+"px";};
	
	//setup position
	element=document.getElementById(msg_config.instanceName).style.display="none";	
	this.resize2();
	
	this.mbox.style.cssText+=";position:absolute;";
	topx=(document.body.clientHeight/2);
	topx=topx-parseInt(this.mbox.clientHeight)+document.body.scrollTop;
	this.mbox.style.top=topx;

	leftx=(document.body.clientWidth/2);
	leftx=leftx-(parseInt(this.mbox.clientWidth)/2)+(document.body.scrollLeft/2);
	this.mbox.style.left=leftx;
	

	

	//opti-C
	//preset drag mode
	this.dragmode=false;
	this.titlebar.style.cursor="default";
	
	if(this.options!="") {
	 for(var i=0;i<s.length;i++) {
	   key=s[i].split("=")[0];
	   val=s[i].split("=")[1];
	    switch(key) {
	    case "modal":
	     if(val=="true") {
		 //alert('Modal');
		 this.shadow.style.display="block";
		 }
	     break;
	    case "drag":
	     if(val=="true") {
		 this.dragmode=true;
		 this.titlebar.style.cssText+=";cursor:pointer;cursor:hand;";
		 }
	     break;
	    case "title":
	     if(val=="false") {this.titlebox.style.display="none";}
	     break;
	    case "buttons":
	     if(val=="false") {this.btnbox.style.display="none";}
	     break;
	    case "left":
		 this.mbox.style.left=val+"px";
	     break;
	    case "left":
		 this.mbox.style.left=val+"px";
	     break;
	    case "top":
		 this.mbox.style.top=val+"px";
	     break;
	    case "width":
		 this.mbox.style.width=val+"px";
	     break;
	    case "height":
		 this.mbox.style.height=val+"px";
	     break;
	    }
	  }	
	 }
	 
	this.shadowbox.style.display="block";
	
	//apply default style
	
	this.estyle();
	
	
	//reference dim override
	if(this.reference!=null&&this.reference.style.display!='none') {
	 var l=this.getElementLeftPosition(this.reference);
	 var t=this.getElementTopPosition(this.reference);
	 l += (this.reference.offsetWidth/2);
	 l -= (this.mbox.offsetWidth/2);

	 t += (this.reference.offsetHeight/2);
	 t -= (this.mbox.offsetHeight/2);
	 this.mbox.style.left=l;
	 this.mbox.style.top=t;
	}

	this.msgcount++;
},

	isinView: function (oObject) {
	var oParent = oObject.offsetParent;
	var iOffsetTop = oObject.offsetTop;
	var iClientHeight = oParent.clientHeight;
	if (iOffsetTop > iClientHeight) {return true;} else{return false;}
	},

	abort: function() {
	this.mygetrequest.abort();	
	this.onclose=null; //override
	this.hide();
	//alert("Connection aborted..");
	this.runEvent("onabort");
	},

	getElementLeftPosition: function(e){
	var x=0;
	while(e){
	x+=e.offsetLeft+e.clientLeft;
	e=e.offsetParent;
	}
	return x;
	},

	getElementTopPosition: function(e){
	var y=0;
	while(e){
	y+=e.offsetTop+e.clientTop;
	e=e.offsetParent;
	}
	return y;
	},
	
	
	jeval:function(fx) {
	with(this) {
	if(typeof(fx)=="string") {
	if(fx.indexOf('on')!=0) {fx='on'+fx;}
	try {var e=eval("this."+fx);if(typeof(e)!="function") return false;} catch(err) {return false;}
	} else {
	if(typeof(fx)=="function") {var e=fx;} else {return false;}
	}	
	//conversion
	var ef=e+"";i=ef.indexOf("{");j=ef.lastIndexOf("}");ef=ef.substring(i+1,j);
	try {eval(ef);} catch(err) {void(0);return false;}
	}
	return true;
	},

	
	createEvent:function(evt,target,handler) {
	if(this.createEvent.arguments.length!=3) {return false;}	
	  with(this) 
		{
		e=function() {jeval(handler);}
		try {if(window.addEventListener){target.addEventListener(evt, e, false);} else {target.attachEvent('on'+evt,e);}} catch(e) 
		{if(document.all) {alert("Cannot create event '"+evt+"':"+e.number+":"+e.description);} else {alert("Cannot create event '"+evt+"'\n"+e);}}
		}
     },

	
	preload: function() {
    this.createEvent('load',window,this.init);	
	},
	
	jstart: function() {
	alert("Got it niggaz!");
	},
	
	
	version: function() {
	alert("Message Box 1.0 by Tony Ogundipe - http://www.ds.mwebng.net!");
	},
	
	fsync: function(element) {
     //try {this.reference=element;element.focus();return true;} catch(err) {alert(element+" is not valid for fsync");return false;}
     try {this.reference=element;element.focus();return true;} catch(err) {return false;}
    },

	hide: function() {
	this.shadow.style.display="none";
	this.shadowbox.style.display="none";
	this.runEvent("onclose");
	}
	
}
msgbox.preload();