var ready = true;
var intLeft;
var intLeft2;
var amount;

var currentIndex = 0;
var nextIndex;
var prevIndex;
var nexterIndex;

var element;
		
(function($) {
	var settings = {
		time : 1000,
		index: 0
	};

	var methods = {
		init : function(options) {
			if (options) {
				$.extend(settings, options);
			}
			
			element = this;
			currentIndex = settings.index;
	
			scroll(0, 0);
			$(this).appendTo("body");
			$(this).wrap("<div id='overlay' />");
			
			$("#overlay").append("<button id='close_button'>X</button>");
			$("#overlay").append("<button id='arrow_next'></button>");
			$("#overlay").append("<span class='description'></span>");
			$(".description").hide();
			
			$(this).find("img").wrap("<div class='picture' />");
			
			$(this).mousemove(function(e) {
				$(".description").css("left", e.clientX);
				$(".description").css("top", e.clientY);
			});
			
			$("#arrow_next").click(function() {
				methods.next();
			});
			
			$("#close_button").click(function() {
				methods.hide();
			});
			
			$(".picture").hover(
				function (e) 
					{
						if($("img", this).attr("alt") != ""){
							$(".description").html($("img", this).attr("alt"));
							$(".description").show();
						}
					}, 
				function () 
					{ 
						$(".description").hide();
					}
			);
		
			amount = $(".picture").length;
			
			if(amount > 0){
				$(this).append("<span id='album_counter'>Foto: 1/" + amount + "</span>");
			} else {
				$(this).append("<span id='album_counter'>Er zijn geen foto's beschikbaar</span>");
			}
			
			$(".picture").css("opacity", 0);
			$(".picture").each(
				function (i) {
					var object = this;
					$(this).find("img").load(function() {
						
						if($(object).find("img").width() > $(element).width() * 0.7){
							var ratio = $(object).find("img").width() / $(object).find("img").height();	
							$(object).find("img").width($(element).width() * 0.7);
							$(object).find("img").height($(object).find("img").width() / ratio);
						}
					
						if($(object).find("img").height() > $("body").height() * 0.7){
							var ratio = $(object).find("img").width() / $(object).find("img").height();	
							$(object).find("img").height($("body").height() * 0.7);
							$(object).find("img").width($(object).find("img").height() * ratio);
						}
						
						if(i != currentIndex){
							$(object).width($(object).find("img").width() * 0.7);
							$(object).height($(object).find("img").height() * 0.7);
						} else {
							$(object).width($(object).find("img").width());
							$(object).height($(object).find("img").height());
						}
						$(object).find("img").css(
							{ 
								height: '100%', 
								width: '100%' 
							}
						);
					});
				}
			);
			$(".picture").css("z-index",  99);	
			
			$(".picture:eq(" + (currentIndex + 1) + ")").find("img").load(function() {
				intLeft2 = ($(element).width() - $(".picture:eq(" + (currentIndex + 1) + ")").width() - 20);		
				$(".picture:eq(" + (currentIndex + 1) + ")").css("left", intLeft2 + "px");
				$(".picture:eq(" + (currentIndex + 1) + ")").css("opacity", 0.5);
			});
			
			if(amount >= 3){
				$(".picture:eq(" + (amount - 1) + ")").css("left", 0);
				$(".picture:eq(" + (amount - 1) + ")").css("opacity", 0.5);
			}
			
			$(".picture:eq(" + currentIndex + ")").find("img").load(function() {
				intLeft = (($(element).width() - $(".picture:eq(" + currentIndex + ")").width() - 20) / 2);		
				$(".picture:eq(" + currentIndex + ")").css("z-index", 100);
				$(".picture:eq(" + currentIndex + ")").css("left", intLeft + "px");
				$(".picture:eq(" + currentIndex + ")").css("opacity", 1);
			});
		
			$("#overlay").height($(document).height());
		},
		show : function() {
			methods.init();
		},
		hide : function() {
			$("#overlay").remove();	
			currentIndex = 0;
		},
		next: function() {
			if(ready && amount != 1){
				if(currentIndex + 1 == amount) {
					nextIndex = 0;	
				} else {
					nextIndex = currentIndex + 1;
				}
				
				if(currentIndex - 1 == -1) {
					prevIndex = amount - 1;	
				} else {
					prevIndex = currentIndex - 1;
				}
				
				if(currentIndex + 2 == amount) {
					nexterIndex = 0;	
				} else if(currentIndex + 2 == amount + 1) {
					nexterIndex = 1;
				} else {
					nexterIndex = currentIndex + 2;
				}		
				
				$(".picture").css("z-index",  99);		
				$(".picture:eq(" + nextIndex + ")").css("z-index", 100);
				
				ready = false;	
	
				$("#album_counter").html("Foto: " + (nextIndex + 1) + "/" + amount);
				if(amount >= 3){
					intLeft2 = ($(element).width() - $(".picture:eq(" + nexterIndex + ")").width() - 20);
				} else {
					intLeft2 = ($(element).width() - ($(".picture:eq(" + nexterIndex + ")").width() * 0.7 - 20));
				}
				
				intLeft = (($(element).width() - ($(".picture:eq(" + nextIndex + ")").width() / 0.7) - 20) / 2);	
				
				setTimeout(
					function() { 
						$(".picture:eq(" + currentIndex + ")").css("z-index", 99); $(".picture:eq(" + nextIndex + ")").css("z-index", 100); 
					}
				, settings.time / 2);
				
				$(".picture:eq(" + currentIndex + ")").animate(
					{
						left: '0', 
						opacity: 0.5, 
						width: ($(".picture:eq(" + currentIndex + ")").width() * 0.7), 
						height: ($(".picture:eq(" + currentIndex + ")").height() * 0.7)
					} 
				, settings.time);
				
				if(amount >= 3){		
					$(".picture:eq(" + prevIndex + ")").animate(
						{
							opacity: 0
						}
					, settings.time);
				}
				
				$(".picture:eq(" + nextIndex + ")").animate(
					{
						left: intLeft, 
						opacity: 1, 
						width: ($(".picture:eq(" + nextIndex + ")").width() / 0.7), 
						height: ($(".picture:eq(" + nextIndex + ")").height() / 0.7)
					}
				, settings.time);
				
				$(".picture:eq(" + nexterIndex + ")").animate(
					{
						opacity: 0.5, 
						left: intLeft2
					}
				, settings.time
				, function() 
					{ 
						ready = true; 
					}
				);
				
				if(currentIndex + 1 == amount){
					currentIndex = 0;
				} else {
					currentIndex = currentIndex + 1;
				}
			}	
		}
	};

	$.fn.photoalbum = function(method) {
		if (methods[method]) {
			return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || ! method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.tooltip');
		}

	};
})(jQuery);
