// lightbox
jQuery.fn.lightbox = function(options) {
	
	var defaults = {
		"overlayOpacity":0.8,
		"overlayColor":"#000",
		"lightboxBorder":10,
		"speed":600,
		"enableKeyListener":true
	};
	var options = $.extend(defaults, options);
	
	return this.each(function() {
		
		var obj = $(this);
		var imgsrc = obj.attr("href");
		var firstimg = $("div#main div.gallery a:first").attr("href");
		var lastimg = $("div#main div.gallery a:last").attr("href");
		
		obj
			.children("span.loupe")
			.hide()
		;
		
		obj
			.mouseenter(function(){
				$(this)
					.children("span.loupe")
					.show()
			})
			.mouseleave(function(){
				$(this)
					.children("span.loupe")
					.hide()
			})
		;
			
		obj
			.click(function(){
				
				var docwidth = document.body.clientWidth;
				var docheight = $(document).height();
				
				$("body")
					.append(
						'<div class="lightboxOverlay">'+
						'</div>'+
						'<div class="lightbox">'+
						'	<span class="lightboxLoader">'+
						'	</span>'+
						'	<img class="lightboxImage" src="" alt="">'+
						'	<div class="lightboxController">'+
						'		<div class="lightboxControllerContainer">'+
						'			<span class="prev">'+
						'			</span>'+
						'			<span class="next">'+
						'			</span>'+
						'			<span class="close">'+
						'			</span>'+
						'		</div>'+
						'	</div>'+
						'</div>'
					)
					.children("div.lightboxOverlay")
					.css({
						"width":""+ docwidth +"px",
						"height":""+ docheight +"px",
						"background-color":options.overlayColor
					})
					.fadeTo(0,options.overlayOpacity)
					.parent()
					.children("div.lightbox")
					.each(function(){
						$(this)
							.css({
								"top":""+ ( (($(window).height()/2 - options.lightboxBorder) - 100) + $(window).scrollTop() )  +"px",
								"left":""+ ( (($(window).width()/2 - options.lightboxBorder) - 100) + $(window).scrollLeft() ) +"px",
								"padding":""+ options.lightboxBorder +"px"
							})
						;
					})
				;
				
				obj
				    .resizeLightbox({
				    	"lightboxBorder":options.lightboxBorder,
				    	"speed":options.speed,
				    	"firstimg":firstimg,
				    	"lastimg":lastimg,
				    	"enableKeyListener":options.enableKeyListener
				    })
				;
				
				return false;
				
			})
		;
	});
};


