/*
    Trifecta Lightbox is based loosely on Lightbox JS by Lokesh Dhakar - http://www.huddletogether.com

    Modifications by Eric DeLabar @ Trifecta Technologies, Inc. - http://www.trifecta.com
    - Reformatted as Prototype OOP
    - Added AJAX functionality to load a URL instead of a picture
    - Added form submission functionality

	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave all names and links)
*/

var LightBox = Class.create();
LightBox.prototype = {
	
	initialize: function() {
		
    	this.instances = new Array();
    	this.id = "iFrameId";
    	this.currentIndex = 0;
    	
    	this.parseLightBoxLinks( document );
    },
    
    parseLightBoxLinks: function( parentElement ) {
    	var me = this;
 		var clickHandler = function( href ) {
 			return function(e) {
 				me.setContent( href );
 				Event.stop( e );
 			}
 		};
    	
    	$A(parentElement.getElementsByClassName("lightbox")).each(
    		function( a ) {
    			a.onclick = clickHandler( a.href ).bindAsEventListener( me );
    		}
    	)
    },
    
    getCurrentInstance: function() {
    	return this.instances.last();
    },
    
    getPreviousInstance: function() {
    	return this.instances[this.instances.length - 2];
    },
    
    setContent: function( url, callback ) {
    	this.instances.push( new LightBoxInstance( this.id + this.currentIndex++ ) );
    	
    	var instance = this.getCurrentInstance();
    	instance.setContent( url );
    	if( callback ) {
    		instance.callback = callback;
    	}
    },
    
    show: function() {
    	var instance = this.getCurrentInstance();
    	
    	instance.show();
    	if( instance.callback ) {
    		instance.callback( instance.getIFrameDocument( instance.container ) );
    	}
    },
    
    hide: function() {
    	var current = this.getCurrentInstance();
    	this.instances = this.instances.without( current );
    	this.currentIndex--;
    	if(current) {
    		current.kill();
    	}
    },
    
    message: function( msg ) {
    	alert( msg );
    },
    
    instantiateLinks: function(parentObject) {
    	var me = this;
    	
		$$('a.lightboxLink').each(function(elt) {
			Event.observe(elt, 'click', me.showLinkInBox(elt, parentObject).bindAsEventListener(me));
		});
	},
	
	showLinkInBox: function(elt, parentObject) {
		return function(evt) {
			this.setContent(elt.href);
			this.getCurrentInstance().setObject(parentObject);
			
			evt.stop();
			return false;
		}
	}
}

var LightBoxInstance = Class.create();
LightBoxInstance.prototype = {

    initialize: function( frameId ) {
    	this.initPage( frameId );
    },

    initPage: function( frameId ) {    
    	this.id = frameId;
    
        this.background = Builder.node( 'div', { style: 'position: absolute; display: none; top: 0px; left: 0px; background-color: black; z-index: +99;' }, new Array() );
        this.container = Builder.node( 'iframe', { style: 'position: absolute; display: none; top: 0px; left: 0px; background-color: white; border: solid 1px black; overflow-x: hidden; z-index: +99', id: frameId }, new Array() );
        
        document.body.appendChild( this.background );
        document.body.appendChild( this.container );
    },
    
    setContent: function( url ) {
        this.container.src = url;
    },
    
    setObject: function( object ) {
    	this.object = object;
    },
    
    getObject: function() {
    	return this.object;
    },
    
    show: function() {
   	 	// This function is called from the ajaxForm in an iFrame to show itself.
   	 	// (i.e. window.parent.formLoader.show(); )
   	 	
        var arrayPageSize = this.getPageSize();
        var arrayPageScroll = this.getPageScroll();  
   	    
   	    var doc = this.getIFrameDocument( this.container );
   	    
   	    // Get the form size from the form itself
   	    this.formWidth = parseInt( Element.getStyle( doc.getElementById("lightBoxDiv"), "width" ) );
   	    this.formHeight = parseInt( Element.getStyle( doc.getElementById("lightBoxDiv"), "height" ) );
   	    	   	    
        // set background to take up whole page and show it
        this.background.style.width = ( arrayPageSize[0] + 'px' );
        this.background.style.height = ( arrayPageSize[1] + 'px' );
        Element.setOpacity( this.background, 0.01 );
        this.background.style.display = 'block';
        
        // set the container to be centered and show it
        var calcTop = ( arrayPageScroll[1] + ( ( arrayPageSize[3] - 35 - this.formHeight ) / 2 ) );
        this.container.style.top = calcTop < 0 ? 0 : calcTop + 'px';
	    this.container.style.left = ( ( ( arrayPageSize[0] - 20 - this.formWidth ) / 2 ) + 'px' );
        this.container.style.width = ( this.formWidth + 6 ) + "px";
        this.container.style.height = ( this.formHeight + 25 ) + "px";
        
        // A reference to 'this' needs to be passed to the 
        // afterFinish function because afterFinish exists 
        // in a different enclosure
        var me = this;
        new Effect.Opacity( 
        	this.background, 
        	{ 
        		duration: 0.25, 
        		transition: Effect.Transitions.linear, 
        		from: 0.01, to: 0.50, 
        		afterFinish: function() 
        		{ 
        			me.container.style.display = 'block';
        		} 
        	} 
        );
    },
    
    // Remove all traces of this instance
    kill: function() {
    	this.container.style.display = 'none';
    	var me = this;	
    	new Effect.Fade(
    		this.background,
    		{
    			duration: 0.25,
    			transition: Effect.Transitions.linear, 
    			from: 0.5,
    			to: 0.0,
    			afterFinish: function()
    			{
    				Element.remove( me.container );
    				Element.remove( me.background );
    				delete( me );
    			}
    		}
    	);
    },
    
    //--- Begin Utility Functions ---//
    
    getIFrameDocument: function( iframe ) {
    	if (iframe.contentDocument) {
		    // For NS6
		    return iframe.contentDocument; 
		} else if (iframe.contentWindow) {
		    // For IE5.5 and IE6
		    return iframe.contentWindow.document;
		} else if (iframe.document) {
		    // For IE5
		    return iframe.document;
		} else {
		    return false;
		}
    },
    
    /* getPageScroll()
       Returns array with x,y page scroll values.
       Core code from - quirksmode.org
    */
    getPageScroll: function() {

	    var yScroll;

	    if ( self.pageYOffset ) {
		    yScroll = self.pageYOffset;
	    } else if ( document.documentElement && document.documentElement.scrollTop ){	 // Explorer 6 Strict
		    yScroll = document.documentElement.scrollTop;
	    } else if ( document.body ) {// all other Explorers
		    yScroll = document.body.scrollTop;
	    }

	    arrayPageScroll = new Array('',yScroll) ;
	    return arrayPageScroll;
    },
    
    /* getPageSize()
       Returns array with page width, height and window width, height
       Core code from - quirksmode.org
       Edit for Firefox by pHaez
    */
    getPageSize: 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( pageWidth, pageHeight, windowWidth, windowHeight );
	    return arrayPageSize;
    }
}