/**
    CoolWindow 程序
    
    创建者:张云飞
    
    使用方法:
    
    
    1.创建窗口
    var aWin = new CoolWin("ID", "宽", "高", "左", "顶", "标题", "窗口内容");
    
    var exWin = new CoolWin("id1", "250", "200", "100", "100", "示例", "COOLWindow示例");
    
    
    2.显示窗口
    
    ShowWindow("id1", null);
    
    3.隐藏窗口
    
    ShowWindow("id1", "none");
    
    3.关闭窗口
    DestroyWindow("id1");

*/




var x0=0,y0=0,x1=0,y1=0;
var offx=6,offy=6;
var moveable=false;
//var hover='orange',normal='#336699';//color;
var hover='coolwindow coolhover',normal='coolwindow coolnormal';
var index=10000;//z-index;
//开始拖动;
function startDrag(obj)
{
    if(event.button==1)
    {
    
        if(window.startDragCallBack)
        {
            startDragCallBack(obj.parentNode.id);
        }
    
        //锁定标题栏;
        obj.setCapture();
        //定义对象;
        var win = obj.parentNode;
        var sha = win.nextSibling;
        //记录鼠标和层位置;
        x0 = event.clientX;
        y0 = event.clientY;
        x1 = parseInt(win.style.left);
        y1 = parseInt(win.style.top);
        //记录颜色;
        normal = win.className;
        //改变风格;
        win.className = hover;
        sha.style.left = x1 + offx;
        sha.style.top = y1 + offy;
        moveable = true;

      

    }
}
//拖动;
function drag(obj)
{
if(moveable)
{
var win = obj.parentNode;
var sha = win.nextSibling;
var winLeft = x1 + event.clientX - x0;
var winTop  = y1 + event.clientY - y0;

win.style.left = winLeft;
win.style.top = winTop;
sha.style.left = parseInt(winLeft) + offx;
sha.style.top = parseInt(winTop) + offy;
}
}
//停止拖动;
function stopDrag(obj)
{
    if(moveable)
    {
    var win = obj.parentNode;
    var sha = win.nextSibling;
    var msg = obj.nextSibling;
    win.className = normal;
    sha.style.left = obj.parentNode.style.left;
    sha.style.top = obj.parentNode.style.top;
    obj.releaseCapture();
    moveable = false;

        if(window.stopDragCallBack)
        {
            stopDragCallBack(win.id);
        
        }        

    }

}
//获得焦点;
function getFocus(obj)
{
if(obj.style.zIndex!=index)
{
index = index + 2;
var idx = index;
obj.style.zIndex=idx;
obj.nextSibling.style.zIndex=idx-1;
}
}
//最小化;
function min(id)
{

var win = document.getElementById( id );

var minButton = document.getElementById( id + "Min");
var sha = document.getElementById( id + "bg");
var titTxt = document.getElementById( id + "TitleText");
var body = document.getElementById( id + "Body");
var flg = body.style.display=="none";
if(flg)
{
var bodyHeight = 0;
if( body.style.height!="" )bodyHeight = body.style.height;
win.style.height = parseInt(bodyHeight) + parseInt(titTxt.style.height) + 2*2;
sha.style.height = win.style.height;
body.style.display = "block";
minButton.innerHTML = "0";
}
else
{
win.style.height = parseInt(titTxt.offsetHeight) + 2*2;
sha.style.height = win.style.height;
minButton.innerHTML = "2";
body.style.display = "none";
}

MoveDivRoot(win.id);

}
//创建一个对象;
function CoolWin(id,w,h,l,t,tit,msg)
{
index = index+2;
this.id = id;
this.width = w;
this.height = h;
this.left = (l==null)?(document.body.offsetWidth / 2 - w / 2 ):l;
this.top = (t==null)?(document.body.offsetHeight/2 - h/2):t;
this.zIndex = index;
this.title = tit;
this.message = msg;
this.obj = null;
this.bulid = bulid;
this.ShowWindow = ShowWindow;
this.DestroyWindow = DestroyWindow;

var me = document.getElementById( id);
if(me == null){
    this.bulid();
}else{
	this.ShowWindow(this.id, "");
}


}
//初始化;
function bulid()
{
var str = ""
+ "<div id=\"" + this.id + "\" "
+ "style='"
+ "z-index:" + this.zIndex + ";"
+ "width:" + this.width + ";"
+ "height:" + this.height + ";"
+ "left:" + this.left + ";"
+ "top:" + this.top + ";"
+ "font-size:8pt;"
+ "font-family:Tahoma;"
+ "position:absolute;"
+ "cursor:default;"
+ "' "
+ "class='" + normal + "' "
+ "onmove=' MoveDivRoot(\"" + this.id + "\") ' "
+ "onmousedown=\'getFocus(this)\'>"
+ "<div id='"+ this.id + "Title'"
+ "style='"
+ "width:" + (this.width-2*2) + ";"
+ "height:20;"
+ "color:white;"
+ "' "
+ "class='titlebar'"
+ "onmousedown='startDrag(this)' "
+ "onmouseup='stopDrag(this)' "
+ "onmousemove='drag(this)' "
+ "ondblclick='min(\"" + this.id + "\")'"
+ ">"
+ "<table border='0' cellpadding='0' cellspacing='0' width='100%'>"
+ "<td width='90%'><span class='title' style='padding-left:3px;height:20px' id='"+  this.id + "TitleText'>" + this.title + "</span></td>"
+ "<td width='10%' align='right'>"
+ "<span style='border-width:0px;color:white;font-family:webdings;' id='" +  this.id + "Min' onclick='min(\"" + this.id + "\")'>0</span>"
+ "<span style='border-width:0px;color:white;font-family:webdings;' id='" +  this.id + "Close' onclick='DestroyWindow(\""+this.id+"\",null)'>r</span>"
+ "</td>"
+ "</table>"
+ "</div>"
+ "<div class='body' id='" +  this.id + "Body' style='"
+ "width:98%;"
+ "height:" + (this.height-20-6) + ";"
+ "background-color:white;"
+ "line-height:14px;"
+ "word-break:break-all;"
+ "padding:3px;"
+ "'>" + this.message + "</div>"
+ "</div>"
+ "<div id=" +  this.id + "bg style='"
+ "width:" + this.width + ";"
+ "height:" + this.height + ";"
+ "top:" + this.top + ";"
+ "left:" + this.left + ";"
+ "z-index:" + (this.zIndex-1) + ";"
+ "position:absolute;"
+ "background-color:black;"
+ "filter:alpha(opacity=40);"
+ "'></div>";
document.body.insertAdjacentHTML("beforeEnd",str);

    var win = document.getElementById( this.id );
    var tit = document.getElementById( this.id + "Title");
    
    tit.style.width = win.clientWidth;
    
    
    
    //以下实现不被SELECT盖住
    var eleHTML = "<div id='" +  this.id + "divRoot' style='z-index:"
    			+ (this.zIndex-5) + ";position:absolute;' ></div>";
    var divRoot = document.createElement( eleHTML );
    var ifm=document.createElement("<iframe frameborder=0 marginheight=0 marginwidth=0 hspace=0 vspace=0 scrolling=no></iframe>")
    ifm.style.width=win.offsetWidth;
    ifm.style.height=win.offsetHeight;
    ifm.id=  this.id + "Iframe";
    document.body.appendChild(divRoot);
    divRoot.appendChild(ifm);  
    var frameDoc = window.frames[ifm.id].document;
    frameDoc.write("<html><body></body></html>");
    frameDoc.body.style.backgroundColor = win.style.backgroundColor;
    
    divRoot.style.position = win.style.position;
    
    MoveDivRoot(win.id);
    
    
}
//显示隐藏窗口
function ShowWindow(id,dis){

	var divRoot = document.getElementById( id + "divRoot");
	var bdisplay = (dis==null)?((document.getElementById(id).style.display=="")?"none":""):dis
	document.getElementById(id).style.display = bdisplay;
	document.getElementById(id+"bg").style.display = bdisplay;
	divRoot.style.display = bdisplay;
	
	if(dis == null || dis=="null"){
		if(window.saveStatus){
			saveStatus(id);
		}
	}
}

