
function XWindow(){
    this.intTopZIndex=1;
    
    this.topDiv=null;
    
    this.bDrag=false;
    this.bResize=false;
    this.bLock=false;
    
    this.intLastX=-1;
    this.intLastY=-1;
    this.xPos=0;
    this.yPos=0;

    this.onclose = null;

    
    var othis=this;
    document.onmousemove=function(e){
        othis._getMouseXY(e);
        
    };
    document.onmouseup=function(){
        othis.bDrag=false;
        othis.bResize=false;
        othis.intLastX = -1;
    };
    document.onmousedown=function(e){
        if(othis.bLock){
            
            if(e&&e.preventDefault){
                e.preventDefault();
                e.stopPropagation();
                //alert("othis.bLock1");
            }else{
                event.cancelBubble=true;
                event.returnValue=false;
                //alert("othis.bLock2");
            }
            
            return false;
        }
        return true;
    };
//    document.onkeydown=function(e){
//        var keycode=(e)?e.which:event.keyCode;
//        alert(keycode);
//    };
}
XWindow.prototype={
    lockWindow:function(){
        this.bLock=true;  
    },
    releaseWindow:function(){
        this.bLock=false;  
    },
    createWindow:function(xWinClass,xData, onclose){
        var othis=this;
        if(onclose)
	    this.onclose = onclose;


        var objWin=document.createElement("div");
        objWin.className="divWindow";
        objWin.style.top=xWinClass.top+"px";
        objWin.style.left=xWinClass.left+"px";
	if(xWinClass.width)
            objWin.style.width=xWinClass.width+"px";
        else
	    objWin.style.width= 426 + "px";
        var objHead=document.createElement("div");
        objHead.style.cursor="move";
        objHead.className="winHead";
        var closeLink=document.createElement("a");
        closeLink.innerHTML="close";
	closeLink.href =  "javascript:void(0)";
        closeLink.onclick=function(){
           // if(confirm("are you sure to close this window")) {
		if(othis.onclose) {
		    this.xData = xData;
		    othis.onclose.call(this);
		}
		var pa = othis.topDiv.parentNode;
		if(pa)
                    pa.removeChild(othis.topDiv);
	
	   // }
        };
        //        var closeLinkText = document.createTextNode("X");
        //        closeLink.appendChild(closeLinkText);
        closeLink.className="ToolButtons";
        objHead.appendChild(closeLink);
        var titleLabel=document.createElement("label");
        titleLabel.innerHTML="<b>"+xWinClass.titleName+"</b>"
        objHead.appendChild(titleLabel);
        
        var objBody=document.createElement("div");
        objBody.className="winBody";
        objBody.style.width = "100%";
	if(xWinClass.height)
            objBody.style.height = xWinClass.height + "px";
	else
	    objBody.style.height = 320 + "px";
        
        var objFoot = document.createElement("div");
        objFoot.className = "winFoot";
        var objFootSpan = document.createElement("span");
        objFootSpan.className = "winFootSpan";
        objFootSpan.innerHTML = "//";
        objFootSpan.onmousedown=function(){
            othis._resizeWindow(objWin);
        };
        objFoot.appendChild(objFootSpan);
        
        
        
        objHead.onmousedown=function(){
            othis._dragWindow(objWin);  
        };
        
        var objHeadToolBar=document.createElement("div");
        objHeadToolBar.className="headToolBarw";
        
        var objFootToolBar=document.createElement("div");
        objFootToolBar.className="footToolBarw";
        
        objWin.appendChild(objHead);
        objWin.appendChild(objHeadToolBar);
        objWin.appendChild(objBody);
        objWin.appendChild(objFootToolBar);
        objWin.appendChild(objFoot);
        
        objWin.headToolBar=objHeadToolBar;
        objWin.mainBody=objBody;
        objWin.footToolBar=objFootToolBar;
        //objWin.style.backgroundColor="blue";
	this.topDiv=objWin;
        this.topDiv.style.zIndex=this.intTopZIndex++;
        return objWin;
    },
    
    _setWindowSize:function(){
       
        if(this.intLastX==-1){
            this.intLastX=this.xPos;
            this.intLastY=this.yPos;
        }else{
            var intChangeX=this.xPos-this.intLastX;
            var intChangeY=this.yPos-this.intLastY;
            this.intLastX=this.xPos;
            this.intLastY=this.yPos;
            
            if(intChangeX+parseInt(this.topDiv.style.width)>=250){
                this.topDiv.style.width=intChangeX+parseInt(this.topDiv.style.width)+"px";
            }
            var wbody=this.topDiv.mainBody;
            //wbody.style.width="100%"
            if(intChangeY+parseInt(wbody.style.height)>=250){
                wbody.style.height=intChangeY+parseInt(wbody.style.height)+"px";
            }
        }
    },
    _moveWindow:function(){
        if(this.intLastX==-1){
            this.intLastX=this.xPos;
            this.intLastY=this.yPos;
        }else{
            var intChangeX=this.xPos-this.intLastX;
            var intChangeY=this.yPos-this.intLastY;
            this.intLastX=this.xPos;
            this.intLastY=this.yPos;
            this.topDiv.style.left=intChangeX+parseInt(this.topDiv.style.left)+"px";
            this.topDiv.style.top=intChangeY+parseInt(this.topDiv.style.top)+"px";
        }
    },
    _resizeWindow:function(elemWin){
        this.topDiv=elemWin;
        this.topDiv.style.zIndex=this.intTopZIndex++;
        this.bResize=true;
    },
    _dragWindow:function(elemWin){
        this.topDiv=elemWin;
        this.topDiv.style.zIndex=this.intTopZIndex++;
        this.bDrag=true;
    },
    
    _getMouseXY:function(e){
        this.xPos = parseInt((e)?e.pageX:event.clientX + document.body.scrollLeft);
        this.yPos = parseInt((e)?e.pageY:event.clientY + document.body.scrollTop);
        if(this.bDrag){
            this._moveWindow();
        }
        if(this.bResize){
            this._setWindowSize();
        }
    }
}

