var 
xgAllDialogs = new Array();

var dialog = new xgdlgs();

function xgdlgs(){
	this.close				=xgd_close;
	this.showLoading = xgd_showLoading;
	this.hideLoading = xgd_hideLoading;
	this.getParent		=xgd_getparent;
	this.getDParent		=xgd_getdparent;
	this.resize            =xgd_resize;
	this.autoPos         =xgd_autopos;
	this.move             =xgd_move;
}

function xgd_close(obj,rvalue){
	if (typeof(obj) != "object") obj = xg.obj(obj);

	var mydlg = this.getParent(obj);
	if (typeof(rvalue)!="undefined"){
		mydlg.returnValue = rvalue;
	}else{
		mydlg.returnValue = null;
	}
	for (var i=0;i<mydlg.children.length;i++){
			mydlg.children[i].close();
	}
	
	mydlg.close();
}

function xgd_resize(obj,width,height){
    if (typeof(obj) != "object") obj = xg.obj(obj);
    var mydlg = this.getParent(obj);
    mydlg.resize(width,height);
}

function xgd_autopos(obj){
     if (typeof(obj) != "object") obj = xg.obj(obj);
    var mydlg = this.getParent(obj);
    mydlg.autopos();
}

function xgd_move(obj,x,y){
     if (typeof(obj) != "object") obj = xg.obj(obj);
    var mydlg = this.getParent(obj);
    mydlg.move(x,y);
}

function xgd_showLoading(obj,label){
	if (typeof(obj) != "object") obj = xg.obj(obj);
	var dlg = this.getParent(obj);
	dlg.showLoading(label);
}

function xgd_hideLoading(obj){
	if (typeof(obj) != "object") obj = xg.obj(obj);
	var dlg = this.getParent(obj);
	dlg.hideLoading();
}

function xgd_getdparent(obj){
	var stop = false;
	var k = 0;
	while (!stop){
		if (typeof(obj.id) != "undefined" && obj.id.startsWith("xgdlg_")){
			for (var i=0;i<xgAllDialogs.length ;i++){
					var d = xgAllDialogs[i];
					
					if (d.dlg.id == obj.id){
						return xgAllDialogs[i];
					}
			}

			break;
		}else{
			k++;
			obj = obj.parentNode;
			if (obj == null) return null;
		}
		if (k > 3) return null;
	}
}

function xgd_getparent(obj){
	var stop = false;

	while (!stop){
		if (typeof(obj.id) != "undefined" && obj.id.startsWith("xgdlg_")){
			for (var i=0;i<xgAllDialogs.length ;i++){
					var d = xgAllDialogs[i];
					if (d.dlg.id == obj.id){
						return xgAllDialogs[i];
					}
			}

			break;
		}else{
			obj = obj.parentNode;
			if (obj == null) return null;
		}
	}
}



