
var _xw2timer = null;

function xgWindow2(){
		
		this.showInner					=xgw2_showInner;
		this.showModal					=xgw2_showModal;
		this._createWindow			=xgw2_createWindow;
		this.close							=xgw2_closeWindow;
		this._type							=null;
		this._cobj							=null;
		this.returnValue				=10;
		this.fid								=null;
		this.src								=null;
		this.childs							=new Array();
		this.onclose						=xgw2_onclose;
		this.__startMonitoringFrame = xgw2_monitoringFrame;
}

function xgw2_showInner(src,width,height,dleft,dtop,skin){
	var winobj = this._createWindow("inner",src,dleft,dtop,width,height,skin);
	top._zIndex++;
	this.src = src;

	if (window._type){
			var pobj = getDialogWithID(window._id);
			pobj.childs[pobj.childs.length] = this;
	}
	top._modalDialogs[top._modalDialogs.length] = this;
}

function xgw2_showModal(src,width,height,dleft,dtop,skin){
	var winobj = this._createWindow("modal",src,dleft,dtop,width,height,skin);
	if (window._type){
			var pobj = getDialogWithID(window._id);
			pobj.childs[pobj.childs.length] = this;
	}
		
	this.src = src;
	top._modalDialogs[top._modalDialogs.length] = this;
	top._zIndex--;
}


function xgw2_closeWindow(){
		if (top._modalDialogs){
				var mydialogs = new Array();
				for (var i=0;i<top._modalDialogs.length ;i++ ){
					var obj = top._modalDialogs[i];
					if (obj.fid != this.fid){
						mydialogs[mydialogs.length]=obj;
					}
				}
			
				top._modalDialogs = mydialogs;
		}//if top_modal

	if (this._type == "inner"){
		try{
			//closing first childs
			for (var i=0;i<this.childs.length ;i++ )	{
				this.childs[i].close();
			}
			//closing object
			var _child = top.window.document.getElementById(this._cobj.id);
			top.window.document.body.removeChild(_child);
		}catch(e){
		}
	}else

	if (this._type == "modal"){
		try{
			for (var i=0;i<this.childs.length ;i++ )	{
				this.childs[i].close();
			}
	
			var bsobj = top.window.document.getElementById(this._cobj.id+"_blackscreen");
			top.window.document.body.removeChild(bsobj);
		}catch(e){
	
		}
	}

	this.onclose(this.returnValue);
}//closeWindow


function xgw2_onclose(){}//default event

