 $(document).ready(function() {


	/* Slideshow */
	var currentphoto = 0;
	var photos = [];
	var captions = [];
	var projcontentY = 0;
	var projcontenton = false;
	var autoslideshow = 0;


	function ssNextPhoto() {
		currentphoto++;
		if  (currentphoto >= photos.length) { 
			currentphoto = 0;
		}
		$("#Photo").fadeOut(400, function() {
			$("#Photo").attr("src", "images/"+photos[currentphoto]);
			$("#Photo").load(function(){$("#Photo").fadeIn(600);});
			if ($("#slidecaptions").text() != "") {
				$("#slideCaption").text((1 + currentphoto) + " of " + photos.length +" " + captions[currentphoto]);
			}
		});
		return false;
	}
	function ssPrevPhoto() {
		currentphoto--;
		if  (currentphoto < 0) { 
			currentphoto = photos.length-1;
		}
		$("#Photo").fadeOut(400, function() {
			$("#Photo").attr("src", "images/"+photos[currentphoto]);
			$("#Photo").load(function(){$("#Photo").fadeIn(600);});
			if ($("#slidecaptions").text() != "") {
				$("#slideCaption").text((1 + currentphoto) + " of " + photos.length +" " + captions[currentphoto]);
			}
		});
		return false;
	}


	function initSlideshow() {
		currentphoto = 0;
		
		projcontentY = $("#projectcontent").css("top");

		if ($("#PrevLink")) {
			$("#PrevLink").click(ssPrevPhoto);
			$("#NextLink").click(ssNextPhoto);
				
			$("#projectinfo").css("cursor", "pointer");
			$("#projectinfo").click(function() {
				projcontenton = !projcontenton;
				if (projcontenton == true) {
					$("#projectcontent").animate({top: "0px"}, 500)
					$("#projectinfo").attr("src", "images/projectinfo_open.gif");
				} else {
					$("#projectcontent").animate({top: projcontentY}, 500);
					$("#projectinfo").attr("src", "images/projectinfo_closed.gif");
				}
			
			});
		}

		$("#Photo").hide();
		$("#Photo").attr("src", "images/"+photos[currentphoto]);
		$("#Photo").fadeIn(400); //attr("src", "images/"+photos[currentphoto])
		if ($("#slidecaptions").text() != "") {
			$("#slideCaption").text((1 + currentphoto) + " of " + photos.length +" " + captions[currentphoto]);
		}

		clearInterval(autoslideshow);
		if ($("#Photo").hasClass("automatic")) {
			autoslideshow = setInterval(ssNextPhoto, 5500)
		} 

	}


	/* Accordion Controls */	  
	var accordion = [null, null];
	
	$(".navsubcontent").hide();
	$(".navcontent > img").css("cursor", "pointer");
	$(".navcontent img").click(function(){ 
		if ($(this).next().hasClass("selected") == false) {
			for (var i = 1; i <= accordion.length; i++) {
				if ($(this).next().hasClass("level"+i)) {
					if (accordion[i] != null) {
						accordion[i].slideUp(500);
						accordion[i].removeClass("selected");
					}
					
					accordion[i] = $(this).next();
				}
			}
			$(this).next().addClass("selected");
			$(this).next().slideDown(500);
		} else {
			$(this).next().removeClass("selected");
			$(this).next().slideUp(500);
		}
	});

	function clickLink() {
		// History
		var url = this.href.split("/");
		url = (url[url.length-1].split("."))[0];
		var hash = url;//this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		$.history.load(hash);
		//return false;

		return false;
		
	}

	 // Click / Load Content
	 $(".navsubcontent a").click(clickLink);
	 $("a.header").click(clickLink);	 
	 $("a.directlink").click(function() {
			for (var i = 1; i <= accordion.length; i++) {
					if (accordion[i] != null) {
						accordion[i].slideUp(500);
						accordion[i].removeClass("selected");
					}
				
			}	 
			return clickLink.call(this);
		 
	 });	 

	 /* Init History */

	function historyInitialize(hash) {
		// hash doesn't contain the first # character.
		if(hash) {

		} else {
			hash = "index";
		}

			// restore ajax loaded state
			$("#content").load("content/" + hash + ".html #loadcontent", function (responseText, textStatus, XMLHttpRequest) {
				if ($("#SlideContainer")) {
					
					photos = ($("#slidephotos").text()).split(",");
					captions = ($("#slidecaptions").text()).split(",");					
					initSlideshow();
				} else {
					photos = [];
					captions = [];
				}
			});
	}
	
	$.history.init(historyInitialize); 

	var hash = String(document.location);
	hash = hash.replace(/^.*#/, '');
	if (hash) {
		$("a[href*='"+hash+"']").parent().parent().prev().click();
		$("a[href*='"+hash+"']").parent().parent().parent().parent().prev().click();
	}



 });
