
function iFrameFudge(handle)
{
    if (isIE() && IWindow.WindowMap['$IW$'+handle])
    {
        /* bloody crap IE - we can't size it until the on load is done */
        var iw = IWindow.WindowMap['$IW$'+handle];
        try {
        iw.frame.style.height = (parseInt(iw.div.style.height)-IWindow.BarHeight) +'px';
        iw.frame.style.width = parseInt(iw.div.style.width) +'px';
        }
        catch (e) { }
    }
}

function IWindow(title)
{
    if (title == undefined)
        return this; // This is so the new function can be used to subclass the Dwindow Object
    this.handle = IWindow.NextWindowHandle++;
    IWindow.OpenCount++;
    this.minrestore = 0;
    IWindow.WindowMap['$IW$'+this.handle] = this;
    
    this.div = document.createElement('div');
    this.div.id = "$IW$"+this.handle;
    this.div.style.zIndex = IWindow.TopZindex++;
    
    if (!arguments[1])
        initWidth = 600;
    else
        initWidth = parseInt(arguments[1])
    if (!arguments[2])
        initHeight = 400;
    else
        initHeight = parseInt(arguments[2])
        
    if (initHeight+IWindow.posTop+parseInt(document.documentElement.scrollTop) > getInsideWindowHeight())
        IWindow.posTop = 0;
    if (initWidth+IWindow.posLeft > getInsideWindowWidth())
        IWindow.posLeft = 0;
    IWindow.posLeft += 20;
    IWindow.posTop += 20;    
    
    this.div.style.position = 'absolute';
    this.div.style.cursor = 'pointer';
    
    this.normLeft = this.div.style.left = IWindow.posLeft+'px';
    this.normTop = this.div.style.top = (IWindow.posTop + parseInt(document.documentElement.scrollTop))+'px';
    this.normWidth = this.div.style.width = initWidth+'px';
    this.normHeight = this.div.style.height = initHeight+'px';

    this.div.className = 'IWindow iwResizeBoth iwDragTarget';
    
    document.body.insertBefore(this.div,document.body.firstChild);
    this.div.innerHTML = '<div class="IWindowBar">'+
                            '<div class="IWindowBarLeft"><img class="hotButton" src="/gfx/popprint.gif" onclick="return IWindow.Print('+this.handle+')"></div>'+
                            '<div class="IWindowBarMiddle iwDrag">'+title+'</div>'+
                            '<div class="popupWindowBarRightWide">'+
                                '<img id="$IWI$'+this.handle+'" class="hotButton popupWindowIconize" src="/gfx/popIconize.gif" onclick="return IWindow.Iconize('+this.handle+')"/>'+
                                '<img id="$IWP$'+this.handle+'" class="hotButton popupWindowMaximize" src="/gfx/popMaximize.gif" onclick="return IWindow.Maximize('+this.handle+')"/>'+
                                '<img class="hotButton popupWindowClose" src="/gfx/popClose.gif" onclick="return IWindow.Close('+this.handle+')"/>'+
                            '</div>'+
                        '</div>'+
                         '<iframe frameborder="0" onload="iFrameFudge(\''+this.handle+'\')" name="$IWFRAME$'+this.handle+'" id="$IWFRAME$'+this.handle+'" src="" width="100%" height="100%"/>';
    this.minButton = getElement('$IWP$'+this.handle);
    this.iconButton = getElement('$IWI$'+this.handle);
    this.frame = getElement('$IWFRAME$'+this.handle);
    try {
	var hot = getElementsByClassName(this.div,"hotButton");
    for (loop = 0; hot && loop < hot.length; loop++)
          hot[loop].addBehavior("/css/behavior/hotButton.htc");
    } catch (e) { }
//    this._positionInView();
}

