// Popwin - Copyright Doug Ashbaugh 2005-2010
// If you would like to use a custom loading image or close button reference them in the next two lines.
var loaderGraphic = 'loading.gif';
var closeButGraphic = 'close.gif';
var nn6 = document.getElementById && !document.all;
var popwinDraggerObj = false; // Set drag true / false
var xCapture, yCapture; // Capture clicked X and Y coordinates.
var popwinValuesObj; // Variable to store all movement values.
var popwinShownCntr; // Counts how many popwins are open so we do not slam IE during resize and drags if none are shown.		

function getPageScroll()
{
    // Returns array with x,y page scroll values.
    var yScroll;
    if (self.pageYOffset)
    {
        yScroll = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    { // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
    }
    else if (document.body)
    { // all other Explorers
        yScroll = document.body.scrollTop;
    }

    arrayPageScroll = new Array('', yScroll)
    return arrayPageScroll;
}

function getPageSize()
{
    // Returns array with page width, height and window width, height
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY)
    {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else
    { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight)
    { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the popwin
    if (yScroll < windowHeight)
    {
        pageHeight = windowHeight;
    }
    else
    {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the popwin
    if (xScroll < windowWidth)
    {
        pageWidth = windowWidth;
    }
    else
    {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

function hidePopwin(divName)
{
    try
    {
        objOverlay = document.getElementById('popwinOverlay');
        objPopwin = document.getElementById('popwin');

        var objContainer = document.getElementById(divName);
        var objPopwinContainer2 = document.getElementById('popwinContainer2');
        var objPopwinContainer3 = document.getElementById('popwinContainer3');
        var objStorage = document.getElementById('popwinStorage');

        // hide popwin and overlay
        objContainer.style.display = 'none';
        objPopwin.style.display = 'none';
        objOverlay.style.display = 'none';

        // Storage of previous popwin	
        objStorage.appendChild(objContainer);

        // Reset Container 3 inside Container 2
        objPopwinContainer3.style.left = objPopwinContainer2.style.left;
        objPopwinContainer3.style.top = objPopwinContainer2.style.top;

        // make select boxes visible
        selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++)
        {
            selects[i].style.visibility = "visible";
        }

        // disable keypress listener
        document.onkeypress = '';

        popwinShownCntr--;

    }
    catch (e)
    {
        alert("ERROR in script popwin.js in function hidePopWin(" + divName + "): " + e);
    }
}

function initPopwin()
{
    var objBody = document.getElementsByTagName("body").item(0);

    // create storage container
    var objStorage = document.createElement("div");
    objStorage.setAttribute('id', 'popwinStorage');
    objStorage.style.display = 'none';
    objBody.insertBefore(objStorage, objBody.firstChild);

    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    var objOverlay = document.createElement("div");
    objOverlay.setAttribute('id', 'popwinOverlay');
    //objOverlay.onclick = function () {hidePopwin(); return false;}
    objOverlay.style.display = 'none';
    objOverlay.style.position = 'absolute';
    objOverlay.style.top = '0';
    objOverlay.style.left = '0';
    objOverlay.style.width = '100%';
    objOverlay.style.height = lgGetWinHeight(); // DLA - resize height now at start of popwin
    objBody.insertBefore(objOverlay, objBody.firstChild);

    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();

    // create popwin div, same note about styles as above
    var objPopwin = document.createElement("div");
    objPopwin.setAttribute('id', 'popwin');
    objPopwin.style.display = 'none';
    objBody.insertBefore(objPopwin, objOverlay.nextSibling);

    // create container 1
    var objContainer1 = document.createElement("div");
    objContainer1.setAttribute('id', 'popwinContainer1');
    objPopwin.appendChild(objContainer1);

    // create container 2
    var objContainer2 = document.createElement("div");
    objContainer2.setAttribute('id', 'popwinContainer2');
    objContainer1.appendChild(objContainer2);

    // create container 3 (Drag Container)
    var objContainer3 = document.createElement("div");
    objContainer3.setAttribute('id', 'popwinContainer3');
    objContainer3.className = 'popwinContainer3';
    objContainer2.appendChild(objContainer3);

    // Resize handlers.
    // Need to modify this to support Firefox and Netscape. Must be last in this init else hoses 
    // other browsers for now...
    if (window.addEventListener)
    {
        // firefox
        window.addEventListener("resize", function ()
        {
            self.popwinResize();
        }, false)
        window.addEventListener("scroll", function ()
        {
            self.popwinResize();
        }, false)
    }
    else
    {
        // IE
        window.attachEvent("onresize", function ()
        {
            self.popwinResize();
        });
        window.attachEvent("onscroll", function ()
        {
            self.popwinResize();
        });
    }

    //alert('initPopwin() ran.');
}

function popwinResize()
{
    if (popwinShownCntr == 0)
    {
        return;
    }
    var objOverlay = document.getElementById('popwinOverlay');
    objOverlay.style.height = lgGetWinHeight();
}

function lgGetWinHeight()
{
    var arrayPageSize = getPageSize();
    var altHeight = arrayPageSize[1];

    var theHeight;
    if (window.innerHeight)
    {
        theHeight = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        theHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        theHeight = document.body.clientHeight;
    }
    if (altHeight > theHeight)
    {
        return (altHeight);
    }
    else
    {
        return (theHeight);
    }
}

function showPopwin(divName)
{
    //alert("showPopwin entry..." + divName);
    try
    {
        var objContainer = document.getElementById(divName);

        // prep objects
        var objPopwin = document.getElementById('popwin');
        var objOverlay = document.getElementById('popwinOverlay');
        var objPopwinContainer3 = document.getElementById('popwinContainer3');

        //var objCaption = document.getElementById('popwinCaption');
        //var objImage = document.getElementById('popwinImage');
        //var objLoaderGraphic = document.getElementById('loaderGraphic');
        //var objPopwinDetails = document.getElementById('popwinDetails');
        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();

        // center loaderGraphic if it exists
/*
		if (objLoaderGraphic) {
			objLoaderGraphic.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoaderGraphic.height) / 2) + 'px');
			objLoaderGraphic.style.left = (((arrayPageSize[0] - 20 - objLoaderGraphic.width) / 2) + 'px');
			objLoaderGraphic.style.display = 'block';
		}
		*/

        // set height of Overlay to take up whole page and show
        var theHeight;
        if (window.innerHeight)
        {
            theHeight = window.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
            theHeight = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
            theHeight = document.body.clientHeight;
        }
        objOverlay.style.height = lgGetWinHeight();

        // objOverlay.style.height = (arrayPageSize[1] + 'px');
        objOverlay.style.display = 'block';

        // Hide select boxes as they will 'peek' through the image in IE
        selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++)
        {
            selects[i].style.visibility = "hidden";
        }

        // place div inside popwin
        objPopwinContainer3.appendChild(objContainer);

        // display div
        objContainer.style.display = 'block';
        objPopwin.style.display = 'block';

        popwinShownCntr++;
    }
    catch (e)
    {
        alert('ERROR in script popwin.js in function showPopwin(' + divName + ') Msg: ' + e);
    }
}

function showCurrentSettings()
{
    try
    {
        if (document.getElementById('currentNav'))
        {
            document.getElementById('moodlyLogo').style.display = 'none';
            document.getElementById('currentNav').style.display = 'block';
        }
    }
    catch (e)
    {
        1;
    }
}
//EOF
