// Floating DHTML Window function
// by m4dm4n
// http://www.desigfx.com - greg@desigfx.com

var d_Windows=new Array();
var task_bar;
var current=0;
var mx=0;
var my=0;
var zIndex = 100;


var in_moving=false;
addHandler(document,'mousedown',dhtml_mousedown);
function dhtml_mousedown(e) {
        try {
                el = typeof(e.target)!='undefined' ? e.target : window.event.srcElement;

                if (el.id.indexOf('drag_')>-1 || el.tagName=="A" || el.tagName=="INPUT" || el.tagName=="TEXTAREA") return;

                el = seekWinDiv(el);
                if (el!==false) {
                        if (seekWindow(el.id)===false) addWindow(el.id,getWinHeader(el.id));

                        in_moving = el.id;
                        mClick(el.id);
                        return;
                }

        } catch (e) {}
}
addHandler(document,'mouseup',dhtml_mouseup);
function dhtml_mouseup(e) {
        try {
                if ( in_moving !== false ) {
                        mRelease( in_moving );
                        return;
                }
        } catch (e) {}
}
//if(document.layers)
//        document.captureEvents(Event.MOUSEMOVE);
addHandler(document,'mousemove',dhtml_mousemove);
function dhtml_mousemove(e) {
        if(document.layers) {
                mx=e.pageX;
                my=e.pageY;
        } else if(document.all) {
                mx=event.clientX;
                my=event.clientY;
        } else {
                mx=e.clientX;
                my=e.clientY;
        }
        for(var i=0;i<d_Windows.length;i++) {
                if(d_Windows[i].click) {
                        d_Windows[i].move(mx,my);
                }
        }
}

function addWindow(id,title) {
        d_Windows[current]=new DivWindow(id);
        d_Windows[current].title=title;
        current++;
}

function mClick(id) {
        var win=seekWindow(id);
        if(win) {
                win.drag(mx,my);
        }
}

function mRelease(id) {
        var win;
        if((win=seekWindow(id))) {
                win.release();
        }
}

function seekWindow(id) {
        for(var i=0;i<d_Windows.length;i++) {
                if(d_Windows[i].id==id) {
                        return d_Windows[i];
                }
        }
        return false;
}

function seekWinDiv( el ) {
         while ( true ) {
                if ( el.id.indexOf('dw_')>-1) {

                        return el;

                } else if (el.offsetParent) {

                        el = el.offsetParent;

                } else return false;
        }
}
/**
 * Return header text of the window
 */
function getWinHeader(id) {
         var el = document.getElementById( "dwname_"+id.substring(3) );
         return ( el ) ? el.innerHTML : '';
}

function closeWindow( id ) {
        if ( id.substring(0, 2) !== 'dw_' ) id = "dw_"+id;
        var w = seekWindow( id );
        if ( w===false ) {
                addWindow( id, getWinHeader(id) );
                w = seekWindow( id );
        }
        w.close();
        populateHidden();
}

function showWindow(id) {
        var w = seekWindow( id );
        if ( w === false ) {
                addWindow( id, getWinHeader(id) );
                w = seekWindow( id );
        }

        w.open();
        populateHidden();

//        sortable_resize();
}

// TODO: Fix the formatting of this ugly code
function populateHidden() {
        var task_bar = document.getElementById("taskbar")

        if ( !task_bar ) return false;

        var res = '<p><table cellSpacing="0" cellPadding="0">';
        for(var i=0;i<d_Windows.length;i++) {
                if( d_Windows[i].hidden ) {
                        res += '<td><img src="/images/buttons/button_left.png" width="20" height="36"></td>'
                        + "<td background=\"/images/buttons/button_middle.png\" nowrap> <a href=\"javascript:showWindow('"+d_Windows[i].id+"')\" style=\"text-decoration:none\"><b>"+d_Windows[i].title+'</b></a> </td>'
                        + '<td><img src="/images/buttons/button_right.png" width="20" height="36"></td>'
                        + '<td>&nbsp;</td>';
                }
        }
        res += "</table></p>";
        task_bar.innerHTML = res;

        return true;
}

var DivWindow=function(id) {

        this.click=false;
        this.myWin;
        this.off_x=0;
        this.off_y=0;
        this.myWin=document.getElementById(id);
        this.x=parseInt(this.myWin.style.left);
        this.y=parseInt(this.myWin.style.top);
        this.id=id;
        this.children=new Array();
        this.curr_child=0;
        this.hidden=false;
        this.title;

        this.close=function() {
                try {

                  this.hidden=true;
                  Effect.Fade( this.myWin );

                } catch (e) {

                        this.hidden=true;
                        this.myWin.style.visibility="hidden";
                        this.myWin.style.display="none";
                
                        for(var i=0;i<this.children.length;i++) {
                                this.children[i].element.style.visibility="hidden";
                                this.children[i].element.style.display="none";
                        }
                }
        }

        this.open=function() {
                try {

                  this.hidden=false;
                  Effect.Appear( this.myWin );

                } catch (e) {

                        this.hidden=false;
                        this.myWin.style.visibility="visible";
                        this.myWin.style.display="block";

                        for(var i=0;i<this.children.length;i++) {
                                this.children[i].element.style.visibility="visible";
                                this.children[i].element.style.display="block";
                        }
                }
        }

        this.drag=function(x,y) {
                if(this.hidden) return;
                this.click=true;
                this.off_x=x-parseInt(this.myWin.style.left);
                this.off_y=y-parseInt(this.myWin.style.top);
                // always on top
                this.myWin.style.zIndex = zIndex++;
        }

        this.release=function() {
                if(this.hidden) return;
                this.click=false;
                this.off_x=0;
                this.off_y=0;
        }

        this.move=function(x,y) {
                if(this.hidden) return;
                this.x=x-this.off_x;
                this.y=y-this.off_y;
                this.myWin.style.top=this.y;
                this.myWin.style.left=this.x;

                // deny selection
                try {document.selection.clear();} catch(e) {this.myWin.focus();}

                for(var i=0;i<this.children.length;i++)
                        this.children[i].move(this.x,this.y);
        }

        this.addChild=function(id) {
                var c=(this.children[this.curr_child++]=new DivChild(id));
                var px=parseInt(c.element.style.left);
                var py=parseInt(c.element.style.top);
                c.off_x=px-this.x;
                c.off_y=py-this.y;
        }
}

var DivChild=function(id) {

        this.off_x=0;
        this.off_y=0;
        this.element=document.getElementById(id);

        this.move=function(x,y) {
                this.element.style.top=y+this.off_y;
                this.element.style.left=x+this.off_x;
        }

}
