// JavaScript Document

/*
Author: Alex Boyce
Copyright: 2009, 2010, 2011
Date: 3/17/09
Revised: 3/30/11

Creative Commons License
*/

(function($) {
		  
	var $outer = document.createElement('div'),
		$inner = document.createElement('div'),
		$content = document.createElement('div'),
		settings = {}
		oH = 0,
		oW = 0;
		
	function fixIE(inner, outer) {
		if($.browser.msie && parseInt($.browser.version) <= 7) {
			var offset = (oH - $(inner).outerHeight()) / 2;
			$(outer).css({top: $(this).scrollTop() + "px"});
			$(inner).css({top: offset + $(this).scrollTop() + "px"});
			$(window).scroll(function() {
				offset = ($(outer).outerHeight() - $(inner).outerHeight()) / 2;
				$(outer).css({top: $(this).scrollTop() + "px"});
				$(inner).css({top: offset + $(this).scrollTop() + "px"});
				});
			}
		}	
	
	$.lightbox = function(options) {
		var defaultSettings = {
			loadingClass: "loadingBox",
			outerID: "outerbox",
			innerID: "innerbox",
			width: "65%",
			height: "65%",
			url: null,
			content: null,
			close_button_path: null,
			params: {},
			method: 'get'
		};
		oH = ($.browser.msie && parseInt($.browser.version) <= 8 ? $(window).height() : window.outerHeight);
		oW = ($.browser.msie && parseInt($.browser.version) <= 8 ? $(window).width() : window.outerWidth);
		
		settings = $.extend(settings, defaultSettings, options);
		
		settings.close_button_path = settings.close_button_path != null ? '<img src="' + settings.close_button_path + '" alt="Close" border="0" />' : 'Close';
		
		this.create = function() {
			$($inner).html("<div class='close'><a href=\"#\" class=\"lightbox-close\">" + settings.close_button_path + "</a></div>").removeClass(settings.loadingClass);
			$($inner).append($content);
				
			$($inner).attr('id', settings.innerID),
			$($outer).attr('id', settings.outerID),
			$($content).addClass('lightbox-content');
			
			$([$inner, $outer]).appendTo("body").hide();
			$($inner).addClass(settings.loadingClass);
			
			$($content).html("<p>Loading... <a href=\"#\" class=\"lightbox-close\">Cancel</a></p>");
		
			if($.browser.msie && parseInt($.browser.version) <= 7) {
				$($outer).css({position: "absolute", height: oH});
				$($inner).css("position", "absolute");
			}
			
			// Calculate height and widht if given a percentage
			
			if (settings.height[settings.height.length - 1] == "%")
				settings.height = oH * (parseInt(settings.height.substr(0, settings.height.length - 1)) / 100);
				
			if (settings.width[settings.width.length - 1] == "%")
				settings.width = oW * (parseInt(settings.width.substr(0, settings.width.length - 1)) / 100);
				
			if (typeof(settings.width) == "string")
				settings.width = settings.width.match(new RegExp("([0-9]*)"))[1];
				
			if (typeof(settings.height) == "string")
				settings.height = settings.height.match(new RegExp("([0-9]*)"))[1];
			
			// Calculate top margin.
			if (settings.height <= $($outer).outerHeight(true)) {
				margin = ((($($outer).outerHeight(true) - settings.height) - 20) / 2) + "px";
			}
			else {
				margin = "3%";
			}
			
			$($inner).css({
				width: settings.width + "px",
				minHeight: settings.height + "px",
				top: margin,
				left: ((oW - settings.width) / 2) + "px"
				});
			
			if (settings.content == null) {
				ajax = eval('$.' + settings.method);
				ajax(settings.url, settings.params, function(data) {
					$($content).html(data);
					});
			}
			else {
				$($content).html(settings.content);
			}
				
			
			return this;
		}
		
		this.show = function() {
			$($outer).animate({height:'show', opacity: .7}, 'slow');
			$($inner).addClass(settings.loadingClass).animate({height: 'show', opacity: 'show'}, 'slow');
			
			$("a.lightbox-close", $inner).bind("click", function(event) {
				event.preventDefault();
				$($outer).animate({height:'hide', opacity:'hide'}, 'slow');
				$($inner).animate({height:'hide', opacity:'hide'}, 'slow', function() {
					$(this).remove();
					$($outer).remove();
					});
				});
		
			fixIE($inner, $outer);
			
			return this;
		}
		
		return this;
	}
	
		  
	$.fn.lightbox = function(options) {
			
			$(this).click(function(event) {
				event.preventDefault();
				if ((options.content == '' || options.content == null || options.content === "undefined") && (options.url == '' || options.url == null || options.url === "undefined"))
					options.url = $(this).attr("href");
				
				$.lightbox(options).create().show();
				
				});
			
			return this;
		 }
	})(jQuery);
