$(document).ready(function(){	
	
	
	var myFile = document.location.toString();
	if (myFile.match('#')) { // the URL contains an anchor
	  // click the navigation item corresponding to the anchor
	  var myAnchor = '#' + myFile.split('#')[1];

	} 
		

	//put all the folioblock elements into an array, and assign each a number

	$("#viewall").click(function(){
		$(".folioblock .nextbutton,.folioblock .previousbutton").hide();
		$("#pageview").show();
		$("#viewall").hide();			
		$(".folioblock").fadeIn("fast");
	});

	$("#pageview").click(function(){
		$(".folioblock").hide();
		$(".folioblock").prepend("<a href=\"#\" class=\"nextbutton\"><img  src=\"images/nextbutton.gif\" /></a><a href=\"#\" class=\"previousbutton\"><img  src=\"images/previousbutton.gif\"  /></a>");
		$(".folioblock .nextbutton,.folioblock .previousbutton").show();
		$(".folioblock:first .previousbutton").hide();
		$(".folioblock:last .nextbutton").hide();
	
		$("#viewall").show();
		$("#pageview").hide();
		//insert the next and back buttons
	
		$(".folioblock").each(function(i){
			//assign an id to each with a unique number 
			$(this).attr("id", "item"+(i+1));
			$(this).find(".nextbutton").attr("href",("#item"+(i+2)));
			$(this).find(".previousbutton").attr("href",("#item"+(i)));


			//need to work out functionality for next and back...
			$(this).find(".nextbutton").click(function(){
				//alert($(this).attr("id"));				
				$(this).parent(".folioblock").hide();
				$("[id='item"+(i+2)+"']").fadeIn();				
			});

			$(this).find(".previousbutton").click(function(){
				//alert($(this).attr("id"));				
				$(this).parent(".folioblock").hide();
				$("[id='item"+(i)+"']").fadeIn();				
			});
		});
		
			if(myAnchor){

				$(myAnchor).fadeIn();
			}else{
					$(".folioblock:first").fadeIn();
			}
		
	});

	$("#viewall").click();
});