function xgw2_createWindow(_type,_src,dleft,dtop,width,height,skin){
		top._xgwin2_windIndex++;
		var _width = width+66;
		var _height = height+45+51;
		var _skin = "white";
		this._type = _type;
		if (skin) _skin = skin;
		var _skinLocation = xg.coreLocation+"imgs/windows/"+_skin;
		var _doc = top.window.document;
		var _left = _doc.body.clientWidth/2-_width/2;
		var _top = _doc.body.clientHeight/2-height/2;
		if (dleft && dleft!=-1) _left = dleft;
		if (dtop&& dleft !=-1) _top = dtop;

		var _div = _doc.createElement("DIV");
		_div.style.position = "absolute";
		_div.style.backgroundColor = "transparent";
		_div.style.left = _left;
		_div.style.top = _top;
		_div.style.width = _width;
		_div.style.height = _height;
		_div.style.zIndex = top._zIndex;

		var _lt = _doc.createElement("DIV");
		_lt.style.position = "absolute"; _lt.style.left = 0; _lt.style.top = 0; _lt.style.width = 57;_lt.style.height=51;
		_lt.style.border = "solid silver 0px";
		_lt.style.backgroundImage = "url('"+_skinLocation+"/lefttopcorner.png')";
		_lt.style.backgroundRepeat = "repeat-x";
		_lt.unselectable = "on";
		__removeObjectEvents(_lt);
		_div.appendChild(_lt);
		
		var _tb = _doc.createElement("DIV");
		__setBoundsOfObject(_tb,57,0,_width-57-50,51);
		_tb.style.backgroundImage = "url('"+_skinLocation+"/topborder.png')";
		_tb.style.border = "solid red 0px";
		__removeObjectEvents(_tb);
		_div.appendChild(_tb);
		
		var _rt = _doc.createElement("DIV");
		__setBoundsOfObject(_rt,_width-50,0,50,51);
		_rt.style.backgroundImage = "url('"+_skinLocation+"/righttopcorner.png')";
		__removeObjectEvents(_rt);
		_div.appendChild(_rt);

		var _lb = _doc.createElement("DIV");
		__setBoundsOfObject(_lb,0,51,33,_height-51-45);
		_lb.style.backgroundImage = "url('"+_skinLocation+"/leftborder.png')";
		__removeObjectEvents(_lb);
		_div.appendChild(_lb);

		var _rb = _doc.createElement("DIV");
		__setBoundsOfObject(_rb,_width-33,51,33,_height-51-45);
		_rb.style.backgroundImage = "url('"+_skinLocation+"/rightborder.png')";
		__removeObjectEvents(_rb);
		_div.appendChild(_rb);

		var _ld = _doc.createElement("DIV");
		__setBoundsOfObject(_ld,0,_height-45,57,45);
		_ld.style.backgroundImage = "url('"+_skinLocation+"/leftbottomcorner.png')";
		__removeObjectEvents(_ld);
		_div.appendChild(_ld);

		var _bb = _doc.createElement("DIV");
		__setBoundsOfObject(_bb,57,_height-45,_width-57-50,45);
		_bb.style.backgroundImage = "url('"+_skinLocation+"/bottomborder.png')";
		__removeObjectEvents(_bb);
		_div.appendChild(_bb);

		var _rd = _doc.createElement("DIV");
		__setBoundsOfObject(_rd,_width-50,_height-45,50,45);
		_rd.style.backgroundImage = "url('"+_skinLocation+"/rightbottomcorner.png')";
		__removeObjectEvents(_rd);
		_div.appendChild(_rd);

		var _dtop = _doc.createElement("DIV");
		var _hh = _height-51-294;
		if (_hh < 0) _hh = 0;
		__setBoundsOfObject(_dtop,33,51,_width-33-33,_hh);
		_dtop.style.backgroundImage = "url('"+_skinLocation+"/topbackground.png')";
		__removeObjectEvents(_dtop);
		_div.appendChild(_dtop);
		
		var _dback = _doc.createElement("DIV");
		var _hh = _height-45-294;
		var _hhh = 294;
		if (_hh < 0){
			_hh = 50;
			_hhh = _height-45-50;
		}

	
		__setBoundsOfObject(_dback,33,_hh,_width-33-33,_hhh);
		_dback.style.backgroundImage = "url('"+_skinLocation+"/downbackground.png')";
		__removeObjectEvents(_dback);
		_div.appendChild(_dback);
		
		var _object = _doc.createElement("DIV");
		__setBoundsOfObject(_object,60,60,200,200);
		__removeObjectEvents(_object);
		_div.appendChild(_object);
		var blackscreen = null;
		if (_type == "modal"){
			//trebuie sa creez blackscreen
			 blackscreen = top.window.document.createElement("DIV");
			blackscreen.style.position = "absolute";
					var _bheight = top.window.document.body.clientHeight;
					var _bwidth = top.window.document.body.clientWidth;
					if (parseInt(top.window.document.body.scrollWidth) > _bwidth) _bwidth = top.window.document.body.scrollWidth;
					if (parseInt(top.window.document.body.scrollHeight) > _bheight) _bheight = top.window.document.body.scrollHeight;
					blackscreen.style.position = "absolute";blackscreen.style.left = 0;blackscreen.style.top=0;blackscreen.style.width=_bwidth;
					blackscreen.style.height=_bheight;blackscreen.style.backgroundImage="url("+xg.coreLocation+"/imgs/shadows/25white.png)";
					blackscreen.style.zIndex = top._zIndex;
					blackscreen.style.overflow = "hidden";
					top.window.document.body.appendChild(blackscreen);
					blackscreen.appendChild(_div);
					top._zIndex++;
		}else{//e inner
			top.window.document.body.appendChild(_div);
		}
		
		var _ifrm = _doc.createElement("IFRAME");
		__setBoundsOfObject(_ifrm,33,60,_width-66,_height-60-50);
		__removeObjectEvents(_ifrm);
		_ifrm.frameBorder = 0;
		_ifrm.scrolling = "no";
		_ifrm.allowTransparency = true;
		_ifrm.style.zIndex = "99999999";

		_div.appendChild(_ifrm);
		_ifrm.id = GenerateID();
		
		_ifrm.src = _src; 
		this._frame = _ifrm;
		this.fid = _ifrm.id;
		this.__startMonitoringFrame(_ifrm.id);
		
		var _loading = _doc.createElement("IMG");
		__removeObjectEvents(_loading);
		//_loading.src = xg.coreLocation+"/imgs/progressbar.gif";
		if (_skin == "white"){
		_loading.src = "http://imgs.heeloo.net/spinner_small.gif";
		}else{
			_loading.src = "http://imgs.heeloo.net/spinner_small_black.gif";
		}
		_loading.style.position = "absolute";
		_loading.style.left = 40;
		_loading.style.top = 40;
		if (_width - 80 < 210){
			_loading.style.width = _width-100;
		}
		_loading.id = _ifrm.id+"_loadingimage";
		_div.appendChild(_loading);
		
		var _title = _doc.createElement("DIV");
		__removeObjectEvents(_title);
		_title.style.position = "absolute";
		_title.style.left = 40;_title.style.top = 40;
		_div.appendChild(_title);

		var _closebutton = _doc.createElement("DIV");
		__setBoundsOfObject(_closebutton,_width-60,33,30,30);
		__removeObjectEvents(_closebutton);
		_closebutton.style.border = "solid silver 0px";
		_div.appendChild(_closebutton);
		_div.id = _ifrm.id + "_container";
		if (blackscreen != null) blackscreen.id = _div.id+"_blackscreen";
		this._cobj = _div;
		var _win = this;

		_closebutton.onclick = function(){
			_win.close();
		}
		
		_closebutton.innerHTML = "<img src='"+xg.coreLocation+"/imgs/windows/close_button.png'/>";
		
		var _clickX = -1;
		var _clickY = -1;
		var _mousePressed = false;
		

		_div.onmousedown = function(evt){
			var ev = __getEventObject(evt);
			_clickX = ev.clientX-top.window.xg.util.getLeft(this);
			_clickY = ev.clientY-top.window.xg.util.getTop(this);
			_mousePressed = true;
		}
		
		_div.onmouseup = function(){
			_clickX = -1;
			_clickY = -1;
			_mousePressed = false;
		}

		_div.onmouseout = function(){
			_clickX = -1; _clickY = -1;
			_mousePressed = false;
		}

		_div.onmousemove = function(evt){
			var ev  = __getEventObject(evt);
				if (_mousePressed){
					var newX = ev.clientX-_clickX;
					var newY = ev.clientY - _clickY;
					_div.style.left = newX;
					_div.style.top = newY;
				}
		}

		return _div;
}

