// static class: FloatingWindow    version: 1.1
// ------ -----  ------------
// created by: Diego Costa
// foxy_dcc@hotmail.com

function FloatingWindow(sId,iLeft,iTop,iWidth,iHeight,sBgColor,oContent,bToolbar,sToolbarTitle,bCloseButton,sPosition,bMinimizeButton)
{
    FloatingWindow.prototype.div;
    FloatingWindow.prototype.divShadow;
    FloatingWindow.prototype.divContainer;
    FloatingWindow.prototype.tblToolbar;
    FloatingWindow.prototype.dragging;
    
    FloatingWindow.prototype.startMouseX;
    FloatingWindow.prototype.startMouseY;
    
    FloatingWindow.prototype.content;
    FloatingWindow.prototype.onclose;
    FloatingWindow.prototype.onminimize;
    
    var tbl;
    var tr1;
    var tr2;
    var td1;
    var td2;
    var tblb;
    var toolBarTbl;
    var toolBarTblb;
    var toolBarTr;
    var toolBarTd1;
    var toolBarTd2;
    var btn1;
    var btn2;
    var div1;
    var iToolBarTd2Width=0;
  
    var click=function(){FloatingWindow.prototype.close();}
    var mouseDown=function(event){FloatingWindow.prototype.startDrag(FloatingWindow.prototype.mouseX(event),FloatingWindow.prototype.mouseY(event));}
    var mouseMove=function(event){FloatingWindow.prototype.drag(FloatingWindow.prototype.mouseX(event),FloatingWindow.prototype.mouseY(event));}
    var mouseUp=function(){FloatingWindow.prototype.drop();}
    var minimize=function(){FloatingWindow.prototype.minimize();};
    
    if(sPosition==null)
    {
        sPosition="absolute";
    }
    
    FloatingWindow.prototype.dragging=false;
    if(bCloseButton==null)
    {
        bCloseButton=true;
    }
    if(bMinimizeButton==null)
    {
        bMinimizeButton=false;
    }
    
    if(document.getElementById(sId))
    {
      document.body.removeChild(document.getElementById(sId));
      //document.body.removeChild(document.getElementById(sId + "Shadow"));
    }
    
    FloatingWindow.prototype.div=document.createElement("div");
    FloatingWindow.prototype.divShadow=document.createElement("div");
    
    tbl=document.createElement("table");
    tblb=document.createElement("tbody");
    tr1=document.createElement("tr");
    tr2=document.createElement("tr");
    td1=document.createElement("td");
    td2=document.createElement("td");
    div1=document.createElement("div");
    
    FloatingWindow.prototype.div.style.position=sPosition;
    FloatingWindow.prototype.div.setAttribute("id",sId);
    FloatingWindow.prototype.div.style.background=sBgColor;
    
    FloatingWindow.prototype.divShadow.style.position=sPosition;
    FloatingWindow.prototype.divShadow.setAttribute("id",sId + "Shadow");
    FloatingWindow.prototype.divShadow.style.background="#dde2e6";
    FloatingWindow.prototype.div.style.left=iLeft + "px";
    FloatingWindow.prototype.div.style.top=iTop + "px";
    FloatingWindow.prototype.div.style.zIndex=2;
    FloatingWindow.prototype.divShadow.style.left=iLeft-10 + "px";
    FloatingWindow.prototype.divShadow.style.top=iTop+10 + "px";
    FloatingWindow.prototype.divShadow.style.display='';
	FloatingWindow.prototype.divShadow.style.opacity=40/100;
	FloatingWindow.prototype.divShadow.style.MozOpacity=40/100;
	FloatingWindow.prototype.divShadow.style.KhtmlOpacity=40/100;
	FloatingWindow.prototype.divShadow.style.filter="alpha(opacity=" + 40+ ")";
	FloatingWindow.prototype.divShadow.style.zIndex=-1;
    div1.style.overflow="auto";
    
    FloatingWindow.prototype.div.style.width=iWidth + "px";
    FloatingWindow.prototype.div.style.height=iHeight + "px";
    FloatingWindow.prototype.div.setAttribute("initialHeight",FloatingWindow.prototype.div.style.height);
    FloatingWindow.prototype.divShadow.style.width=iWidth + "px";
    FloatingWindow.prototype.divShadow.style.height=iHeight + "px";
    FloatingWindow.prototype.divShadow.setAttribute("initialHeight",FloatingWindow.prototype.divShadow.style.height);
    div1.style.width=iWidth-5 + "px";
    div1.style.height=iHeight-5 + "px";
    
    if(bToolbar)
    {
        toolBarTbl=document.createElement("table");
        toolBarTblb=document.createElement("tbody");
        toolBarTr=document.createElement("tr");
        toolBarTd1=document.createElement("td");
        toolBarTd2=document.createElement("td");
        if(bCloseButton)
        {
            btn1=document.createElement("button");
            btn1.innerHTML="<font color='black' face='Tahoma' size='1'><b>X</b></font>";
            btn1.style.height="20px";
            btn1.style.width="20px";
            if(navigator.appName=="Microsoft Internet Explorer")
            {
                btn1.attachEvent("onclick",click);
            }
            else
            {
                btn1.addEventListener("click",click,false);
            }
            iToolBarTd2Width+=20;
        }
        
        if(bMinimizeButton)
        {
            btn2=document.createElement("button");
            btn2.innerHTML="<font color='black' face='Tahoma' size='1'><b>_</b></font>";
            btn2.style.height="20px";
            btn2.style.width="20px";
            if(navigator.appName=="Microsoft Internet Explorer")
            {
                btn2.attachEvent("onclick",minimize);
            }
            else
            {
                btn2.addEventListener("click",minimize,false);
            }
            iToolBarTd2Width+=20;
        }
        
        
        toolBarTbl.style.width=iWidth-6 + "px";
        toolBarTbl.style.height="25px";
        toolBarTd1.style.width=(iWidth-6)-iToolBarTd2Width + "px";
        toolBarTd2.style.width=iToolBarTd2Width + "px";
        
        if(navigator.appName=="Microsoft Internet Explorer")
        {
            toolBarTd1.align="center";
            toolBarTd1.style.color="white";

            toolBarTd1.attachEvent("onmousedown",mouseDown);
            toolBarTd1.attachEvent("onmouseup",mouseUp);
            document.body.attachEvent("onmousemove",mouseMove);
            document.body.attachEvent("onmouseup",mouseUp);
            /*
            toolBarTd1.setAttribute("onmousedown",mouseDown);
            toolBarTd1.setAttribute("onmouseup",mouseUp);
            document.body.setAttribute("onmousemove",mouseMove);
            document.body.setAttribute("onmouseup",mouseUp);
            */
            //toolBarTd1.setAttribute("onmouseout",mouseUp);
            //btn.setAttribute("onclick",click);
        }
        else
        {
            toolBarTd1.setAttribute("align","center");
            toolBarTd1.addEventListener("mousedown",mouseDown,false);
            toolBarTd1.addEventListener("mouseup",mouseUp,false);
            document.body.addEventListener("mousemove",mouseMove,false);
            document.body.setAttribute("mouseup",mouseUp,false);
            
            //toolBarTd1.addEventListener("mouseout",mouseUp,false);
        }
        
        toolBarTd1.style.background="#8bb3c8";
        toolBarTd1.innerHTML="<font color='white' face='Tahoma' size='2'><b>" + sToolbarTitle + "</b></font>";
        if(bCloseButton)
        {
            toolBarTd2.appendChild(btn1);
        }
        if(bMinimizeButton)
        {
            toolBarTd2.appendChild(btn2);
        }
        toolBarTr.appendChild(toolBarTd1);
        toolBarTr.appendChild(toolBarTd2);
        
        toolBarTblb.appendChild(toolBarTr);
        toolBarTbl.appendChild(toolBarTblb);
        
        FloatingWindow.prototype.tblToolbar=toolBarTbl;
        
        td1.appendChild(toolBarTbl);
        tr1.appendChild(td1);
        tblb.appendChild(tr1);
    }

    if(typeof oContent=="object")
    {
        div1.appendChild(oContent);
        FloatingWindow.prototype.content=div1.childNodes.item(0);
        FloatingWindow.prototype.content.setAttribute("initialHeight",FloatingWindow.prototype.content.style.height);
    }
    else
    {
        div1.innerHTML=oContent;
    }
    
    /*
    if(div1.childNodes.length>0)
    {
        FloatingWindow.prototype.content=div1.childNodes.item(0);
        FloatingWindow.prototype.content.setAttribute("initialHeight",FloatingWindow.prototype.content.style.height);
    }
    */
    
    FloatingWindow.prototype.divContainer=div1;
    FloatingWindow.prototype.divContainer.setAttribute("initialHeight",FloatingWindow.prototype.divContainer.style.height);
    
    td2.appendChild(div1);
    tr2.appendChild(td2);
    tblb.appendChild(tr2);
    tbl.appendChild(tblb);
    FloatingWindow.prototype.div.appendChild(tbl);
    //document.body.appendChild(FloatingWindow.prototype.divShadow);

    document.body.appendChild(FloatingWindow.prototype.div);
    btn1=null;
    btn2=null;
}

