﻿registerNS("blaze.ui");
blaze.ui.Popup=function(el,x,y){
	this.x=x;
	this.y=y;
	this.element=null;
	this.bkg=null;
	this.dim=true;
	this.autoHide=true;
	this.autoHideDelay=500;
	this.dimColor="#FFF";
	this.visible=false;
	this.showEffect=null;
	this.hideEffect=null;
	this.onhide=null;
	this.onshow=null;
	this.timeout=null;
	if(typeof(el)=="string"||el.tagName){
		this.element=blaze.getElement(el);
	}else{
		for(var a in el){this[a]=el[a];}
	}
	this.init();
}
blaze.ui.Popup.prototype.init=function(){
	var el=this.element;
	if(!el||el==null){return;}
	this.setAutoHide();
	with(el.style){
		position="absolute";
		display="none";
		zIndex=10000;
	}
	if(!el.parentNode){document.body.appendChild(el);}
	this.bkg=blaze["popupBkg"];
	if(this.bkg==null){
		this.bkg=blaze.addElement(null,"div");
		blaze.setOpacity(this.bkg,50);
		with(this.bkg.style){
			position="absolute";display="none";
			zIndex=8000;top="0px";left="0px";
			backgroundColor=this.dimColor;
			width=window.document.body.scrollWidth+"px";
			height=window.document.body.scrollHeight+"px";
		}
		blaze["popupBkg"]=this.bkg;
	}
}
blaze.ui.Popup.prototype.setAutoHide=function(){
	if(this.autoHide){
		var caller=this;
		if(!blaze.popups){blaze.popups=[];}
		var i=blaze.popups.length;
		blaze.popups[i]=this;
		this.element.onmouseover=function(){
			if(this.timeout){clearTimeout(this.timeout);}
		}
		this.element.onmouseout=function(){
			this.timeout=setTimeout("blaze.popups["+i+"].hide();",caller.autoHideDelay);
		}
	}else{
		this.element.onmouseover=null;
		this.element.onmouseout=null;
	}
}

blaze.ui.Popup.prototype.toggleSelects=function(show){
	if(blaze.toggleSelects(show)){
		var mys=this.element.getElementsByTagName("SELECT");
		for(var i=0;i<mys.length;i++){
			mys[i].style.visibility="visible";
		}
		return true;
	}
	return false;
}

blaze.ui.Popup.prototype.show=function(x,y){
	this.getCoords(x,y);
	this.setAutoHide();
	if(typeof(this.onshow)=="function"){this.onshow(x,y);}

	if(this.dim){
		with(this.bkg.style){
			width=window.document.body.scrollWidth+"px";
			height=window.document.body.scrollHeight+"px";
			display="block";
		}
	}
	this.toggleSelects();
	if(this.showEffect&&this.showEffects.apply){
		this.showEffect.apply(this.element);
	}else{
		this.element.style.display="block";
	}
	this.visible=true;
}
blaze.ui.Popup.prototype.hide=function(){
	var el=this.element;
	this.toggleSelects(true);
	if(this.dim){this.bkg.style.display="none";}
	if(this.hideEffect&&this.hideEffects.apply){
		this.hideEffect.apply(el);
	}else{
		el.style.display="none";
	}
	if(typeof(this.onhide)=="function"){this.onhide();}
	this.visible=false;
}
blaze.ui.Popup.prototype.getCoords=function(x,y){
	var el=this.element;
	if(!el){return;}
	if(x){this.x=x;}
	if(y){this.y=y;}
	if(this.x==null||this.y==null){
		var sh=false;
		if(el.style.display=="none"){el.style.display="block";sh=true;}
		if(this.x==null){this.x=(document.body.scrollWidth-el.clientWidth)/2;}
		if(this.y==null){this.y=(document.body.scrollHeight-el.clientHeight)/2;}
		if(sh){el.style.display="none";}
	}
	el.style.left=this.x+"px";
	el.style.top=this.y+"px";
}