
// -----------------------------------------------------------------------------------
//
//	Sharity Lightbox dialogs by Miriam Technologies, Inc - http://www.miriamtech.com
//	Based on Lightbox v2.04 by Lokesh Dhakar - http://www.lokeshdhakar.com
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//  	- Free for use in both personal and commercial projects
//		- Attribution requires leaving author name, author link, and the license info intact.
//
// -----------------------------------------------------------------------------------

var Lightbox = Class.create();

Lightbox.prototype = {
	current: undefined,
    
    // initialize()
    // Constructor runs on completion of the DOM loading.
    // Assumes that the proper lightbox HTML has already been created.
    //
    initialize: function() {
		this.resizeDuration = 0.2;
		this.overlayDuration = 0.2;	// shadow fade in/out duration
		this.overlayOpacity = 0.5;	// controls transparency of shadow overlay
		this.borderSize = 10;			// matches padding in the CSS

		this.keyboardAction = this.keyboardAction.bindAsEventListener(this);

		$('lightbox').observe('click', (function(event) { if (event.element().id == 'lightbox') this.end(); }).bind(this));
		$('loadingLink').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
		$('lightboxClose').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));

	    var th = this;
        (function(){
            var ids = 
                'lightbox outerImageContainer imageContainer lightboxImage loading loadingLink ' + 
                'imageDetails caption numberDisplay';   
            $w(ids).each(function(id){ th[id] = $(id); });
            th['overlay'] = $('lightboxOverlay');
       }).defer();
    },
    
	//
	//  start()
	//  Display overlay and lightbox.
	//
	start: function() {
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		
		// stretch overlay to fill page and fade in
		var options = arguments[0] || {};

		var arrayPageSize = this.getPageSize();

		new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: this.overlayOpacity });

		// calculate top and left offset for the lightbox 
		var arrayPageScroll = document.viewport.getScrollOffsets();
		var lightboxTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
		var lightboxLeft = arrayPageScroll[0];
		$('lightbox').setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
		
		$('loading').show();
		$('lightboxImage').hide();
		if (options.close != "hidden") $("lightboxClose").show();	
	},

	//
	//  show()
	//  Display the lightbox contents
	//
	show: function(){
		$('loading').hide();
		var options = Object.extend({
			duration: this.resizeDuration, 
			queue: "end"
			} , arguments[0] || { });
		new Effect.Appear('lightboxImage', options);
	},

    //
    //  resizeImageContainer()
    //
    resizeImageContainer: function(imgWidth, imgHeight) {

        // get current width and height
        var widthCurrent  = this.outerImageContainer.getWidth();
        var heightCurrent = this.outerImageContainer.getHeight();

        // get new width and height
        var widthNew  = (imgWidth  + this.borderSize * 2);
        var heightNew = (imgHeight + this.borderSize * 2);

        // scalars based on change from old to new
        var xScale = (widthNew  / widthCurrent)  * 100;
        var yScale = (heightNew / heightCurrent) * 100;

        // calculate size difference between new and old image, and resize if necessary
        var wDiff = widthCurrent - widthNew;
        var hDiff = heightCurrent - heightNew;

        if (hDiff != 0) new Effect.Scale(this.outerImageContainer, yScale, {scaleX: false, duration: this.resizeDuration, queue: 'front'}); 
        if (wDiff != 0) new Effect.Scale(this.outerImageContainer, xScale, {scaleY: false, duration: this.resizeDuration, delay: this.resizeDuration}); 

        // if new and old image are same size and no scaling transition is necessary, 
        // do a quick pause to prevent image flicker.
        var timeout = 0;
        if ((hDiff == 0) && (wDiff == 0)){
            timeout = 100;
            if (Prototype.Browser.IE) timeout = 250;   
        }

/*
        (function(){

            this.showImage();
        }).bind(this).delay(timeout / 1000);
*/
    },
    
    //
    //  enableKeyboardNav()
    //
    enableKeyboardNav: function() {
        document.observe('keydown', this.keyboardAction); 
    },

    //
    //  disableKeyboardNav()
    //
    disableKeyboardNav: function() {
        document.stopObserving('keydown', this.keyboardAction); 
    },

    //
    //  keyboardAction()
    //
    keyboardAction: function(event) {
        var keycode = event.keyCode;

        var escapeKey;
        if (event.DOM_VK_ESCAPE) {  // mozilla
            escapeKey = event.DOM_VK_ESCAPE;
        } else { // ie
            escapeKey = 27;
        }

        var key = String.fromCharCode(keycode).toLowerCase();
        
        if (key.match(/x|o|c/) || (keycode == escapeKey)){ // close lightbox
            this.end();
        }
    },


	//
	//  end()
	//
	end: function() {
		this.disableKeyboardNav();
		this.lightbox.hide();
		$("lightboxClose").hide();

		new Effect.Fade(this.overlay, { duration: this.overlayDuration });
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
	},

    //
    //  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];
	}
}

var lb;

document.observe('dom:loaded', function () { Lightbox.current = new Lightbox(); } );

