
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) { }
    }
}

//IWindow.Style = 0;
//ILib.LoadResourceFile('jscss','IWindow', 'css');
IWindow.Style = 1;
ILib.LoadResourceFile('jscss','IWindowPanel', 'css');

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);
    var c = '<iframe frameborder="0" onload="iFrameFudge(\'' + this.handle + '\')" name="$IWFRAME$' + this.handle + '" id="$IWFRAME$' + this.handle + '" src="" width="100%" height="100%"/>';
    var l = '';
    if (IWindow.Style == 0) {
        l = '<div class="IWindowBar">' +
                            '<div class="IWindowBarLeft"><img class="hotButton" src="'+ILib.Resource('jscss/img/popprint.png')+'" 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="'+ILib.Resource('jscss/img/popIconize.png')+'" onclick="return IWindow.Iconize(' + this.handle + ')"/>' +
                                '<img id="$IWP$' + this.handle + '" class="hotButton popupWindowMaximize" src="'+ILib.Resource('jscss/img/popMaximize.png')+'" onclick="return IWindow.Maximize(' + this.handle + ')"/>' +
                                '<img class="hotButton popupWindowClose" src="'+ILib.Resource('jscss/img/popClose.png')+'" onclick="return IWindow.Close(' + this.handle + ')"/>' +
                            '</div>' +
                        '</div>' +
                        c;
    }
    else {
        l = '<div class="IWindowWrapper">';
        l += '<div class="_pml">';
        l += '<div class="_pmr">';

        l += '<div>';
        l += '<div class="_pc">';

        l += c;

        l += '</div>'; // _pc
        l += '</div>'; // unclassed

        l += '<div class="_ptc iwDrag">';
        l += '<div class="_ptl"></div>';
        l += '<div class="_ptr _ptr-1"></div>';
        l += '<div class="_ptm">';
        l += '<p class="left">' + title + '</p>';
        l += '</div>'; // _ptm
        l += '</div>'; // _ptc
        l += '</div>'; // _pmr

        l == '</div>'; // _pml

        l += '<div class="_pbc"><div class="_pbl"></div><div class="_pbm"></div><div class="_pbr"></div></div>';

        l += '</div>'; // IWindowWrapper
    }                                                 
    this.div.innerHTML = l;
                             
    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("/theme/common/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(ILib.GetDocumentScrollTop()) > ILib.GetInsideWindowHeight())
        IWindow.posTop = 0;
    if (initWidth+IWindow.posLeft > ILib.GetInsideWindowWidth())
        IWindow.posLeft = 0;
    IWindow.posLeft += 20;
    IWindow.posTop += 20;    
    
    this.normLeft = this.div.style.left = IWindow.posLeft+'px';
    this.normTop = this.div.style.top = (IWindow.posTop + parseInt(ILib.GetDocumentScrollTop()))+'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);
    var c = '<div class="DWindowContent" id="$IWFRAME$' + this.handle + '" style="width:' + (initWidth - 32) + 'px;height:' + initHeight + 'px"></div>';
    var l = '';
    if (IWindow.Style == 0) {
        l = '<div class="IWindowBar">' +
                            '<div class="IWindowBarLeft"><img class="hotButton" src="'+ILib.Resource('jscss/img/popprint.png')+'" 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="'+ILib.Resource('jscss/img/popIconize.png')+'" onclick="return IWindow.Iconize(' + this.handle + ')"/>' +
                                '<img id="$IWP$' + this.handle + '" class="hotButton popupWindowMaximize" src="'+ILib.Resource('jscss/img/popMaximize.png')+'" onclick="return IWindow.Maximize(' + this.handle + ')"/>' +
                                '<img class="hotButton popupWindowClose" src="'+ILib.Resource('jscss/img/popClose.png')+'" onclick="return IWindow.Close(' + this.handle + ')"/>' +
                            '</div>' +
                        '</div>' +
                        c;
    } else {

        l = '<div class="IWindowWrapper">';
        
        l += '<div class="_pml">';
        l += '<div class="_pmr">';

        l += '<div>';
        l += '<div class="_pc">';

        l += c;

        l += '</div>'; // _pc
        l += '</div>'; // unclassed

        l += '<div class="_ptc iwDrag">';

        l += '<div class="_ptl">';
        l += '<img class="hotButton" src="'+ILib.Resource('/jscss/img/popprint.png')+'" onclick="return IWindow.Print(' + this.handle + ')"/>';
        l += '</div>'; // _ptl

        l += '<div class="_ptr _ptr-1">';
        l += '<img id="$IWI$' + this.handle + '" class="hotButton popupWindowIconize" src="'+ILib.Resource('jscss/img/popIconize.png')+'" onclick="return IWindow.Iconize(' + this.handle + ')"/>';
        l += '<img id="$IWP$' + this.handle + '" class="hotButton popupWindowMaximize" src="'+ILib.Resource('jscss/img/popMaximize.png')+'" onclick="return IWindow.Maximize(' + this.handle + ')"/>';
        l += '<img class="hotButton popupWindowClose" src="'+ILib.Resource('jscss/img/popClose.png')+'" onclick="return IWindow.Close(' + this.handle + ')"/>';
        l += '</div>'; // _ptr

        l += '<div class="_ptm">';
        l += '<p class="left">' + title + '</p>';
        l += '</div>'; // _ptm

        l += '</div>'; // _ptc
        l += '</div>'; // _pmr
        l += '</div>'; // _pml

        l += '<div class="_pbc"><div class="_pbl"></div><div class="_pbm"></div><div class="_pbr"></div></div>';
        l += '</div>'; // IWindowWrapper
    }            
    this.div.innerHTML = l;                         
    
    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("/theme/common/css/behavior/hotButton.htc");
    } catch (e) { }
    this._positionInView();
}

