//by leili
UI.WindowManager = Class.create({
	initialize: function(){
		this.wins = $A([]);
	},
	current : null,
	push : function(win){
		var first = this.wins.first();
		if(first) first.hide();
		
		this.wins.push(win);
		//win.show(modal);
	},
	pop : function(){
		var temp = this.wins.pop();
		temp.release();
		var first = this.wins.first();
		return first;
	},
	remove : function(win){
		this.wins = this.wins.without(win);
	},
	add : function(win){
		this.wins.push(win);
	}
});

Global.DEFAULT_WINDOW_MANAGER = new UI.WindowManager();

UI.Window = Class.create({
	initialize : function(options) {
		Object.extend(this,options);
		this.parent = $(this.parent||document.body);
		Element.setStyle(this.parent,{position:'relative'});
		this.resizeBackground = this.resizeBackground.bindAsEventListener(this);
		this.resize = this.resize.bindAsEventListener(this);
		this.isPreload=this.isPreload||false;
		if(this.isPreload){
			this.content = this.createContent();
		}
	},
	
	show : function(modal){
		Global.DEFAULT_WINDOW_MANAGER.current = this;
		modal = modal || false;
		if(modal){
			if(this.background){
				this.resizeBackground();
				this.background.show();
			}else
				this.background = this.createBackground();
			
			Element.observe(window,"resize",this.resizeBackground);
			//$(window).observe("resize",this.resizeBackground);
		}
		
		if(this.content){
			this.resize();
		}else
			this.content = this.createContent();
		Element.show(this.content.parent);
		
		Element.observe(window,"resize",this.resize);
		
	},
	
	release : function(){
		if(this.content){
			Element.hide(this.content.parent);
			Element.stopObserving(window,"resize",this.resize);
			this.content.parent.remove();
		}
		
		if(this.background){
			this.background.hide();
			Element.stopObserving(window,"resize",this.resizeBackground);
			this.background.remove();
		}
		(this.callback||Prototype.emptyFunction)();
	},
	
	hide : function(){
		if(this.content){
			//Element.hide(this.content);
			Element.hide(this.content.parent);
			Element.stopObserving(window,"resize",this.resize);
		}
		
		if(this.background){
			this.background.hide();
			Element.stopObserving(window,"resize",this.resizeBackground);
		}
		//(this.callback||Prototype.emptyFunction)();
	},
	resizeBackground : function(event){
		var bgd = this.calculateBackgroundDimensions();
		this.background.style.height = bgd.height + "px";
		this.background.style.width =  bgd.width + "px";
	},
	resize : function(event){
		this.content.parent.style.left = (this.parent.clientWidth/2 - this.width/2) + "px";
		this.content.parent.style.top =(screen.availHeight/2 - 250) + "px";
	},
	createContent : function(){
		//var bodyClientHeight = this.parent.clientHeight || this.parent.offsetHeight;
		//var bodyClientWidth = this.parent.clientWidth || this.parent.offsetWidth;
		
		var table = new Element("table");
		table.border = 0;

		table.width = this.width + "px";
		table.height = this.height + "px";
								
		Element.setStyle(table,{position:'absolute'});
		Element.setStyle(table,{top:0,left:0,zIndex:100000,display:'none'});
		
		var tbody = new Element("tbody");
		table.insert(tbody);
		
		var tr = new Element("tr");
		tbody.insert(tr);

		var td = new Element("td");
		td.align = "center";
		td.valign = "middle";
		tr.insert(td);

		var iframe = new Element("iframe");
		iframe.frameBorder = 0;
		iframe.scrolling = "auto";
		iframe.vspace = 0;
		iframe.hspace = 0;
		iframe.marginHeight = 0;
		iframe.marginWidth = 0;
		iframe.width = this.width + "px";
		iframe.height = this.height + "px";
		iframe.setStyle({border:"solid #300 5px"});
		iframe.src = this.url||"about:blank";

		td.insert(iframe);
		
		this.parent.insert(table);
		table.style.left = (this.parent.clientWidth/2 - this.width/2) + "px";
		table.style.top =(screen.availHeight/2 - 250) + "px";
		
		iframe.parent = table;
		return iframe;
	},
	setDimension : function(width,height){
		this.content.width = width + "px";
		this.content.height = height + "px";
		this.content.parent.width = width + "px";
		this.content.parent.height = height + "px";
	},
	calculateBackgroundDimensions : function(){
		var bodyClientHeight = this.parent.offsetHeight;
		if(bodyClientHeight <screen.availHeight)
			bodyClientHeight = screen.availHeight;
		
		var bodyClientWidth = this.parent.offsetWidth;
		
		return {width:bodyClientWidth,height:bodyClientHeight};
	},
	createBackground : function(){
		var result = new Element("div");
		result.style.position = "absolute";
		//Position.absolutize(result);

		result.style.top = '0px';
		result.style.left = '0px';
		result.style.background = "url(" + Global.CONTEXT_PATH + "/images/dark_bg.gif)";
		var bgd = this.calculateBackgroundDimensions();
		result.style.height = bgd.height + "px";
		result.style.width =  bgd.width + "px";
		
		Element.setOpacity(result,1.0);
		result.style.zIndex = 9999;
		//result.hide();
		
		this.parent.insert(result);
		return result;
	}
});
/*
 * url: 
 * contentReadyCallBack
 * closeCallBack
 * showCallBack
 * contentEl
 * 
 */
UI.PopWindow = Class.create({
	initialize : function(options) {
		Object.extend(this,options);
		this.popWindow=null;
		this.url=this.url||null;
		this.contentEl=this.contentEl||null;
		if(this.popWindow==null){
			this.createPopWindow();
		 }
	},
	
	createPopWindow: function(){
		this.popWindow = document.createElement("div");
		this.popWindow.style.position = "absolute";
		this.popWindow.style.top = '0px';
		this.popWindow.style.left = '0px';
		this.popWindow.style.display="none";
		document.body.appendChild(this.popWindow);
		if(this.url!=null){
			new HTMLRequest({url: this.url, isHideVeil:true, callback: this._requestCallback.bind(this)});
		}else{
			this._requestCallback({"responseText":this.contentEl.innerHTML});
			this.contentEl.style.display="none";
		}
		
	},
	_requestCallback: function(request){
		this.popWindow.innerHTML=request.responseText;
		if(this.contentReadyCallBack){
			this.contentReadyCallBack();
		}
		addListener(document,"mousedown", this._mousedown.bind(this));
	},
	_mousedown: function(e){
		var target= e.srcElement || e.target;
		 var els=this.popWindow.getElementsByTagName("*");
		 var contains=false;
		 for(var i=0; i<els.length; i++){
			 if(target==els[i]){
				 contains=true;
			 }
		 }
		 if(!contains){
			 this.hide();
		 }
	},
	/*
	 * args: showCallBack arguments
	 */
	show: function(el, args){
		var actualLeft = el.offsetLeft;
		var actualTop = el.offsetTop;
		var current = el.offsetParent;

		while (current !== null){
			actualLeft += current.offsetLeft;
			actualTop += current.offsetTop;
			current = current.offsetParent;
		}
		this.popWindow.style.left=actualLeft+"px";
		this.popWindow.style.top=(actualTop+el.offsetHeight+2)+"px";
		 if(this.showCallBack){
			 this.showCallBack(args);
		 }
		 $(this.popWindow).show();
	},
	
	/*
	 * args: closeCallBack arguments
	 */
	hide: function(args){
		 if(this.closeCallBack){
			 this.closeCallBack(args);
		 }
		 $(this.popWindow).hide();
		 //removeListener(document,"mousedown", this._mousedown);
	}
	
});