var shadowBoxWidth;
var shadowBoxHeight;
var shadowBoxTop;
var shadowBoxLeft;
var eventX;
var eventY;
var zoomRatio;
var shadowBoxHref;
var shadowBox;
var startTime;
var screenBox;
var titleBox;
var closeBox;
var iframe;
var shadowBoxTitle;
var shadowDate = new Date();

var IE = document.all?true:false;

function openShadowBox(href, width, height, e, title)
{
         var viewportwidth;
         var viewportheight;

        shadowBox = document.getElementById("shadowBox");
        screenBox = document.getElementById("screen");
        titleBox = document.getElementById("titleBox");
        closeBox = document.getElementById("closeBox");


        shadowDate = new Date();
        endTime = shadowDate.getTime() + openTime;

        shadowBox.innerHTML = "";

        if(!shadowBox)
        {
                shadowBox = document.createElement("div");
                //document.appendChild(shadowBox);
        }

        shadowBox.style.position = "absolute";
        shadowBox.style.width = 0;
        shadowBox.style.height = 0;
        //shadowBox.innerHTML = "<h1>Hello there</h1>";
        shadowBox.style.backgroundColor = "#000000";
        shadowBox.style.border = "1px solid #ffffff";
        shadowBox.style.zIndex = "150";
        shadowBox.style.display = "block";

        screenBox.style.position = "absolute";
        screenBox.style.width = document.body.scrollWidth + "px";
        screenBox.style.height = document.body.scrollHeight + "px";
        screenBox.style.backgroundColor = "#000000";
        screenBox.style.zIndex = "100";
        screenBox.style.top = "0px";
        screenBox.style.left = "0px";
        screenBox.style.display = "block";

        if (IE)
        {
                screenBox.style.filter = "alpha(opacity=0)"
                shadowBox.style.filter = "alpha(opacity=0)"
        }
        else
        {
                screenBox.style.opacity = 0;
                shadowBox.style.filter = "alpha(opacity=0)"
        }


        if (typeof window.innerWidth != 'undefined')
        {
             viewportwidth = window.innerWidth,
             viewportheight = window.innerHeight
        }
        else if (typeof document.documentElement != 'undefined'
            && typeof document.documentElement.clientWidth !=
            'undefined' && document.documentElement.clientWidth != 0)
        {
              viewportwidth = document.documentElement.clientWidth,
              viewportheight = document.documentElement.clientHeight
        }
        else
        {
              viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
              viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }

        if (IE)
        { // grab the x-y pos.s if browser is IE
                tempX = event.clientX + document.body.scrollLeft;
                tempY = event.clientY + document.body.scrollTop;
        }
        else
        { // grab the x-y pos.s if browser is NS
                tempX = e.pageX;
                tempY = e.pageY;
        }
        if (tempX < 0){tempX = 0;}
        if (tempY < 0){tempY = 0;}

        eventX = tempX;
        eventY = tempY;
        shadowBoxWidth = width;
        shadowBoxHeight = height;
        shadowBoxTop = (viewportheight / 2) - (height / 2) + document.body.scrollTop;
        shadowBoxLeft = (viewportwidth / 2) - (width / 2) + document.body.scrollLeft;
        zoomRatio = 0;
        shadowBoxHref = href;
        shadowBoxTitle = title;
        //shadowBox.style.top = ((viewportheight / 2) - (height / 2) + document.body.scrollTop)+ "px";
        //shadowBox.style.left = ((viewportwidth / 2) - (width / 2) + document.body.scrollLeft) + "px";

        shadowBox.style.top = tempY;
        shadowBox.style.left = tempX;

        //setTimeout("fadeInScreen()", delay);
        setTimeout("zoomShadowBox()", 1);

}

