var Project = function() {
	return {
		init: function() {
			Project.archive.init();
			Project.slideshow.init([{ "element": "#slideshow ul", "type": "default" }]);
		}
	};
}();


Project.archive = function() {
	var parent;
	var sibling;
	var DOMReady = function() {
		$("#archive a").live("click", function(e) {
			sibling = this.nextSibling.nodeType !== 3 ? this.nextSibling : this.nextSibling.nextSibling;
			if (sibling) {
				e.preventDefault();
				parent = $(sibling.parentNode);
				if (parent.hasClass("active")) {
					parent.removeClass("active");
				} else {
					parent.addClass("active");
				}
			}
		});		
	};
	return {
		init: function() {
			$(document).ready(DOMReady);
		}
	};
}();

Project.slideshow = function() {
	var slideshowInit = false;
	
	var show = function($li) {
		$li.addClass("active");
		var $slideshow = $li.parent().prev();
		var no = $li.data("no");
		var positions = $slideshow.data("positions");
		var	position = 0;
		for (var i = 0; i < no; i++) {
			position += positions[i];
		}
		var type = $slideshow.data("type");
		if (type == "vertical") {
			var $parent = $slideshow.parent();
			$slideshow.parent().height(positions[no]);
			$parent.css({"marginBottom": Math.max($slideshow.data("maxSize") - positions[no], 0) + "px"});
			$slideshow.animate({"marginTop": -position + "px"}, 250, "easeInOutQuart");
		} else {
			$slideshow.animate({"marginLeft": -position + "px"}, 250, "easeInOutQuart");
		}
		$li.siblings().removeClass("active");
	};
	
	var addSlide = function($slideshow, $slide) {
		var type = $slideshow.data("type");
		var width = $slide.find("img").width();
		$slide.width(width);
		var size = width;
		if (type == "vertical") {
			size = $slide.height();
		} else {
			$slide.css({"float": "left"});	
		}
		var positions = $slideshow.data("positions");
		positions[$slide.data("no")] = size;
		$slideshow.data("positions", positions);
		var maxSize = Math.max(size, $slideshow.data("maxSize") ? $slideshow.data("maxSize") : 0);
		$slideshow.data("maxSize", maxSize);
		if (type == "vertical") {
			if ($slide.data("no") == 0) {
				$slideshow.parent().height(maxSize);
			}
		} else {
			$slideshow.parent().width(maxSize);
			$slideshow.width(size + $slideshow.width());
		}
		$slide.css({
			"visibility": "visible"
		});
	};
	
	var DOMReady = function(options) {
		for (var i = 0, ln = options.length; i < ln; i++) {
			var $el = $(options[i].element);
			var $items = $el.children();
			if ($items.length > 1) {
				$el.parent().css({
					position: "relative"
				});
				var $container = $el.parent().wrapInner("<div class=\"slideshow\"></div>").children();
				$container.wrapInner("<div></div>");
				var $slideshow = $container.children();
				$slideshow.data("type", options[i].type ? options[i].type : "default");
				$slideshow.data("positions", []);
				var $wrapper = $slideshow.parent();
				$wrapper.css({overflow: "hidden"});
				//$wrapper.height(0);
				
				var $ul = $("<ul class=\"slideshow-navigation\"></ul>");
				$items.each(function(j) {
					$slide = $(this);
					$slide.data("no", j);
					
					// adding images when they're loaded
					var $images = $(this).find("img");
					if ($images.length > 0) {
						var image = $images.get(0);
						if (!image.complete) {
							$images.load(addSlide($slideshow, $slide));
						} else {
							addSlide($slideshow, $slide);
						}
					}
				
					// adding navigation
					var $li = $("<li>" + (j + 1) + "</li>");
					if (j == 0) {
						$li.addClass("active");
					}
					$li.data("no", j);
					$ul.append($li);
				});
				$wrapper.append($ul);
				
				if (!slideshowInit) {
					slideshowInit = true;
					$("ul.slideshow-navigation li").live("click", function() {
						show($(this));
					});
				}
			}
			
		}
	};

	return {
		init: function(options) {
			$(document).ready(function() {
				DOMReady(options);
			});
		}
	};
}();


Project.init();
