/**
 * Amara lightbox
 * 
 * Originally created by: Chris Campbell http://particletree.com 2/1/2006
 * 
 * Inspired by the lightbox implementation found at
 * http://www.huddletogether.com/projects/lightbox/
 *
 * It has been rewritten by Ross Motley for jQuery.  I opted for rewriting
 * rather than using colorbox to avoid having to change all of the site code
 * which depends on the lightbox interface.
 */

(function ($, window) {
	/**
	 * Open a lightbox from a link and (optional) href
	 */
	var lightbox = function (ctrl, href) {
		this.initialize(ctrl, href);
	}
	lightbox.isGlobalInit = false;
	lightbox.globalInit = function () {
		if (lightbox.isGlobalInit) {
			return;
		}
		var html = "<div id='overlay'></div><div id='lightbox' class='loading'><div id='lbLoadMessage'><p>Loading</p></div></div>";
		$('body').append(html);

		// Only do this once
		lightbox.isGlobalInit = true;
	}

	lightbox.prototype = {
		yPos : 0,
		xPos : 0,

		initialize: function(ctrl, href) {
			ctrl = $(ctrl);
			var thiss = this;

			if (typeof href != 'undefined') {
				// we have a custom href
				this.content = href;
			} else {
				this.content = ctrl.attr('href');
			}

			this._closeClickObserver = function () {
				thiss.deactivate()
			};

			// Attach click event to href
			ctrl.click(function(e){
				e.preventDefault();
				thiss.activate();
			});
		},

		// Do we need to do ie hacks?
		doIeHacks: function () {
			return ($.browser.msie && $.browser.version < 8);
		},

		// Turn everything on - mainly the IE fixes
		activate: function(){
			lightbox.globalInit();

			if (this.doIeHacks()){
				this.getScroll();
				this.prepareIE('100%', 'hidden');
				this.setScroll(0,0);
				this.hideSelects('hidden');
			}
			this.displayLightbox("block");
		},

		// Ie requires height to 100% and overflow hidden or else you can scroll
		// down past the lightbox
		prepareIE: function(height, overflow){
			var bod = document.getElementsByTagName('body')[0];
			bod.style.height = height;
			bod.style.overflow = overflow;

			var htm = document.getElementsByTagName('html')[0];
			htm.style.height = height;
			htm.style.overflow = overflow;
		},

		// In IE, select elements hover on top of the lightbox
		hideSelects: function(visibility){
			var selects = document.getElementsByTagName('select');
			for(var i = 0; i < selects.length; i++) {
				selects[i].style.visibility = visibility;
			}
		},

		// Taken from lightbox implementation found at
		// http://www.huddletogether.com/projects/lightbox/
		getScroll: function(){
			this.yPos = $(window).scrollTop();
		},

		setScroll: function(x, y){
			window.scrollTo(x, y);
		},

		displayLightbox: function(display){
			if(display != 'none') {
				$('#overlay').show();
				$('#lightbox').show();

				// Add close observer to overlay clicks
				$('#overlay').click(this._closeClickObserver);
				
				this.loadInfo();
			} else {
				$('#overlay').hide();
				$('#lightbox').hide();
			}
		},

		// Begin Ajax request based off of the href of the clicked linked
		loadInfo: function() {
			var url = this.content;
			var thiss = this;

			$.ajax({
				url: url,
				success: function(data) {
					thiss.processInfo(data);
				}
			});
		},

		// Display Ajax response
		processInfo: function(response) {
			var info = "<div id='lbContent'>" + response + "</div>";
			$('#lbLoadMessage').after(info);

			$('#lightbox').removeClass("loading").addClass('done');

			this.actions();
		},

		// Perform "actions" on the newly opened/loaded lightbox content
		actions: function(){
			var thiss = this;

			// Click actions
			$('#lightbox a.lbAction').click(function (event) {
				event.preventDefault();
				var methodName = this.rel;
				thiss[methodName]();
			});

			// Coolform submit actions + focus first element
			$('#lightbox form.lbCoolForm').submit(function (event) {
				event.preventDefault();
				thiss.coolForm(this);
			}).find(':input').first().focus();
		},


		/* Not used
		// Example of creating your own functionality once lightbox is initiated
		insert: function(e){
		   var link = Event.element(e).parentNode;
		   Element.remove($('lbContent'));

		   var myAjax = new Ajax.Request(
				  link.href,
				  {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		   );

		},*/

		// Close lightbox
		deactivate: function(){
			// Remove the lightbox content
			$('#lbContent').remove();

			if (this.doIeHacks()){
				this.setScroll(0,this.yPos);
				this.prepareIE("auto", "auto");
				this.hideSelects("visible");
			}
			this.displayLightbox("none");

			// Detach click observer from overlay
			$('#overlay').unbind('click', this._closeClickObserver);
		},

		// "coolform" was submitted
		coolForm: function(form){
			form = $(form);
			var thiss = this;

			$.ajax({
				url: form.attr('action'),
				dataType: 'json',
				data: form.serialize(),
				success: function (data) {
					thiss.coolFormCallback(data)
				}
			});
		},

		coolFormCallback: function(response){
			this.deactivate();
		}
	}


	/**
	 * Opens up a lightbox from some HTML
	 */
	var lightboxHtml = function () {
		this.initialize();
	};
	lightboxHtml.prototype = new lightbox();
	lightboxHtml.prototype.loadInfo = function () {
		// No info to load, go straight to processing:
		this.processInfo(this.html);
	}
	// open a dialog up with some html
	lightboxHtml.prototype.openDialog = function (html) {
		this.html = html;
		this.activate();
	}


	/**
	 * Working lightbox
	 *
	 * Shows a "working" message when some admin things are being saved
	 */
	var lightboxWorking = function () {
		this.initialize();
	};
	lightboxWorking.prototype = new lightboxHtml();
	// open a dialog up to say that the page is busy
	lightboxWorking.prototype.open = function (html) {
		$('#lightbox').addClass('working');
		this.html = "<h1>Working...</h1>";
		this.activate();
	}
	lightboxWorking.prototype.deactivate = function (html) {
		$('#lightbox').removeClass('working');
		lightbox.prototype.deactivate.apply(this);
	}

	// Put into 'global' scope
	window.lightbox = lightbox;
	window.lightboxHtml = lightboxHtml;
	window.lightboxWorking = lightboxWorking;
})(jQuery, window);
