// -----------------------------------------------------------------------------------
//
//	ajaxbox v1.00
//	by Andy Burton - http://www.bfinternet.co.uk
//	Last Modification: 21/03/08
//
//	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.
//	
//  Thanks: Lokesh Dhakar (http://lokeshdhakar.com/projects/lightbox2/)
// -----------------------------------------------------------------------------------

//
//  Configurationl
//
AjaxLightboxOptions = Object.extend({
    fileLoadingImage:        '/images/loadingAnimation.gif',     
	overlayOpacity: 0.8
}, window.LightboxOptions || {});

// -----------------------------------------------------------------------------------

var AjaxLightbox = Class.create();

AjaxLightbox.prototype = {
	
    // initialize()
    // Constructor runs on completion of the DOM loading.
    //
    initialize: function() {    
        
        this.updateList();
		
		this.overlayDuration	= 0.2;
		
        var objBody = $$('body')[0];
		
		objBody.appendChild(Builder.node('div',{id:'overlay'}));
		
        objBody.appendChild(Builder.node('div',{id:'lightbox'}, [
			Builder.node ('div', {id:'lbContent'},
				Builder.node('div',{id:'loading'}, 
					Builder.node('img', {id:'loadingImage', src: AjaxLightboxOptions.fileLoadingImage})
				)
			)
        ]));
		
		this.overlay	= $('overlay');
		this.lightbox	= $('lightbox');
		
		$('overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('lightbox').hide().observe('click', (function(event) 
		{
			if (event.element().id == 'lightbox' || event.element().id == 'lbLoadMessage' || 
			event.element().id == 'loading' || event.element().id == 'loadingImage')
			{
				this.end();
			}
		}).bind(this));
		
		this.overlay.style.visibility	= 'visible';
		this.lightbox.style.visibility	= 'visible';
		
    },

    //
    // updateImageList()
    // Sets all links with rel attribute of ajax_lightbox to open a lightbox window
    //
    updateList: function() {   
		
		$$('a[rel=ajax_lightbox]', '.lbOn').each (this.linkClick.bind (this));
		
    },
	
	// Set a link event listener
	
	linkClick : function (element)
	{
		Event.observe (element, 'click', this.start.bindAsEventListener (this), false);
		element.onclick = function(){return false;};
	},
	
	//
	// open ()
	// Open a lightbox given a URL
	//
	
	open: function (url)
	{
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		
        // stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        this.overlay.setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		
        new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: AjaxLightboxOptions.overlayOpacity });
		
        // calculate top and left offset for the lightbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
		var arrayViewportScroll = document.viewport.getDimensions();
		
        var lightboxTop = arrayPageScroll[1] + ((arrayViewportScroll.height - this.lightbox.getHeight ()) / 2);
        var lightboxLeft = arrayPageScroll[0] + ((arrayViewportScroll.width - this.lightbox.getWidth ()) / 2);
        this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
		
		new Ajax.Updater ($('lbContent'), url, {
			evalScripts: true,
			onComplete: this.start_finished.bindAsEventListener (this)
		});	
	},
	
	//
	// open_safe ()
	// Open a lightbox given a URL
	// Doesn't be hatin' on the BeYu flash
	//
	
	open_safe: function (url)
	{

		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		
        // stretch overlay to fill page and fade in
        var arrayPageSize = this.getPageSize();
        this.overlay.setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
		
		$('overlay').style.opacity = '0.8';
		new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: AjaxLightboxOptions.overlayOpacity });
		$('overlay').style.display = 'block';
		
        //new Effect.Appear(this.overlay, { duration: this.overlayDuration, from: 0.0, to: AjaxLightboxOptions.overlayOpacity });
		
        // calculate top and left offset for the lightbox 
        var arrayPageScroll = document.viewport.getScrollOffsets();
		var arrayViewportScroll = document.viewport.getDimensions();
		
        var lightboxTop = arrayPageScroll[1] + ((arrayViewportScroll.height - this.lightbox.getHeight ()) / 2);
        var lightboxLeft = arrayPageScroll[0] + ((arrayViewportScroll.width - this.lightbox.getWidth ()) / 2);
        this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
		
		new Ajax.Updater ($('lbContent'), url, {
			evalScripts: true,
			onComplete: this.start_finished.bindAsEventListener (this)
		});	
	},
    
    //
    //  start ()
    //  Display overlay and lightbox
    //
    start: function (e) 
	{
		 this.open (Event.findElement (e,'a').href);
    },
	
	start_finished : function (e)
	{
		
		var holder	= $$('#lbContent .popUpHolder').first ();
		
		if (holder)
		{
			
			var dimensions	= holder.getDimensions ();
			var arrayPageScroll = document.viewport.getScrollOffsets();
			var arrayViewportScroll = document.viewport.getDimensions();
			
			var lightboxTop = arrayPageScroll[1] + ((arrayViewportScroll.height - dimensions.height) / 2);
			this.lightbox.setStyle({ top: lightboxTop + 'px'});
			//var lightboxLeft = arrayPageScroll[0] + ((arrayViewportScroll.width - dimensions.width) / 2);
			//this.lightbox.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();
			
		}
		
		if ($('beyu_launch'))
		{
			
			var arrayPageScroll = document.viewport.getScrollOffsets();
			var arrayViewportScroll = document.viewport.getDimensions();
			
			var lightboxLeft = arrayPageScroll[0] + ((arrayViewportScroll.width - 991) / 2);
			this.lightbox.setStyle({ left: lightboxLeft + 'px'});
			
		}
		
		$('lightbox').addClassName ('done');
		this.actions ();
		
	},
	
    //
    //  end()
    //
    end: function() {
		
        this.lightbox.hide();
		this.lightbox.removeClassName ('done');
		
		$('lbContent').innerHTML	= '';
		$('lbContent').appendChild (
			Builder.node('div',{id:'loading'}, 
				Builder.node('img', {id:'loadingImage', src: AjaxLightboxOptions.fileLoadingImage})
			)
		);
		
       // new Effect.Fade(this.overlay, { duration: this.overlayDuration });
        $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
		this.overlay.hide ();
    },

    //
    //  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];
	},
	
	// Search through new links within the lightbox, and attach click event
	actions: function(){
		actions = document.getElementsByClassName('lbAction');
		
		for(i = 0; i < actions.length; i++) {
			Event.observe(actions[i], 'click', this[actions[i].rel].bindAsEventListener(this), false);
			actions[i].onclick = function(){return false;};
		}

	},
	
	goto: function(url){
		new Ajax.Updater ($('lbContent'), url, {
			evalScripts: true,
			onComplete: this.start_finished.bindAsEventListener (this)		  
		});
	 
	},
    
    postto: function(url, params){
        new Ajax.Updater ($('lbContent'), url, {
            evalScripts: true,
            method: 'post',
            parameters: params,
            onComplete: this.start_finished.bindAsEventListener (this)        
        });
     
    },
	
	deactivate: function(){
		this.end ();
	}
	
}

var ajaxbox	= false;

// Causes IE to crash with the "Operation Aborted" error
//document.observe('dom:loaded', function () { ajaxbox = new AjaxLightbox(); });

Event.observe(window, 'load', function () { ajaxbox = new AjaxLightbox(); });