//销毁窗口
function DestroyWindow(id){

	var dwObj = document.getElementById(id);
	var divRoot = document.getElementById( id + "divRoot" );
	var bgDiv = document.getElementById(id + "bg");
	
	if(window.saveStatus){
		saveStatus(id);
	}
	
	if(divRoot != null){
		divRoot.parentElement.removeChild(divRoot);
	}
	if(bgDiv != null){
		bgDiv.parentElement.removeChild(bgDiv);
	}
	if(dwObj != null){
		dwObj.parentElement.removeChild(dwObj);
	}
		
}


//实现不被SELECT盖住
function MoveDivRoot(winId)
{
    var win = document.getElementById(winId);
    var divRoot = document.getElementById(winId + "divRoot");
    var ifm = divRoot.childNodes[0];
    
    divRoot.style.left = win.style.left;
    divRoot.style.top = win.style.top;
    divRoot.style.width = win.style.width;
    divRoot.style.height = win.style.height;
    
    ifm.style.width = win.offsetWidth;
    ifm.style.height = win.offsetHeight;
    
     
}


//startDrag回调函数  (隐藏窗口内的滚动条，否则IE会出错)
function startDragCallBack(winId)
{
	
	//公告处理
	var LrcShower = document.getElementById("LrcShower" + winId);
	if(LrcShower!=null)
	{
		LrcShower.setAttribute("viewStatus", LrcShower.style.overflowY);
    	LrcShower.style.overflowY='hidden';
   	}
   	
   	
   	//TEXTAREA处理
   	var taElements = $(winId).getElementsByTagName( "TEXTAREA" );
	for( var i=0; i<taElements.length; i++ )
	{
		var ta = taElements[i];
		ta.setAttribute("overflowX", ta.style.overflowX);
		ta.setAttribute("overflowY", ta.style.overflowY);
		ta.style.overflowX = 'hidden';
		ta.style.overflowY = 'hidden';
	}
	
}

