// lightWindow.js v1.2.1

//

// Copyright (c) 2007 Einstein Industries

// Author: Kevin P Miller | http://www.stickmanlabs.com

// 

// LightWindow is freely distributable under the terms of an MIT-style license.

//

// I don't care what you think about the file size...

//   Be a pro: 

//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast

//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files

//



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



if(typeof Effect == 'undefined')

  throw("lightWindow.js requires including script.aculo.us' effects.js library!");



var lightWindow = Class.create();	

lightWindow.prototype = {

	//

	//	Setup Variables

	//

	element : null,

	contentToFetch : null,

	boxOverFlow : 'hidden',

	retroIE : null,

	windowType : null,

	animating : false,

	scrollX : null,

	scrollY : null,

	imageArray : [],

	preloadImage : null,

	activeGallery : null,

	activeImage : 0,

	galleryDirection : null,

	showDataToggle : false,

	galleryToggle : false,

	showTitleToggle : false,

	//

	//	Initialize the lightWindow.

	//

	initialize : function(options) {

		this.options = Object.extend({

			resizeSpeed : 9,

			cushion : 10,

			dimensions : {

				image : {height : 250, width : 250},

				page : {height : 100, width : 500},

				inline : {height : 250, width : 500},

				media : {height : 250, width : 250},

				external : {height : 250, width : 250},

				dataHeight : 40,

				titleHeight : 25

			},

			classNames : {	

				standard : 'lWOn',

				action : 'lWAction'

			},

			fileTypes : {

				page : ['asp', 'aspx', 'cgi', 'htm', 'html', 'pl', 'php4', 'php3', 'php', 'php5', 'phtml', 'rhtml', 'shtml', 'txt', 'vbs', 'rb'],

				media : ['aif', 'aiff', 'asf', 'avi', 'divx', 'm1v', 'm2a', 'm2v', 'm3u', 'mid', 'midi', 'mov', 'moov', 'movie', 'mp2', 'mp3', 'mpa', 'mpa', 'mpe', 'mpeg', 'mpg', 'mpg', 'mpga', 'pps', 'qt', 'rm', 'ram', 'swf', 'viv', 'vivo', 'wav'],

				image : ['bmp', 'gif', 'jpg', 'png', 'tiff']

			},

			mimeTypes : {

				avi : 'video/avi',

				aif : 'audio/aiff',

				aiff : 'audio/aiff',

				gif : 'image/gif',

				bmp : 'image/bmp',

				jpeg : 'image/jpeg',

				m1v : 'video/mpeg',

				m2a : 'audio/mpeg',

				m2v : 'video/mpeg',

				m3u : 'audio/x-mpequrl',

				mid : 'audio/x-midi',

				midi : 'audio/x-midi',

				mjpg : 'video/x-motion-jpeg',

				moov : 'video/quicktime',

				mov : 'video/quicktime',

				movie : 'video/x-sgi-movie',

				mp2 : 'audio/mpeg',

				mp3 : 'audio/mpeg3',

				mpa : 'audio/mpeg',

				mpa : 'video/mpeg',

				mpe : 'video/mpeg',

				mpeg : 'video/mpeg',

				mpg : 'audio/mpeg',

				mpg : 'video/mpeg',

				mpga : 'audio/mpeg',

				pdf : 'application/pdf',

				png : 'image/png',

				pps : 'application/mspowerpoint',

				qt : 'video/quicktime',

				ram : 'audio/x-pn-realaudio-plugin',

				rm : 'application/vnd.rn-realmedia',

				swf	: 'application/x-shockwave-flash',

				tiff : 'image/tiff',

				viv : 'video/vivo',

				vivo : 'video/vivo',

				wav : 'audio/wav'				

			},			

			loadingDialog : {

				message : 'Loading',

				image :  'images/ajax-loading.gif',

				options : '<a onclick="javascript: mylightWindow.deactivate();">Cancel</a>',

				delay : 3.0

			},

			authorLead : 'by ',

			galleryTab : {

				name : 'Galleries',

				height : 20,

				visible : true

			},

			overlay : {

				color : '#000000',

				opacity : 70,

				image : 'images/black-70.png'

			},

			formMethod : 'get',

			hideFlash : true,

			showTitleBar : true

		}, options || {})

		this.duration = ((11-this.options.resizeSpeed)*0.15);

		this.setupLinks();

		this.addLightWindowMarkup(false);

		this.setupDimensions(true);

	},

	// 

	//  Set Links Up

	//

	setupLinks : function () {

		var links = $$('.'+this.options.classNames.standard);

		links.each(function(link) {

			if (this.fileType(link.href) == 'image') {

				if (gallery = this.getGalleryInfo(link.rel)) {

					if (!this.imageArray[gallery[0]]) this.imageArray[gallery[0]] = new Array();

					if (!this.imageArray[gallery[0]][gallery[1]]) this.imageArray[gallery[0]][gallery[1]] = new Array();

					this.imageArray[gallery[0]][gallery[1]].push(new Array(link.href, link.getAttribute('title'), link.getAttribute('caption'), link.getAttribute('author'), link.getAttribute('rel'), link.getAttribute('params')));

				}

			}

			var url = link.getAttribute('href');

			if (link.href.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));

			container = url.substring(url.indexOf('#')+1);

			if($(container)) $(container).style.display = 'none';

			Event.observe(link, 'click', this.activate.bindAsEventListener(this, link));

			link.onclick = function() {return false;};

		}.bind(this));	

	},

	//

	//  Initialize specific window

	//

	initializeWindow : function (id) {

		var link = $(id);

		if (this.fileType(link.href) == 'image') {

			if (gallery = this.getGalleryInfo(link.rel)) {

				if (!this.imageArray[gallery[0]]) this.imageArray[gallery[0]] = new Array();

				if (!this.imageArray[gallery[0]][gallery[1]]) this.imageArray[gallery[0]][gallery[1]] = new Array();

				this.imageArray[gallery[0]][gallery[1]].push(new Array(link.href, link.getAttribute('title'), link.getAttribute('caption'), link.getAttribute('author'), link.getAttribute('rel'), link.getAttribute('params')));

			}

		}

		var url = link.getAttribute('href');

		if (link.href.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));

		container = url.substring(url.indexOf('#')+1);

		if($(container)) $(container).style.display = 'none';

		Event.observe(link, 'click', this.activate.bindAsEventListener(this, link));

		link.onclick = function() {return false;};

	},

	//

	//	Add the markup to the page.

	//

	addLightWindowMarkup : function(rebuild) {

	    if (!rebuild) {

			var overlay = document.createElement('div');

			overlay.setAttribute('id', 'overlay');

			if (this.checkBrowser('firefox')) {

				overlay.style.backgroundImage = 'url('+this.options.overlay.image+')';

			   	overlay.style.backgroundRepeat = 'repeat';

			} else {

				overlay.style.backgroundColor = this.options.overlay.color;

			    overlay.style.MozOpacity = '.'+this.options.overlay.opacity;

			    overlay.style.opacity = '.'+this.options.overlay.opacity;

			    overlay.style.filter = 'alpha(opacity='+this.options.overlay.opacity+')';

			}

			var lw = document.createElement('div');

			lw.setAttribute('id', 'lightWindow');

		} else {

			var lw = $('lightWindow');

		}



		if (this.options.showTitleBar) lw = this.addTitleBarMarkup(lw);

				

		var lwc = document.createElement('div');

		lwc.setAttribute('id', 'lightWindow-contents');

		

		var lwcc = document.createElement('div');

		lwcc.setAttribute('id', 'lightWindow-contents-container');

		lwc.appendChild(lwcc);						

						

		var lwl = document.createElement('div');

		lwl.setAttribute('id', 'lightWindow-loading');



		var lwi = document.createElement('img');

		lwi.setAttribute('src', this.options.loadingDialog.image);

		lwl.appendChild(lwi);



		var lwld = document.createElement('span');

		lwld.setAttribute('id', 'lightWindow-loading-message');

		lwld.innerHTML += this.options.loadingDialog.message;

		lwl.appendChild(lwld);

		

		var lwlo = document.createElement('span');

		lwlo.setAttribute('id', 'lightWindow-loading-options');

		lwlo.setAttribute('style', 'display:none;');

		lwlo.innerHTML += this.options.loadingDialog.options;

		lwl.appendChild(lwlo);

		

		lwc.appendChild(lwl);

		

		lw.appendChild(lwc);

		

		if (!rebuild) {

			var body = document.getElementsByTagName('body')[0];

			body.appendChild(overlay);

			body.appendChild(lw);	

			Event.observe(overlay, 'click', this.deactivate.bindAsEventListener(this), false);

			overlay.onclick = function() {return false;};

		}

		this.addDataWindowMarkup();

		this.actions('#lightWindow-loading-options');

	},

	//

	//	Add the Title Bar Markup

	//

	addTitleBarMarkup : function(lw) {

	

		var lwdt = document.createElement('div');

		lwdt.setAttribute('id', 'lightWindow-title-bar');

		lwdt.style.visibility = 'hidden';



		var lwdtt = document.createElement('div');

		lwdtt.setAttribute('id', 'lightWindow-title-bar-title');

		lwdt.appendChild(lwdtt);

				

		var lwdtc = document.createElement('div');

		lwdtc.setAttribute('id', 'lightWindow-title-bar-close');





		var lwdtca = document.createElement('a');

		lwdtca.setAttribute('id', 'lightWindow-title-bar-close-link');

		lwdtca.innerHTML = 'close';

		Event.observe(lwdtca, 'click', this.deactivate.bindAsEventListener(this));

		lwdtca.onclick = function() {return false;};

		lwdtc.appendChild(lwdtca);

		lwdt.appendChild(lwdtc);

		

		lw.appendChild(lwdt);

		return lw;

		

	},

	//

	//	Add the Data Window Markup

	//

	addDataWindowMarkup : function() {

		var lw = $('lightWindow');

		

		var lwd = document.createElement('div');

		lwd.setAttribute('id', 'lightWindow-data');	

		lwd.style.display = 'none';



		// This container needs to be here to get the data slide to slide as a group

		var lwds = document.createElement('div');

		lwds.setAttribute('id', 'lightWindow-data-slide');

		

		if (!this.options.showTitleBar) {

			var lwdt = document.createElement('div');

			lwdt.setAttribute('id', 'lightWindow-data-title');	

			lwds.appendChild(lwdt);

		}

		

		var lwdc = document.createElement('div');

		lwdc.setAttribute('id', 'lightWindow-data-caption');	

		lwds.appendChild(lwdc);

				

		var lwda = document.createElement('div');

		lwda.setAttribute('id', 'lightWindow-data-author');	

		lwds.appendChild(lwda);

				

		var lwdi = document.createElement('div');

		lwdi.setAttribute('id', 'lightWindow-data-image');	

		lwds.appendChild(lwdi);



		lwd.appendChild(lwds);

		lw.appendChild(lwd);

	},

	//

	//	Add Photo Window Markup

	//

	addPhotoWindowMarkup : function() {

		var lwc = $('lightWindow-contents');

		

		var lwpc = document.createElement('div');

		lwpc.setAttribute('id', 'lightWindow-photo-container');

		lwpc.style.display = 'none';

		

		if (images = parseInt(this.getParameter('lWShowImages'))) {

			for (var x = 0; x < images; x++) {

				lwp = document.createElement('img');

	    		lwp.setAttribute('id', 'lightWindow-photo-'+x);

	    		lwpc.appendChild(lwp);

			}

		} else {

			lwp = document.createElement('img');

    		lwp.setAttribute('id', 'lightWindow-photo-0');

    		lwpc.appendChild(lwp);

		}

		

		// You ask why I do this?  I ask why you insist on using a browser worse than IE? ...Safari!

		lwps = document.createElement('img');

		lwps.setAttribute('id', 'lightWindow-photo-sizer');

		lwps.style.display = 'none';

		lwps.style.height = '1px';	

		lwpc.appendChild(lwps);



    	lwc.appendChild(lwpc);

	},

	//

	//	Add Gallery Window Markup

	//

	addGalleryWindowMarkup : function() {

		var lwpc = $('lightWindow-photo-container');



		var lwpg = document.createElement('div');

		lwpg.setAttribute('id', 'lightWindow-photo-galleries');

		lwpg.style.display = 'none';

		if (!this.options.galleryTab.visible) lwpg.style.visibility = 'hidden';

		

		var lwptc = document.createElement('div');

		lwptc.setAttribute('id', 'lightWindow-photo-tab-container');

		

		var lwpgt = document.createElement('a');

		lwpgt.setAttribute('id', 'lightWindow-photo-galleries-tab');

		lwpgt.className = 'up';

		lwpgt.innerHTML = this.options.galleryTab.name;

		Event.observe(lwpgt, 'click', this.getGallery.bindAsEventListener(this));

		lwpgt.onclick = function() {return false;};

		

		lwptc.appendChild(lwpgt);

		lwpg.appendChild(lwptc);

						

		var lwpgl = document.createElement('div');

		lwpgl.setAttribute('id', 'lightWindow-photo-galleries-list');	

		lwpg.appendChild(lwpgl);



    	lwpc.appendChild(lwpg);

	},

	//

	//	Activate the lightWindow.

	//

	activate : function(e, link){

		link.blur();

		this.element = link;

		this.element.title = link.getAttribute('title');

		this.element.author = link.getAttribute('author');

		this.element.caption = link.getAttribute('caption');

		this.element.rel = link.getAttribute('rel');

		this.element.params = this.element.getAttribute('params');

		this.windowType = this.fileType(this.contentToFetch = link.href);

		if (this.element.caption || this.element.author) this.showDataToggle = true;

		if (this.options.showTitleBar && this.element.title) this.showTitleToggle = true;

		else if (!this.options.showTitleBar && this.element.title) this.showDataToggle = true;

		if (this.getGalleryInfo(this.element.rel)) this.galleryToggle = true;

		this.prepareIE(true);

		this.toggleTroubleElements('hidden', false);

		this.displayLightWindow(true);

		this.setupDimensions(true);

		this.monitorKeyboard(true);	

		this.loadInfo();	

	},

	//

	//	Turn off the window

	//

	deactivate : function(){

		var queue = Effect.Queues.get('lightWindowAnimation').each(function(e) {e.cancel();});

		queue = Effect.Queues.get('lightWindowAnimation-loading').each(function(e) {e.cancel();});

		if ($('lightWindow-iframe')) Element.remove($('lightWindow-iframe'));

		Element.remove($('lightWindow-contents'));

		if ($('lightWindow-data')) Element.remove($('lightWindow-data'));

		if ($('lightWindow-title-bar')) Element.remove($('lightWindow-title-bar'));

		this.displayLightWindow(false);	

		this.boxOverFlow = 'hidden';

		this.prepareIE(false);

		this.setStatus(false);

		this.showDataToggle = this.galleryToggle = this.showTitleToggle = false;

		this.addLightWindowMarkup(true);

		this.setupDimensions(true);

		this.monitorKeyboard(false);

		this.toggleTroubleElements('visible', false);		

	},

	//

	//	Setup our actions

	//

	actions : function(prefix) {

		if (prefix) links = $$(prefix+' .'+this.options.classNames.action);

		else links = $$('.'+this.options.classNames.action);

		links.each(function(link) {

			Event.observe(link, 'click', this[link.rel].bindAsEventListener(this, link), false);

			link.onclick = function() {return false;};

		}.bind(this));

	},

	//

	//	Set the staus of our animation to keep things from getting clunky

	//

	setStatus : function(status) {

		this.animating = status;

		// We have to put this here to avoid a flicker in FF Mac

		if (this.showTitleToggle && !status && $('lightWindow-title-bar')) {

			$('lightWindow-title-bar').setStyle({ 

				visibility : 'visible'

			});

		}

	},

	//

	//	Setup Dimensions of lightWindow.

	//

	setupDataDimensions : function() {

		if ($('lightWindow-contents') && $('lightWindow-data') && this.showDataToggle) {

			$('lightWindow-data').setStyle({

				height : this.options.dimensions.dataHeight+'px',

		  		width : (parseFloat($('lightWindow-contents').style.width)+this.options.cushion*2)+'px'

			});

			$('lightWindow-data-slide').setStyle({

				height : this.options.dimensions.dataHeight+'px',

		  		overflow : 'hidden' // Because of IE

			});

		}	

		if (this.showTitleToggle && $('lightWindow-title-bar')) {

			$('lightWindow-title-bar').setStyle({

				height : this.options.dimensions.titleHeight+'px',

	  			width : (parseFloat($('lightWindow-contents').style.width)+this.options.cushion*2)+'px'

			});

		}

	},

	//

	//	Setup Dimensions of lightWindow.

	//

	setupDimensions : function(reset) {

		if (this.showDataToggle || (this.galleryToggle && this.options.galleryTab.visible)) var adjust = this.options.dimensions.dataHeight;

		else var adjust = 0;



		var originalHeight, originalWidth, titleHeight;

		switch (this.windowType) {

			case 'page' :

				originalHeight = this.options.dimensions.page.height;

				originalWidth = this.options.dimensions.page.width;

				break;



			case 'image' :

				originalHeight = this.options.dimensions.image.height;

				originalWidth = this.options.dimensions.image.width;

				break;

				

			case 'media' :

				originalHeight = this.options.dimensions.media.height;

				originalWidth = this.options.dimensions.media.width;

				break;

			

			case 'external' : 

				originalHeight = this.options.dimensions.external.height;

				originalWidth = this.options.dimensions.external.width;

				break;

				

			case 'inline' :

				originalHeight = this.options.dimensions.inline.height;

				originalWidth = this.options.dimensions.inline.width;

	.options.dimensions.dataHeight+'px',

		  		overflow : 'hidden' // Because of IE

			});

		}	

		if (this.showTitleToggle && $('lightWindow-title-bar')) {

			$('lightWindow-title-bar').setStyle({

				height : this.options.dimensions.titleHeight+'px',

	  			width : (parseFloat($('lightWindow-contents').style.width)+this.options.cushion*2)+'px'

			});

		}

	},

	//

	//	Setup Dimensions of lightWindow.

	//

	setupDimensions : function(reset) {

		if (this.showDataToggle || (this.galleryToggle && this.options.galleryTab.visible)) var adjust = this.options.dimensions.dataHeight;

		else var adjust = 0;



		var originalHeight, originalWidth, titleHeight;

		switch (this.windowType) {

			case 'page' :

				originalHeight = this.options.dimensions.page.height;

				originalWidth = this.options.dimensions.page.width;

				break;



			case 'image' :

				originalHeight = this.options.dimensions.image.height;

				originalWidth = this.options.dimensions.image.width;

				break;

				

			case 'media' :

				originalHeight = this.options.dimensions.media.height;

				originalWidth = this.options.dimensions.media.width;


