// JavaScript Document

/*
Author: Alex Boyce
Copyright: 2009
Date: 3/17/09
Revised: 6/9/09

Creative Commons License
*/

(function($) { 
	$.expr[':'].isImage = function(e, i, m) {
		return ($(e).is('img') && e.src && (/\.(gif|jpe?g|png|bmp|GIF|JPE?G|PNG|BMP)$/).test(e.src));
	}
		  
	$.fn.gallery = 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",
				preload: false
			},
			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"});
						});
					}
				}
			
			function getImage(img) {
				var thumb_arr = img.split("/"),
				max_thumb = thumb_arr.length,
				big_img = "";
				
				for (j=0; j < max_thumb; j++) {
					if (j != max_thumb - 2) {
						big_img += thumb_arr[j];
						if (j < max_thumb - 1) {
							big_img += "/";
						}
					}
				}
				
				return big_img;
			}
	
			
			if (settings.preload) {
					$(this).children("a img:isImage").each(function(i) {
						var thumb = $(this).attr("src"),
						big_image = getImage(thumb);					
						
						var newImage = new Image();
						newImage.src = big_image;
						newImage.load;
					});
			}
			
			$("a", $inner).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();
			});
			
			
			$("a", this).click(function(event) {
				if ($("img", this).is(":isImage")) {
					var thumb = $("img", this).attr("src"),
					big_image = getImage(thumb);
					
					var largeImage = new Image;
					largeImage.width = 700;
					largeImage.height = 525;
					$($inner).add(largeImage);
					largeImage.src = big_image;
					
					largeImage.onerror = function() {
						$($inner).html("ERROR LOADING IMAGE <a href=\"#\">Close</a>");
					}
					
					$(largeImage).ready(function() {
						$($inner).removeClass(settings.loadingClass);
						if ($($inner).height <= $($outer).height()) {
							margin = ((($($outer).height() - $($inner).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);
						
						$($inner).html("<a href=\"#\"><img src=\"" + largeImage.src + "\" width=\"" + largeImage.width + "\" height=\"" + largeImage.height + "\" border=\"0\"></a><p align=\"center\"><a href=\"#\">Close</a>").css({
							width: largeImage.width + "px",
							height: largeImage.height + 35 + "px",
							top: margin
							}).css("left", (($($outer).width() - $($inner).width()) / 2) + "px");
						
						//$($outer).animate({height:'show', opacity:'show'}, 'slow');
						//$($inner).addClass(settings.loadingClass).animate({height: 'show', opacity: 'show'}, 'slow');
					
						fixIE($inner, $outer);
						
						$("a", $inner).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();
							});
					});
					
					
					largeImage.onload = function () {
						largeImage.width = this.width;
						largeImage.height = this.height;
					}
					
					event.preventDefault();
				}
			});
			
			return this;
		 }
	})(jQuery);