function __getEventObject(evt){
	var ev = null;
			if (xg.browser.sName != "ie"){
				ev = evt;
			}else{
				ev = top.event;
			}
	return ev;
}

function __removeObjectEvents(obj){
	obj.onmousedown = function(){ return false}
	obj.onmouseover = function(){ return false}
	obj.onmousemove = function(){ return false}
}

function __setBoundsOfObject(obj,_left,_top,_width,_height){
	obj.style.position = "absolute";
	obj.style.left = _left; obj.style.top = _top; obj.style.width = _width; obj.style.height= _height;
}

function xgw2_monitoringFrame(id){
		var _myframe = document.getElementById(id);
		if (_myframe == null) _myframe = top.window.document.getElementById(id);
		try{
			var _state = "-";
			if (xg.browser.sName  == "ie"){
				_state = _myframe.readyState;
			}else{
				_state = _myframe.contentDocument.readyState;
				if (!_state){
					_state = "complete";
				}
			}
			if (_state == "complete"){
				var _myloading = document.getElementById(id+"_loadingimage");
				if (_myloading == null) _myloading = top.window.document.getElementById(id+"_loadingimage");
				if (_myloading)	{
						_myloading.style.visibility = "hidden";
						_myframe.contentWindow._id = _myframe.id;
						_myframe.contentWindow._type = "inner";
						__removeObjectEvents(_myframe);
						window.clearTimeout(_xw2timer);
				}else{
					_xw2timer = window.setTimeout("xgw2_monitoringFrame('"+id+"')",1000);
				}
			}else{
				_xw2timer= window.setTimeout("xgw2_monitoringFrame('"+id+"')",1000);
			}
		}catch(e){

		}
}