function DWindow(title)
{
    this.handle = IWindow.NextWindowHandle++;
    IWindow.OpenCount++;
    this.minrestore = 0;
    
    useDivId = '$IW$'+this.handle;
    
    if (arguments[3])
        IWindow.UserMap[arguments[3]] = this.handle;
    
    IWindow.WindowMap[useDivId] = this;
    
    this.div = document.createElement('div');
    this.div.id = useDivId;
    this.div.style.zIndex = IWindow.TopZindex++;
    
    if (!arguments[1])
        initWidth = 600;
    else
        initWidth = parseInt(arguments[1])
    if (!arguments[2])
        initHeight = 400;
    else
        initHeight = parseInt(arguments[2])
        
    if (initHeight+IWindow.posTop+parseInt(document.documentElement.scrollTop) > getInsideWindowHeight())
        IWindow.posTop = 0;
    if (initWidth+IWindow.posLeft > getInsideWindowWidth())
        IWindow.posLeft = 0;
    IWindow.posLeft += 20;
    IWindow.posTop += 20;    
    
    this.normLeft = this.div.style.left = IWindow.posLeft+'px';
//    alert(Scroll Top='+parseInt(document.documentElement.scrollTop)+", top offset="+IWindow.posTop);
    this.normTop = this.div.style.top = (IWindow.posTop + parseInt(document.documentElement.scrollTop))+'px';
    this.normWidth = this.div.style.width = initWidth+'px';
    this.normHeight = this.div.style.height = initHeight+'px';
    
    this.div.style.position = 'absolute';
    this.div.className = 'IWindow iwResizeBoth iwDragTarget';
    
    document.body.insertBefore(this.div,document.body.firstChild);
    this.div.innerHTML = '<div class="IWindowBar">'+
                            '<div class="IWindowBarLeft"><img class="hotButton" src="/gfx/popprint.gif" onclick="return IWindow.Print('+this.handle+')"></div>'+
                            '<div class="IWindowBarMiddle iwDrag">'+title+'</div>'+
                            '<div class="popupWindowBarRightWide">'+
                                '<img id="$IWI$'+this.handle+'" class="hotButton popupWindowIconize" src="/gfx/popIconize.gif" onclick="return IWindow.Iconize('+this.handle+')"/>'+
                                '<img id="$IWP$'+this.handle+'" class="hotButton popupWindowMaximize" src="/gfx/popMaximize.gif" onclick="return IWindow.Maximize('+this.handle+')"/>'+
                                '<img class="hotButton popupWindowClose" src="/gfx/popClose.gif" onclick="return IWindow.Close('+this.handle+')"/>'+
                            '</div>'+
                        '</div>'+
                         '<div class="DWindowContent" id="$IWFRAME$'+this.handle+'" style="width:'+(initWidth-10)+'px;height:'+initHeight+'px"></div>';
    this.minButton = getElement('$IWP$'+this.handle);
    this.iconButton = getElement('$IWI$'+this.handle);
    this.frame = getElement('$IWFRAME$'+this.handle);
    try {
	var hot = getElementsByClassName(this.div,"hotButton");
    for (loop = 0; hot && loop < hot.length; loop++)
          hot[loop].addBehavior("/css/behavior/hotButton.htc");
    } catch (e) { }
    this._positionInView();
}

IWindow.WindowMap = new Array();
IWindow.UserMap = new Array();
IWindow.OpenCount = 0;
IWindow.NextWindowHandle = 1;
IWindow.BarHeight = 28;
IWindow.TopZindex = 100;
IWindow.posLeft = 170;
IWindow.posTop = 130;
IWindow.MaxAdjust = 24;

DWindow.prototype = new IWindow();
DWindow.prototype.constructor = DWindow;

IWindow.Close = function(id)
{
    if (IWindow.WindowMap['$IW$'+id])
        IWindow.WindowMap['$IW$'+id]._close();

}

IWindow.UserClose = function(id)
{
    if (IWindow.UserMap[id] != null)
    {
        IWindow.WindowMap['$IW$'+IWindow.UserMap[id]]._close();
        IWindow.UserMap.Remove(id);
    }

}

IWindow.Print = function(id)
{
    if (IWindow.WindowMap['$IW$'+id])
        IWindow.WindowMap['$IW$'+id]._print();
}

IWindow.Maximize = function(id)
{
    if (IWindow.WindowMap['$IW$'+id])
        IWindow.WindowMap['$IW$'+id]._maximize();
}
IWindow.Iconize = function(id)
{
    if (IWindow.WindowMap['$IW$'+id])
        IWindow.WindowMap['$IW$'+id]._iconize();
}

IWindow.prototype.setContent = function (url)
{
    this.frame.src = url;
    return this;
}

