// -----------------------------------------------------------------------------------
//
//
// -----------------------------------------------------------------------------------
/*

    Table of Contents
    -----------------
    Configuration

    Lightbox Class Declaration
    - initialize()
    - end()

    Function Calls
    - document.observe()

*/
// -----------------------------------------------------------------------------------

//
//  Configuration
//
LightframeOptions = Object.extend({
	closeImg: '/images/lightbox/closelabel.gif',
	overlayOpacity: 0.8,   // controls transparency of shadow overlay
	animate: true,         // toggles resizing animations
	resizeSpeed: 7,        // controls the speed of the image resizing animations (1=slowest and 10=fastest)
	xScale: 904,
	yScale: 604

}, window.LightframeOptions || {});

// -----------------------------------------------------------------------------------

var Lightframe = Class.create();

Lightframe.prototype = {

	// initialize()
	//
	initialize: function() {

		this.updateFrameList();

		if (LightframeOptions.resizeSpeed > 10) LightframeOptions.resizeSpeed = 10;
		if (LightframeOptions.resizeSpeed < 1)  LightframeOptions.resizeSpeed = 1;

		this.resizeDuration = LightframeOptions.animate ? ((11 - LightframeOptions.resizeSpeed) * 0.15) : 0;
		this.overlayDuration = LightframeOptions.animate ? 0.2 : 0;  // shadow fade in/out duration

		var fcHeight = 604 + 'px';
		var fcWidth = 904 + 'px';
		//
		// Code inserts html at the bottom of the page that looks similar to this:
		//
		//<div id="overlay"></div>
		//<div id="lightframe">
		//	<div id="FrameContainer">
		//		<a id="lightframeClose"><img src="close.gif"></a>
		//		<iframe id="Frame"></iframe>
		//	</div>
		//</div>
		//
		//

		var objBody = $$('body')[0];

		objBody.appendChild(Builder.node('div',{id:'lf_overlay'}));

		objBody.appendChild(Builder.node('div',{id:'lightframe'}, [
			Builder.node('div',{id:'FrameContainer'}, [
				Builder.node('a',{id:'lightframeClose', href: '#' },
					Builder.node('img', { src: LightframeOptions.closeImg })
				),
				Builder.node('iframe',{id:'lightframeIFrame'})
			])
		]));


		$('lf_overlay').hide().observe('click', (function() { this.end(); }).bind(this));
		$('lightframeClose').observe('click', (function(event) { event.stop(); this.end(); }).bind(this));
		$('lightframe').hide().observe('click', (function(event) { if (event.element().id == 'lightframe') this.end(); }).bind(this));
		$('FrameContainer').setStyle({ height: fcHeight , width: fcWidth });
		$('lightframeIFrame').style.visibility = 'hidden';
		$('lightframeClose').style.visibility = 'hidden';

		var th = this;
		(function(){
			var ids =
			'lf_overlay lightframe FrameContainer';
			$w(ids).each(function(id){ th[id] = $(id); });
		}).defer();
	},

	start: function(frameLink) {

		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });

		// stretch overlay to fill page and fade in
		var arrayPageSize = this.getPageSize();
		$('lf_overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });

		new Effect.Appear('lf_overlay', { duration: this.overlayDuration, from: 0.0, to: LightframeOptions.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];
		this.lightframe.setStyle({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();

		Effect.BlindDown('FrameContainer', { duration:1.0 ,
			afterFinish: function () {
				$('lightframeIFrame').setStyle({ width: LightframeOptions.xScale + 'px' , height: LightframeOptions.yScale + 'px' } );
				$('lightframeIFrame').src = frameLink.href;
				$('lightframeClose').style.visibility = 'visible';
				$('lightframeIFrame').style.visibility = 'visible';
			}
		});

	},

	//
	// updateFrameList()
	//
	updateFrameList: function() {
		this.updateFrameList = Prototype.emptyFunction;

		document.observe('click', (function(event){
		var target = event.findElement('a[rel^=lightframe]') || event.findElement('area[rel^=lightframe]');
		if (target) {
			event.stop();
			this.start(target);
		}
		}).bind(this));
	},

    //
    //  end()
    //
	end: function() {
		$('lightframeIFrame').style.visibility = 'hidden';
		$('lightframeClose').style.visibility = 'hidden';
		Effect.BlindUp( 'FrameContainer' , { duration: 1.0, afterFinish: function() { Effect.Fade( 'lf_overlay', { from: 0.7, to: 0 , duration: 0.5 } ) } } );
		$$('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,windowWidth,windowHeight];
	}


}

document.observe('dom:loaded', function () { new Lightframe(); });
