﻿registerNS("blaze.ui");
blaze.ui.Window=function(el,x,y){
	this.x=x;
	this.y=y;
	this.width=500;
	this.height=null;
	this.title=null;
	this.icon=null;
	this.hasClose=true;
	this.autoShow=true;
	this.bkg=null;
	this.popup=null;
	this.contents=null;
	this.oninit=null;
	this.titlebar=null;
	if(typeof(el)=="string"&&el.indexOf(" ")>-1){
		this.element=document.createElement("div");this.element.innerHTML=el;
	}else{
		this.element=$(el);
		if(this.element==null){
			for(var a in el){this[a]=el[a];}
		}else{
			this.title=this.element.getAttribute("title");
		}
		if(this.contents!=null){this.element=document.createElement("div");this.element.innerHTML=this.contents;}
	}
	
	var caller=this;
	var l=blaze.getImporter(function(){caller.init();});
	l.add("ui.popup");
	l.load();
}

blaze.ui.Window.prototype.show=function(x,y){
	this.popup.show(x,y);
	this.titlebar.style.display="block";
}

blaze.ui.Window.prototype.hide=function(){
	this.titlebar.style.display="none";
	this.popup.hide();
}

blaze.ui.Window.prototype.getContent=function(){
	if(this.element){return this.element.innerHTML;}
	return null;
}

blaze.ui.Window.prototype.setContent=function(content){
	this.element.innerHTML=content;
}

blaze.ui.Window.prototype.init=function(){
	var el=this.element;
	if(!el||el.getAttribute("windowUsed")){return;}
	this.element.setAttribute("windowUsed","true");
	blaze.loadStyle("/lib/ui/window/window.css",true);
	var w=blaze.addElement(null,"div",{"class":"window"});
	if(this.width){w.style.width=this.width+"px";}
	if(this.height){w.style.height=this.height+"px";}
	var tb=blaze.addElement(w,"div",{"class":"titlebar Bkg"});
	if(this.hasClose){
		var ctrl=blaze.addElement(tb,"div",{"class":"control"});
		blaze.addElement(ctrl,blaze.createIcon(blaze.getBase("/lib/ui/window/close.png"),12,12),{"class":"close"});
		blaze.addElement(ctrl,"span",null,"Close");
		var caller=this;
		ctrl.onclick=function(){caller.hide();}
	}
	if(this.icon){blaze.addElement(tb,blaze.createIcon(this.icon),{"class":"icon"});}
	if(this.title){blaze.addElement(tb,"span",{"class":"label"},this.title);}
	this.titlebar=tb;
	el.style.margin="20px";
	w.appendChild(el);

	if(el.style.display=="none"){el.style.display="";}
	this.popup=new blaze.ui.Popup(w,this.x,this.y);
	this.popup.autoHide=false;
	if(this.autoShow){this.show();}
	this.fire("oninit");
}

blaze.ui.Window.prototype.fire=function(en){
	if(typeof(this[en])=="function"){this[en]();}
}