function xgDialog(url,width,height,left,top){
	if (typeof(noajax) == "undefined") noajax = false;
	if (typeof(skin) == "undefined") skin = "silver";
	if (typeof(left) == "undefined") left = null;
	if (typeof(top) == "undefined") top = null;
	this.url								=url;
	this.left								=left;
	this.top								=top;
	this.width							= width;
	this.height							=height;
	this.noajax							=noajax;
	this.objIndex						= xgAllDialogs.length;
	this.hideClose					=false;
	this.leftBorder					=4;
	this.topBorder					=13;
	this.rightBorder				=6;
	this.bottomBorder			=8;
	this.setBorder					=xgdialog_setborder;
	this.skin								= skin;
	this.showLoading				=xgdialog_showloading;
	this.hideLoading				=xgdialog_hideloading;
	this.children						=new Array();
	this.returnValue				=new Array();
	xgAllDialogs[xgAllDialogs.length] = this;


	this.create							=xg_dlg_create;
	this.destroy						=xg_dlg_destroy;
	this.show							=xg_dlg_show;
	this.close							=xg_dlg_close;
	this.resize							=xg_dlg_resize;
	this.autopos						=xg_dlg_autopos;
	this.move							=xg_dlg_move;
	this._script						="";
}

	function xg_dlg_create(parent,popup){
		
		if (typeof(parent) == "undefined"){
				alert("You need to specify a parent for this dialog!");
				return;
		}else{
			if (parent != null)	{
				parent.children[parent.children.length] = this;
			}
		}

		if (typeof(popup) == "undefined"){
			popup = false;
		}else{
			popup = true;
		}
		
		var zindex = 9000+xgAllDialogs.length*5;
		var d_height = document.body.clientHeight;
		var d_width = document.body.clientWidth;
	
	if (!popup){
		var _whites = document.createElement("DIV");
		_whites.style.position = "absolute";
		_whites.style.left = "0px";
		_whites.style.top = "0px";
		_whites.style.width = d_width+"px";
		_whites.style.height = d_height+"px";
		_whites.style.backgroundImage = "url('http://imgs.heeloo.net/shadows/10white.png')";
		_whites.style.visibility = "hidden";
		_whites.style.zIndex = zindex+1;
		document.body.appendChild(_whites);	
		this._whitespace = _whites;
	}
		var url = this.url;
		var width = this.width;
		var height = this.height;
		var skin = this.skin;
		var noajax = this.noajax;
		var _dlg = document.createElement("DIV");
		_dlg.style.position = "absolute";
		var myID= GenerateID();
		this.dlg = _dlg;
		this._myid = myID;

		var left = document.body.clientWidth/2-width/2;
		var top = document.body.clientHeight/2-height/2;	
		if (this.left != null) left= this.left;
		if (this.top != null) top = this.top;

		_dlg.style.overflow = "hidden";
			var _closebtn = document.createElement("DIV");
			_closebtn.className = "close_delete";
			_closebtn.style.position = "absolute";
			_closebtn.style.left = (width - 30)+"px";
			_closebtn.style.top = "15px";
			_closebtn.style.width = "10px";
			_closebtn.style.height = "10px";
			_closebtn.style.zIndex=3001;
			if (this.hideClose) _closebtn.style.display = "none";
			_closebtn.onmouseover =function(){
					this.className = "close_delete_hover";
			}
			_closebtn.onmouseout = function(){
					this.className = "close_delete";
			}

			var mydlg = this;

			_closebtn.onclick = function(){
					mydlg.close();	
			}
			_dlg.appendChild(_closebtn);
				var leftBorder = 6+this.leftBorder; //10
				var topBorder = 7+this.topBorder; //20
				var rightBorder = 5+this.rightBorder; //22
				var bottomBorder = 7+this.bottomBorder;//25
				var spinner_small = "spinner_small";
			
				if (skin == "black") spinner_small = "spinner_small_black";
				var _content = document.createElement("DIV");
				_content.style.position ="absolute";
				_content.style.left = "0px";
				_content.style.top = "0px";
				_content.style.width = "100%";
				_content.style.height = "100%";
				_dlg.appendChild(_content);
				_content.innerHTML = "<div unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);width:"+(parseFloat(width)-40)+"px;height:15px;float:left;'></div>"+
				"<div unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);width:40px;height:15px;background-position:right top;float:right'></div>"+
				"<div unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);background-position:left 70%;width:"+(parseInt(width)-40)+"px;height:"+(parseInt(height)-32)+"px;float:left;'></div>"+
						"<div unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);background-position:right 70%;background-repeat:no-repeat;width:40px;height:"+(parseInt(height)-32)+"px;float:right;'></div>"+
						"<div unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);float:left;background-position:left bottom;background-repeat:no-repeat;width:"+(parseInt(width)-40)+"px;height:17px;'></div>"+
						"<div  unselectable='on' onselectstart='return false;' style='background-image:url(http://imgs.heeloo.net/wnd/bg"+skin+".png);float:right;background-position:right bottom;background-repeat:no-repeat;width:40px;height:17px;'></div>"+
						""+
						"<div id='dialog_container_"+myID+"'  unselectable='on'  style='-webkit-user-select:text;background-image:url(http://imgs.heeloo.net/"+spinner_small+".gif);background-repeat:no-repeat;background-position:center center;overflow:hidden;z-index:3000;position:absolute;left:"+leftBorder+"px;top:"+topBorder+"px;width:"+(parseInt(width)-leftBorder-rightBorder)+"px;height:"+(parseInt(height)-topBorder-bottomBorder)+"px;'></div>"+
						"<div id='dialog_loading_xgdlg_"+myID+"' unselecteble='on' style='visibility:hidden;background-image:url(http://imgs.heeloo.net/shadows/10white.png);z-index:30001;position:absolute;left:5px;top:7px;width:"+(parseInt(width)-12)+"px;height:"+(parseInt(height)-16)+"px;'><table border=0 width=100% height=100%><tr><td align=left valign=bottom><div id='dialog_loading_text_xgdlg_"+myID+"' style='margin:5px;'></div></td></tr></table></div>";
				;
			
			_dlg.style.left = left+"px"; _dlg.style.top = top+"px"; _dlg.style.width = (width)+"px"; _dlg.style.height = (height)+"px";
			_dlg.style.zIndex = zindex+2;
			_dlg.style.visibility = "hidden";
			_dlg.id = "xgdlg_"+myID;
			document.body.appendChild(_dlg);
			
		
		
		if (noajax){
			var _dlgx = xg.obj("dialog_container_"+myID);
			if (url.indexOf("?")==-1)	{
				url +="?did="+myID;
			}else{
				url+="&did="+myID;
			}
			_dlgx.innerHTML = "<iframe style='width:100%;height:100%' src='"+url+"' frameborder='0' scrolling='no'></iframe>";
			xg.obj("dialog_container_"+myID).style.backgroundImage = "none";

		}else{
				if (url.indexOf("?")==-1)	{
				url +="?did="+myID;
			}else{
				url+="&did="+myID;
			}
				xg.xhr.post(url,function(resObj){
					
					try{
					xg.obj("dialog_container_"+myID).style.backgroundImage = "none";
					}catch(E){ }
					
					if (resObj.indexOf("<body")>-1){
							resObj = resObj.substring(resObj.indexOf("<body"));
							resObj = resObj.substring(resObj.indexOf(">")+1);
						}

				var mdiv = xg.obj("dialog_container_"+myID);
					var myobj = xg.obj("dialog_container_"+myID);
					if (myobj == null) return;
					myobj.innerHTML = resObj;
					
					xg_dlg_evaluateScripts(mdiv);
					
						var strScript = url;
						if (strScript.indexOf("?")>-1) strScript = strScript.substring(0,strScript.indexOf("?"));
						strScript = strScript.substring(0,strScript.lastIndexOf("."))+".sc";
						if (url.indexOf("?")>-1){
							var _params = url.substring(url.indexOf("?"));
							strScript = strScript+_params;
						}
						try{
						var element = document.createElement('script');
							mydlg._script = strScript;
							element.setAttribute("src",strScript);
							mdiv.appendChild(element);
						}catch(Ex){
						}
						
						//invalidate select for everything
						xg.util.unselectable(mdiv);
				});
		}

	return this;
}
function xg_dlg_evaluateScripts(mdiv){
		for (var i=0;i<mdiv.childNodes.length;i++){
			var child = mdiv.childNodes[i];
			if (child.tagName =="SCRIPT"){
			    try{
				eval(child.text);
				}catch(E){
				}
			}
			if (child.childNodes.length > 0){
				xg_dlg_evaluateScripts(child);
			}
		}

}
function xg_dlg_resize(width,height){
	this.width = width;
	this.height = height;
    var leftBorder = 6+this.leftBorder; //10
    var topBorder = 7+this.topBorder; //20
    var rightBorder = 5+this.rightBorder; //22
    var bottomBorder = 7+this.bottomBorder;//25
                
	this.dlg.style.width = parseFloat(width)+"px";
	this.dlg.style.height = parseFloat(height)+"px";
	var cn = this.dlg.childNodes.item(1).childNodes;
	cn.item(0).style.width = parseFloat(width-40)+"px";
	cn.item(2).style.width = parseFloat(width-40)+"px";
	cn.item(2).style.height = parseFloat(height-32)+"px";
	cn.item(3).style.height = parseFloat(height-32)+"px";
	cn.item(4).style.width = parseFloat(width-40)+"px";
	var maindiv = cn.item(6);
	maindiv.style.width = (parseInt(width)-leftBorder-rightBorder)+"px";
	maindiv.style.height = (parseInt(height)-topBorder-bottomBorder)+"px";
	var ldiv = cn.item(7);
	ldiv.style.width = (parseInt(width)-12)+"px";
	ldiv.style.height = (parseInt(height)-16)+"px";
}

