var YzPanel=function(bHasShadow)
{
	this.window=window.top;
	
	var doc=this.window.document;
	var panelFrame=doc.createElement("IFRAME");
		with(panelFrame)
		{
			id="YzPanelIFrame_"+Yz.util.newGuid(); src="JavaScript:;";
			frameBorder="0"; scrolling="no";
			style.position="absolute";
			style.zIndex=5000; width=0; height=0;
			unselectable="on";
			allowTransparency=true;
		}
		
		doc.body.appendChild(panelFrame);	

	var panelWin=panelFrame.contentWindow;	
	var panelDoc=panelWin.document;
		panelDoc.open();
		panelDoc.write('<html><head></head><body style="margin:0px;padding:0px;overflow:hidden;background-color:transparent;"><div></div><\/body><\/html>');
		panelDoc.close();
	
		panelDoc.oncontextmenu=Yz.util.cancelEvent;
		
		//这句很重要，如果没有在firefox下不指定width和height的情况下
		//显示不出来,例如context menu的使用就遇到了这个问题
		panelDoc.body.firstChild.style.cssFloat='left';
		
		if(Yz.isIE)
		{
			panelFrame.onfocus=panel_onfocus;
			panelFrame.onblur=panel_onblur;
		}
		else
		{
			panelFrame.contentWindow.onfocus=panel_onfocus;
			panelFrame.contentWindow.onblur=panel_onblur;
		}
		
		this.reference=0;
		this.hasFocus=false;
		this.opened=false;
		this.frameId=panelFrame.id;
		
		Yz.instance.add(this);
		
		panelFrame.panelId=this.handle;
		panelFrame.panelHandle=this.handle;
		
		panelFrame.unselectable="on";
		
		if(bHasShadow)
		{
			//create the shadow
			var shadow=this.window.document.createElement("div"),shadowId="YzPanelShadow_"+Yz.util.newGuid();
				shadow.style.position="absolute";
				shadow.style.backgroundColor=Yz.isIE ? "black" : "gray";
				shadow.style.zIndex=panelFrame.style.zIndex-1;
				if(Yz.isIE)
					shadow.style.filter="progid:DXImageTransform.Microsoft.alpha(opacity=35) progid:DXImageTransform.Microsoft.Blur(pixelradius=3)";
				else
					shadow.style.opacity=0.30;
				shadow.style.display="none";
				shadow.id=shadowId;
				this.window.document.body.appendChild(shadow);
				this.shadowId=shadowId;
				shadow=null;
		}
		doc=null; panelWin=null; panelDoc=null; panelFrame=null;
}
Yz.panel=YzPanel;
YzPanel.fromHandle=function(handle){return Yz.instance.get(handle);}
YzPanel.prototype=
{
	show : function(x,y,w,h,relElement,comboboxHeight)
	{
		var winTop=window.top;
		
		if(this.parentId){YzPanel.fromHandle(this.parentId).lock();}
		
		var frame=winTop.document.getElementById(this.frameId);
			
		var body=frame.contentWindow.document.body.firstChild;
		
			body.style.width	= w ? w + 'px' : '' ;
			body.style.height	= h ? h + 'px' : '' ;
				
		var mainWidth = body.offsetWidth ;
			if (!w){frame.width=1;} if(!h){frame.height=1;}
			mainWidth = body.offsetWidth || body.firstChild.offsetWidth;
	
		var relEltWin=Yz.util.getElementWindow(relElement);
		//if(relElement.tagName!="BODY" && relEltWin!=window.top)
		//{
			var pos=Yz.util.getElementPosition(relElement,winTop);
				x += pos.X ; y += pos.Y ;
		//}
		var scr=Yz.util.getScrollPosition(winTop);
		var vs=Yz.util.getViewPaneSize(winTop);
		var iViewPaneHeight	= vs.Height+scr.Y;
		var iViewPaneWidth	= vs.Width +scr.X;
		
			//if(relEltWin==winTop) x+=scr.X;
			
			if ((x + mainWidth) > iViewPaneWidth)
			{
				x-=mainWidth;
			}
			
			//if(relEltWin==winTop)y+=scr.Y;
			
			//--------------------------------------------------------------
			//combobox 下拉框方向决定代码
			comboboxHeight=parseInt(comboboxHeight);
			if(isNaN(comboboxHeight) || comboboxHeight<0)comboboxHeight=0;
			if(comboboxHeight>0)
			{
				y+=comboboxHeight-1;
				if ( ( y + body.offsetHeight ) > iViewPaneHeight )
					y-=comboboxHeight-2;
			}
			//--------------------------------------------------------------
			
			if ( ( y + body.offsetHeight ) > iViewPaneHeight )
			{
				y-=body.offsetHeight;
			}
			
			
			if ( x < 0 )x = 0 ;
		
			frame.style.left= x + 'px' ;
			frame.style.top	= y + 'px' ;
	
			frame.width  = (mainWidth || w)+"px";
			frame.height = (body.offsetHeight || h)+"px";
						
			//显示阴影
			if(this.shadowId)
			{
				var sd=winTop.document.getElementById(this.shadowId);
				if(Yz.isIE==false)
				{
					sd.style.left=x+3+"px";
					sd.style.top=y+3+"px";
					sd.style.width=frame.width+"px";
					sd.style.height=frame.height+"px";
				}
				else
				{
					sd.style.left=x+"px";
					sd.style.top=y+"px";
					sd.style.width=parseInt(frame.width)-2+"px";
					sd.style.height=parseInt(frame.height)-2+"px";
				}
				sd.style.display="";
				sd=null;
			}
			
			if(Yz.isIE)
				frame.focus();
			else
				frame.contentWindow.focus();
				
			this.opened=true;
	},
	
	hide : function()
	{
		if(this.opened==false)return;
		var frame=window.top.document.getElementById(this.frameId);
		
		//隐藏Shadow
		if(this.shadowId)
		{
			window.top.document.getElementById(this.shadowId).style.display="none";
		}
		if(frame)
		{	
			frame.width=0; frame.height=0;
		}
		this.opened=false;
			
		if(this.parentId)
			YzPanel.fromHandle(this.parentId).unlock();
		
		if(this.onHide) Yz.util.delay(this.onHide,null,this);	
	},
	lock : function(){this.reference++;},
	unlock : function()
	{
		this.reference--;
			
		if(this.reference<=0 && !this.hasFocus)
			this.hide();
	},
	getBody : function(){return this.window.document.getElementById(this.frameId).contentWindow.document.body.firstChild;},
	getDocument : function(){return this.window.document.getElementById(this.frameId).contentWindow.document;},
	dispose : function()
	{
		if(!this.frameId)return;
		if(this.onDispose)
		{
			this.onDispose(this);
			this.onDispose=null;
		}
		try
		{
			var frame=this.window.document.getElementById(this.frameId);
			var body=frame.contentWindow.document.body.firstChild;
				Yz.util.removeNode(body);
				body=null;
				
				Yz.util.removeNode(frame);
				frame=null;
							
				this.frameId=null;
				this.Handle=null;
		}
		catch(e)
		{
			alert("YzPanel_Final_release_error:"+e.message);
		}
		if(this.shadowId)
		{	
			var	sd=this.window.document.getElementById(this.shadowId);
				Yz.util.removeNode(sd);
				sd=null;
		}
		this.window=null;
	},
	
	importStyleSheet : function(cssFile)
	{
		Yz.util.importStyleSheet(this.getDocument(),cssFile);
	},
	getPanelFrame : function()
	{
		return window.top.document.getElementById(this.frameId);
	},
	setFocus : function()
	{
		var frame=window.top.document.getElementById(this.frameId);
		if(Yz.isIE)
			frame.focus();
		else
			frame.contentWindow.focus();
		this.hasFocus=true;	
	},
	
	appendDomElement : function(domElt)
	{
		return this.getBody().appendChild(domElt);
	},
	getType : function(){return "YzPanel";}
}

function panel_onfocus(e)
{
	var panelFrame=Yz.isIE ? this : this.frameElement;
		YzPanel.fromHandle(panelFrame.panelId).hasFocus=true;
		panelFrame=null;
}
function panel_onblur(e)
{
	var panelFrame=Yz.isIE ? this : this.frameElement;
	var panelInst=YzPanel.fromHandle(panelFrame.panelId);
	
		panelInst.hasFocus=false;
		if(panelInst.reference==0)
		{
			if(Yz.isIE)
			{
				panelInst.hide();
			}
			else
			{
				Yz.util.delay(panelInst.hide,null,panelInst);
			}
		}
}