/* =======================================================================================
 * Multifaceted Lightbox
 * by Greg Neustaetter - http://www.gregphoto.net
 *
 * INSPIRED BY (AND CODE TAKEN FROM)
 * ==================================
 * The Lightbox Effect without Lightbox
 * PJ Hyett
 * http://pjhyett.com/articles/2006/02/09/the-lightbox-effect-without-lightbox
 *
 * Lightbox JS: Fullsize Image Overlays
 * by Lokesh Dhakar - http://www.huddletogether.com
 *
 * For more information on this script, visit:
 * http://huddletogether.com/projects/lightbox/
 *
 * Licensend under:
 * Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
 * (basically, do anything you want, just leave my name and link)
 *
 * History:
 * ========
 * Feb. 13, 2007 (mlee) - Added showBoxByIFrame (iframe deployment) and initIFrame (creates iframe container)
 * June 6, 2007 (mrandall) - Added initDisplayNews (iframe deployment) and hideNews
 * June 15, 2007 (mrandall) - Added initMobileAlerts (iframe deployment)
 *
 * Deployment Notes:
 * =================
 * To deploy, copy lightbox directory to appropriate directory and adjust image urls in Lightbox.init() and Lightbox.initIFrame()
 * ==================================================================================== */

var Lightbox = {
	initFlag: false,
    lightboxType: null,
    lightboxCurrentContentID: null,

    showBoxByAJAX: function(href, boxWidth, boxHeight) {
        this.lightboxType = 'ajax';
        this.setLightboxDimensions(boxWidth, boxHeight);
        var contents = $('boxContents');
        var myAjax = new Ajax.Updater(contents, href, {method: 'get'});
        this.showBox();
        return false;
    },

    showBoxByIFrame: function(href, boxWidth, boxHeight) {
        this.lightboxType = "iframe";
        this.setLightboxDimensions(boxWidth, boxHeight);
        $("lbFrame").src = href;
        this.showBox();
        return false;
    },

    setLightboxDimensions: function(width, height) {
        var windowSize = this.getPageDimensions();
        if(width) {
            if(width < windowSize[0]) {
                $('box').style.width = width + 'px';
            } else {
                $('box').style.width = (windowSize[0] - 50) + 'px';
            }
        }
        if(height) {
            if(height < windowSize[1]) {
                $('box').style.height = height + 'px';
            } else {
                $('box').style.height = (windowSize[1] - 50) + 'px';
            }
        }
    },

    resizeEvent: function() {
    	this.center('box');
    },

    showBox: function() {
        Element.show('overlay');
        this.center('box');
        Event.observe(window, 'resize', Lightbox.cachedResize);
        return false;
    },

	hideAds: function() {
		$$('div.ad').invoke('hide');
		$$('div.flash').invoke('hide');		
	},

	showAds: function() {
		$$('div.ad').invoke('show');
		$$('div.flash').invoke('show');
	},
	
    hideBox: function(){
        var contents = $('boxContents');
        if (this.lightboxType == 'id') {
            var body = document.getElementsByTagName("body").item(0);
            Element.hide(this.lightboxCurrentContentID);
            body.appendChild($(this.lightboxCurrentContentID));
        }
        Event.stopObserving(window, 'resize', Lightbox.cachedResize);
        Element.remove($('box'));
        Element.remove($('overlay'));
		this.showAds();
        return false;
    },

    hideNews: function(){
        var contents = $('boxContents');
        if (this.lightboxType == 'id') {
            var body = document.getElementsByTagName("body").item(0);
            Element.hide(this.lightboxCurrentContentID);
            body.appendChild($(this.lightboxCurrentContentID));
        }
        if (this.lightboxType != "iframe") {
            contents.innerHTML = '';
        } else {
            $("lbFrame").src = "/js/lib/lightbox/blank.html";
        }
        $('box').style.width = null;
        $('box').style.height = null;
        Element.hide('box');
        Element.hide('overlay');
		this.showAds();
        return false;
    },

    // taken from lightbox js, modified argument return order
    getPageDimensions: function(){
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth;
            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
            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 = windowWidth;
        } else {
            pageWidth = xScroll;
        }
        arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight)
            return arrayPageSize;
    },

    center: function(element){
        try {
            element = document.getElementById(element);
        } catch(e) {
            return;
        }
        var windowSize = this.getPageDimensions();
        var window_width  = windowSize[0];
        var window_height = windowSize[1];

        $('overlay').style.height = windowSize[3] + 'px';

        element.style.position = 'absolute';
        element.style.zIndex   = 99;

        var scrollY = 0;

        if ( document.documentElement && document.documentElement.scrollTop ){
            scrollY = document.documentElement.scrollTop;
        }else if ( document.body && document.body.scrollTop ){
            scrollY = document.body.scrollTop;
        }else if ( window.pageYOffset ){
            scrollY = window.pageYOffset;
        }else if ( window.scrollY ){
            scrollY = window.scrollY;
        }

        var elementDimensions = Element.getDimensions(element);
        var setX = ( window_width  - elementDimensions.width  ) / 2;
        var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;

        setX = ( setX < 0 ) ? 0 : setX;
        setY = ( setY < 0 ) ? 0 : setY;

        element.style.left = setX + "px";
        element.style.top  = setY + "px";
        Element.show(element);
    },

    init: function() {
        var lightboxtext = '<div id="overlay" style="display:none"></div>';
        lightboxtext += '<div id="box" style="display: none">';
        lightboxtext += '<img id="close" src="/images/common/lightbox/x_close.gif" onclick="Lightbox.hideBox()" alt="Close" title="Close this window" />';
        lightboxtext += '<div id="boxContents"></div>';
        lightboxtext += '</div>';
        var body = document.getElementsByTagName("body").item(0);
        new Insertion.Bottom(body, lightboxtext);
    },

	initFrame: function(lightboxtext) {
		this.hideAds();

		var body = document.getElementsByTagName("body").item(0);
		new Insertion.Bottom(body, lightboxtext);
		this.initFlag = true;
	}
}

Lightbox.cachedResize = Lightbox.resizeEvent.bind(Lightbox);
