/*
	Javascript Window
	Copyright 2008 - Jan Egert <honza@egert.cz>
*/
function CWindow(_parent, _name, _title, _width, _height, _left, _top)
{
	this._parent=_parent;
	this._window=null;
	this._header=null;
	this._hideBtn=null;
	this._closeBtn=null;
	this._content=null;
	this._name=_name;
	this._title=_title;
	this._width=_width;
	this._height=_height;
	this._top=_top;
	this._left=_left;
	this._addInfo=new Array();
	var _addInfo=this._addInfo;
	this.showButtons=new Array(1,1);
	this.onclose=null;
	this.onbeforeclose=null;
	this.oncreate=null;
	this.onbeforecreate=null;
	this.onshow=null;
	this.onhide=null;
	this.onmove=null;
	
	this.create = function()
	{
			if(this.getObj("win_"+_name+"_window") != null)
			{
				this.close();
			}
			if(this.onbeforecreate != null) this.onbeforecreate;
			this._window=this.crElem("table");
			this._window.cellPadding=0;
			this._window.cellSpacing=0;
			this._window.border=0;


			this._window.className="win_window";
			this._window.id="win_"+_name+"_window";
			this._window.style.width=(_width=="auto"?"":_width+"px");
			this._window.style.height=(_height=="auto"?"":_height+"px");
			this._window.style.top=(_top=="auto"?"":_top+"px");
			this._window.style.left=(_left=="auto"?"":_left+"px");

			var thead=this.crElem("thead");
			
			var tr_h=this.crElem("tr");
			tr_h.className="win_header";
				var title=this.crElem("td");
					title.innerHTML=_title;
					title.id='win_'+_name+"_title";
					title.className="win_title";
				this.append(tr_h, title);
				
				if(this.showButtons[0])
				{
					this._hideBtn=this.crElem("td");				
					this._hideBtn.innerHTML='hide';
					this._hideBtn.id='win_'+_name+"_hide";
					this._hideBtn.className="win_hide";
					this._hideBtn.onclick=this.showhide;
					this._hideBtn.onmouseover=function(){this.className="win_hide_over";}
					this._hideBtn.onmouseout=function(){this.className="win_hide";}
						
					this.append(tr_h, this._hideBtn);
				}
				if(this.showButtons[1])
				{
					this._closeBtn=this.crElem("td");
					this._closeBtn.innerHTML='X';
					this._closeBtn.id='win_'+_name+"_close";
					this._closeBtn.className="win_close";	
					this._closeBtn.onmouseover=function(){this.className="win_close_over";}
					this._closeBtn.onmouseout=function(){this.className="win_close";}
					this._closeBtn.onclick=this.close;	
					
					this.append(tr_h, this._closeBtn);
				}
					
			this.append(thead, tr_h);
			this.append(this._window, thead);
			var tbody=this.crElem("tbody");
			tbody.id='win_'+_name+"_tbody";
			var tr_b=this.crElem("tr");
				this._content=this.crElem("td");
				this._content.colSpan=3;
					this._content.id='win_'+_name+"_content";
					this._content.className="win_content";
				this.append(tr_b, this._content);
			this.append(tbody, tr_b);
			this.append(this._window, tbody);
			this.append(_parent, this._window);
			
			Drag.init(thead, null);
			var win=document.getElementById("win_"+_name+"_window");
			thead.onDrag = function(x, y)
			{
				win.style.top=_top+y+'px';
				win.style.left=_left+x+'px';
				if(this.onmove != null) this.onmove;
			}	
			if(this.onclose != null) this.onclose;
	}
	
	this.crElem=function(elem){return document.createElement(elem);}
	this.setAttr=function(obj, attr, value){obj.setAttribute(attr, value);}
	this.append=function(parent, child){return parent.appendChild(child);}
	this.setContent=function(obj, value){obj.innerHTML=value;}
	this.getContent=function(obj){return obj.innerHTML;}
	this.getObj=function(id){return document.getElementById(id);}
	this.showhide=function(e)
	{
		var con=document.getElementById("win_"+_name+"_tbody");
		var win=document.getElementById("win_"+_name+"_window");
		var btn=document.getElementById("win_"+_name+"_hide");
		if(con.style.display=='none')
		{
			con.style.display='';
			if(_height != "auto") win.style.height=_height+"px";
			btn.innerHTML='hide';
			btn.className='win_hide';
			if(this.onshow != null) this.onshow;
		}
		else
		{
			con.style.display='none';
			win.style.height="16px";
			btn.innerHTML='show';
			btn.className='win_hide';
			if(this.onhide != null) this.onhide;
		}
	}
	this.close=function(e)
	{
		if(this.onbeforeclose != null) this.onbeforeclose;
		var win=document.getElementById("win_"+_name+"_window");
		win.style.display='none';
		win.parentNode.removeChild(win);
		if (document.layers) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = captureMousePosition;
		if(this.onclose != null){ this.onclose(); }
	}
}

