var YzDebug=
{
	isIE : /*@cc_on!@*/false,
	initialized : false,
	initWidth : 300,
	initHeight: 200,
	getCssText : function()
	{
		var headBgImg="/Admin/Skin/Images/YzDialog/dialog.titlebar.bg.gif";
		if(Yz && Yz.config)
		{
			headBgImg=Yz.config.skin+"Images/YzDialog/dialog.titlebar.bg.gif";
		}
		
		var a=[];
			a.push("*{font-size:12px;}");
			a.push("#divMain{border:1px solid #3977AA;background-color:#ffffff;}");
			a.push("#divInner{border:1px solid #B4D0E4;}");
			a.push("#divHead{background-image:url("+headBgImg+");height:22px;background-color:#eeeddb;padding-left:5px;cursor:move;color:#124EB1;}");
			a.push("#divFoot{background-color:#F5F9FC;padding:5px 10px 5px 0px;text-align:right;border-top:1px solid #B4D0E4;}");
			a.push("#divBody{position:relative;background-color:#ffffff;overflow:hidden;}");
			a.push("#divBody textarea{position:relative;top:-1px;border:none;width:100%;height:100%;overflow:auto;}")
			return a.join("\n");
	},
	
	getWindow : function()
	{
		return window.top.document.getElementById("__YzDebugWindow");
	},
	
	initialize : function()
	{
		if(this.initialized)return false;
		var win=window.top;
		var doc=win.document;
		var ifrm=doc.createElement("IFRAME");
	
		with(ifrm)
		{
			id="__YzDebugWindow"; src="JavaScript:void(0)";
			frameBorder="0"; scrolling="no";
			style.position="absolute";
			style.zIndex=20000; width=0; height=0;
			unselectable="on";
		}
		doc.body.appendChild(ifrm);	
		
		var cssText=this.getCssText();
		var ifrmWin=ifrm.contentWindow;	
		var ifrmDoc=ifrmWin.document;
			ifrmDoc.open();
			ifrmDoc.write('<html><head><style type="text/css">'+cssText+'</style></head><body style="margin:0px;padding:0px;"><div id="divMain"></div><\/body><\/html>');
			ifrmDoc.close();
			
		var	main=ifrmDoc.body.firstChild;
		var html="<div id='divHead'>Debug window</div>"+
				 "<div id='divBody'><textarea></textarea></div>"+
				 "<div id='divFoot'><input type='button' value='clear' /></div>";
			main.innerHTML="<div id='divInner'>"+html+"</div>";
			
			
		win.__debugWinId="__YzDebugWindow";
		this.opened=false;
		
		this.initialized=true;
		
		var	head=ifrmDoc.getElementById("divHead");
			head.onmousedown=(/*@cc_on!@*/false) ? YzDebugHead_onmousedown : YzDebugHead_onmousedown_ff;
		var foot=ifrmDoc.getElementById("divFoot");
			foot.firstChild.onclick=YzDebug.clearOutput;
		
		main.onmousemove=YzDebugMain_onmousemove;	
		main.onmousedown=YzDebugMain_onmousedown;
			
	},
	clear : function(){YzDebug.output("",true);},
	clearOutput : function()
	{
		YzDebug.output("",true);
	},
	output : function(obj,bClear)
	{
		if(!this.initialized)this.initialize();
		if(!this.opened)this.open();
		
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
		var	doc=ifrm.contentWindow.document;
		var ta=doc.getElementById("divBody").firstChild;	
		if(bClear)
			ta.value=YzDebug.dump(obj)+"\n";
		else
			ta.value+=YzDebug.dump(obj)+"\n";	
	},
	calcSize : function()
	{
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
		var doc=ifrm.contentWindow.document;
		
		var main=doc.getElementById("divMain");
		var head=doc.getElementById("divHead");
		var body=doc.getElementById("divBody");
		var foot=doc.getElementById("divFoot");
		
		var paddingTB=0;
			if(!this.isIE)
			{
				var ss=doc.styleSheets[0];
				var rules=ss.cssRules,rule=null;
				for(var i=0;i<rules.length;i++)
				{
					var rule=rules[i];
					if(rule.selectorText=="#divBody")break;
				}
				if(rule && rule.style)
				{
					paddingTB=parseInt(rule.style.paddingTop)+parseInt(rule.style.paddingBottom);
					if(isNaN(paddingTB))paddingTB=0;
				}
			}
			main.style.height=ifrm.offsetHeight-(this.isIE ? 0 : 4)+"px";
			body.style.height=main.clientHeight-head.offsetHeight-foot.offsetHeight-paddingTB-2+"px";
	},
	open : function()
	{
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
		var doc=ifrm.contentWindow.document;
		
		var vs=getViewPaneSize(window.top);
		ifrm.style.display="";
		ifrm.style.width=this.initWidth+"px";
		ifrm.style.height=this.initHeight+"px";
		ifrm.style.left=vs.Width-this.initWidth-10+"px";
		ifrm.style.top="10px";
		YzDebug.calcSize();
		
		YzDebug.opened=true;
	},
	
	moveTo : function(x,y)
	{
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
			ifrm.style.left=(this.startLeft+x-this.startX)+"px";
			ifrm.style.top=(this.startTop+y-this.startY)+"px";
	},
	dump : function(obj)
	{
		if(typeof obj!="object" && typeof obj!="function")
		{
			return obj;
		}
		var a=[];
		for(var p in obj)
		{
			a.push(p+":"+obj[p]);
		}
		return a.join("\n");
	},
	resize : function()
	{
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
		var doc=ifrm.contentWindow.document;
		var body=doc.getElementById("divBody");
		var main=doc.body.firstChild;
		var head=doc.getElementById("divHead");
		var foot=doc.getElementById("divFoot");
		
		body.style.height=body.offsetHeight-(main.offsetHeight-ifrm.offsetHeight)+"px";
		main.style.height=ifrm.offsetHeight+(this.isIE ? 0 : -4)+"px";
		body.style.height=main.clientHeight-head.offsetHeight-foot.offsetHeight-2+"px";
	},
	startResize : function(x,y,dir)
	{
		var ifrm=window.top.document.getElementById(window.top.__debugWinId);
		this.resizeData={};
		this.resizeData.startWidth=ifrm.offsetWidth;
		this.resizeData.startheight=ifrm.offsetHeight;
		this.resizeData.startLeft=parseInt(ifrm.style.left);
		this.resizeData.startX=x;
		this.resizeData.startY=y;
		this.resizeData.dir=dir;
		
		var main=ifrm.contentWindow.document.body.firstChild;
		if(main.setCapture)
		{
			main.setCapture();
			main.onmouseup=YzDebugMain_onmouseup;
			main.onmousemove=YzDebugMain_onmousemove_resize;
			return;
		}
		var win=ifrm.contentWindow;
			win.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			win.onmouseup=YzDebugMain_onmouseup;
			win.onmousemove=YzDebugMain_onmousemove_resize;
	},
	endResize : function()
	{
		this.resizeData=null;
		
		var ifrm=YzDebug.getWindow();
		var main=ifrm.contentWindow.document.body.firstChild;
		if(main.releaseCapture)
		{
			main.releaseCapture();
			main.onmouseup=null;
			main.onmousemove=YzDebugMain_onmousemove;
			return false;
		}
		var win=ifrm.contentWindow;
			win.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			win.onmousemove=null;
			win.onmouseup=null;
			
	},
	resizing : function(x,y)
	{
		var ifrm=YzDebug.getWindow();
		
		var sw=this.resizeData.startWidth;
		var sh=this.resizeData.startheight;
		var sl=this.resizeData.startLeft;
		var sx=this.resizeData.startX;
		var sy=this.resizeData.startY;
		var ox=x-sx,oy=y-sy;
		
		var w=0,l=0,h=0,r=false,maxW=800,maxH=600,minW=100,minH=100;
		switch(this.resizeData.dir)
		{
			case "w": w=sw-ox; if(w>minW && w<maxW)l=sl+ox;	break;
			case "e": w=sw+ox; break;
			case "n": h=sh+oy; if(h>minH && h<maxH)r=true;	break;
			case "se":
				w=sw+(ox); h=sh+(oy);
				if((w>minW && w<maxW) || (h>minH && h<maxH))
					r=true;
			break;
			case "sw":
				w=sw-(ox); h=sh+(oy);
				if((w>minW && w<maxW) || (h>minH && h<maxH))
				{
					l=sl+(ox);	r=true;
				}
			break;
		}
		if(w>minW && w<maxW)ifrm.style.width=w+"px";
		if(l>0)ifrm.style.left=l+"px";
		if(h>minH && h<maxH)ifrm.style.height=h+"px";
		if(r)YzDebug.resize();
	}
};
Yz.debug=YzDebug;
Yz.trace=function(obj,bClear)
{
	YzDebug.output(obj,bClear);
}

