/*********************************
lightbox by Jeremy Dowell

REQUIRED COMPONENTS
lightbox.css

USAGE
var instance = new lightBox();
instance.option = value;
instance.init(); --optional if using clone
-- one or the other of the following three must be done. not both, not neither.
instance.clone = cloneID;
instance.createFromHTML(element);
instance.createFromElement(element);

instance.popBox();


CHANGES
-- 6.12.09 - JD
    - Added createFromElement method. Allows the lightbox to build content from the passed document element instead of a cloned id.
        - Example:
            var el = document.createElement('div');
            el.innerHTML = "Hi Mom!";
            instance.createFromElement(el);
    - Added createFromHTML method. Allows the lightbox to build content from the passed document element instead of a cloned id.
        - Example:
            var html = "<div><h1>OBAMA FOR PRESIDENT OF SPACE</h1></div>";
            instance.createFromHTML(html);
    - Added onClose property. This allows the developer to attach a function to be run when the lightbox closes. Default: null
        - Example: instance.onClose = function() { window.location= "http://www.google.com" }; //This will forward the current page to google when the user closes the lightbox
    - Added showClose property. This allows the developer to determine if the close buttons are visible or not. Default: true
        - isntance.showClose = false;
-- 6.23.09 JD
    - Added the ability to determine whether the body element or the window is taller for determining the height for the lightbox
 2011-11-10 Peter Siewert
   Ticket 2418: Added lightbox.dirPrefix 
                Added showLightboxText() convenience method
*********************************/

function buildLightboxText(text) {
    var ans = new lightBox();
    ans.contentClass = "lightbox";
    ans.showClose = false;
    ans.init();
    ans.setContent("<center><h1>" + text + "</h1></center>");
    return ans;
}
function showLightboxText(text) {
    var ans = buildLightboxText(text);
    ans.popBox();
    return ans;
}
var currentLightbox = null;

//Class declaration
var lightBox = function() {
    //private properties
    this.body = null;
    this.capsule = null;
    this.shadow = null;
    this.content = null;
    this.contentDiv = null;
    this.containerDiv = null;

    //Public Propertieso
    this.clone = null;
    this.contentClass = "lbContent";
    this.popWidth = 500;
    this.popHeight = 530;
    this.width = null;
    this.onClose = null;
    this.showClose = true;
    this.dirPrefix = "";
}


lightBox.prototype.init = function()
{
    this.createBox();
}

lightBox.prototype.createBox = function()
{

    var self = this;

    this.body = document.getElementsByTagName("body")[0];
    this.capsule = document.createElement("div");
    this.capsule.id = "lightboxCapsule";

    this.capsule.className = "lbCapsule";
    this.capsule.style.display = "none";
    this.body.appendChild(this.capsule);
    this.shadow = document.createElement("div");
    this.shadow.id = "lightbox";
    this.shadow.innerHTML = "&nbsp;"
    this.capsule.appendChild(this.shadow);
    this.containerDiv = document.createElement("div");
    this.containerDiv.id = "lightboxContainer";
    this.containerDiv.className = "lbContainer";
    this.capsule.appendChild(this.containerDiv);

    //
    if (this.clone != null)
    {
        var cloneID = document.getElementById(this.clone);
        if (cloneID != null)
        {
            this.contentDiv = cloneID.cloneNode(true);

            this.capsule.appendChild(this.contentDiv);
            this.contentDiv.id = "lightBoxContent";
            this.contentDiv.style.display="block";
            if (this.contentClass != null)
            {
                this.contentDiv.className = this.contentClass;
            }
        }
    }

    else
    {

        this.contentDiv = document.createElement("div");
        this.contentDiv.id = "lightBoxContent";
        if (this.contentDiv != null)
        this.capsule.appendChild(this.contentDiv);
        if (this.content != null)
        {

            this.contentDiv.appendChild(this.content);
            if (this.contentClass != null)
            {
                this.contentDiv.className = this.contentClass;
                //this.contentDiv.innerHTML = this.content;
            }

        }
    }

    this.contentDiv.innerHTML = this.contentDiv.innerHTML;
    this.shadow.className = "lightBox";

    if (this.showClose == true)
    {
        var closeTopDiv = document.createElement("div");
        var closeBottomDiv = document.createElement("div");

        closeTopDiv.innerHTML = "<img src='" + this.dirPrefix + "aspnet_client/enSyt3/Lightbox/close-x.gif'/>";
        closeTopDiv.className = "closeButtonTop";
        closeTopDiv.onclick = function() { self.closeBox(); } ;


        closeBottomDiv.innerHTML = "<img src='" + this.dirPrefix + "aspnet_client/enSyt3/Lightbox/closeword.gif'/>";
        closeBottomDiv.className = "closeButtonBottom";
        closeBottomDiv.onclick = function() { self.closeBox(); } ;


        this.containerDiv.appendChild(closeTopDiv);
        this.containerDiv.appendChild(this.contentDiv);
        this.containerDiv.appendChild(closeBottomDiv);
    }
    else
    {
        this.containerDiv.appendChild(this.contentDiv);
    }
    if (this.width != null)
    {
        this.containerDiv.style.width = this.width;
    }
}


lightBox.prototype.createFromElement = function(el)
{
    this.contentDiv.innerHTML = "";
    this.contentDiv.appendChild(el);
}

lightBox.prototype.createFromHTML = function(html)
{
    this.contentDiv.innerHTML = "";
    var el = document.createElement("span");
    el.innerHTML = html;
    this.contentDiv.appendChild(el);
}


lightBox.prototype.popBox = function(refEl) {
    var height = 0;
    if (currentLightbox != null) {
        currentLightbox.closeBox();
    }
    
    if (this.capsule == null) {
        this.createBox();
    }

    if (document.documentElement.clientHeight > this.body.clientHeight) {
        height = document.documentElement.clientHeight;
    }
    else {
        height = this.body.clientHeight;
    }
    this.capsule.style.height = height + 'px';
    this.capsule.style.display = "block";
    scroll(0, 0);

    currentLightbox = this;
}

lightBox.prototype.closeBox = function()
{
    if(this.onClose != null)
    {
        this.onClose();
    }
    this.capsule.style.display = "none";
}


lightBox.prototype.setContent = function(content)
{
    this.contentDiv.innerHTML = content;
}


lightBox.prototype.addContent = function(content)
{
    this.contentDiv.innerHTML += content;
}


lightBox.prototype.clearContent = function()
{
    this.contentDiv.innerHTML = "";
}




lightBox.prototype.ajaxLoad = function(url, width,height)
{
    var self = this;
    xmlhttp=null;

    if (window.XMLHttpRequest)
    {
        // code for all modern browsers
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE5 and IE6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp!=null)
      {
      xmlhttp.onreadystatechange=state_Change;
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
    else
    {
      alert("Your browser does not support XMLHTTP.");
    }

    function state_Change()
    {
        if (xmlhttp.readyState==4)
        {// 4 = "loaded"
          if (xmlhttp.status==200)
            {// 200 = OK
            // ...our code here...]
            console.log(xmlhttp.responseText);
            self.content = xmlhttp.responseText;
            self.popBox();
            }
          else
          {
            popWin(url, width, height);
          }
        }
    }
}

lightBox.prototype.popWin = function(URL)
{

        var strOptions = "width=" + this.popWidth + ",height=" + this.popHeight + ",toolbar=no,menubar=no,status=no,resize=" + ADJUST + ",resizable=" + ADJUST + ",directories=no,scrollbars=yes";
        window.open(URL, "popUp", strOptions);
}