IWindow.WindowMap = new Array();
IWindow.UserMap = new Array();
IWindow.OpenCount = 0;
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(ILib.GetInsideWindowWidth()-2);
    var height = parseFloat(ILib.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 = (ILib.GetDocumentScrollTop()+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((ILib.GetInsideWindowWidth() - 2) + ILib.GetDocumentScrollLeft());
    var bHeight = parseInt((ILib.GetInsideWindowHeight() - IWindow.MaxAdjust) + ILib.GetDocumentScrollTop());
    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',ILib.Resource('jscss/img/popMaximize.png'));
        this.iconButton.setAttribute("src",ILib.Resource('jscss/img/popIconize.png'));
        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.minButton.setAttribute('src', ILib.Resource('jscss/img/popMaximize.png'));
        this.iconButton.setAttribute("src", ILib.Resource('jscss/img/popRestore.png'));
        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', ILib.Resource('jscss/img/popRestore.png'));
        this.iconButton.setAttribute("src", ILib.Resource('jscss/img/popIconize.png'));
        this.div.style.position = 'absolute';
        this.div.style.width = parseInt(ILib.GetInsideWindowWidth()-2)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(ILib.GetInsideWindowHeight()-IWindow.MaxAdjust)+"px";
        this.div.style.top = parseInt(ILib.GetDocumentScrollTop())+"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",ILib.Resource('jscss/img/popRestore.png'));
        this.div.style.width = parseInt(ILib.GetInsideWindowWidth()-2)+'px';
        this.frame.style.width = this.div.style.width;
        this.div.style.height = parseInt(ILib.GetInsideWindowHeight()-IWindow.MaxAdjust)+'px';
        this.div.style.top = parseInt(ILib.GetDocumentScrollTop())+'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",ILib.Resource('jscss/img/popMaximize.png'));
        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(ILib.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((ILib.GetInsideWindowHeight()-IWindow.BarHeight-2)+this.y+parseInt(ILib.GetDocumentScrollTop()))+"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(ILib.GetInsideWindowWidth()))
        {
            this.y -= IWindow.BarHeight;
            this.x = 0;
        }
        this.Items[loop].div.style.left = this.x+'px';
        this.Items[loop].div.style.top = parseInt((ILIb.GetInsideWindowHeight()-IWindow.BarHeight-2)+this.y+parseInt(document.documentElement.scrollTop))+"px";
        this.x += parseInt(this.Items[loop].div.style.width);
    }
}
