// JavaScript Document

/*
Author: Alex Boyce
Copyright: 2009
Date: 3/17/09
Revised: 6/9/09

Creative Commons License
*/

(function($) {
		  
	$.fn.lightbox = function(options) {
			var defaultSettings = {
				outerCSS: {
					position: "fixed",
					top: "0",
					left: "0",
					height: "100%",
					width: "100%",
					backgroundColor: "#999999",
					opacity: .7,
					zIndex: 1000,
					overflowY: "auto"
					},
				innerCSS: {
					backgroundColor: "white", 
					padding: "5px",
					position: "fixed",
					zIndex: 1050
					},
				imageCSS: {},
				loadingClass: "loadingBox",
				outerID: "outerbox",
				innerID: "innerbox",
				width: "65%",
				height: "65%",
				url: null,
				content: null
			},
			settings = $.extend({}, defaultSettings, options),
			$outer = $("<div/>").attr("id", settings.outerID).css(settings.outerCSS).appendTo("body").hide(),
			$inner = $("<div/>").attr("id", settings.innerID).css(settings.innerCSS).appendTo("body").hide();
			$($inner).append("Loading... <a href=\"#\">Cancel</a>");
			
			if($.browser.msie && parseInt($.browser.version) <= 7) {
				$($outer).css({position: "absolute", height: $(window).height() + "px"});
				$($inner).css("position", "absolute");
			}
			
			function fixIE(inner, outer) {
				if($.browser.msie && parseInt($.browser.version) <= 7) {
					var offset = ($(window).height() - $(inner).height()) / 2;
					$(outer).css({top: $(this).scrollTop() + "px"});
					$(inner).css({top: offset + $(this).scrollTop() + "px"});
					$(window).scroll(function() {
						offset = ($(outer).height() - $(inner).height()) / 2;
						$(outer).css({top: $(this).scrollTop() + "px"});
						$(inner).css({top: offset + $(this).scrollTop() + "px"});
						});
					}
				}			
			
			$(this).click(function(event) {
				$($inner).removeClass(settings.loadingClass);
				if (settings.height <= $($outer).height()) {
					margin = ((($($outer).height() - settings.height) - 20) / 2) + "px";
				}
				else {
					margin = "3%";
				}
				
				$($outer).animate({height:'show', opacity:'show'}, 'slow');
				$($inner).addClass(settings.loadingClass).animate({height: 'show', opacity: 'show'}, 'slow');
			
				fixIE($inner, $outer);
				
				if (settings.url == null) {
					$($inner).html("<p align=\"right\"><a href=\"#\" class=\"lightbox-close\">Close</a></p>" + settings.content).css({
					width: settings.width,
					height: settings.height,
					top: margin
					}).css("left", (($($outer).width() - settings.width) / 2) + "px");
					
					$("a.lightbox-close", $inner).bind("click", function(event) {
						$($outer).animate({height:'hide', opacity:'hide'}, 'slow');
						$($inner).animate({height:'hide', opacity:'hide'}, 'slow', function() {
							$(this).html("Loading... <a href=\"#\">Cancel</a>");																
							});
						event.preventDefault();
						});
				}
				else {
					$.get(settings.url, function(data, status) {
						if(status == "success") {
							$($inner).html("<p align=\"right\"><a href=\"#\" class=\"lightbox-close\">Close</a></p>" + data).css({width: settings.width + "px", height: settings.height + "px", top: margin, left: (($($outer).width() - settings.width) / 2) + "px"});						
						}
				
						$("a.lightbox-close", $inner).bind("click", function(event) {
							$($outer).animate({height:'hide', opacity:'hide'}, 'slow');
							$($inner).animate({height:'hide', opacity:'hide'}, 'slow', function() {
								$(this).html("Loading... <a href=\"#\">Cancel</a>");																
								});
							event.preventDefault();
							});
						});
				}

				fixIE($inner, $outer);
					
				event.preventDefault();
				});
			
			return this;
		 }
	})(jQuery);