var project_awards = true;

$(function(){

	// Captions \\
	$("<span>").attr("id", "project-image-caption").appendTo("#project-image");
	
	var current_img;
	var project_img = 0;
	var project_total = 0;
	
	project_total = $("#project-images").children("li").size();
	
	// Clear the first image
	$("#project-image img").attr("src", "");

	function changeImage(i) {
		project_img += i;
		if (project_img >= project_total) {
			project_img = 0;
		}
		if (project_img < 0) {
			project_img = project_total - 1;
		}
		current_img = $("#project-images a:eq(" + project_img + ")");
		swapImage();
	}
	
	function swapImage() {
		$("#project-content-button").removeClass("project-content-selected");
		
		var img = current_img.attr("href");
		$("#project-image img").attr("src", img);
		$("#project-image").show();
		$("#project-awards").hide();
		$("#project-images a").css("background-image", "none");
		current_img.css("background-image", "url(\"/images/commons/image-selected.png\")");
		
		$("#project-image-caption").text(current_img.attr("title"));
	}

	$("#project-images a").click(function(event){
		event.preventDefault();
		project_img = $("#project-images a").index($(this));
		current_img = $(this);
		swapImage();
	});
	
	$("#project-content").hide();
	$("#project-images a:first").trigger("click");

	
	// Awards and Look Book background \\
	
	if (sub_page != "") {
		$("<span>").attr("id", "project-interface-bg").appendTo("#project");
	}
	
	// Content Info Button \\
	$("<div>").attr("id", "project-content-button").text("info").appendTo("#project");
	$("#project-content-button").addClass("project-content-selected").click(function(event){
		$("#project-content").show();
		$("#project-image").hide();
		$("#project-awards").hide();
		$("#project-images a").css("background-image", "none");
		$("#project-content-button").addClass("project-content-selected");
		if ($("#project-awards").css("display") == "none") {
			$("#project-awards-button-image").attr("src", "/images/commons/awards-button-off.png");
			if ($("#project-image").css("display") == "none") {$("#project-content-button").addClass("project-content-selected");}
		} else {
			$("#project-awards-button-image").attr("src", "/images/commons/awards-button-on.png");
		}
	});
	
	$("<div>").attr("id", "project-awards-button").appendTo("#project");
	$("<img>").attr("id", "project-awards-button-image").attr("src", "/images/commons/awards-button-off.png").attr("alt", "Winner's Circle").attr("title", "Winner's Circle").appendTo("#project-awards-button");

	$("<div>").attr("id", "project-awards-close").appendTo("#project-awards");
	$("<img>").attr("src", "/images/commons/awards-close.png").attr("alt", "Close Winner's Circle").attr("title", "Close Winner's Circle").appendTo("#project-awards-close");

	$("#project-awards-button").click(function(event){
		$("#project-content-button").removeClass("project-content-selected");
		
		$("#project-awards").toggle();
		if ($("#project-awards").css("display") == "none") {
			$("#project-awards-button-image").attr("src", "/images/commons/awards-button-off.png");
			if ($("#project-image").css("display") == "none") {$("#project-content-button").addClass("project-content-selected");}
		} else {
			$("#project-awards-button-image").attr("src", "/images/commons/awards-button-on.png");
		}
	});

	$("#project-awards-close").click(function(event){
		$("#project-awards").hide();
		$("#project-awards-button-image").attr("src", "/images/commons/awards-button-off.png");
		if ($("#project-image").css("display") == "none") {$("#project-content-button").addClass("project-content-selected");}
	});
	
	$("#portfolio-nav > li span").click(function(event){
		$("#portfolio-nav > li span").siblings("ul").hide();
		$(this).siblings("ul").toggle();
		
		$("span").removeClass("selected-category");
		$("span").parent().removeClass("selected-category-list");
		
		$(this).addClass("selected-category");
		$(this).parent().addClass("selected-category-list");
	});
	
	$("#pn-" + sub_page).addClass("selected-category");
	$("#pn-" + sub_page + "-" + item_page).addClass("selected-project");
	$("#pn-" + sub_page).parent().addClass("selected-category-list");
	$("#pn-" + sub_page + "-" + item_page).parent().addClass("selected-project-list");
	$("#pn-" + sub_page).trigger("click");
	
	if (project_awards == false) {$("#project-awards-button").hide();}
	
	// Project images \\
	$("<span>").attr("id", "project-img-nav").appendTo("#project-image");
	$("<span>").attr("id", "project-img-prev-big").appendTo("#project-img-nav");
	$("<span>").attr("id", "project-img-next-big").appendTo("#project-img-nav");
	$("<span>").attr("id", "project-img-prev").appendTo("#project-img-nav");
	$("<span>").attr("id", "project-img-next").appendTo("#project-img-nav");
	
	$("#project-img-prev, #project-img-prev-big").click(function(event){changeImage(-1);});
	
	$("#project-img-next, #project-img-next-big").click(function(event){changeImage(1);});
	
	
	// Keywords \\
	var keywords_str = $("#project-keywords").text();
	keywords_str = keywords_str.replace(/, /g, " • ");
	$("#project-keywords").text(keywords_str);
	
	if (item_page == "") {
		$("#project-content-button").remove();
		$("#project-awards-button").remove();
	}


	// History \\
	var history_options = {path: "/", expires: 10};
	var history_cookie = "portfolio_history";
	var history_list = $.cookie(history_cookie);
	if (history_list == null) {
		history_list = "";
	}
	if (item_page != "") {
		if (history_list == "") {
			history_list = item_page;
		} else if (history_list.match(item_page) == null) {
			history_list += "," + item_page;
		}
		$.cookie(history_cookie, history_list, history_options);
	}
	if (history_list != "") {
		var history_array = history_list.split(",");
		for (var i = 0; i < history_array.length; i++) {
			if (history_array[i] != item_page) {
				$("a[id*=" + history_array[i] + "]").addClass("visited-project");
			}
		}
	}

});
