(function($) {
	
	var sc_interval;
	var sc_numberOfPages;
	var sc_currentPage = 0;
	
	function getHash() {
		return (h = window.location.hash) ? h : false;
	}
	
	function showProject(hash) {
		
		// IE sets all anchor hrefs to full path, even if only contains # portion
		if(hash) {
			hash = hash.substr(hash.indexOf('#'));
		}
		
		if(!hash || hash == "#") {
			hash = "#" + $(".project:first").attr("id");
		}
				
		// replace viewer content with project content
		var newHTML = $(hash).html();
		$("#extra-vw-info").html(newHTML);
		
		// hide the images list
		$("#extra-vw-info .pj-images").hide();

		// clear the image viewer
		$("#extra-vw-imageframe").empty();
		
		// add in all the img elements and default to hide
		$("#extra-vw-info .pj-images a").each(
			
			function(index) {
				var href = $(this).attr("href");
				$("<img src=\"" + href + "\" />").appendTo("#extra-vw-imageframe");
			}
		);
		// hide the img elements that were created
		 $("#extra-vw-imageframe img:gt(0)").hide();
		
		// reset the interval timer so don't get multiples firing
		clearInterval(sc_interval);
		
		// slideshow events chain
		sc_interval = setInterval(function(){
			$('#extra-vw-imageframe :first-child').fadeOut("slow")
			.next("img").fadeIn("slow")
			.end().appendTo("#extra-vw-imageframe");}, 
		3000);
		
	}
	
	function updateControls() {
				
		if(sc_currentPage <= 0) {
			$("a#extra-vw-previous").hide();
		}else {
			$("a#extra-vw-previous").show();		
		}
		
		if(sc_currentPage >= (sc_numberOfPages-1)) {
			$("a#extra-vw-next").hide();
		}else {
			$("a#extra-vw-next").show();		
		}	
	}
	
	//init the viewer
	
	$(document).ready(function() {
		
		// hide the project elements
		$(".project").hide();
		
		// show the project viewer
		$("#extra-viewer").show();
		
		// create thumbs list from first thumb in each project
		$(".project").each(
			
			function(index) {
				
				var id = $(this).attr("id");
				var thumbURL = $(this).find(".pj-images img:first").attr("src");
				var title = $(this).find("h3:first").text();
				
				$("<li><a href=\"#" + id + "\" title=\"" + title + "\"><img src=\"" + thumbURL + "\" /></a></li>").appendTo("#extra-vw-thumbslist");
			}
		);
		
		// set width of thumbs container
		var thumbsCount = $("#extra-vw-thumbslist li").length;
		$("#extra-vw-thumbslist").css("width", 105 * thumbsCount + "px");
		
		// set number of pages required
		sc_numberOfPages = Math.ceil(thumbsCount / 3);
				
		// add click handlers to thumbs
		$("#extra-vw-thumbslist li a").click(
			function() {
				// allow default behaviour so named anchors append to URL for bookmarking etc
				showProject($(this).attr("href"));
			}
		);
		
		// thumb viewer controls
		$("a#extra-vw-next").click(
			function(event) {
				event.preventDefault();
				sc_currentPage++;
				updateControls();
				$("#extra-vw-thumbslist").animate({"marginLeft" : (315 * sc_currentPage * -1) + "px"});
			}
		);
		
		$("a#extra-vw-previous").click(
			function(event) {
				event.preventDefault();
				sc_currentPage--;
				updateControls();
				$("#extra-vw-thumbslist").animate({"marginLeft" : (315 * sc_currentPage * -1) + "px"});
			}	
		);
		
		updateControls();
		showProject(getHash());
		
	});
	
	
})(jQuery);
