/*　
 * ttSlideshow - Yet Another jQuery Slideshow Plugin
 * Version 2.00 (2010-11-10)
 * Copyright (C) 2010 Taiyo Totsuka
 *
 * Licensed under the MIT License
 * http://www.opensource.org/licenses/mit-license.php 
 *
 * Requires jQuery 1.4.2 and above, jQuery UI 1.8.5 and above
 */

(function($) {
	$.fn.ttSlideshow = function(options) {

		var o = $.extend({
			slide_wrapper: null,
			slide_directory: null,
			slide_interval: null,
			slide_links: null,
			slide_caption: null,
			caption_wrapper: null,
			transition_type: null,
			transition_options: null,
			transition_speed: null,
			thumbnav: null,
			thumbnav_wrapper: null,
			auto_play: null
		}, options || {});

		$(this).each(function() {

			$(o.slide_wrapper).empty();
			if(!o.slide_wrapper) {
				return false;
			}

			$.ajaxSetup({
				cache: true
			});

			$.getJSON(o.slide_directory + "slides.js", function(data) {
//				var aaa = 0;

				$.each(data.slides || data, function(i, slides) {

					if(slides.image_src !== undefined) {
						var slide_html = "<div class='slides'>";
						if (o.slide_links == "true") {
							slide_html += "<a href='" + slides.image_link + "' title='" + slides.image_title + "'>";
							slide_html += "<img src='" + o.slide_directory + slides.image_src + "' alt='" + slides.image_title + "' width='" + data.image_width + "' height='" + data.image_height + "' />";
							slide_html += "</a></div>\n";
						} else if (o.slide_caption == "true") {
							slide_html += "<img src='" + o.slide_directory + slides.image_src + "' alt='" + slides.image_title + "' width='" + data.image_width + "' height='" + data.image_height + "' />";
							slide_html += "</div>\n";
						} else {
							slide_html += "<img src='" + o.slide_directory + slides.image_src + "' width='" + data.image_width + "' height='" + data.image_height + "' />";
							slide_html += "</div>\n";
						}
						$(o.slide_wrapper).prepend(slide_html);
//						aaa = (i++) + 1;
					}
				});
				initialState();
//				alert(aaa);
			});

			function initialState() {
				$(o.slide_wrapper).children(":last-child").addClass("selected-slide");
				if (o.slide_caption === "true") {
					var image_title = $(o.slide_wrapper).children(".selected-slide").find("img").attr("alt");
					$(o.caption_wrapper).empty().html(image_title);
				}
			};

//			function slideShow() {
//				if (!$(o.slide_wrapper).children(".selected-slide").prev().size()) {
//					if (o.thumbnav === "true") {
//						$(o.thumbnav_wrapper)
//							.children().removeClass("selected-thumbnav")
//							.end()
//							.children(":first-child").addClass("selected-thumbnav");
//					}
//					$(o.slide_wrapper).children(".selected-slide")
//						.nextAll().show()
//						.end()
//						.effect(o.transition_type, o.transition_options, o.transition_speed, function() {
//							$(o.slide_wrapper)
//								.children().removeClass("selected-slide")
//								.end()
//								.children(":last-child").addClass("selected-slide")
//								.end()
//								.children(":first-child").show();
//						});
//						if (o.slide_caption === "true") {
//							var image_title = $(o.slide_wrapper).children(":last-child").find("img").attr("alt");
//							$(o.caption_wrapper).empty().html(image_title);
//						}
//				} else {
//					if (o.slide_caption === "true") {
//						var image_title = $(o.slide_wrapper).children(".selected-slide").prev().find("img").attr("alt");
//					}
//					if (o.thumbnav === "true") {
//						$(o.slide_wrapper).children().each(function(i) {
//							if ($(this).hasClass("selected-slide")) {
//								var total_slides = ($(o.slide_wrapper).children().length);
//								$(o.thumbnav_wrapper)
//									.children().removeClass("selected-thumbnav")
//									.end()
//									.children(":nth-child(" + (total_slides - i + 1) + ")").addClass("selected-thumbnav");
//							}
//						});
//					}
//					$(o.slide_wrapper)
//						.children(".selected-slide").effect(o.transition_type, o.transition_options, o.transition_speed, function() {
//							$(o.slide_wrapper).children(".selected-slide")
//								.prev().addClass("selected-slide")
//								.end()
//								.removeClass("selected-slide");
//						})
//					if (o.slide_caption === "true") {
//						$(o.caption_wrapper).empty().html(image_title);
//					}
//				}
//			};

			function previousSlide() {
				if (!$(o.slide_wrapper).children(".selected-slide").next().size()) {
					return false;
				} else {
					if (o.slide_caption === "true") {
						var image_title = $(o.slide_wrapper).children(".selected-slide").next().find("img").attr("alt");
					}
					if (o.thumbnav === "true") {
						$(o.slide_wrapper).children().each(function(i) {
							if ($(this).hasClass("selected-slide")) {
								var total_slides = ($(o.slide_wrapper).children().length);
								$(o.thumbnav_wrapper)
									.children().removeClass("selected-thumbnav")
									.end()
									.children(":nth-child(" + (total_slides - i - 1) + ")").addClass("selected-thumbnav");
							}
						});
					}
					$(o.slide_wrapper).children(".selected-slide")
						.next().addClass("selected-slide")
						.prev().removeClass("selected-slide")
//						.prev().show()
//						.toggle(o.transition_type, o.transition_options, o.transition_speed);
					$(o.slide_wrapper).children(".selected-slide")
						.first().toggle(o.transition_type, o.transition_options, o.transition_speed);
					if (o.slide_caption === "true") {
						$(o.caption_wrapper).empty().html(image_title);
					}
				}
			};

			function nextSlide() {
				if (!$(o.slide_wrapper).children(".selected-slide").prev().size()) {
					if (o.thumbnav === "true") {
						$(o.thumbnav_wrapper)
							.children().removeClass("selected-thumbnav")
							.end()
							.children(":first-child").addClass("selected-thumbnav");
					}
					$(o.slide_wrapper).children(".selected-slide")
						.nextAll().show()
						.end()
						.toggle(o.transition_type, o.transition_options, o.transition_speed, function() {
							$(o.slide_wrapper)
								.children().removeClass("selected-slide")
								.end()
								.children(":last-child").addClass("selected-slide")
								.end()
								.children(":first-child").show();
						});
						if (o.slide_caption === "true") {
							var image_title = $(o.slide_wrapper).children(":last-child").find("img").attr("alt");
							$(o.caption_wrapper).empty().html(image_title);
						}
				} else {
					if (o.slide_caption === "true") {
						var image_title = $(o.slide_wrapper).children(".selected-slide").prev().find("img").attr("alt");
					}
					if (o.thumbnav === "true") {
						$(o.slide_wrapper).children().each(function(i) {
							if ($(this).hasClass("selected-slide")) {
								var total_slides = ($(o.slide_wrapper).children().length);
								$(o.thumbnav_wrapper)
									.children().removeClass("selected-thumbnav")
									.end()
									.children(":nth-child(" + (total_slides - i + 1) + ")").addClass("selected-thumbnav");
							}
						});
					}
					$(o.slide_wrapper)
						.children(".selected-slide")
							.prev().show()
							.end()
							.toggle(o.transition_type, o.transition_options, o.transition_speed, function() {
								$(o.slide_wrapper).children(".selected-slide")
									.prev().addClass("selected-slide")
									.end()
									.removeClass("selected-slide");
							})
					if (o.slide_caption === "true") {
						$(o.caption_wrapper).empty().html(image_title);
					}
				}
			};

			$(function() {
				$("#previous-trigger").click(function() {
					if (!$(o.slide_wrapper).children().is(":animated")) {
						previousSlide();
						return false;
					} else {
						return false;
					}
				});
				$(document).keyup(function(event) {
					if (event.keyCode === 37) {
						if (!$(o.slide_wrapper).children().is(":animated")) {
							previousSlide();
							return false;
						} else {
							return false;
						}
					}
				});
			});

			$(function() {
				$("#next-trigger").click(function() {
					if (!$(o.slide_wrapper).children().is(":animated")) {
						nextSlide();
						return false;
					} else {
						return false;
					}
				});
				$(document).keyup(function(event) {
					if (event.keyCode === 39) {
						if (!$(o.slide_wrapper).children().is(":animated")) {
							nextSlide();
							return false;
						} else {
							return false;
						}
					}
				});
			});

			$(function() {
				$(o.slide_wrapper).find("a").live("click", function(event) {
					$(this).filter(function() {
						return this.hostname && this.hostname !== location.hostname;
					}).attr("target", "_blank");
				});
				$("a[rel=jump]").click(function() {
					if (!$(o.slide_wrapper).children().is(":animated")) {
						var i = $(this).attr("class").replace(/jump-/g,"");
						i = parseInt(i, 10);
						var total_slides = ($(o.slide_wrapper).children().length);
						if ($(o.slide_wrapper).children(":nth-child(" + (total_slides - i) + ")").hasClass("selected-slide")) {
							return false;
						}
						if ($(o.slide_wrapper).children().is(":animated")) {
							return false;
						} else {
							$(o.slide_wrapper).children(":nth-child(" + (total_slides - i) + ")")
								.show()
								.nextUntil(".selected-slide")
									.hide();
							$(o.slide_wrapper).children(".selected-slide")
								.effect(o.transition_type, o.transition_options, o.transition_speed, function() {
									$(o.slide_wrapper).children(":nth-child(" + (total_slides - i) + ")")
										.addClass("selected-slide")
										.prevAll()
											.removeClass("selected-slide")
											.show()
										.end()
										.nextAll()
											.removeClass("selected-slide")
											.hide();
								})
							if (o.thumbnav === "true") {
								var total_slides = ($(o.slide_wrapper).children().length);
								$(o.thumbnav_wrapper)
									.children().removeClass("selected-thumbnav")
									.end()
									.children(":nth-child(" + (i + 1) + ")").addClass("selected-thumbnav");
							}
							if (o.slide_caption === "true") {
								var image_title = $(o.slide_wrapper).children(":nth-child(" + (total_slides - i) + ")").find("img").attr("alt");
								$(o.caption_wrapper).empty().html(image_title);
							}
//							$(o.slide_wrapper).children(":nth-child(" + (total_slides - i) + ")")
//								.addClass("selected-slide")
//								.show()
//								.prevAll()
//									.removeClass("selected-slide")
//									.show()
//								.end()
//								.nextAll()
//									.removeClass("selected-slide")
//									.hide();
//							if (o.thumbnav === "true") {
//								$(o.slide_wrapper).children().each(function(i) {
//									if ($(this).hasClass("selected-slide")) {
//										var total_slides = ($(o.slide_wrapper).children().length);
//										$(o.thumbnav_wrapper)
//											.children().removeClass("selected-thumbnav")
//											.end()
//											.children(":nth-child(" + (total_slides - i) + ")").addClass("selected-thumbnav");
//									}
//								});
//							}
//							if (o.slide_caption === "true") {
//								var image_title = $(o.slide_wrapper).children(".selected-slide").find("img").attr("alt");
//								$(o.caption_wrapper).empty().html(image_title);
//							}
							return false;
						}
					}
				});
			});

			if (o.auto_play === "true") {
				$(function() {
					var interval_id = setInterval(nextSlide, o.slide_interval);
					$("a[rel=jump], #previous-trigger, #next-trigger").click(function() {
						clearInterval(interval_id);
						interval_id = setInterval(nextSlide, o.slide_interval);
						return false;
					});
					$(document).keyup(function(event) {
						if (event.keyCode === 37 || event.keyCode === 39) {
							if (!$(o.slide_wrapper).children().is(":animated")) {
								clearInterval(interval_id);
								interval_id = setInterval(nextSlide, o.slide_interval);
							}
						}
					});
				});
			}
		});
	}
})(jQuery);