FloatingWindow.prototype.close=function()
{
    FloatingWindow.prototype.content.parentNode.removeChild(FloatingWindow.prototype.content);
    document.body.removeChild(FloatingWindow.prototype.div);
    if(document.getElementById(FloatingWindow.prototype.divShadow.id))
    {
        document.body.removeChild(FloatingWindow.prototype.divShadow);
    }
    if(FloatingWindow.prototype.onclose)
    {
        FloatingWindow.prototype.onclose();
    }
}

FloatingWindow.prototype.minimize=function()
{
    if(FloatingWindow.prototype.content.style.display=="")
    {
        FloatingWindow.prototype.content.style.display="none";
        FloatingWindow.prototype.content.style.height="0px";
        FloatingWindow.prototype.divContainer.style.display="none";
        FloatingWindow.prototype.divContainer.style.height="0px";
        FloatingWindow.prototype.div.style.height=FloatingWindow.prototype.tblToolbar.style.height;
        FloatingWindow.prototype.divShadow.style.height=FloatingWindow.prototype.tblToolbar.style.height;
    }
    else
    {
        FloatingWindow.prototype.content.style.display="";
        FloatingWindow.prototype.content.style.height=FloatingWindow.prototype.content.getAttribute("initialHeight");
        FloatingWindow.prototype.divContainer.style.display="";
        FloatingWindow.prototype.divContainer.style.height=FloatingWindow.prototype.divContainer.getAttribute("initialHeight");
        FloatingWindow.prototype.div.style.height=FloatingWindow.prototype.div.getAttribute("initialHeight");
        FloatingWindow.prototype.divShadow.style.height=FloatingWindow.prototype.divShadow.getAttribute("initialHeight");
    }
    if(FloatingWindow.prototype.onminimize)
    {
        FloatingWindow.prototype.onminimize();
    }
}

