
function Lightbox() {
	this.containerElem = null;
}

Lightbox.prototype.showUrl = function(url, options) {
	options = $.extend(options, {
		url: url
	});
	this.show(options);
}

Lightbox.prototype.show = function(options) {
	log('show()');
	
	var lightbox = this;
	
	this.close();
	
	this.options = $.extend({
		content: null,
		url: null,
		elem: null,
		closeOnClickOutside: true,
		width:'50%',
		className: null
	}, options);
	
	log('  this.options.content: ' + this.options.content);
	log('  this.options.url: ' + this.options.url);
	log('  this.options.elem: ' + this.options.elem);

	this.backgroundElem = $('<div id="lightbox-background"></div>').appendTo('body');
	this.backgroundElem.click(function(e) {
		return lightbox.onClickBackground(e);
	});
	
	this.containerElem = $('<div id="lightbox-container"></div>').appendTo('body');
	if (this.options.className) {
		this.containerElem.addClass(this.options.className);
	}
	this.containerElem.css('width', this.options.width);
	this.containerElem.click(function(e) {
		return lightbox.onClickContainer(e);
	});
	
	this.contentElem = $('<div id="lightbox-content"></div>').appendTo(this.containerElem);
	
	var shadow_ul = $('<div id="lightbox-shadow-ul" />').appendTo(this.containerElem);
	var shadow_ur = $('<div id="lightbox-shadow-ur" />').appendTo(this.containerElem);
	var shadow_u = $('<div id="lightbox-shadow-u" />').appendTo(this.containerElem);
	
	var shadow_bl = $('<div id="lightbox-shadow-bl" />').appendTo(this.containerElem);
	var shadow_br = $('<div id="lightbox-shadow-br" />').appendTo(this.containerElem);
	var shadow_b = $('<div id="lightbox-shadow-b" />').appendTo(this.containerElem);
	
	var shadow_l = $('<div id="lightbox-shadow-l" />').appendTo(this.containerElem);
	var shadow_r = $('<div id="lightbox-shadow-r" />').appendTo(this.containerElem);
	
	if (this.options.content) {
		// Show content.
		this.contentElem.html(this.options.content);
		this.reposition();
		this.captureForms();
		
	} else if (this.options.url) {
		// Load a URL.
		this.contentElem.html('<img src="/media/images/ajax-loader-circles.gif" style="display:block; margin:0 auto;" />');
		lightbox.reposition();
		
		$.ajax({
			url: this.options.url,
			dataType: 'html',
			success: function(data) {
				lightbox.contentElem.html(data);
				lightbox.reposition();
				lightbox.captureForms();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				lightbox.contentElem.html(XMLHttpRequest.responseText);
			}
		});
		
	} else if (this.options.elem) {
		// Copy another element's HTML.
		var elem = $(this.options.elem);
		
		// Copy over all classes from the element.
		var classes = elem.attr('class').split(' ');
		for (var i=0; i<classes.length; i++) {
			this.contentElem.addClass(classes[i]);
		}
		
		this.contentElem.html(elem.html());
		
		this.reposition();
		this.captureForms();
	}
}

Lightbox.prototype.captureForms = function() {
	log('captureForms()');
	var lightbox = this;
	var forms = this.contentElem.find('form');
	log('  found ' + forms.length + ' forms.');
	for (var i=0; i<forms.length; i++) {
		var form = $(forms[i]);
		form.submit(function(e) {
			// Submit the form via AJAX, capture response.
			e.preventDefault();
			
			log('Submitting form via ajax.');
			log('  action: ' + form.attr('action'));
			log('  data: ' + form.serialize());
			$.ajax({
				url: form.attr('action'),
				data: form.serialize(),
				type: 'post',
				dataType: 'html',
				success: function(data) {
					// Show the form processor's response in this lightbox.
					log('Got form response, displaying in lightbox.');
					if (/^redirect (.+)$/.test(data)) {
						// Redirect in lightbox, grab the new URL.
						var matches = /^redirect (.+)$/.exec(data);
						log('  redirecting to url "' + matches[1] + '" in lightbox.');
						lightbox.show({ url: matches[1] });
					} else if (/^redirect_page (.+)$/.test(data)) {
						// Redirect entire page.
						var matches = /^redirect_page (.+)$/.exec(data);
						log('  redirecting page to url "' + matches[1] + '".');
						window.location = matches[1];
					} else {
						// Normal HTML content.
						lightbox.show({	content: data });
					}
				},
				error: function() {
					log('Got form response, error.');
					alert('Error: ajax failed when submitting lightbox form.');
				}
			});
		});
	}

	// Make lightbox links clickable.
	attachLightboxClick(this.contentElem.find('a.lightbox, a.lightbox-internal'));
}

Lightbox.prototype.close = function() {
	if (this.backgroundElem) {
		this.backgroundElem.remove();
		this.backgroundElem = null;
	}
	if (this.containerElem) {
		this.containerElem.remove();
		this.containerElem = null;
	}
}

Lightbox.prototype.onClickBackground = function(e) {
	if (this.options.closeOnClickOutside) {
		this.close();
	}
}

Lightbox.prototype.onClickContainer = function(e) {
}

Lightbox.prototype.onClickContent = function(e) {
	log('onClickContent()');
	e.stopPropagation();
}

Lightbox.prototype.reposition = function() {
	log('reposition()');
	if (this.containerElem) {
		var width = this.containerElem.attr('offsetWidth');
		this.containerElem.css('marginLeft', -width/2);
		
		var height = this.containerElem.attr('offsetHeight');
		
		
		var halfHeight = height/2;
		if ( halfHeight > $(window).height()/2) {
		// Make sure the lightbox doesn't get positioned above the top of the browser client area.
			halfHeight = $(window).height()/2;
		}
		this.containerElem.css('marginTop', -halfHeight);
		log('height: ' + height);
	}
}

function attachLightboxClick(elems) {
	elems = $(elems);
	elems.each(function() {
		$(this).click(function(e) {
			e.preventDefault();
			
			log('attachLightboxClick.click()');
			log('  this.href: ' + this.href);
			log("  $(this).attr('href'): " + $(this).attr('href'));
			var options = $(this).metadata();
			if ($(this).attr('href').substr(0, 1) == '#') {
				// Elem ID.
				options.elem = $(this).attr('href');
				log('  options.elem: ' + options.elem);
				lightbox.show(options);
			} else {
				// URL.
				options.url = $(this).attr('href');
				log('  options.url: ' + options.url);
				lightbox.show(options);
			}
			return false;
		});
	});
}

var lightbox = new Lightbox();

$(function() {
	$(window).resize(function() {
		lightbox.reposition();
	});
	
	attachLightboxClick($('a.lightbox'));
});
