﻿var zBoxes = [];
var zBox_TagsToHide = ["select","object","embed"];       

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)
{
    zBox_HideAll(box);
    
    for(var i=0;i<zBox_TagsToHide.length;i++)
    {
        zBox_ToggleTags(zBox_TagsToHide[i],true,box);
    }
        
    var zBox_bg = zBox_GetBackground();
    var temp = document.body.offsetHeight - box.Top;
    var bgHeight = document.body.offsetHeight;
    var bgWidth = document.body.offsetWidth;
    
    if(box.Div.offsetHeight > temp)
         bgHeight = (box.Div.offsetHeight - temp) + document.body.offsetHeight;     
    
    zBox_bg.style.height = Math.round(bgHeight) + "px";
    zBox_bg.style.width = Math.round(bgWidth) + "px";        
    zBox_bg.style.display = 'block';
    
     if(IEVersion() < 7)
        zBox_bg.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";   
}

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_IE6div";
        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.getElementById(this.ID);
    this.FooterDiv = this.GetChildDiv("overlay_footer");
    this.MidDiv = this.GetChildDiv("overlay_mid");
    this.HeaderDiv = this.GetChildDiv("overlay_header");
    this.PostShowScripts = [];
    this.PostHideScripts = [];
    this.State = "";
    this.Top = 0;
    this.Left = 0;
    
    //var att = document.createAttribute("isZBox");
    //att.value = "true";            
    //this.Div.setAttributeNode(att);
    //alert(this.Div.getAttribute("isZBox"));
}

zBox.prototype.Show = zBox_Show;
function zBox_Show()
{       
    this.Div.style.display = "block";
    this.RunPostShowScripts();
    this.PositionBox();
    zBox_PreparePage(this);
    this.Div.style.visibility = "visible";
    
    if(IEVersion() < 7)
    {
        this.HeaderDiv.className += " overlayHeaderIE6";
        this.HeaderDiv.style.background = "none";
        
        this.MidDiv.className += " overlayMidIE6"; 
        this.MidDiv.style.background = "none";

        this.FooterDiv.className += " overlayFooterIE6"; 
        this.FooterDiv.style.background = "none";              
    }
    
    this.State = "visible";                
}


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 + scrollPos.scrollX);
    this.Top = Math.round((pageSize.height - emSize.height) / 2 + scrollPos.scrollY);

    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";
   this.RunPostHideScripts();
   zBox_RestorePage();        
}

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;     
}

zBox.prototype.RunPostShowScripts = zBox_RunPostShowScripts;
function zBox_RunPostShowScripts()
{	
	try
	{
	    for(var i=0;i<this.PostShowScripts.length;i++)
	    {
	        
	        eval(this.PostShowScripts[i]);
	    }
	}
	catch(e){}
}

zBox.prototype.AddPostShowScript = zBox_AddPostShowScript;
function zBox_AddPostShowScript(s)
{
	this.PostShowScripts.push(s);	
}

zBox.prototype.RunPostHideScripts = zBox_RunPostHideScripts;
function zBox_RunPostHideScripts()
{	
	try
	{
	    for(var i=0;i<this.PostHideScripts.length;i++)
	    {
	        eval(this.PostHideScripts[i]);
	    }
	}
	catch(e){}
}

zBox.prototype.AddPostHideScript = zBox_AddPostHideScript;
function zBox_AddPostHideScript(s)
{
	this.PostHideScripts.push(s);	
}

 
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; 
}

window.onresize = zBox_ResetVisibleBox;    