(function($){
	$.mhGalleryGoto = function(type, sec, gotonum, callback) {
		console.log("t"+type+"s"+sec+"g"+gotonum);
		// type - 2 number; 1 next; 0 prev;
		that = "#"+sec;
		numid = Number(sec.split("-")[1]);
		id = Number($(that).attr("showing"));
		if (type == 1) {
			if (id == $(that).attr("max")) {
				$(that).attr("showing", 1);
				show = 1;
			} else {
				$(that).attr("showing", id+1);
				show = id+1;
			}
		} else if (type == 0) {
			if (id == 1) {
				$(that).attr("showing", $(that).attr("max"));
				show = $(that).attr("max");
			} else {
				$(that).attr("showing", id-1);
				show = id-1;
			}
		} else {
			if (id != gotonum && gotonum != 0) {
				$(that).attr("showing", gotonum);
				show = gotonum;
			} else {
				return false;
			}
		}
		
		if ($(that).find("img:nth-child("+(show)+")").hasClass('dark')) {
			$(that).addClass("dark");
		} else {
			$(that).removeClass("dark");
		}
		
		$(that).find("img:nth-child("+(id)+")").css("z-index", (100+id+1)).fadeOut("slow");
		$(that).find("img:nth-child("+(show)+")").css("z-index", (100+id+1)).fadeIn("slow");
		$(that).find(".titlebar").find(".left").html($(that).find("img:nth-child("+(show)+")").attr("alt"));
		$(that).find(".titlebar").find(".right").find(".actual").html($(that).attr("showing")+"/"+$(that).attr("max"));
		
		callback();
	}
	
	$.fn.mhGallery = function(options) {

		var options = $.extend({
			theme: 'mhgal', 
			afterCreationCallback: function(){},
			afterChangeCallback: function(){}
		}, options);
		
		var gid = 0;

		var ret = this.each(function() {
			$(this).attr("showing", 1).attr("max", $(this).find("img").length).attr("id", "gal-"+gid).addClass(options.theme).append("<div class=\"controls\"><a href=\"#\" class=\"prev\"></a><a href=\"#\" class=\"next\"></a><div class=\"titlebar\"><span class=\"left\"></span><span class=\"right\"><span class=\"actual\"></span><span class=\"num\"></span></span></div></div>");
			$(this).find('img:first').show(0);
			if ($(this).find('img:first').hasClass('dark')) {
				$(this).addClass('dark');
			}
			links = "";
			for (i=1;i<Number($(this).attr("max"))+1;i++) {
				links = links+"<a class=\"goto\" href=\"#\">"+i+"</a> ";
			}
			$(this).find(".num").html("Přejít na: "+links);
			$(this).find(".actual").html("1/"+$(this).attr("max"));
			$(this).find(".left").html($(this).find("img").first().attr("alt"));
					
			$('body').live('click', function(event) {
				t = event.target;
				id = $(t).closest('.images').attr('id');
				switch ($(t).attr('class')) {
					case 'next': 
						$.mhGalleryGoto(1, id, 0, options.afterChangeCallback);
						return false;
					case 'prev': 
						$.mhGalleryGoto(0, id, 0, options.afterChangeCallback);
						return false;
				}
				
				if ($(t).closest('.goto')) {
					$.mhGalleryGoto(2, $(t).closest('.images').attr("id"), Number($(t).closest('.goto').text()), options.afterChangeCallback);
					return false;	
				}
			});
			
			$(this).find('.right').hover(function() {
				$(this).find(".num,.actual").stop(true, true);
				$(this).find(".actual").fadeOut(150, function() {
					$(this).parent().find(".num").slideDown(150);
				});
			}, function() {
				$(this).find(".num,.actual").stop(true, true);
				$(this).delay("slow").find(".num").slideUp(150, function() {
					$(this).parent().find(".actual").fadeIn(150);
				});
			});
			
			$(this).hover(function() {
				$(this).find('.controls').stop(true, true).fadeTo(500, 1);
			}, function() {
				$(this).find('.controls').delay(1000).fadeTo(500, 0);
			});
			
			$(this).find('.titlebar').hover(function() {
				$(this).stop(true, true).fadeTo("slow", 0.9);
			}, function() {
				$(this).delay("slow").fadeTo("slow", 0.6);
			});
			
			gid++;
		});

		$(".controls").delay(2000).fadeOut("slow").delay(1000).animate({opacity: 0}, 10);
		
		setInterval(function() {
			$("section.images").each(function() {
				var id = $(this).attr('id');
				if (($(this).find('.controls').css('opacity')) != 1) {
					console.log("a");
					$.mhGalleryGoto(1, id, 0, options.afterChangeCallback);
				}
			});
		}, 20000);
		
		options.afterCreationCallback();

		return ret;
	};
})(jQuery);
