/*
* HELPERS
*/
function formatTitle(title, currentArray, currentIndex, currentOpts) {
	title = title.split(" | ");
	if(title.length == 1) {
		title = title[0];
	}
	return "<div class=\"fancybox-caption\">" + title[1] + "</div>";
}

function updateSlider() {	
	selected_cats = $("a.category_limit.selected").map(function(){
		return $(this).data("category").replace("_", " ");
	});
	selected_cats = Array.prototype.join.call(selected_cats, "|");
	$.getJSON("/index.php/v2/json_images_by_category/" + selected_cats).success(function(data){
		if(data){
			var container = $("#slides");
			if(container.slides()) {
				$(".current_slide").empty();
				container.slides("stop");
				$(".controls").text("Play");
				container.slides("destroy");
				container.empty();
			}
			$("#portfolio_by_project_template").tmpl(data).appendTo("#slides");
			var options = {
				width: 800,
				height: 500,
				navigation: true,
				pagination: false,
				navigateEnd: function(current) {
					currentSlide(current);
				},
				loaded: function(){
					currentSlide(1);
				}
			}
			if(data.length < 2) {
				options.pagination = false;
				options.navigation = false;
				$(".controls").hide();
			} else {
				$(".controls, .current_slide").show();
			}
			container.slides(options);
			$("#slides img, #slides p.caption").show();
			
			$(".controls").click(function(e) {
				e.preventDefault();
				var slidesStatus = $("#slides").slides("status","state");
				if (!slidesStatus || slidesStatus === "stopped") {
					$("#slides").slides("play");
					$(this).text("Stop");
				} else {
					$("#slides").slides("stop");
					$(this).text("Play");
				}
			});
			
			$("#slides .slide a").fancybox({
				"titlePosition": "inside",
				"titleFormat": formatTitle
			});
		}
	});
}
function fetchProject(id){
	$("#project_list").hide();
	$("#project_detail_loading").show();
	$("#project_list li a div").hide();
	$.getJSON("/index.php/v2/json_entry/" + id, function(data) {
		container = $("#slides");
		container.slides("stop");
		$(".controls").text("Play");
		container.slides("destroy");
		container.empty();
	})
	.success(function(data){
		var image_array = [];
		$(data.channel_images).each(function(){
			image_array.push(this.slide);
		});
		$.preLoadImages(
			image_array, function(){
				$("#project_template").tmpl(data).appendTo("#project_detail");
				$("#project_detail_loading").hide();
				$("#project_detail").fadeIn();

				$.preLoadImages(image_array, function(){
					var options = {
						width: 800,
						height: 500,
						pagination: false,
						navigation: (image_array.length < 2) ? false : true,
						navigateEnd: function(current) {
							currentSlide(current);
						},
						loaded: function(){
							currentSlide(1);
						}
					}
					$("#slides").slides(options);
					$("#slides img, #slides p.caption").show();
					$(".controls").click(function(e) {
						e.preventDefault();
						var slidesStatus = $("#slides").slides("status","state");
						if (!slidesStatus || slidesStatus === "stopped") {
							$("#slides").slides("play");
							$(this).text("Stop");
						} else {
							$("#slides").slides("stop");
							$(this).text("Play");
						}
					});
					$("#slides .slide a").fancybox({
						"titlePosition": "inside",
						"titleFormat": formatTitle
					});
				});
			}
		);
	});
}
function currentSlide(current) {
	$(".current_slide").text(current + " of " + $("#slides").slides("status","total") );
}
/*
* ON LOAD
*/
$(function(){
	// Page Stuff
	$("#navigation li").mouseenter(function(){
		$(this).find("span").css("display", "block");
	}).mouseleave(function() {
		$(this).find("span").css("display", "none");
	});
	$("#project_list_types").click(function(e) {
	    e.preventDefault();
	});
	
	// Homepage Specific
	if($("body").hasClass("homepage")) {
		$(".hero.homepage").css("width", $(window).width() - 270 + "px");
		$(window).resize(function() {
			$(".hero.homepage").css("width", $(window).width() - 270 + "px");
		});
		$("#homepage_hero_image").load(function(){
			$(this).fadeIn();
		});
		$(".hero_gradient h2").fadeIn();
		$(".hero_gradient").click(function(){
			document.location.href = $(this).data("project");
		});
	}

	// Portfolio Specific - Projects
	if($("body").hasClass("projects")) {
		$.address.strict(false);
		$.address.init(function(event) {
			if($.address.value()) {
				fetchProject($.address.value());
			}
		});
		
		$("div.details, div.overlay, #project_detail").hide();

		$("#project_list li").mouseenter(function() {
			$(this).children("a").children("div").fadeIn(200);
	    }).mouseleave(function(e) {
			$(this).children("a").children("div").fadeOut(200);
	    });

		$("#project_list li a").live("click", function(e){
			fetchProject($(this).parent().attr("id").split("_")[2]);
			return false;
		});
		
		$("a.close").live("click", function(){
			$(this).parent().hide();
			$("#project_detail").empty();
			$("#project_list").fadeIn();
			return false;
		});
	};

	// Portfolio Specific - Spaces
	if($("body").hasClass("spaces")) {
		$.address.strict(false);
		$("a.category_limit").click(function(e){
			e.preventDefault();
			$("a.category_limit").removeClass("selected");
			$(this).toggleClass("selected");
			updateSlider($(this).data("category"));
		});
		$.address.init(function(event) {
			if($.address.value()) {
				$("." + $.address.value()).toggleClass("selected");
				updateSlider();
			} else {
				updateSlider();
			}
		});
	}
	
	$("#homepage_hero_image").fadeIn();
});
