/*
Created By: Chris Campbell
******* modified by fvz *************
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/


/*-----------------------------------------------------------------------------------------------*/

//document.observe('dom:loaded', lightbox_initialize);
document.observe('dom:loaded', lightboxActive);

var lightbox = Class.create();
var lb_active = 0;
lightbox.prototype = {
    _closefunctions: Array(),

    add_close: function(func) {
        this._closefunctions.push(func);
    },

    call_close: function() {
        for(var i = 0; i < this._closefunctions.length; ++i) {
            this._closefunctions[i](); 
        }
    },

    initialize: function(ctrl,href) {
        this.content = href;
        if (ctrl) {
            Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
            ctrl.onclick = function(){return false;};
        }
    },

    // Turn everything on - mainly the IE fixes
    activate: function(){
        this.displayLightbox();
    },

    //
    // getPageScroll()
    // Returns array with x,y page scroll values.
    // Core code from - quirksmode.org
    //
    getPageScroll: function(){
        var x,y;
        if (self.pageYOffset) {
            x = self.pageXOffset;
            y = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
            x = document.documentElement.scrollLeft;
            y = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
            x = document.body.scrollLeft;
            y = document.body.scrollTop;
        }
        return {x:x,y:y};
    },

    displayLightbox: function(){
            if (Prototype.Browser.IE) {
                var arrayPageSize = this.getPageSize();
                $('overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
            }

                var temp = this.getPageScroll();
                var lightboxTop = temp.y + 50;
                $('lightbox').setStyle({ top: lightboxTop + 'px' });
		$('lightbox').setStyle({ position: 'absolute' });


            $('overlay').show();
            $('lightbox').innerHtml = '<div id="lbLoadMessage"><p>Loading...</p></div>';
            $('lightbox').show();

            this.loadInfo();
    },

    // Begin Ajax request based off of the href of the clicked linked
    loadInfo: function() {
        var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
        );

    },

    // Display Ajax response
    processInfo: function(response){
        //info = "<div id='lbContent'>" + response.responseText + "</div>";
        //new Insertion.Before($('lbLoadMessage'), info)
        //$('lightbox').className = "done";
        $('lightbox').update(response.responseText);
        //this.actions();
    },

    // Search through new links within the lightbox, and attach click event
    actions: function(){
        lbActions = document.getElementsByClassName('lbAction');

        for(i = 0; i < lbActions.length; ++i) {
            Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
            lbActions[i].onclick = function(){return false;};
        }

    },

    // Example of creating your own functionality once lightbox is initiated
    insert: function(e){
       link = Event.element(e).parentNode;
       Element.remove($('lightbox'));

       var myAjax = new Ajax.Request(
              link.href,
              {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
       );

    },

    // Example of creating your own functionality once lightbox is initiated
    deactivate: function(){
        close_lightbox();
    },

    //
    //  getPageSize()
    //
    getPageSize: function() {

         var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) { // all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth;
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
            pageWidth = xScroll;
        } else {
            pageWidth = windowWidth;
        }

        return [pageWidth,pageHeight];
    }

}

/*-----------------------------------------------------------------------------------------------*/

function close_lightbox() {
    $('lightbox').hide();
    $('lightbox').innerHtml = '';
    $('overlay').hide();
    lightbox.call_close();
    //Element.remove($('lbContent'));
    //return true;
};

function open_lightbox_manual(href,func) {
    if(lb_active != 0){
        lightbox = new lightbox(0,href);
    
        if (func) {
            lightbox.add_close(func);
        }

        lightbox.displayLightbox();
    }
    return false;
}

function open_lightbox(ctrl) {
    return open_lightbox_manual(ctrl.href);
};

// Onload, make all links that need to trigger a lightbox active
//function lightbox_initialize(){
//    //addLightboxMarkup();
//    lbox = document.getElementsByClassName('lbOn');
//    for(i = 0; i < lbox.length; ++i) {
//        valid = new lightbox(lbox[i],lbox[i].href);
//    }
//}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
//function addLightboxMarkup() {
//    var objBody                 = $$('body')[0];
//    var overlay = new Element('div', { 'class': 'overlay', 'id': 'overlay' });
//    overlay.hide();
//    var lb = new Element('div', { 'class': 'lightbox', 'id': 'lightbox' });
//    lb.hide();
//    objBody.appendChild(overlay);
//    objBody.appendChild(lb);
//}

function lightboxActive(){
	lb_active = 1;
}
