
/*--------------------------------------------------------------------------------------------*/
/*  xWindow  - copyright Stuart Colville 2007 (C)   */
/*  Version 1.02                                    */
/*  Author: Stuart Colville                         */    
/*--------------------------------------------------------------------------------------------*/

/*-------------------------------------xWindow Object-----------------------------------------*/
function xWindow(id, name, ledgeid, html, img)
{
    this.name = name;
    this.open = false;
    this.status = "MAX";
    this.innerHTML = html;
    this.id = id;
    this.contentid = this.id + "content";
    this.windowTitleID = "WindowTitle" + this.id; 
    if(img !="")
        this.imgTag = "<img class='xWindowImageFrame' src='"+img+"'/>"
    else
        this.imgTag = "";
        
    this.baseHTML =     "<div class='dropshadow2'><div id='"+ this.id + "' class='xWindowFrame'>" + 
                        "<div class='xWindowHead'>" + this.imgTag +
                            "<div id='WindowTitle" + id +"' class='xWindowHeadText'>" + name + "</div>" +
                                "<div class='xWindowControl'>" + 
                                    "<a  class='xWindowMinButton' id='status"+ this.id + "' href=javascript:ToggleWindow('" + this.id + "')><img src='images/xWindow/xWindowMinimise.gif' alt='Minimise'/></a> <a  class='xWindowCloseButton' id='close"+ this.id + "' href=javascript:objManager.CloseWindow('" +this.id+ "')><img src='images/xWindow/xWindowClose.gif' alt='close'/></a>" + 
                                "</div>" + 
                        "</div>"+                                
                            "<div class='xWindowContent' id='"+this.contentid+"'>" + this.innerHTML + "</div>" +
                        "</div></div>";
    this.width = 100;
    this.minHeight = 60;
    this.maxHeight = 500;
    this.currentHeight = null;
    this.Value = Value;
    this.xWindowShelf = null;
    this.ledgeid = ledgeid
    this.minimiseOnOpen = false;
    this.hideCloseButton = false;
    /*functions*/
    this.MaximiseWindow = MaximiseWindow;
    this.MinimiseWindow = MinimiseWindow;
    this.Close = Close;
    this.ToggleWindow = ToggleWindow;
    this.SetContent = SetContent;
    this.SetTitle = SetTitle;
    this.Open = Open;
    
}



function Open(xWindowShelf)
{
    //set the shelf to block display
    document.getElementById(xWindowShelf.id).innerHTML = this.baseHTML;
    setStyle(xWindowShelf.id, 'display', 'block');
    
    //for hiding the close button 
    if(this.hideCloseButton)
        setStyle('close'+ this.id, 'display', 'none');
    
    //we might want to keep the window minimised when it opens
    if(!this.minimiseOnOpen)
    {
        this.status = "MAX";
        MaximiseWindow(this.id);
    }
    else
    {
        this.status = "MIN";    
        ToggleMaxMinButton(this.id);
        
           
        //set the content display type to none as we want to hide it!
        setStyle(this.contentid, 'display', 'none');
        setStyle(this.id, 'height',20);
       //MinimiseWindow(this.id);
    }
    
    this.open = true;
    this.xWindowShelf = xWindowShelf;
}

function Close()
{
    // set the style to display none
    setStyle(this.xWindowShelf.id, 'display', 'none');
    //free up the shelf - it's available so push it back on the shelf array    
    xWindowLedgeList[this.ledgeid].xWindowChefList.push(this.xWindowShelf);
    //change the status of the window
    this.status = "";
    this.open = false;
}

function ClearContent()
{
        document.getElementById(this.contentid).innerHTML = "";
}

function SetContent(html)
{
        document.getElementById(this.contentid).innerHTML = html;
}

function SetTitle(html)
{
        document.getElementById(this.windowTitleID).innerHTML = html;
}

function Value()
{
    return this;
}

function ToggleWindow(xWindowID)
{
    if(xWindowList[xWindowID].status == "MAX")
        MinimiseWindow(xWindowID);
    else
    {
        MaximiseWindow(xWindowID);
    }
}

function MaximiseWindow(xWindowID)
{
        xWindowList[xWindowID].status = "MAX";
        var currentHeight = getStyle(xWindowID, 'height');
        
        var newHeight = parseInt(currentHeight) + 20 + 'px';
        setStyle(xWindowID, 'height', newHeight);
        if (parseInt(currentHeight) <= xWindowList[xWindowID].maxHeight)
            setTimeout("MaximiseWindow('" + xWindowID + "')",10);
        else
        {
            setStyle(xWindowList[xWindowID].contentid, 'display', 'block');
            
            ToggleMaxMinButton(xWindowID);        
         }     
        
}