// resizeLightbox
jQuery.fn.resizeLightbox = function(options) {
	
	var defaults = {
		"lightboxBorder":10,
		"speed":600,
		"firstimg":"",
		"lastimg":"",
		"enableKeyListener":true
	};
	var options = $.extend(defaults, options);
	
	return this.each(function() {
		
		var obj = $(this);
		var imgsrc = obj.attr("href");
		
		var docwidth = document.body.clientWidth;
		var docheight = $(document).height();
		
		$(document).unbind('keydown');
		
		$('div.lightbox img.lightboxImage')
			.hide()
		;
		$('div.lightbox div.lightboxController')
			.hide()
		;
		$('div.lightbox span.lightboxLoader')
			.each(function(){
				$(this)
			    	.css({
			    		"top":""+ ($(this).parent().height()/2 + options.lightboxBorder) - 16 +"px",
			    		"left":""+ ($(this).parent().width()/2 + options.lightboxBorder) - 16 +"px"
			    	})
			    ;
			})
			.show()
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.close")
			.unbind('click')
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.close")
		    .each(function(){
		    	$(this)
		    		.click(function(){
		    			$("div.lightbox")
		    				.remove()
		    			;
		    			$("div.lightboxOverlay")
		    				.fadeOut(options.speed/2,function(){
		    					$("div.lightboxOverlay")
		    						.remove()
		    					;
		    				})
		    			;
		    		})
		    	;
		    })
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.next")
			.unbind('click')
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.next")
			.each(function(){
			    $(this)
			    	.click(function(){
			    		if (imgsrc == options.lastimg) {
			    			$("div.lightbox div.lightboxController div.lightboxControllerContainer span.close")
			    				.trigger("click")
			    			;
			    		} else {
			    			obj
			    				.parent()
			    				.next()
			    				.children("a")
			    				.resizeLightbox({
			    					"lightboxBorder":options.lightboxBorder,
			    					"speed":options.speed,
			    					"firstimg":options.firstimg,
				    				"lastimg":options.lastimg,
				    				"enableKeyListener":options.enableKeyListener
			    				})
			    			;
			    		}
			    	})
			    ;
			})
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.prev")
			.unbind('click')
		;
		$("div.lightbox div.lightboxController div.lightboxControllerContainer span.prev")
			.each(function(){
			    $(this)
			    	.click(function(){
			    		if (imgsrc == options.firstimg) {
			    			$("div.lightbox div.lightboxController div.lightboxControllerContainer span.close")
			    				.trigger("click")
			    			;
			    		} else {
			    			obj
			    				.parent()
			    				.prev()
			    				.children("a")
			    				.resizeLightbox({
			    					"lightboxBorder":options.lightboxBorder,
			    					"speed":options.speed,
			    					"firstimg":options.firstimg,
				    				"lastimg":options.lastimg,
				    				"enableKeyListener":options.enableKeyListener
			    				})
			    			;
			    		}
			    	})
			    ;
			})
		;
		
		var objImagePreloader = new Image();
		objImagePreloader.onload = function() {
		    $('div.lightbox img.lightboxImage').attr('src',imgsrc);
		    
		    var imgWidth = $('div.lightbox img.lightboxImage').width();
		    var imgHeight = $('div.lightbox img.lightboxImage').height();
		    var lightboxWidth = imgWidth;
		    var lightboxHeight = imgHeight + $("div.lightbox div.lightboxController").height();
		    var documentHeight = docheight;
		    var documentWidth = docwidth;
		    var lightboxTop = ( (($(window).height()/2 - options.lightboxBorder) - (imgHeight/2)) - ($("div.lightbox div.lightboxController").height()/2) + $(window).scrollTop() );
		    var lightboxLeft = ( (($(window).width()/2 - options.lightboxBorder) - (imgWidth/2)) + $(window).scrollLeft() );
		    
		    if (lightboxTop < 10) {
		    	lightboxTop = 10;
		    } else if (lightboxTop+lightboxHeight+options.lightboxBorder*2+10 > documentHeight) {
		    	lightboxTop = documentHeight - (lightboxHeight+options.lightboxBorder*2+10);
		    }
		    if (lightboxLeft < 10) {
		    	lightboxLeft = 10;
		    } else if (lightboxLeft+lightboxWidth+options.lightboxBorder*2+10 > documentWidth) {
		    	lightboxLeft = documentWidth - (lightboxWidth+options.lightboxBorder*2+10);
		    }
		    
		    $('div.lightbox span.lightboxLoader')
		    	.hide()
		    ;
		    
		    lightboxHeight = lightboxHeight - $("div.lightbox div.lightboxController").height();
		    
		    $("div.lightbox")
		    	.animate({
		    		"width":""+ lightboxWidth +"px",
		    		"height":""+ lightboxHeight +"px",
		    		"top":""+ lightboxTop +"px",
		    		"left":""+ lightboxLeft +"px"
		    	},options.speed,function(){
		    		$('div.lightbox img.lightboxImage')
		    			.fadeIn(options.speed)
		    		;
		    		
		    		$('div.lightbox div.lightboxController')
		    			.show()
		    			.children("span")
		    			.remove()
		    		;
		    		
		    		obj
		    			.children("span")
					    .clone()
					    .prependTo("div.lightbox div.lightboxController")
					;
					
					$('div.lightbox div.lightboxController')
		    			.children("span")
		    			.addClass("description")
		    		;
		    		
		    		lightboxHeight = lightboxHeight + $("div.lightbox div.lightboxController").height();
		    		$(this)
		    			.animate({
		    				"height":""+ lightboxHeight +"px"
		    			},options.speed/2,function(){
		    				if (options.enableKeyListener) {
								$(document).keydown(function(event) {
									
									if (event.keyCode == 27 || event.keyCode == 88) {
										$("div.lightbox div.lightboxController div.lightboxControllerContainer span.close")
			    							.trigger("click")
			    						;
									} else if (event.keyCode == 37) {
										$("div.lightbox div.lightboxController div.lightboxControllerContainer span.prev")
			    							.trigger("click")
			    						;
									} else if (event.keyCode == 39) {
										$("div.lightbox div.lightboxController div.lightboxControllerContainer span.next")
			    							.trigger("click")
			    						;
									}
								});
							}
						})
		    		;
		    	})
		    ;
		    objImagePreloader.onload=function(){};
		};
		objImagePreloader.src = imgsrc;
		
	});
};


// resizeOverlay
jQuery.fn.resizeOverlay = function(options) {
	
	var defaults = {};
	var options = $.extend(defaults, options);
	
	return this.each(function() {
		
		var obj = $(this);
		
		var docwidth = document.body.clientWidth;
		var docheight = $(document).height();
		var widthreset = parseInt($("body").css("min-width"));
		if (docwidth < widthreset) {
			docwidth = widthreset;
		}
		obj
			.css({
			    "width":""+ widthreset +"px"
			})
			.css({
			    "width":""+ docwidth +"px",
			    "height":""+ docheight +"px"
			})
		;
		
		var imgWidth = $('div.lightbox img.lightboxImage').width();
		var lightboxLeft = ( (($(window).width()/2 - parseInt($("div.lightbox").css("padding-left"))) - (imgWidth/2)) + $(window).scrollLeft() );
		if (lightboxLeft < 10) {
			lightboxLeft = 10;
		}
		
		$("div.lightbox")
			.css({
				"left":""+ lightboxLeft +"px"
			})
		;
		
	});
};
