﻿/*
    duffyBox (revised)
    Chad Lahn
    12/22/2007
*/


function duffyBox() {
    // Define DOM objects to create handlers
    this.overlay = document.createElement("div");
    this.overlayIE6 = document.createElement("div");
    this.box = document.createElement("div");
    this.closeBtn = document.createElement("a");
    this.title = document.createElement("div");
    this.iFrame = document.createElement("iframe");
    
    // Define variables
    this.iFrameMinHeight = 50;
    this.currentHeight = null;
    this.timeout = null;
    this.titleText = null;
    this.iFrameDoc = null;
    this.iFrameWidth = null;
    this.currentTop = null;
    this.sizeDelay = 100; // Length of time (ms) after setSize gets called to initiate adjustment
    this.isOpen = false;
    this.loaded = false;
    this.isAdjusting = false;
        
    // Define methods
    this.isIE = function() { return (typeof document.all != "undefined"); }
    this.isIE6 = function() { return (this.isIE() && typeof document.body.style.maxHeight == "undefined"); };
    this.winInnerHeight = function() { return (this.isIE()) ? document.documentElement.clientHeight : window.innerHeight; };
    this.bodyOffsetHeight = function() { return document.body.offsetHeight + 25; }; //Additional offset needed as height returned is inaccurate
    this.iFrameMaxHeight = function() { return (this.iFrameDoc != null) ? (this.isIE()) ? (this.iFrameDoc != null) ? this.iFrameDoc.body.offsetHeight : 0 : (this.iFrame.contentDocument != null) ? this.iFrame.contentDocument.height + 15 : 0 : 0; };
    this.offsetWidth = function() { return (this.currentHeight < this.iFrameMaxHeight()) ? 40 : 20; };
    this.getOverlayHeight = function() { return (this.winInnerHeight() > this.bodyOffsetHeight()) ? this.winInnerHeight() + "px"  :  this.bodyOffsetHeight() + "px"; };
    this.getDesiredHeight = function() { return ((this.winInnerHeight() - 80) < this.iFrameMaxHeight()) ? ((this.winInnerHeight() - 80) > this.iFrameMinHeight) ? (this.winInnerHeight() - 80) : this.iFrameMinHeight : this.iFrameMaxHeight(); };
    this.getDesiredTop = function() { return ((this.winInnerHeight() - (this.currentHeight + 30)) / 2); };
    
    this.hideShowSelects = function()
    {
        if(this.isIE6())
        {
            var boxes = document.getElementsByTagName("select");
            for(var s = 0; s < boxes.length; s++) 
                boxes[s].style.visibility = (!this.isOpen) ? "" : "hidden";
        }
    };
    this.clearTree = function(node)
    {
        for(var x = node.childNodes.length - 1; x > -1; x--)
        {
            if(node.childNodes[x].hasChildNodes)
                this.clearTree(node.childNodes[x]);
            
            node.removeChild(node.childNodes[x]);
        }
    };
    this.setTitle = function(txt) 
    {
        var txtNode = document.createTextNode(txt);
        if(this.title.childNodes.length > 0)
            this.title.replaceChild(txtNode, this.title.childNodes[0]);
        else
            this.title.appendChild(txtNode);
    };
    this.setWidth = function()
    {
        this.box.style.width = (this.iFrameWidth + this.offsetWidth() + 2) + "px"; // Space for border
        
        //this.box.style.background = '#dddddd';
        this.iFrame.style.width = this.iFrameWidth + this.offsetWidth() + "px";    
    };
    this.setHeight = function ()
    {
        this.box.style.height = (this.currentHeight + 24) + "px"; // Space for title
        this.iFrame.style.height = this.currentHeight + "px";
         this.iFrame.style.background = '#ffffff';
        this.overlay.style.height = this.getOverlayHeight();
       
        if(this.isIE6())
            this.overlayIE6.style.height = this.getOverlayHeight();
    };
    this.setTop = function ()
    {
        document.documentElement.scrollTop = 0;
        this.currentTop = (this.currentTop < 25) ? 25 : this.currentTop;
        this.box.style.marginTop = this.currentTop + "px";
        if(this.isIE6())
            this.box.style.top = this.currentTop + "px";
    };
    this.adjust = function()
    {
        if(this.loaded)
        {
            this.currentHeight = this.getDesiredHeight();
            
            this.setHeight();
            this.setWidth();
            
            this.currentTop = this.getDesiredTop();            
            this.setTop();
            this.isAdjusting = false;
        }
               
    };    
    this.setSize = function()
    {
        if(this.isOpen)
        {
            if(this.isAdjusting && this.timeout != null)
                clearTimeout(this.timeout);
                
            this.isAdjusting = true;
            this.timeout = setTimeout("inContextBox.adjust()", this.sizeDelay);
        }
    };
    this.open = function(info,siteid,pageTitle)
    {
        if(info == "publish")
        {
            window.open("Publish.aspx", "Publish", "", "width=500,height=750,scrollbars");
        }
        else
        {
        
        
           
            this.loaded = false;
            this.close();
            
            document.documentElement.scrollTop = 0;
            this.isOpen = true;
            this.loaded = false;
            
           // document.getElementsByTagName("html")[0].style.overflow="hidden";
            this.hideShowSelects();
            
            this.titleText = info +' : '+ pageTitle;
            this.iFrameWidth = 200;
            this.currentHeight = 0;
            this.currentTop = this.getDesiredTop();

            // Set initial heights Write objects to the dom
            this.iFrame.setAttribute("id","inContextFrame");
            this.iFrame.setAttribute("name","inContextFrame");        
            this.box.appendChild(this.closeBtn);
            this.box.appendChild(this.title);
            this.box.appendChild(this.iFrame);

            if(this.isIE6())
                this.overlay.appendChild(this.overlayIE6);        
            
            this.overlay.appendChild(this.box);
            document.body.appendChild(this.overlay);
            
            this.setTitle("Loading");
            this.setHeight();
            this.setWidth();
            this.setTop();
             if (info == "Edit")
                {
               this.box.style.background = '#145e1b'; 
               }
            else if(info == "New")
                {
                this.box.style.background = '#E57607'; 
               }
            else if(info == "Delete")
                {
                this.box.style.background = '#b02828'; 
               }
            else if(info == "Logout")
                {
                this.box.style.background = '#666666'; 
               }
            this.iFrame.src = "/control.aspx?control=" + info + "&siteid=" +siteid;
        }
    };
    this.init = function(doc)
    {
        if(this.isOpen)
        {
            this.iFrameDoc = doc;
            this.iFrameWidth = this.iFrameDoc.body.getElementsByTagName('div')[2].offsetWidth;
            
            if(!this.loaded)
            {   
                this.setTitle(this.titleText);
                this.loaded = true;
            }
            
            this.adjust();
        }
    };
    this.close = function()
    {
        if(this.isOpen)
        {
        
            this.isOpen = false;
            this.loaded = false;
            
            this.iFrameDoc = null;
            this.iFrame.src = "about:blank";
            
            this.clearTree(this.overlay);
            
            document.body.removeChild(this.overlay);
            
            this.hideShowSelects();
        }
    };  
    
    
    // Initialize main components upon instantiation
    this.overlay.className = "overlay";
    this.box.className = "box";
    this.title.className = "title";
    this.overlayIE6.className = "overlayIE6";
    this.closeBtn.setAttribute("href","javascript:inContextBox.close();");
    this.closeBtn.setAttribute("title","Close/Cancel");
    this.iFrame.frameBorder = "0";
};
var inContextBox = new duffyBox();

function addEvent(elm, evType, fn, useCapture) {
    
    if(typeof useCapture == "undefined") useCapture = false; // defaults to event bubbling
	if (typeof elm.addEventListener != "undefined" && !inContextBox.isIE()) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (typeof elm.attachEvent != "undefined") {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
		
	}
	else {
		elm['on' + evType] = fn;
	}
}

addEvent(window, "resize", function() { inContextBox.setSize(); });
addEvent(window, "scroll", function() { inContextBox.setSize(); });


function disableBtn(btn, doing)
{
    btn.className = "inContextDisabled";
    btn.innerHTML = doing;
    addEvent(btn, "focus", function() { this.blur(); });
}