/////////////////////////////////////////////////////////////////////////////
function mainHitTest(elt,x,y)
{
	var oh=elt.offsetHeight,ow=elt.offsetWidth;
	var bw=ow-elt.clientWidth+1; //border width
	var r="";
	if(x<=bw && y>20 && y<(oh-bw-4))
	{
		r="w";
	}
	else if(x<=bw && y>=(oh-bw-4))
	{
		r="sw";
	}
	else if(x>=(ow-bw) && y>20 && y<(oh-bw-4))
	{
		r="e";
	}
	else if(x>=(ow-bw) && y>=(oh-bw-4))
	{
		r="se";
	}
	else if(y>=(oh-bw-4) && x>bw && x<(ow-bw))
	{
		r="n";
	}
	return r;
}
function YzDebugMain_onmousemove(e)
{
	this.style.cursor="default";
	
	var win=getElementWindow(this);
	var e=e || win.event;
	var elt=e.target || e.srcElement;
	if(elt.id=="divInner")elt=elt.parentNode;
	if(elt.id==this.id)
	{
		var x=e.x || e.pageX;
		var y=e.y || e.pageY;
		var r=mainHitTest(elt,x,y);
		
		if(r!="")this.style.cursor=r+"-resize";
	}
};
function YzDebugMain_onmousedown(e)
{
	var win=getElementWindow(this);
	var e=e || win.event;
	var elt=e.target || e.srcElement;
	if(elt.id=="divInner")elt=elt.parentNode;
	if(elt.id==this.id)
	{
		var x=e.x || e.pageX;
		var y=e.y || e.pageY;
		var r=mainHitTest(this,x,y);
		if(r!="")
		{
			YzDebug.startResize(e.screenX || e.pageX,e.screenY || e.pageY,r);
		}
	}
};
function YzDebugMain_onmouseup(e)
{
	YzDebug.endResize();
};
function YzDebugMain_onmousemove_resize(e)
{
	if(!e)e=getElementWindow(this).event;
	var x=e.screenX || e.pageX;
	var y=e.screenY || e.pageY;
	YzDebug.resizing(x,y);
}
/////////////////////////////////////////////////////////////////////////////
function YzDebugHead_onmousedown(e)
{
	var win=getElementWindow(this);
	var e=e || win.event;
	
	YzDebug.startX=e.screenX || e.pageX;
	YzDebug.startY=e.screenY || e.pageY;
	YzDebug.startLeft=parseInt(win.frameElement.style.left);
	YzDebug.startTop=parseInt(win.frameElement.style.top);
	
	this.onmouseup=YzDebugHead_onmouseup;
	this.onmousemove=YzDebugHead_onmousemove;
	this.setCapture();
}