IWindow.prototype.setBig = function ()
{
    /* we class big a 3/4 of the screen size and centered */
    /* just a helper/short cut method added 'cause I'm lazy */
    var bigFraction = 0.90;
    var width = parseFloat(getInsideWindowWidth()-2);
    var height = parseFloat(getInsideWindowHeight()-IWindow.MaxAdjust);
    this.normWidth = this.div.style.width = (width*bigFraction)+'px';
    this.normHeight = this.div.style.height = (height*bigFraction)+'px';
    this.normTop = this.div.style.top = (parseInt(document.documentElement.scrollTop)+parseFloat((height-(height*bigFraction))/2))+'px';
    this.normLeft = this.div.style.left = parseFloat((width-(width*bigFraction))/2)+'px';
    return this;
}

IWindow.prototype._positionInView = function()
{

    var bWidth = parseInt((getInsideWindowWidth()-2)+document.documentElement.scrollLeft);
    var bHeight = parseInt((getInsideWindowHeight()-IWindow.MaxAdjust)+document.documentElement.scrollTop);
    var height = parseInt(this.div.style.height);
    var width = parseInt(this.div.style.width);
    var bottom = parseInt(this.div.style.top)+height+16;
    var right = parseInt(this.div.style.left)+width+20;
    while (right > bWidth && parseInt(this.div.style.left) > 0)
    {
        right--;
        this.div.style.left = (parseInt(this.div.style.left)-1)+'px';
    }
    while (bottom > bHeight && parseInt(this.div.style.top) > 0)
    {
        bottom--;
        this.div.style.top = (parseInt(this.div.style.top)-1)+'px';
    }

}

IWindow.prototype._close = function()
{
    this.div.innerHTML = '';
    this.div.style.display = 'none';
    document.body.removeChild(this.div);
    if (this.minrestore==2)
        TrayManager.Remove(this);
    windowId = '$IW$'+this.id;
    delete IWindow.WindowMap[windowId];
    if (IWindow.OpenCount == 0)
    {
        IWindow.TopZindex = 100;
        IWindow.posLeft = 170;
        IWindow.posTop = 130;
    }
    IWindowMap.Remove(windowId);
}

IWindow.prototype._print = function()
{
    try {
        window.frames[this.frame.id].focus();
        window.frames[this.frame.id].print();
    } catch (e) {
        alert ('Browser does not support specific frame printing');
    }
}

IWindow.prototype._iconize = function()
{
    if (this.minrestore==2) {
        this.div.style.position = 'absolute';
        this.div.style.top = parseInt(this.normTop)+'px';
        this.div.style.left = parseInt(this.normLeft)+'px';
        this.div.style.width = parseInt(this.normWidth)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(this.normHeight)+'px';
        this.minButton.setAttribute("src","/gfx/popMaximize.gif");
        this.iconButton.setAttribute("src","/gfx/popIconize.gif");
        this.frame.style.height = (parseInt(this.div.style.height)-IWindow.BarHeight) + 'px'; /* because of IE */
        this.frame.style.display = 'block';
        TrayManager.Remove(this);
        this.minrestore=0; // Restore to normal
    }
    else
    {
        this.minrestore=2; // Iconize
        this.normTop = parseInt(this.div.style.top);
        this.normLeft = parseInt(this.div.style.left);
        this.normWidth = parseInt(this.div.style.width);
        this.normHeight = parseInt(this.div.style.height);
        this.div.style.width = '180px';
        this.div.style.height = IWindow.BarHeight;
        this.iconButton.setAttribute("src","/gfx/popRestore.gif");
        this.minButton.setAttribute("src","/gfx/popMaximize.gif");
        this.frame.style.display = 'none';
        TrayManager.Add(this);
    }
    return this;
}