function MinimiseWindow(xWindowID)
{        
        
        //set the content display type to none as we want to hide it!
        setStyle(xWindowList[xWindowID].contentid, 'display', 'none');
        xWindowList[xWindowID].status = "MIN";
        var currentHeight = getStyle(xWindowID, 'height');
        var newHeight = parseInt(currentHeight)  - 20 + 'px';
        setStyle(xWindowID, 'height', newHeight);
        if (parseInt(currentHeight) >= xWindowList[xWindowID].minHeight)
            setTimeout("MinimiseWindow('" + xWindowID + "')",10);
        else            
            ToggleMaxMinButton(xWindowID);    
}

function ToggleMaxMinButton(xWindowID)
{
        var link;
        if (xWindowList[xWindowID].status == "MIN")
            link = "<img src='images/xWindow/xWindowMaximise.gif' alt='Maximise'/>";
        else
            link = "<img src='images/xWindow/xWindowMinimise.gif' alt='Minimise'/>";
     
        document.getElementById('status' + xWindowID).innerHTML = link;
}

/*-------------------------------------xWindowLedge Object-----------------------------------------*/
function xWindowLedge(id)
{
    this.id = id;
    this.xWindowChefList = new Array();
    this.AddWindowShelf = AddWindowShelf;
}

function AddWindowShelf(xWindowShelf)
{
    this.xWindowChefList.push(xWindowShelf);    
}

/*-------------------------------------xWindowShelf Object-----------------------------------------*/
function xWindowShelf(id, windowLedgeID)
{
    this.id = 'WindowShelf' + id;
    this.isEmpty = true;
    document.getElementById(windowLedgeID).innerHTML = document.getElementById(windowLedgeID).innerHTML + "<div id='" + this.id+ "'/>";    
}

    xWindowList = new Array();
    xWindowLedgeList = new Array();
    var windowShelfCounter=0;

/*-------------------------------------xWindowManager Object-----------------------------------------*/
function xWindowManager()
{
    //functions
    this.AddWindow = AddWindow;
    this.ShowWindow = ShowWindow;
    this.CloseWindow = CloseWindow;
    this.AddWindowLedge = AddWindowLedge;
    this.SetWindowContent = SetWindowContent;
    this.AddWindowShelf = AddWindowShelf;
    this.AddWindowShelves = AddWindowShelves;
}

//adds a window ledge for the shelves to sit on
function AddWindowLedge(id)
{
    var windowLedge = new xWindowLedge(id);
    xWindowLedgeList[windowLedge.id] = windowLedge;
}

function AddWindowShelves(noOfShelves, parentWindowLedgeID)
{
        //create a window shelf object for the window to sit on - shelf must sit on a ledge
        //hence we pass the ledge id so we can start drawing the empty shelves on the correct ledge
        var windowShelf;
        for(var i  = 0 ; i < noOfShelves; i++)
        {
            windowShelf = new xWindowShelf(windowShelfCounter,parentWindowLedgeID);
            //add the object to a public list 
            xWindowLedgeList[parentWindowLedgeID].xWindowChefList.push(windowShelf);
            windowShelfCounter++;
        }
}

function AddWindow(objWindow)
{
        if (xWindowList[objWindow.id] == null)
            xWindowList[objWindow.id] = objWindow;
        else
            alert("error - window has already been added");
}

function ShowWindow(xWindowID)
{
        var emptyShelf;
        var xWindow;
        
        //get the window object that requested to be open
        xWindow = xWindowList[xWindowID];
        if (xWindow.open == false)
        {
            emptyShelf = xWindowLedgeList[xWindow.ledgeid].xWindowChefList.pop();                
            if(emptyShelf != null)
            {
                //call the open function on the window
                xWindow.Open(emptyShelf);
                xWindowList[xWindowID] = xWindow.Value();     
            }
            else
                alert('Could not find a shelf to place the window on');
        }
        else
            alert('The window selected is already open.');
}


function CloseWindow(xWindowID)
{  
            //find the shelf that the window is on
            var xWindow = xWindowList[xWindowID];
            xWindow.Close();
            xWindowList[xWindowID] = xWindow.Value();
}


function SetWindowContent(xWindowID, html)
{
            var xWindow = xWindowList[xWindowID];
            xWindow.SetContent(html);
            xWindowList[xWindowID] = xWindow.Value();
}


/*------------------------------------------------------------------------------------------*/
function WriteHTML(ID, Text)
{
        document.getElementById(ID).innerHTML = Text;
}

function getStyle(el,styleProp)
{
    var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

// Set a style property
function setStyle(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