function YzDebugHead_onmouseup()
{
	this.releaseCapture();
	this.onmouseup=null;
	this.onmousemove=null;
}
function YzDebugHead_onmousemove(e)
{
	var win=getElementWindow(this);
	var e=e || win.event;
	var x=e.screenX || e.pageX;
	var y=e.screenY || e.pageY;
	
	YzDebug.moveTo(x,y);
}
///////////////////////////////////////////////////////////////////
function YzDebugHead_onmousedown_ff(e)
{
	var ifrm=YzDebug.getWindow();
	YzDebug.startX=e.screenX;
	YzDebug.startY=e.screenY;
	YzDebug.startLeft=parseInt(ifrm.style.left);
	YzDebug.startTop=parseInt(ifrm.style.top);
	
	var win=ifrm.contentWindow;
	
	win.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
	win.onmousemove=function(ev)
	{
		YzDebug.moveTo(ev.screenX,ev.screenY);
	}
	
	win.onmouseup=function()
	{
		win.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		win.onmousemove=null;
		win.onmouseup=null;
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////
function getElementDocument(elt){return elt.ownerDocument || elt.document;}
function getElementWindow(elt){return getDocumentWindow(getElementDocument(elt));}
function getDocumentWindow(doc){return doc.parentWindow || doc.defaultView;}
function getViewPaneSize(win)
{
	if(!win.attachEvent)return{Width : win.innerWidth, Height : win.innerHeight } ;
	var oSizeSource ;
	var oDoc = win.document.documentElement ;
	if ( oDoc && oDoc.clientWidth )				// IE6 Strict Mode
		oSizeSource = oDoc ;
	else
		oSizeSource = win.document.body ;		// Other IEs
	if ( oSizeSource )
		return { Width : oSizeSource.clientWidth, Height : oSizeSource.clientHeight } ;
	else
		return { Width : 0, Height : 0 };
}

