/*
    "FloatingWindow 1.2.2"
    by Diego Costa (foxy_dcc@hotmail.com)
	All Rights Reserved.

              version: 1.2   - 09-09-2010 : Methods modified to allow the instancing of multiple FloatingWindows.
              version: 1.2.1 - 11-11-2010 : Public properties added. Optional (nullable) width and height. "setHeight" method added.
              version: 1.2.2 - 16-11-2010 : "setOnclose" public property added.
              version: 1.2.3 - 29-03-2011 : "replaceCloseButton" and "replaceMinimizeButton" public methods added.
*/

function FloatingWindow(sId,iLeft,iTop,iWidth,iHeight,sBgColor,oContent,bToolbar,sToolbarTitle,bCloseButton,sPosition,bMinimizeButton)
{
    var div;
    var divShadow;
    var divContainer;
    var tblToolbar;
    var dragging;
    var bMinimized=false;
    
    var startMouseX;
    var startMouseY;
    
    var content;
    var onclose;
    var 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;
    
    /* when dragging */
    var onDrag=null;
    var displacementLeft=0;
    var displacementTop=0;
    this.onDrag=null;
    /* ------------- */
    
    this.addOnDrag=function(pointer)
    {
        onDrag=pointer;
    }  
  
    var close=function()
    {
        if(content)
        {
            content.parentNode.removeChild(content);
        }
        
        document.body.removeChild(div);
        if(document.getElementById(divShadow.id))
        {
            document.body.removeChild(divShadow);
        }
        if(onclose)
        {
            onclose();
        }
    }

    var minimize=function()
    {
        if(!bMinimized)
        {
            if(content)
            {
                content.style.display="none";
                content.style.height="0px";
            }
            divContainer.style.display="none";
            divContainer.style.height="0px";
            div.style.height=tblToolbar.style.height;
            divShadow.style.height=tblToolbar.style.height;
            bMinimized=true;
        }
        else
        {
            if(content)
            {
                content.style.display="";
                content.style.height=content.getAttribute("initialHeight");
            }
            divContainer.style.display="";
            divContainer.style.height=divContainer.getAttribute("initialHeight");
            div.style.height=div.getAttribute("initialHeight");
            divShadow.style.height=divShadow.getAttribute("initialHeight");
            bMinimized=false;
        }
        
        if(onminimize)
        {
            onminimize();
        }
    }

    this.setWidth=function(iWidth)
    {
        div.style.width=iWidth + "px";
        if(divShadow)
        {
            divShadow.style.width = iWidth + "px";
        }
        divContainer.style.width=iWidth-5 + "px";
        if(tblToolbar)
        {
            tblToolbar.style.width=iWidth-6 + "px";
            tblToolbar.childNodes[0].childNodes[0].style.width=iWidth-10 + "px";
            tblToolbar.childNodes[0].childNodes[1].style.width="10px";
        }
    }

    this.setHeight = function(iHeight)
    {
        div.style.height = iHeight + "px";
        if (divShadow)
        {
            divShadow.style.height = iHeight + "px";
        }
        divContainer.style.height = iHeight - 5 + "px";
    }

    var startDrag=function(iAbsoluteX,iAbsoluteY)
    {
        startMouseX=iAbsoluteX;
        startMouseY=iAbsoluteY;
        
        dragging=true;
    }

    var drag=function(iNewAbsoluteX,iNewAbsoluteY)
    {
        if(dragging)
        {
            var xDiff=0;
            var yDiff=0;
            
            xDiff=iNewAbsoluteX-startMouseX;
            yDiff=iNewAbsoluteY-startMouseY;
            
            div.style.left=parseInt(div.style.left.substr(0,div.style.left.indexOf("px",0)))+xDiff + "px";
            div.style.top=parseInt(div.style.top.substr(0,div.style.top.indexOf("px",0)))+yDiff + "px";
            
            if(divShadow)
            {
                divShadow.style.left=parseInt(divShadow.style.left.substr(0,divShadow.style.left.indexOf("px",0)))+xDiff + "px";
                divShadow.style.top=parseInt(divShadow.style.top.substr(0,divShadow.style.top.indexOf("px",0)))+yDiff + "px";
            }

            startMouseX=iNewAbsoluteX;
            startMouseY=iNewAbsoluteY;
            
            displacementLeft=xDiff;
            displacementTop=yDiff;
           
            if(onDrag)
            {
                onDrag(displacementLeft,displacementTop);
            }
        }
    }

    var drop=function()
    {
        dragging=false;
    }
  
  
    var click=function(){close();}
    var mouseDown=function(event){startDrag(FloatingWindow.prototype.mouseX(event),FloatingWindow.prototype.mouseY(event));}
    var mouseMove=function(event){drag(FloatingWindow.prototype.mouseX(event),FloatingWindow.prototype.mouseY(event));}
    var mouseUp=function(){drop();}
    
    if(sPosition==null)
    {
        sPosition="absolute";
    }
    
    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"));
    }
    
    div=document.createElement("div");
    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");
    
    div.style.position=sPosition;
    div.setAttribute("id",sId);
    div.style.background=sBgColor;
    
    divShadow.style.position=sPosition;
    divShadow.setAttribute("id",sId + "Shadow");
    divShadow.style.background="#dde2e6";
    div.style.left=iLeft + "px";
    div.style.top=iTop + "px";
    div.style.zIndex=2;
    divShadow.style.left=iLeft-10 + "px";
    divShadow.style.top=iTop+10 + "px";
    divShadow.style.display='';
	divShadow.style.opacity=40/100;
	divShadow.style.MozOpacity=40/100;
	divShadow.style.KhtmlOpacity=40/100;
	divShadow.style.filter="alpha(opacity=" + 40+ ")";
	divShadow.style.zIndex=-1;
    div1.style.overflow="auto";

    if(iWidth)
    {
        div.style.width = iWidth + "px";
        divShadow.style.width = iWidth + "px";
        div1.style.width = iWidth - 5 + "px";
    }
    if (iHeight)
    {
        div.style.height = iHeight + "px";
        divShadow.style.height = iHeight + "px";
        div1.style.height = iHeight - 5 + "px";
    }

    div.setAttribute("initialHeight",div.style.height);
    divShadow.setAttribute("initialHeight",divShadow.style.height);
    
    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;
        }
        
        if(iWidth)
        {
            toolBarTbl.style.width = iWidth - 6 + "px";
            toolBarTd1.style.width = (iWidth - 6) - iToolBarTd2Width + "px";
            toolBarTd2.style.width = iToolBarTd2Width + "px";
        }
        if (iHeight)
        {
            toolBarTbl.style.height = "25px";
        }
        
        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 (bMinimizeButton)
        {
            toolBarTd2.appendChild(btn2);
        }
        if(bCloseButton)
        {
            toolBarTd2.appendChild(btn1);
        }
        
        toolBarTr.appendChild(toolBarTd1);
        toolBarTr.appendChild(toolBarTd2);
        
        toolBarTblb.appendChild(toolBarTr);
        toolBarTbl.appendChild(toolBarTblb);
        
        tblToolbar=toolBarTbl;
        
        td1.appendChild(toolBarTbl);
        tr1.appendChild(td1);
        tblb.appendChild(tr1);
    }

    if(typeof oContent=="object")
    {
        div1.appendChild(oContent);
        content=div1.childNodes.item(0);
        content.setAttribute("initialHeight", 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);
    }
    */
    
    divContainer=div1;
    divContainer.setAttribute("initialHeight",divContainer.style.height);

    td2.appendChild(div1);
    tr2.appendChild(td2);
    tblb.appendChild(tr2);
    tbl.appendChild(tblb);
    div.appendChild(tbl);
    //document.body.appendChild(FloatingWindow.prototype.divShadow);

    document.body.appendChild(div);

    this.replaceCloseButton = function(newCloseButton)
    {
        if (bCloseButton)
        {
            if (navigator.appName == "Microsoft Internet Explorer")
            {
                newCloseButton.attachEvent("onclick", click);
            }
            else
            {
                newCloseButton.addEventListener("click", click, false);
            }
            toolBarTd2.removeChild(btn1);
            if (toolBarTd2.childNodes > 0)
            {
                toolBarTd2.insertBefore(newCloseButton, btn2);
            }
            else
            {
                toolBarTd2.appendChild(newCloseButton);
            }
        }
    }

    this.replaceMinimizeButton = function(newMinimizeButton)
    {
        if (bMinimizeButton)
        {
            if (navigator.appName == "Microsoft Internet Explorer")
            {
                newMinimizeButton.attachEvent("onclick", minimize);
            }
            else
            {
                newMinimizeButton.addEventListener("click", minimize, false);
            }
            toolBarTd2.removeChild(btn2);
            toolBarTd2.appendChild(newMinimizeButton);
        }
    }
    
    
    //btn1=null;
    //btn2=null;

    this.toolbarTable=toolBarTbl;
    this.mainDiv=div;
    this.content=content;
    this.displacementLeft=displacementLeft;
    this.displacementTop = displacementTop;
    this.startDrag = function(iAbsoluteX, iAbsoluteY) { startDrag(iAbsoluteX, iAbsoluteY); };
    this.drag = function(iAbsoluteX, iAbsoluteY) { drag(iAbsoluteX, iAbsoluteY); };
    this.drop = function() { drop(); };
    this.dragging = dragging;
    this.divContainer = divContainer;
    this.setOnclose = function(pFunction) { onclose = pFunction; }
    //this.close = function() { close(); }
    this.close = close;
}



//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);
}
