﻿var zBoxes = [];
var zBox_TagsToHide = ["select","object","embed"]; 
var zBox_DropShadow = false;      

function zBox_CreateBox(id)
{
    zBoxes.push(new zBox(id));
}

function zBox_GetBox(id)
{
    for(var i=0;i<zBoxes.length;i++)
    {
        if(zBoxes[i].ID == id)
            return  zBoxes[i];
    }
}

function zBox_GetVisibleBox()
{
    for(var i=0;i<zBoxes.length;i++)
    {
        if(zBoxes[i].State == "visible")
            return  zBoxes[i];
    }
}

function zBox_HideAll(box)
{
    for(var i=0;i<zBoxes.length;i++)
    {
        if(zBoxes[i].ID != box.ID)
            zBoxes[i].Hide();
    }
}

function zBox_ToggleTags(tagName,hide,box)
{    
    var tags = document.getElementsByTagName(tagName);
    for (var i=0; i<tags.length; i++)
    {            
        if(!zBox_IsInsideBox(tags[i],box))
        {        
            if(hide)
              tags[i].style.visibility = 'hidden';
            else
              tags[i].style.visibility = 'visible';                        
        }
    }
}

function zBox_RestorePage()
{
    for(var i=0;i<zBox_TagsToHide.length;i++)
    {
        zBox_ToggleTags(zBox_TagsToHide[i],false,null);
    }
    
    var zBox_bg = zBox_GetBackground();
    zBox_bg.style.display = 'none';
}


function zBox_PreparePage(box)
{
    // hide any box that might be showing
    zBox_HideAll(box);
    
    // hide incompatible tags
    for(var i=0;i<zBox_TagsToHide.length;i++)
    {
        zBox_ToggleTags(zBox_TagsToHide[i],true,box);
    }
        
    // set up and position the background
    var zBox_bg = zBox_GetBackground();    
    var bgHeight = 0;
    var bgWidth = 0;

    if(window.innerHeight)
    {   
        bgHeight = window.innerHeight;
        bgWidth = window.innerWidth;        
    }
    else
    {
        bgHeight = document.documentElement.clientHeight;
        bgWidth = document.documentElement.clientWidth;
    }
       
    
    if(IEVersion() < 7)
    {
        zBox_bg.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=60)";
        
        if(bgHeight < document.documentElement.scrollHeight)
            bgHeight = document.documentElement.scrollHeight;        
        
        bgWidth = document.documentElement.scrollWidth;
    } 
    
    zBox_bg.style.height = Math.round(bgHeight) + "px";
    zBox_bg.style.width = Math.round(bgWidth) + "px";
    zBox_bg.style.display = 'block';    
      
}

function zBox_GetBackground()
{
    var bg = document.getElementById('zBox_bg');
    
    if(!bg)
    {                
        bg = document.createElement("div");
        bg.id = "zBox_bg";

        if(IEVersion() < 7)
            bg.className = "zbox_bg_divIEold";
        else
            bg.className = "zbox_bg_div";
        
        bg.style.display = "none";        
        document.body.appendChild(bg);                
    }
    
    return bg;
}
        
function zBox_IsInsideBox(node,box)
{
    if(node.tagName != "BODY")
    {
        if(box && (node.id == box.ID))        
            return true;
        else
            return zBox_IsInsideBox(node.parentNode,box);
    }
    
    return false;
}


function zBox_ResetVisibleBox()
{
    var box = zBox_GetVisibleBox();    
    if(box)
    {
        box.PositionBox();
        zBox_PreparePage(box);
    }
}

var Utils = 
{
    getScrollPos: function()
    {
      var docElem = document.documentElement;
      return {
        scrollX: document.body.scrollLeft || window.pageXOffset || (docElem && docElem.scrollLeft),
        scrollY: document.body.scrollTop || window.pageYOffset || (docElem && docElem.scrollTop)
      };
    },

    getPageSize: function()
    {
      return {
        width: window.innerWidth || (document.documentElement && document.documentElement.clientWidth) || document.body.clientWidth,
        height: window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight
      };
    },

    getElementSize: function(obj)
    {
      return {
        width: obj.offsetWidth || obj.style.pixelWidth,
        height: obj.offsetHeight || obj.style.pixelHeight
      };
    }
         
}
        
