(function($) {
	$.fn.initImageGallery = function(images, params) {
		
		if(!params) params = {};
		if(!params.text_next) params.text_next = "Next";
		if(!params.text_prev) params.text_prev = "Prev";
		if(!params.text_of) params.text_of = " of ";
		if(!params.lang) params.lang = "ja";
		
		var $target = $(this);
		$target.data("images", images);
				
		$target.addClass("gallery_container");
		if(!$(".image img, .image a, .next, .prev, .total, .current", $target).length != 6) { 
			$target.html();
			if(params.lang == "en") var $total = "<span class=\"current\"/>" + params.text_of + "<span class=\"total\"/>";
			else var $total = "<span class=\"total\"/>" + params.text_of + "<span class=\"current\"/>";
			$target.html("<div class=\"image\"><a><img/></a></div><div class=\"nav\"><a class=\"prev\"/>" + $total + "<a class=\"next\"/></div>");
		}
		if(params.current >= 0) var current = params.current;
		else var current = 0;
		$target.data("current", current);
				
		var $img = $(".image img", $target);
		if(!$img.attr("src")) {
			$img.attr("src", images[current].image);
			$img.attr("alt", images[current].title);
			var $a = $(".image a", $target);
			$a.attr("title", images[current].title);
			$a.attr("href", images[current].link);
			if(images[current].link=="#"){
				$a.bind("click",function(){return false;});
				$a.attr("style","cursor: default;");
			} else {
				$a.unbind("click");
				$a.attr("style","cursor: pointer;");
			}
			
			$(".total", $target).text(images.length);
			$(".current", $target).text(current+1);
			
			if(current == 0) $(".prev", $target).addClass("end");
			if(current == images.length-1) $(".next", $target).addClass("end");
		}
		
		$(".next", $target).text(params.text_next).click(function() {
			var $target = $(this).parents(".gallery_container").eq(0);
			var images = $target.data("images");
			var current = $target.data("current");
			if(current < images.length - 1) {
				$target.data("manual_change_flag", true);
				$(".image", $(this).parents(".gallery_container").eq(0)).fadeOut(200, function() {
					var $target = $(this).parents(".gallery_container").eq(0);
					var images = $target.data("images");
					var current = $target.data("current");
					if(current < images.length - 1)	$target.data("current", ++current);

					if(current > 0) $(".prev", $target).removeClass("end");
					if(current == images.length-1) $(".next", $target).addClass("end");
				
					var $img = $("img", $(this));
					$img.attr("src", images[current].image);
					$img.attr("alt", images[current].title);
					var $a = $("a", $(this));
					$a.attr("title", images[current].title);
					$a.attr("href", images[current].link);
					if(images[current].link=="#"){
						$a.bind("click",function(){return false;});
						$a.attr("style","cursor: default;");
					} else {
						$a.unbind("click");
						$a.attr("style","cursor: pointer;");
					}
					
					$(".current", $target).text(current + 1);
					$(this).fadeIn(200);
				});
			}
		});
		$(".prev", $target).text(params.text_prev).click(function() {
			var $target = $(this).parents(".gallery_container").eq(0);
			var images = $target.data("images");
			var current = $target.data("current");
			if(current > 0) {
				$target.data("manual_change_flag", true);
				$(".image", $(this).parents(".gallery_container").eq(0)).fadeOut(200, function() {
					var $target = $(this).parents(".gallery_container").eq(0);
					var images = $target.data("images");
					var current = $target.data("current");
					if(current > 0) $target.data("current", --current);

					if(current < images.length - 1) $(".next", $target).removeClass("end");
					if(current == 0) $(".prev", $target).addClass("end");
				
					var $img = $("img", $(this));
					$img.attr("src", images[current].image);
					$img.attr("alt", images[current].title);
					var $a = $("a", $(this));
					$a.attr("title", images[current].title);
					$a.attr("href", images[current].link);
					if(images[current].link=="#"){
						$a.bind("click",function(){return false;});
						$a.attr("style","cursor: default;");
					} else {
						$a.unbind("click");
						$a.attr("style","cursor: pointer;");
					}
					
					$(".current", $target).text(current + 1);
					$(this).fadeIn(200);
				});
			}
		});
		
		if(params.interval && images.length > 1) {
			var interval = setInterval(function() {
				if($target.data("manual_change_flag")) $target.data("manual_change_flag", false);
				else {
					$(".image", $target).fadeOut(200, function() {
						var $target = $(this).parents(".gallery_container").eq(0);
						var images = $target.data("images");
						var old_current = $target.data("current");
						var current = old_current + 1;
						if(current == images.length) current = 0;
						// var current = Math.floor(Math.random() * images.length);
						// while(old_current == current) current = Math.floor(Math.random() * images.length);
						$target.data("current", current);
						
						if(current == images.length - 1) $(".next", $target).addClass("end");
						if(current < images.length - 1) $(".next", $target).removeClass("end");
						if(current == 0) $(".prev", $target).addClass("end");
						if(current > 0) $(".prev", $target).removeClass("end");
						
						var $img = $("img", this);
						$img.attr("src", images[current].image);
						$img.attr("alt", images[current].title);
						var $a = $("a", this);
						$a.attr("title", images[current].title);
						$a.attr("href", images[current].link);
						if(images[current].link=="#"){
							$a.bind("click",function(){return false;});
							$a.attr("style","cursor: default;");
						} else {
							$a.unbind("click");
							$a.attr("style","cursor: pointer;");
						}
						
						$(".current", $target).text(current + 1);
						$(this).fadeIn(200);
					});
				}
			}, params.interval);
		}
		return false;
	}
})(jQuery);