function zoomShadowBox()
{
        shadowDate = new Date();
        zoomRatio = 1 - Math.pow(((endTime - shadowDate.getTime()) / openTime), 8);
        if (zoomRatio > 1) zoomRatio = 1

        //alert(endTime - shadowDate.getTime());

        //zoomRatio = zoomRatio + ((1 - zoomRatio) / speed);

        shadowBox.style.width = (shadowBoxWidth * zoomRatio) + "px";
        shadowBox.style.height = (shadowBoxHeight * zoomRatio) + "px";
        shadowBox.style.left = (eventX + ((shadowBoxLeft - eventX) * zoomRatio)) + "px";
        shadowBox.style.top = (eventY + ((shadowBoxTop - eventY) * zoomRatio)) + "px";

        if (IE)
        {
                shadowBox.style.filter = "alpha(opacity=" + (zoomRatio * 100) + ")"
                screenBox.style.filter = "alpha(opacity=" + (backgroundOpacity * zoomRatio * 100) + ")"
        }
        else
        {
                shadowBox.style.opacity = zoomRatio;
                screenBox.style.opacity = zoomRatio * backgroundOpacity;
        }

        //alert(zoomRatio);

        if (zoomRatio < 0.999)
        {
                setTimeout("zoomShadowBox()", 5);
        }
        else
        {
                iframe=document.createElement("iframe");
                iframe.src = shadowBoxHref;
                iframe.style.border = "0px none #000000";
                iframe.style.padding = "0px";
                iframe.style.margin= "0px";
                iframe.style.width = "100%";
                iframe.style.height = (shadowBoxHeight - 2) + "px";
                shadowBox.appendChild(iframe);
                //alert(document.title);
                zoomRatio = 1;


               titleBox.style.display = "block";
               titleBox.style.position = "absolute";
               titleBox.style.width = shadowBoxWidth;
               titleBox.style.height = "30px";
               titleBox.style.top = (shadowBoxTop - 30) + "px";
               titleBox.style.left = shadowBoxLeft;
               titleBox.style.color = "#ffffff";
               titleBox.style.zIndex = 150;
               //titleBox.innerHTML = oDoc.title;

               closeBox.style.display = "block";
               closeBox.style.position = "absolute";
               closeBox.style.width = shadowBoxWidth;
               closeBox.style.height = "30px";
               closeBox.style.top = (shadowBoxTop + shadowBoxHeight + 5) + "px";
               closeBox.style.left = shadowBoxLeft;
               closeBox.style.textAlign = "right";
               closeBox.style.color = "#ffffff";
               closeBox.style.zIndex = 150;
               closeBox.innerHTML = "<a href='javascript:closeShadowBox()'><img border='0' src='/images/close.png'></a>";

                getTitle();
        }
}

function closeShadowBox()
{

        shadowDate = new Date();
        endTime = shadowDate.getTime() + openTime;
        startClose();

}

function startClose()
{
        titleBox.style.display = "none";
        closeBox.style.display = "none";

        //zoomRatio = zoomRatio - ((0 + zoomRatio) / speed);
        shadowDate = new Date();
        zoomRatio = Math.pow(((endTime - shadowDate.getTime()) / openTime), 8);
        if (zoomRatio < 0) zoomRatio = 0



        if (IE)
        {
                shadowBox.style.filter = "alpha(opacity=" + (zoomRatio * 100) + ")"
                screenBox.style.filter = "alpha(opacity=" + (backgroundOpacity * zoomRatio * 100) + ")"
        }
        else
        {
                shadowBox.style.opacity = zoomRatio;
                screenBox.style.opacity = zoomRatio * backgroundOpacity;
        }

        //alert(zoomRatio);

        if (zoomRatio > 0.001)
        {
                setTimeout("startClose()", 5);
        }
        else
        {
                shadowBox.style.display = "none";
                screenBox.style.display = "none";
        }
}


function getTitle()
{
        if (showTitle == 1)
        {

                try
                {
                       var oDoc = iframe.contentWindow || iframe.contentDocument;

                       if (oDoc.document)
                       {
                               oDoc = oDoc.document;
                       }
                       //alert(oDoc.title);

                       if (oDoc.title == "")
                       {
                               setTimeout("getTitle()", 1000);
                       }
                       else
                       {
                               titleBox.innerHTML = oDoc.title;
                       }
                }
                catch(e)
                {
                        setTimeout("getTitle()", 1000);
                }
        }
        else if (showTitle == 2)
        {
                titleBox.innerHTML = shadowBoxTitle;
        }
}
