﻿// Require JQuery 1.3.2 or higher
var oldScrOfY = 0;
var oldScrOfX = 0;
var stepY = 0, speed = 150;
var maxWidth = 600, minWidth = 300;
var maxHeight = 500, minHeight = 100;
var isShowing = false, firstTime = true;
var oldContainerID = '';
var scrollTimerID = null;

function JBox(JBoxConfig)
{
    if (!document.getElementById('DIVPlaceHolder'))
    {
        var DIVPlaceHolder = document.createElement("div");
        DIVPlaceHolder.id = 'DIVPlaceHolder';
        DIVPlaceHolder.style.display = 'none';
    }
    else document.getElementById('DIVPlaceHolder').style.display = 'none';
    
    isShowing = true;
    var reload = JBoxConfig.reload ? JBoxConfig.reload : false;

    if (reload && document.getElementById('DIVC0verFrameID'))
    {
        if (JBoxConfig.containerID != oldContainerID)
        {
            var container = document.getElementById(oldContainerID)
            oldContainerID = JBoxConfig.containerID;
            document.getElementById('DIVPlaceHolder').appendChild(container);
            document.getElementsByTagName("body")[0].removeChild(document.getElementById('DIVC0verFrameID'));
        }
    }
        
    if (!document.getElementById('DIVC0verFrameID'))
    {
        var DIVFrameContent = document.createElement("div");
        DIVFrameContent.className = "FrameContent";

        var DIVFrameTitle = document.createElement("div");
        DIVFrameTitle.className = "FrameTitle";
        var SPANTitle = document.createElement("span");
        SPANTitle.appendChild(document.createTextNode(JBoxConfig.title));

        var DIVFrameClose = document.createElement("div");
        DIVFrameClose.className = "FrameClose";
        DIVFrameClose.onclick = function() { if (JBoxConfig.onclose) JBoxConfig.onclose(); JBoxClose(); }

        DIVFrameTitle.appendChild(SPANTitle);
        DIVFrameTitle.appendChild(DIVFrameClose);


        var DIVFrameCenter = document.createElement("div");
        DIVFrameCenter.className = "FrameCenter";
        DIVFrameCenter.appendChild(DIVFrameTitle);
        DIVFrameCenter.appendChild(DIVFrameContent);

        var DIVFrameLeft = document.createElement("div");
        DIVFrameLeft.className = "FrameLeft";

        var DIVFrameRight = document.createElement("div");
        DIVFrameRight.className = "FrameRight";

        var DIVFrame = document.createElement("div");
        DIVFrame.className = "DIVFrame";
        DIVFrame.id = "DIVFrameID";

        DIVFrame.appendChild(DIVFrameLeft);
        DIVFrame.appendChild(DIVFrameCenter);
        DIVFrame.appendChild(DIVFrameRight);

        var nWidth = document.offsetWidth;
        var nHeight = document.offsetWidth;

        var DIVCoverFrame = document.createElement("div");
        DIVCoverFrame.className = "DIVCoverFrame";
        DIVCoverFrame.id = "DIVC0verFrameID";

        // Conver document
        DIVCoverFrame.style.top = '0px';
        DIVCoverFrame.style.left = '0px';
        DIVCoverFrame.style.width = GetDocumentWidth() + 'px';
        DIVCoverFrame.style.height = GetDocumentHeight() + 'px';

        DIVCoverFrame.appendChild(DIVFrame);

        // Load content
        var container = document.getElementById(JBoxConfig.containerID);
        oldContainerID = JBoxConfig.containerID;
        container.style.margin = '0px auto';
        DIVFrameContent.appendChild(container);

        // Resize DIVFrame;
//        DIVFrameContent.style.width = (container.offsetWidth > maxWidth ? maxWidth : (container.offsetWidth < minWidth ? minWidth : container.offsetWidth)) + 'px';
//        DIVFrameContent.style.height = (container.offsetHeight > maxHeight ? maxHeight : (container.offsetHeight < minHeight ? minHeight : container.offsetHeight)) + 30 + 'px';

//        DIVFrameCenter.style.width = (container.offsetWidth > maxWidth ? maxWidth : (container.offsetWidth < minWidth ? minWidth : container.offsetWidth)) + 'px';
//        DIVFrameCenter.style.height = ((container.offsetHeight > maxHeight ? maxHeight : (container.offsetHeight < minHeight ? minHeight : container.offsetHeight)) + 74) + 'px';

//        DIVFrame.style.width = ((container.offsetWidth > maxWidth ? maxWidth : (container.offsetWidth < minWidth ? minWidth : container.offsetWidth)) + 20) + 'px';
//        DIVFrame.style.height = DIVFrameLeft.style.height = DIVFrameRight.style.height = ((container.offsetHeight > maxHeight ? maxHeight : (container.offsetHeight < minHeight ? minHeight : container.offsetHeight)) + DIVFrameTitle.offsetHeight + 80) + 'px';
        
        document.getElementsByTagName("body")[0].appendChild(DIVCoverFrame);
        
        container.style.display = "block";
    }
    else
        document.getElementById('DIVC0verFrameID').style.display = 'block';
    AdujstFrame();
    //window.onscroll = AdujstFrame;
    // Use AddEvent method instead
//    addEvent(window, "scroll", function()
//    {
//        if (scrollTimerID != null) {
//            clearTimeout(scrollTimerID);
//        }
//        scrollTimerID = setTimeout(AdujstFrame, 300);
//    });
    addEvent(window, "scroll", AdujstFrame);
}