//Box Object
function zBox(id)
{
    this.ID = id;
    this.Div = document.body.appendChild(document.getElementById(this.ID));
    this.FooterDiv = this.GetChildDiv("zbox_footer");
    this.MidDiv = this.GetChildDiv("zbox_mid");
    this.HeaderDiv = this.GetChildDiv("zbox_header");
    this.PostShowScripts = [];
    this.PostHideScripts = [];
    this.State = "";
    this.Top = 0;
    this.Left = 0;
    this.OnShow = new zBox_Event();
    this.OnHide = new zBox_Event();
}

zBox.prototype.Show = zBox_Show;
function zBox_Show()
{       
    zBox_SetEvents(true);    
    this.Div.style.display = "block";
    this.PositionBox();
    zBox_PreparePage(this);
    this.Div.style.visibility = "visible";
    
    if(zBox_DropShadow & (IEVersion() < 7))
    {
        this.HeaderDiv.className += " zbox_headerIEold";
        this.HeaderDiv.style.background = "none";
        
        this.MidDiv.className += " zbox_midIEold"; 
        this.MidDiv.style.background = "none";

        this.FooterDiv.className += " zbox_footerIEold"; 
        this.FooterDiv.style.background = "none";              
    }
    
    this.State = "visible";
    this.OnShow.Execute();                
}


zBox.prototype.PositionBox = zBox_PositionBox;
function zBox_PositionBox()
{
    //this.Div.style.display = "block";
    
    var pageSize = Utils.getPageSize();
    //var scrollPos = Utils.getScrollPos();
    var emSize = Utils.getElementSize(this.Div);

    this.Left = Math.round((pageSize.width - emSize.width) / 2 );
    this.Top = Math.round((pageSize.height - emSize.height) / 6  );

    this.Div.style.left = this.Left + 'px';
    //if (this.Top < scrollPos.scrollY) this.Top = scrollPos.scrollY;
    this.Div.style.top = this.Top + 'px';
}


zBox.prototype.Hide = zBox_Hide;
function zBox_Hide()
{
   this.Div.style.visibility = "hidden";
   this.Div.style.display = "none";
   this.State = "hidden";
   zBox_RestorePage();
   this.OnHide.Execute();      
}

zBox.prototype.GetChildDiv = zBox_GetChildDiv;
function zBox_GetChildDiv(id)
{
   var divs = this.Div.getElementsByTagName("div");
   for(var i=0;i<divs.length;i++)
   {
        if(divs[i].id == id)
            return divs[i];
   }
   
   return null;     
}

function IEVersion() 
{    
    var version = 999; // we assume a sane browser    
    if (navigator.appVersion.indexOf("MSIE") != -1)      
    // bah, IE again, lets downgrade version number      
        version = parseFloat(navigator.appVersion.split("MSIE")[1]);    
        return version;
    
    return 0; 
}

function zBox_SetEvents(enable)
{
    if(enable)
    {
        window.onresize = zBox_ResetVisibleBox;

        if(IEVersion()<7)
            window.onscroll = zBox_ResetVisibleBox;

        document.onkeydown = zBox_KeyPress;  
    }    
}

function zBox_KeyPress(e)
{
    if (!e) var e = window.event;
    
    if(e.keyCode == 27)
      zBox_GetVisibleBox().Hide();  	        
    
}

function zBox_Event(){this.eventHandlers = [];}

zBox_Event.prototype.AddHandler = function(eventHandler){
    this.eventHandlers.push(eventHandler);
}

zBox_Event.prototype.Execute = function(args)
{
    for(var i = 0; i < this.eventHandlers.length; i++)
    { this.eventHandlers[i](args);}
}

