//###############Debug##############

var __nn6=document.getElementById&&!document.all;
var __isdrag=false;
var __ispredrag=false;
var __y,__x;
var __oDragObj;
var __debugWindow;

function __getElementById(id) { return document.all? document.all(id) : document.getElementById(id);};
function __getWindowScroll() {
    var T, L, W, H;
	var w = window;
	with (w.document) {
		if (w.document.documentElement && documentElement.scrollTop) {
		  T = documentElement.scrollTop;
		  L = documentElement.scrollLeft;
		} else if (w.document.body) {
		  T = body.scrollTop;
		  L = body.scrollLeft;
		}
		if (w.innerWidth) {
		  W = w.innerWidth;
		  H = w.innerHeight;
		} else if (w.document.documentElement && documentElement.clientWidth) {
		  W = documentElement.clientWidth;
		  H = documentElement.clientHeight;
		} else {
		  W = body.offsetWidth;
		  H = body.offsetHeight
		}
	}
    return { top: T, left: L, width: W, height: H };
};

function __centerElement(el) {
    var windowScroll = __getWindowScroll();
	//var _el = jQuery(el);
	el.style.width = windowScroll.width/3 +"px";
	el.style.top = (windowScroll.height - el.style.height.substr(0,el.style.height.indexOf("px")))/2 + windowScroll.top +"px";
    el.style.left = (windowScroll.width - el.style.width.substr(0,el.style.width.indexOf("px")))/2 + windowScroll.left +"px";
};

function __insertHtml(where, el, html){      
    where = where.toLowerCase();      
    if(el.insertAdjacentHTML){      
     
        switch(where){      
            case "beforebegin":      
                el.insertAdjacentHTML('BeforeBegin', html);      
                return el.previousSibling;      
            case "afterbegin":      
                el.insertAdjacentHTML('AfterBegin', html);      
                return el.firstChild;      
            case "beforeend":      
                el.insertAdjacentHTML('BeforeEnd', html);      
                return el.lastChild;      
            case "afterend":      
                el.insertAdjacentHTML('AfterEnd', html);      
                return el.nextSibling;      
        }      
        throw 'Illegal insertion point -> "' + where + '"';      
    }      
                    
    var range = el.ownerDocument.createRange();      
    var frag;      
    switch(where){      
         case "beforebegin":      
            range.setStartBefore(el);      
            frag = range.createContextualFragment(html);      
            el.parentNode.insertBefore(frag, el);      
            return el.previousSibling;      
         case "afterbegin":      
            if(el.firstChild){      
                range.setStartBefore(el.firstChild);      
                frag = range.createContextualFragment(html);      
                el.insertBefore(frag, el.firstChild);      
                return el.firstChild;      
             }else{      
                el.innerHTML = html;      
                return el.firstChild;      
             }      
        case "beforeend":      
            if(el.lastChild){      
                range.setStartAfter(el.lastChild);      
                frag = range.createContextualFragment(html);      
                el.appendChild(frag);      
                return el.lastChild;      
            }else{      
                el.innerHTML = html;      
                return el.lastChild;      
            }      
        case "afterend":      
            range.setStartAfter(el);      
            frag = range.createContextualFragment(html);      
            el.parentNode.insertBefore(frag, el.nextSibling);      
            return el.nextSibling;      
    }      
    throw 'Illegal insertion point -> "' + where + '"';      
};


function __moveMouse(e) {
	if(__isdrag){
		__oDragObj.style.top  =  (__nn6 ? nTY + e.clientY - __y : nTY + event.clientY - __y)+"px";
		__oDragObj.style.left  =  (__nn6 ? nTX + e.clientX - __x : nTX + event.clientX - __x)+"px";
		return false;
	}
};

function __initDrag(e) {
	var oDragHandle = __nn6 ? e.target : event.srcElement;
	if(oDragHandle.id=='__oDragHandle'){
		try{
			 __isdrag = true;
			 __oDragObj = __getElementById('__debugWindow');
			 nTY = parseInt(__oDragObj.style.top+0);
			 __y = __nn6 ? e.clientY : event.clientY;
			 nTX = parseInt(__oDragObj.style.left+0);
			 __x = __nn6 ? e.clientX : event.clientX;
			 document.onmousemove=__moveMouse;
			 return false;
		}catch(eee){_debug(eee.name + ": " + eee.message+" ;<br/>stack:" + eee.stack);}
	}

};

document.onmousedown=__initDrag;

//document.onmousedown=function(){__ispredrag=true;};

document.onmouseup=function(){__isdrag=false;};

var __debugWindowHtml = "<table id='__debugWindow' cellspacing='0' cellpadding='0' style='display:none;position:absolute;z-index:100000;border:1px solid #4c4f45;font-size:12px;font-family:Tahoma,Verdana,\"微软雅黑\",\"宋体\"'>"+
	"<tr><td id='__oDragHandle' align='right' style='height:18px;line-height:18px;text-align:right;background:#6d7162;border-top:1px solid #878b7d;border-bottom:1px solid #4c4f45;'>"+
		"<span onclick='__getElementById(\"__debugInfoTd\").innerHTML=\"\"' style='font-size:10px;font-weight:bold;cursor:pointer;text-decoration:none;color:#d7d8d4;display:inline;width:16px;height:16px;margin-right:5px;' onmouseover='this.style.color=\"#d7d8d4\"' onmouseout='this.style.color=\"#b2b5ac\"'>E</span>"+
		"<span onclick='__getElementById(\"__debugWindow\").style.display=\"none\"' style='font-size:10px;font-weight:bold;cursor:pointer;text-decoration:none;color:#d7d8d4;display:inline;margin-right:5px;padding:3px;' onmouseover='this.style.color=\"#d7d8d4\"' onmouseout='this.style.color=\"#b2b5ac\"'>X</span>"+
	"</td></tr>"+
	"<tr><td id='__debugInfoTd' valign='top' style='height:140px;background-color:#8f9782;'></td></tr>"+
	"<tr><td id='__debugConsoleTd' style='height:60px;border-width:1px;border-style:solid;border-color:#2D2D2C #777976 #777976 #2D2D2C;background:url(/images/debug_cbg.gif) no-repeat #4E4E4D;'><textarea id='__debugConsole' style='width:98%;height:60px;background-color:transparent;border-width:0px;margin:6px;font-family:Verdana;color:#66FF66;overflow: hidden;'></textarea></td></tr>"+
	"</table>";

function _debug(debugInfo){
	__debugWindow = __getElementById("__debugWindow");
	if(__debugWindow==null || __debugWindow=='undefined'){
		__insertHtml("beforeend",document.body,__debugWindowHtml);
		__debugWindow = __getElementById("__debugWindow");
		__centerElement(__debugWindow);
	}
	var __debugInfoTd = __getElementById("__debugInfoTd");
	
	__debugWindow.style.display='';

	__insertHtml("beforeend", __debugInfoTd, "<div style='line-height:19px;border-bottom:1px solid #9ba290;font-family:Verdana;color:#f2f2f2;'>"+debugInfo+"</div>");
};

var doDebug = true;