function GetDocumentHeight()
{
    //return document.body.clientHeight > 0 ? document.body.clientHeight : document.documentElement.clientHeight;
    return window.pageYOffset ? (document.documentElement.clientHeight + window.pageYOffset) : (document.body.scrollTop ? (document.documentElement.clientHeight + ocument.body.scrollTop) : (document.documentElement.scrollTop + document.documentElement.clientHeight))
}

function GetDocumentWidth()
{
    //return document.body.clientWidth;
    return window.pageXOffset ? (document.documentElement.clientWidth + window.pageXOffset) : (document.body.scrollLeft ? (document.documentElement.clientWidth + ocument.body.scrollLeft) : (document.documentElement.scrollLeft + document.documentElement.clientWidth))
}

function GetWindowWidth()
{
    return window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
}
function GetWindowHeight()
{
    return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
}

function GetWindow()
{
    return (document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function JBoxClose()
{
    if (document.getElementById('DIVC0verFrameID'))
    {
        document.getElementById('DIVC0verFrameID').style.display = 'none';
        if (scrollTimerID != null)
            clearTimeout(scrollTimerID);
            
        isShowing = false;
        firstTime = true;
    }
    //document.getElementsByTagName("body")[0].removeChild(document.getElementById('DIVC0verFrameID'));
}
function AdujstFrame(firstTime)
{
    var scrOfX = 0, scrOfY = 0;

    var setFramePostion = function()
    {
        var DIVFrame = document.getElementById('DIVFrameID');
        DIVFrame.style.top = (GetWindowHeight() / 2) - (DIVFrame.offsetHeight / 2) + scrOfY + 'px';
        DIVFrame.style.left = (GetWindowWidth() / 2) - (DIVFrame.offsetWidth / 2) + scrOfX + 'px';
    }
    if (typeof (window.pageYOffset) == 'number')
    {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
    {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
    {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    if (isShowing)
    {
        if (firstTime) { setFramePostion(); firstTime = false; }
        else
        {
//            if (scrollTimerID != null) {
//                clearTimeout(scrollTimerID);

//                scrollTimerID = setTimeout(setFramePostion, speed);
//            }
//            scrollTimerID = setTimeout(setFramePostion, speed);
              setTimeout(setFramePostion, speed);
        }  

        var DIVC0verFrame = document.getElementById('DIVC0verFrameID');
        DIVC0verFrame.style.width = GetDocumentWidth() + 'px';
        DIVC0verFrame.style.height = GetDocumentHeight() + 'px';
    }
}