IWindow.prototype._maximize = function()
{
    if (this.minrestore==2) {
        TrayManager.Remove(this);
        this.minButton.setAttribute("src","/gfx/popRestore.gif");
        this.iconButton.setAttribute("src","/gfx/popIconize.gif");
        this.div.style.position = 'absolute';
        this.div.style.width = parseInt(getInsideWindowWidth()-2)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(getInsideWindowHeight()-IWindow.MaxAdjust)+"px";
        this.div.style.top = parseInt(document.documentElement.scrollTop)+"px";
        this.div.style.left = '0px';
        this.frame.style.height = (parseInt(this.div.style.height)-IWindow.BarHeight) + 'px'; /* because of IE */
        this.frame.style.display = 'block';
        this.minrestore=1 // maximize from iconized
    }
    else if (this.minrestore==0){
        this.normTop = parseInt(this.div.style.top);
        this.normLeft = parseInt(this.div.style.left);
        this.normWidth = parseInt(this.div.style.width);    
        this.normHeight = parseInt(this.div.style.height);
        this.minButton.setAttribute("src","/gfx/popRestore.gif");
        this.div.style.width = parseInt(getInsideWindowWidth()-2)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(getInsideWindowHeight()-IWindow.MaxAdjust)+'px';
        this.div.style.top = parseInt(document.documentElement.scrollTop)+'px';
        this.div.style.left = '0px';
        this.frame.style.height = (parseInt(this.div.style.height)-IWindow.BarHeight) + 'px'; /* because of IE */
        this.minrestore=1 //maximize window
    }
    else{
        this.div.style.top = parseInt(this.normTop)+'px';
        this.div.style.left = parseInt(this.normLeft)+'px';
        this.div.style.width = parseInt(this.normWidth)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(this.normHeight)+'px';
        this.minButton.setAttribute("src","/gfx/popMaximize.gif");
        this.frame.style.height = (parseInt(this.div.style.height)-IWindow.BarHeight) + 'px'; /* because of IE */
        this.minrestore=0 //restore window
    }
    return this;
}

DWindow.prototype.setContent = function (url)
{
    this.frame.innerHTML = "Loading...";
    asyncNavigate(url,'$IWFRAME$'+this.handle);
    return this;
}

DWindow.prototype._print = function()
{
    var divs = document.getElementsByTagName('DIV');
    
    var pDiv = document.createElement('DIV');
    pDiv.id = "$IWPRN$"+this.handle;
    pDiv.innerHTML = this.frame.innerHTML;
    pDiv.onafterprint = new Function("printCompleted(\""+pDiv.id+"\")");
   
    for (d = 0; d < divs.length; d++)
    {
            addClassName(divs[d],'noprn');
    }
    
    addClassName(pDiv,'prn');
    document.body.insertBefore(pDiv,document.body.firstChild);
    
    window.print();
    // Let the onafterprint tidy up
}

_TrayManager.OnScroll = function()
{
    if (isIE())
    {
        TrayManager.arangeTray();
    }
    eval(TrayManager.onScroll);
}

function _TrayManager()
{
    this.Items = new Array();
    this.x = 0;
    this.y = 0;
    this.onScroll = window.onscroll;    
    window.onscroll = _TrayManager.OnScroll;
}

TrayManager = new _TrayManager();

_TrayManager.prototype.Add = function(window)
{
    if (this.x+parseInt(window.div.style.width) > parseInt(getInsideWindowWidth()))
    {
        this.y -= IWindow.BarHeight;
        this.x = 0;
    }
    window.div.style.left = this.x+'px';
    this.x += parseInt(window.div.style.width);
    if (isIE())
        this.x += 12;
    window.div.style.top = parseInt((getInsideWindowHeight()-IWindow.BarHeight-2)+this.y+parseInt(document.documentElement.scrollTop))+"px";
    if (!isIE())
        window.div.style.position='fixed';
    this.Items.push(window);
}

_TrayManager.prototype.Remove = function(window)
{
    for (loop=0;loop<this.Items.length;loop++)
    {
        if (this.Items[loop] == window)
        {
            for(;loop<this.Items.length;loop++)
                this.Items[loop] = this.Items[loop+1];
            this.Items.length = this.Items.length-1;
            this.arangeTray();
        }
    }
}

_TrayManager.prototype.arangeTray = function()
{
    this.x = 0;
    this.y = 0;
    for (loop=0;loop<this.Items.length;loop++)
    {
        if (this.x+parseInt(this.Items[loop].div.style.width) > parseInt(getInsideWindowWidth()))
        {
            this.y -= IWindow.BarHeight;
            this.x = 0;
        }
        this.Items[loop].div.style.left = this.x+'px';
        this.Items[loop].div.style.top = parseInt((getInsideWindowHeight()-IWindow.BarHeight-2)+this.y+parseInt(document.documentElement.scrollTop))+"px";
        this.x += parseInt(this.Items[loop].div.style.width);
    }
}