///////////////////////////////////////////////////////////////
//// Making the popDiv
///////////////////////////////////////////////////////////////
/*
 * CONFIG VARS
 */
//Number of pixels wide the popup should be
var popDivWidth = 395;
//Number of pixels the popDiv should be from the top of the window
var keepFromTop = 245;
/*
 * END CONFIG VARS
 * !GO NO FURTHER!
 */
var currentLivePopDiv = false;
var posMonTimer;
function makePopDiv() {
        if (!currentLivePopDiv) {
                //Build the outer containing DIV in memory
                var popDivOuter = document.createElement('div');
                popDivOuter.setAttribute('class','popDivOuter');
                popDivOuter.setAttribute('className','popDivOuter');
                popDivOuter.setAttribute('id','popDivOuter');
                //Build the inner containing DIV in memory
                var popDivFirstInner = document.createElement('div');
                popDivFirstInner.setAttribute('class','popDivFirstInner');
                popDivFirstInner.setAttribute('className','popDivFirstInner');
                popDivFirstInner.setAttribute('id','popDivFirstInner');
                //Build the title bar DIV in memory
                var popDivTitleBar = document.createElement('div');
                popDivTitleBar.setAttribute('class','popDivTitleBar');
                popDivTitleBar.setAttribute('className','popDivTitleBar');
                popDivTitleBar.setAttribute('id','popDivTitleBar');
                //Build the div for holding the X img in memory
                var popDivCloserDiv = document.createElement('div');
                popDivCloserDiv.setAttribute('class','popDivCloserDiv');
                popDivCloserDiv.setAttribute('className','popDivCloserDiv');
                popDivCloserDiv.setAttribute('id','popDivCloserDiv');
                //Put the 'X' image in memory
                var popDivCloserDivImg = document.createElement('img');
                popDivCloserDivImg.setAttribute('id','popDivCloserDivImg');
                popDivCloserDivImg.setAttribute('src','../assets/close.gif');
                popDivCloserDivImg.setAttribute('alt','X');
                //Build the content DIV in memory
                var popDivContent = document.createElement('div');
                popDivContent.setAttribute('class','popDivContent');
                popDivContent.setAttribute('className','popDivContent');
                popDivContent.setAttribute('id','popDivContent');
                //Assemble new elements in memory
                popDivCloserDiv.appendChild(popDivCloserDivImg);
                popDivTitleBar.appendChild(popDivCloserDiv);
                popDivFirstInner.appendChild(popDivTitleBar);
                popDivFirstInner.appendChild(popDivContent);
                popDivOuter.appendChild(popDivFirstInner);
                //Insert the assembled elements into the document
                document.getElementById('popDivInsertionPoint').appendChild(popDivOuter);
                var popDivContainer = document.getElementById('popDivOuter');
                //Turn on the display for the insertion point
                document.getElementById('popDivInsertionPoint').setAttribute("style","display: visible;");
                //Set the event handler on the 'X' image for closing the popDiv
                document.getElementById('popDivCloserDivImg').onclick = function(){closePopDiv();}
                //Set width and centering position
                resizePopDiv(popDivWidth);
                screenCenter('popDivOuter');
                //Mark status variable to show popDiv is currently up
                currentLivePopDiv = true;
        }
        else {
                killCurrentPopDiv();
                makePopDiv();
        }
}
function killCurrentPopDiv() {
        if (currentLivePopDiv) {
                document.getElementById('popDivInsertionPoint').setAttribute("style","display: none;");
                document.getElementById('popDivInsertionPoint').removeChild(document.getElementById('popDivOuter'));
        }
        clearTimeout(posMonTimer);
        currentLivePopDiv = false;
}
function resizePopDiv(newSize) {
        document.getElementById('popDivOuter').style.width = ""+newSize+"px";
}
function screenCenter(idOfElememntToCenter) {
        var element = document.getElementById(idOfElememntToCenter);
        var elementWidth = parseInt(element.style.width);
        var centeringPos = screen.availWidth/2 - elementWidth/2;
        element.style.left = ""+centeringPos+"px";
}
function monitorPopDivYPosition() {
        if (currentLivePopDiv) {
                var scrolledY;
                //Various IE properties
                if(document.body.scrollTop || document.documentElement.scrollTop) {
                        scrolledY = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
                }
                //NS/MZ possibility
                else if(window.pageYOffset){
                        scrolledY = window.pageYOffset;
                }
                //We're screwed as far as the scrolling goes... ;)
                else {
                        scrolledY = 0;
                }
                newYPosNum = (scrolledY+keepFromTop);
                document.getElementById('popDivOuter').style.top = ""+newYPosNum+"px";
                posMonTimer = setTimeout("monitorPopDivYPosition()",50);
        }
}
//////
function displayPopDiv() {
        makePopDiv();
        setTimeout("monitorPopDivYPosition(0)",100);
}
function closePopDiv() {
        killCurrentPopDiv();
}
///////////////////////////////////////////////////////////////
//// Content Retrieval
///////////////////////////////////////////////////////////////
function XMLReqObj() {
        this.initXMLReqObj();
        this.targetElementID = '';
        this.DEAD = false;
}
XMLReqObj.prototype.initXMLReqObj = function () {
        this.xmlClient = null;
        try {
                if (window.XMLHttpRequest) {
                        this.xmlClient = new XMLHttpRequest();
                }
                else if(window.ActiveXObject){
                        this.xmlClient = new ActiveXObject("MsXml2.XmlHttp");
                }
                else {
                        throw "Method Not Available";
                }
        }
        catch(e) {
                this.initFailure();
        }
        if (!this.DEAD && this.xmlClient === null) {
                this.initFailure();
        }
}
XMLReqObj.prototype.initFailure = function() {
        this.DEAD = true;
        this.xmlClient = null;
        alert("***\n\nYour browser does not properly support the XMLHttpRequest Object.\n\nYou will not be able to use the full functionality of this page.\n\n***");
}
XMLReqObj.prototype.processReqStatChange = function() {
        if (this.xmlClient.readyState == 4) {
                if (this.xmlClient.status == 200) {
                        this.setContent(this.xmlClient.responseText);
                        this.xmlClient = null;
                }
                else {
                        this.setContent("There was a problem retrieving the XML data:\n" + this.xmlClient.statusText);
                }
        }
}
XMLReqObj.prototype.getFile = function(URL) {
        if (this.xmlClient && !this.DEAD) {
                this.xmlClient.onreadystatechange = function () {
                  xmlobj.processReqStatChange();
                }
                //Open the request
                this.xmlClient.open("GET", URL, true);
                //Forces content load directly from originating server--not from any cache enroute
                this.xmlClient.setRequestHeader('Cache-Control','no-cache');
                this.xmlClient.setRequestHeader('Pragma','no-cache');
                //Send the request
                this.xmlClient.send(null);
        }
        else if(this.DEAD) {
                this.initFailure();
        }
        else {
                this.initXMLReqObj();
                this.getFile(URL);
        }
}
XMLReqObj.prototype.setContent = function(content) {
        document.getElementById(this.targetElementID).innerHTML = content;
}

