﻿var zBoxes = [];
var zBox_TagsToHide = ["select","object","embed"]; 
var zBox_DropShadow = false;      

function zBox_CreateBox(id, nocopy)
{
    try
    {
        zBoxes.push(new zBox(id, nocopy));
    }
    catch(e)
    {}
}

function zBox_GetBox(id)
{
    try
    {
        for(var i=0;i<zBoxes.length;i++)
        {
            if(zBoxes[i].ID == id)
                return  zBoxes[i];
        }
    }
    catch(e)
    {}
}

function zBox_ShowBox(id)
{
    try
    {
        zBox_GetBox(id).Show();
    }
    catch(e)
    {}
}

function zBox_GetVisibleBox()
{try{
    
    for(var i=0;i<zBoxes.length;i++)
    {
        if(zBoxes[i].State == "visible")
            return  zBoxes[i];
    }
    
}catch(e){}}

function zBox_HideAll(box)
{try{

    for(var i=0;i<zBoxes.length;i++)
    {
        if(zBoxes[i].ID != box.ID)
            zBoxes[i].Hide(true);
    }
    
 }catch(e){}}

function zBox_ToggleTags(tagName,hide,box)
{try{   
    
    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';                        
        }
    }

}catch(e){}}

function zBox_RestorePage()
{try{
    
    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';

}catch(e){}}


function zBox_PreparePage(box)
{try{
    
    // 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);
    }
    
    zBox_PositionBackground();
    
    var zBox_bg = zBox_GetBackground();
    zBox_bg.style.display = 'block';    
      
 }catch(e){}}

function zBox_GetBackground()
{
    try{
    
        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;
    
    }catch(e){}
}

function zBox_PositionBackground()
{try{
    
    // set up and position the background
    var zBox_bg = zBox_GetBackground();    
    var bgHeight = 0;
    var bgWidth = 0;
     
    if(document.body.clientHeight > document.body.scrollHeight)
        bgHeight = document.body.clientHeight;        
    else
        bgHeight = document.body.scrollHeight;
    
    bgWidth = document.body.clientWidth;        
          
    if(IEVersion() < 7)
    {
        zBox_bg.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=60)";
    } 
    
    zBox_bg.style.height = Math.round(bgHeight) + "px";
    zBox_bg.style.width = Math.round(bgWidth) + "px";
    
   
 }catch(e){}}
        
function zBox_IsInsideBox(node,box)
{try{

    if(node.tagName != "BODY")
    {
        if(box && (node.id == box.ID))        
            return true;
        else
            return zBox_IsInsideBox(node.parentNode,box);
    }
    
    return false;

}catch(e){}}


function zBox_ResetVisibleBox()
{try{

    var box = zBox_GetVisibleBox();    
    if(box)
    {
        box.PositionBox();
        zBox_PositionBackground();
    }

}catch(e){}}

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)
{try{

    this.ID = id;
    //this.Div = document.body.appendChild(document.getElementById(this.ID));       
    this.PostShowScripts = [];
    this.PostHideScripts = [];
    this.State = "";
    this.Top = 0;
    this.Left = 0;
    this.OnShow = new zBox_Event();
    this.OnHide = new zBox_Event();
    this.ElementsLoaded = false;
    this.LoadElements();
    

}catch(e){}}

zBox.prototype.LoadElements = zBox_LoadElements;
function zBox_LoadElements()
{
    try
    {
        if(!this.ElementsLoaded)
        {
//            if(this.NoElementCopy)
//                this.Div = document.getElementById(this.ID);
//            else
                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.ElementsLoaded = true;    
    
    }catch(e){}
    
}

zBox.prototype.Show = zBox_Show;
function zBox_Show()
{   
    try
    {
        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();
    
    }catch(e){}
         
}

zBox.prototype.PositionBox = zBox_PositionBox;
function zBox_PositionBox()
{
    try{
    
        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 + scrollPos.scrollX);
        this.Top = Math.round((pageSize.height - emSize.height) / 2 + scrollPos.scrollY);

        //non-ie broswers need to shift over to make up for the scroll bar.
        if(IEVersion() == 999)
            this.Left -= 9;

        this.Div.style.left = this.Left + 'px';
        if (this.Top < scrollPos.scrollY) this.Top = scrollPos.scrollY;
        this.Div.style.top = this.Top + 'px';
    
    }catch(e){}
}


zBox.prototype.Hide = zBox_Hide;
function zBox_Hide(background)
{
   try{
   
       this.Div.style.visibility = "hidden";
       this.Div.style.display = "none";
       this.State = "hidden";
       zBox_RestorePage();
       if(!background)
         this.OnHide.Execute();
     
   }catch(e){}     
}

zBox.prototype.GetChildDiv = zBox_GetChildDiv;
function zBox_GetChildDiv(id)
{
   try{
   
       var divs = this.Div.getElementsByTagName("div");
       for(var i=0;i<divs.length;i++)
       {
            if(divs[i].id == id)
                return divs[i];
       }
       
       return null;
   
   }catch(e){}   
}

function IEVersion() 
{try{
    
    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; 

}catch(e){}}

function zBox_SetEvents(enable)
{try{

    if(enable)
    {
        window.onresize = zBox_ResetVisibleBox;

        //if(IEVersion()<7)
            //window.onscroll = zBox_ResetVisibleBox;

        document.onkeydown = zBox_KeyPress;  
    }    

}catch(e){}}

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);}
}



