(function ($) {
	$.fn.nickoverlay = function (options) {
		
		return this.each(function () {

			var img = $("<img />");
			var nextimg = $("<img />");
			var previmg = $("<img />");
			var gallery = $("li");
			var imgheight = 553;
			var imgratio = 0;
			var imgwidth = 0;
		
			var overlay = $("<div />").css({
				position: "absolute",
				height: parseInt($(document).height())-parseInt($("#container").offset().top),
				background: "#FFF",
				margin: 0,
				padding: 0,
				top: $("#container").offset().top,
				width: "100%",
				left: 0,
				opacity: 0
			});
			
			
			
			var imgcontainer = $("<div />").css({
				position: "absolute",
				display: "none"
			});

			var caption = $("<div />").css({
				position: "absolute",
				display: "none",
				bottom: 40,
				height: 80,
				width: 150,
				right: "-170px",
				textAlign: "left"
			});
			
			var links = $("<div />").css({
				position: "absolute",
				display: "none",
				height: 14,
				bottom: 0,
				width: 200,
				marginLeft: -100,
				left: "50%",
				textAlign: "center",
				textTransform: "uppercase"
			});
			
			var next = $("<a />").css({
				position: "absolute",
				display: "none",
				background: "url(/lookbook/lookbook_arrows.png) no-repeat right 5px",
				textIndent: -1000,
				outline: "none",
				height: 18,
				width: 20,
				bottom: 0,
				right: 0,
				zIndex: 10000
			}).attr("href", "#").text("Next");
			
			var prev = $("<a />").css({
				position: "absolute",
				display: "none",
				background: "url(/lookbook/lookbook_arrows.png) no-repeat left 5px",
				textIndent: -1000,
				outline: "none",
				height: 18,
				width: 20,
				bottom: 0,
				left: 0,
				zIndex: 10000
			}).attr("href", "#").text("Previous");
			
			
			arrows = function() {
				$(next).clone().appendTo("#img_container").fadeIn(500).click(function() {
					$(".next").trigger("mousedown");
					return false;
				});

				$(prev).clone().appendTo("#img_container").fadeIn(500).click(function() {
					$(".prev").trigger("mousedown");
					return false;
				});
				$(window).keyup(function(e){
					if(e.keyCode=="39") {
						$(".next").trigger("mousedown");
					}
					if(e.keyCode=="37") {
						$(".prev").trigger("mousedown");
					}
					if(e.keyCode=="27" || e.keyCode=="32") {
						$("#overlay").trigger("click");
					}
				});
				
			}
			
			ol = function (elm, opts, callback) {


					
					var windowheight = parseInt($(window).height());	
					var containerPosY = ($(opts.wrapper).offset().top)-210+parseInt($(opts.wrapper).height())/2;
					var windowwidth = parseInt($(window).width());
					
					if($("#img_container").length==0) {
						$(imgcontainer).clone().attr("id", "img_container").appendTo("body").show().css({
							zIndex: 200,
							top: 200,
							left: (windowwidth/2),
							width: 20,
							height: imgheight+20
						});
					
					}
					else {
						$("#links").show();
						
					}
					
					
					
					if($(elm).find(".caption").length>0) {
						if($("#caption").length==0) $(caption).clone().attr("id", "caption").html($(".caption", elm).html()).appendTo("#img_container");
						else $("#caption").html($(".caption", elm).html()).hide();
					}

					else {
						$("#caption").text("");
					}
					
					var elmparent = $(elm).parents("li");
					
					if($(elmparent).find(".links").length>0) {
						if($("#links").length==0) $(links).clone().attr("id", "links").html($(".links", elmparent).html()).appendTo("#img_container");
						else $("#links").html($(".links", elmparent).html());
						
					}
					
					else {
						$("#links").text("");
					}

					if($("#overlay").length==0) {
						$(overlay).clone().attr("id", "overlay").appendTo("body").fadeTo(1000, 0.7).css({
							height: parseInt($("body").height())-parseInt($("#container").offset().top)
							
						}).click(function() {
							window.location.hash = "";
							$("#overlay").fadeTo(1000, 0, function() {
								$("#overlay").remove();
								$(window).unbind("keyup");
								$(".current").removeClass("current");
							});
							$("#img_container").fadeOut(400, function() {
								$(this).remove();
							})
						});
					}

					if($(".current_image").length==0) {
						the_id = "current_image";
					}
					else {
						$("#current_image").attr("id", "old_image");
						the_id = "current_image";
						$("#old_image").animate({width: "100%", opacity: 0}, 1000);
						

					}

					$(img).clone().addClass("current_image")

					.attr("id", the_id)
					.load(function() {

						$(this).appendTo("#img_container").hide();


						imgratio = imgheight/parseInt($(this).height());
						imgwidth = parseInt($(this).width());
						imgwidth = Math.round(imgwidth*imgratio);


						$(this).css({
							height: imgheight,
							width: imgwidth,
							position: "absolute",							
							zIndex: 10
							
						});
						
						$(window).unbind("keyup");
						
						$("#img_container").animate({
							height: imgheight+20,
							width: imgwidth,
							top:  200,
							left: (windowwidth/2)-(imgwidth/2-10)
						}, {
							duration: 1000,
							easing: "swing",
							
							complete: function() {
								if($(".current_image").not("#current_image").length==0) {
									//$("#links").show();
									
									$("#current_image").fadeIn(1000, function() {
											$("#caption").fadeIn(1000);
											$("#links").fadeIn(1000);
											
											if(callback) callback();
											
											
									});
								}
								else {
									if(callback) callback();
									
								}
							}
						});
						
						if($(".current_image").not("#current_image").length>0) {
							// $("#links").show();
						
							$("#current_image").fadeIn(1200, function() {
								$(".current_image").not("#current_image").remove();
								
									if($("#caption").length>0) {
										$("#caption").fadeIn(1000);
										$("#links").fadeIn(1000);

									}

							});
						}

					}).error(function() {
						
						}).attr("src", $(elm).attr("href"));

						return false;
						
						
					}
					
					if(window.location.hash) {
						var perma = window.location.hash;
						perma = parseInt(perma.replace(/^.*#look\-/, ''));		
						perma = perma-1;
						$("li:not(.clone):eq("+perma+")").trigger("mousedown");
					}
					
					

				
		});
		
		}
	
})(jQuery);