var xmlobj = new XMLReqObj();

function sleep(sleepTime) {
        var fellAsleep = new Date().getTime();
        while (new Date().getTime() < fellAsleep+sleepTime) {
                ; //Do Nothing
        }
}

//The function to initiate and call the other content retrieval
function changeDisplay(cType,URL,targetElementID) {
        var popDIVContentBlock =  document.getElementById(targetElementID);
        popDIVContentBlock.innerHTML = '<p style="text-align: center;font-weight: bold"><br />Loading Content...<br /></p>';
        switch (cType) {
                case 'text':
                xmlobj.targetElementID = targetElementID;
                xmlobj.getFile(URL);
                break;
                
                case 'img':
                var preLoaded = new Image();
                preLoaded.onload = function () {
                        var img = document.createElement("IMG");
                        img.setAttribute('id','popDivImg');
                        img.setAttribute('style','padding: 0px; margin: 0px');
                        img.onload = function () {
                                widthAttr = img.width;
                                heightAttr = img.height;
                                if (widthAttr >= heightAttr) {
                                        widthAdj(img,widthAttr,heightAttr);
                                        heightAdj(img,widthAttr,heightAttr);
                                }
                                else {
                                        heightAdj(img,widthAttr,heightAttr);
                                        widthAdj(img,widthAttr,heightAttr);
                                }
                                widthAttr = parseInt(img.getAttribute('width'));
                                resizePopDiv(widthAttr+8);
                                screenCenter('popDivOuter');
                        }
                        img.setAttribute('src',URL);
                        popDIVContentBlock.replaceChild(img,popDIVContentBlock.firstChild);
                        img = document.getElementById('popDivImg');
                }
                preLoaded.src = URL;
                break;
        }
}
function widthAdj(img,widthAttr,heightAttr) {
        if (widthAttr > (screen.availWidth-100)) {
                origW =  widthAttr;
                widthAttr = screen.availWidth-100
                percOfOrig = widthAttr/origW;
                heightAttr = Math.round(percOfOrig * heightAttr);
                img.setAttribute("width",widthAttr);
                img.setAttribute("height",heightAttr);
        }
}
function heightAdj(img,widthAttr,heightAttr) {
        if (heightAttr > (screen.availHeight-300)) {
                origH =  heightAttr;
                heightAttr = screen.availHeight-300
                percOfOrig = heightAttr/origH;
                widthAttr = Math.round(percOfOrig * widthAttr);
                img.setAttribute("width",widthAttr);
                img.setAttribute("height",heightAttr);
        }
}
///////////////////////////////////////////////////////////////
//// The Event Handler to call it all
///////////////////////////////////////////////////////////////
function doPopDiv(cType,URL) {
        displayPopDiv();
        changeDisplay(cType,URL,'popDivContent');
}