function xg_dlg_autopos(){
		var left = document.body.clientWidth/2-this.width/2;
		var top = document.body.clientHeight/2-this.height/2;	
		this.move(left,top);
}

function xgdialog_setborder(tb){
	this.leftBorder = tb;
	this.rightBorder = tb;
	this.topBorder = tb;
	this.bottomBorder = tb;
}
function xg_dlg_move(left,top){
	if (left == null) left = document.body.clientWidth/2-this.width/2;
	if (top == null) top = document.body.clientHeight/2-this.height/2;	
    
	this.dlg.style.left = parseFloat(left)+"px";
	this.dlg.style.top = parseFloat(top)+"px";
	this.left = left;
	this.top = top;
}

function xg_dlg_show(){
		xg_setOpacity(0,this.dlg.id);
		this.dlg.style.visibility = "visible";

		xg_fadeIn(this.dlg.id);
		if (this._whitespace)
			this._whitespace.style.visibility = "visible";
}

function xg_dlg_close(){

		if (typeof(this.onclose) != "undefined"){
			try{
		this.onclose(this.returnValue);
			}catch(e){
				alert(e);
			}
	}
	this.dlg.style.visibility = "hidden";
	if (this._whitespace) this._whitespace.style.visibility = "hidden";
}

function xg_dlg_destroy(){
	var scripts = document.getElementsByTagName("SCRIPT");
	for (var i=0;i<scripts.length;i++){
		if (scripts[i].src.indexOf(this._script)>-1){
			try{
			
			scripts[i].parentNode.removeChild(scripts[i]);
			}catch(Ex){
				alert("ERROR:"+Ex);
			}
		}
	}
		document.body.removeChild(this.dlg);
		try{
		document.body.removeChild(this._whitespace);
		}catch(e){
		}
}

function xgdialog_showloading(txt){
	var o = xg.obj("dialog_loading_"+this.dlg.id);
	var t = xg.obj("dialog_loading_text_"+this.dlg.id);
	if (typeof(txt) != "undefined"){
		t.innerHTML = "<img src='http://imgs.heeloo.net/spinner_mac.gif' border='0' style='float:left;height:16px;margin-right:5px;'/>"+txt;
	}else{
		t.innerHTML = "";
	}
	o.style.visibility = "visible";
}

function xgdialog_hideloading(){
	var o = xg.obj("dialog_loading_"+this.dlg.id);
	o.style.visibility = "hidden";
}