FloatingWindow.prototype.setWidth=function(iWidth)
{
    FloatingWindow.prototype.div.style.width=iWidth + "px";
    if(FloatingWindow.prototype.divShadow)
    {
        FloatingWindow.prototype.divShadow.style.width=iWidth + "px";
    }
    FloatingWindow.prototype.divContainer.style.width=iWidth-5 + "px";
    if(FloatingWindow.prototype.tblToolbar)
    {
        FloatingWindow.prototype.tblToolbar.style.width=iWidth-6 + "px";
        FloatingWindow.prototype.tblToolbar.childNodes[0].childNodes[0].style.width=iWidth-10 + "px";
        FloatingWindow.prototype.tblToolbar.childNodes[0].childNodes[1].style.width="10px";
    }
}

FloatingWindow.prototype.startDrag=function(iAbsoluteX,iAbsoluteY)
{
    FloatingWindow.prototype.startMouseX=iAbsoluteX;
    FloatingWindow.prototype.startMouseY=iAbsoluteY;
    
    FloatingWindow.prototype.dragging=true;
}

FloatingWindow.prototype.drag=function(iNewAbsoluteX,iNewAbsoluteY)
{
    if(FloatingWindow.prototype.dragging)
    {
        var xDiff=0;
        var yDiff=0;
        
        xDiff=iNewAbsoluteX-FloatingWindow.prototype.startMouseX;
        yDiff=iNewAbsoluteY-FloatingWindow.prototype.startMouseY;
        
        FloatingWindow.prototype.div.style.left=parseInt(FloatingWindow.prototype.div.style.left.substr(0,FloatingWindow.prototype.div.style.left.indexOf("px",0)))+xDiff + "px";
        FloatingWindow.prototype.div.style.top=parseInt(FloatingWindow.prototype.div.style.top.substr(0,FloatingWindow.prototype.div.style.top.indexOf("px",0)))+yDiff + "px";
        
        if(FloatingWindow.prototype.divShadow)
        {
            FloatingWindow.prototype.divShadow.style.left=parseInt(FloatingWindow.prototype.divShadow.style.left.substr(0,FloatingWindow.prototype.divShadow.style.left.indexOf("px",0)))+xDiff + "px";
            FloatingWindow.prototype.divShadow.style.top=parseInt(FloatingWindow.prototype.divShadow.style.top.substr(0,FloatingWindow.prototype.divShadow.style.top.indexOf("px",0)))+yDiff + "px";
        }

        FloatingWindow.prototype.startMouseX=iNewAbsoluteX;
        FloatingWindow.prototype.startMouseY=iNewAbsoluteY;
    }
}

FloatingWindow.prototype.drop=function()
{
    FloatingWindow.prototype.dragging=false;
}

//Funciones Mouse: http://javascript.about.com/library/blmousepos.htm
FloatingWindow.prototype.mouseX=function(evt)
{
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
       return evt.clientX + (document.documentElement.scrollLeft ?
       document.documentElement.scrollLeft :
       document.body.scrollLeft);
    else return null;
}
FloatingWindow.prototype.mouseY=function(evt)
{
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
       return evt.clientY + (document.documentElement.scrollTop ?
       document.documentElement.scrollTop :
       document.body.scrollTop);
    else return null;
}

FloatingWindow.prototype.getTotalTopOffset=function(element)
{
    var fReturnValue=0;
    if(element.offsetParent)
    {
        fReturnValue=getTotalTopOffset(element.offsetParent);
    }
    
    fReturnValue+=element.offsetTop;
    return(fReturnValue);
}