//stopDrag回调函数   (显示隐藏的滚动条)
function stopDragCallBack(winId)
{
	//公告处理
    var LrcShower = document.getElementById("LrcShower" + winId);
    if(LrcShower!=null)
	{
	    var ofy = LrcShower.getAttribute("viewStatus");
	    LrcShower.style.overflowY = ofy;
	}
	
	
	//TEXTAREA处理
	var taElements = $(winId).getElementsByTagName( "TEXTAREA" );
	for( var i=0; i<taElements.length; i++ )
	{
		var ta = taElements[i];
		ta.style.overflowX = ta.getAttribute("overflowX");
		ta.style.overflowY = ta.getAttribute("overflowY");
	}   
    
    
}


//取得事件发生位置
function getEventPos(ev)
{
	ev = ev||event;
	var obj = ev.srcElement;
	return getElementPos( obj );
}

//取得元素的位置
function getElementPos(ele)
{
	var obj = ele;
	objLeft   = obj.offsetLeft;
	objTop    = obj.offsetTop;
	objParent = obj.offsetParent;
	while( objParent.tagName.toUpperCase() != "BODY" && objParent.tagName.toUpperCase() != "HTML" ){
		objLeft  += objParent.offsetLeft;
	    objTop   += objParent.offsetTop;
	    objParent = objParent.offsetParent;
	}
	
	var arr = new Array();
	arr["left"] = objLeft;
	arr["top"] = objTop;
	
	return arr;
}



function showMsg( msg , id){
	
	if(!id){
		id = "showMsg";
	}
			
	var showMsg = $(id);
	if( !showMsg ){
		var ele 		= document.createElement("<div></div>");
		ele.id 			= id;
		ele.className 	= id;
		document.body.appendChild(ele);
		showMsg = ele;
	}
	
	var cssText = "VERTICAL-ALIGN: middle; COLOR: red; TEXT-ALIGN: center;";
	
	showMsg.innerHTML = "<table width='100%' height='100%'><tr><td>" + msg + "</td></tr></table>";
	
	showMsg.style.position = "absolute";
	showMsg.style.border = "#ff6600 thin solid";
	showMsg.style.display = "";
	showMsg.style.backgroundColor = "#e3e3e3";
	
	var left = document.documentElement.scrollLeft + document.documentElement.clientWidth/2;
	var top  = document.documentElement.scrollTop + document.documentElement.clientHeight/2;
	
	showMsg.style.width = 192;
	showMsg.style.height = 72;
	showMsg.style.left = left - parseInt(showMsg.style.width)/2;
	showMsg.style.top  = top - parseInt(showMsg.style.height)/2;
	
	
}

function closeMsg(id){
	if(!id){
		id = "showMsg";
	}
	
	if( $(id) ){
		$(id).style.display = "none";
	}
}


	
function openDialog(url, rect)
{
	if( rect==null )
	{
		rect.left	= 100;
		rect.top  	= 50;
		rect.width	= 700;
		rect.height	= 500;
	}
	showModelessDialog(url, window ,"dialogLeft:" + rect.left + "px;dialogTop:" + rect.top + "px;dialogWidth:" + rect.width + "px;dialogHeight:" + rect.height + "px;status:false;edge:Raised; enter: Yes; help: No; resizable: Yes; status: No");
	//showModalDialog (url, "openDialog" ,"toolbar=no,location=no,directories=no,status=yes,menubar=no,left=" + rect.left + ",top=" + rect.top + ",width=" + rect.width + ",height=" + rect.height);
}


