$(document).ready(function() {
  
  //Header home page anchor actions
  $("#header h1").mouseover(function(){ $(this).css("cursor","pointer"); }).click(function(){ location.href= "/"; });
  $("#subHeader h1").mouseover(function(){ $(this).css("cursor","pointer"); }).click(function(){ location.href= "/"; });
  
  //Gallery array of image src
  var galleryArray = [];
  $("#thumbs img").each(function(i){
	galleryArray[i] = $(this).attr("src")						 
  });
  
  //Photo Gallery Thumbnail viewer
  $("#thumbs img").fadeTo(300, 0.8); //makes all images 80% visible
  $("#thumbs img:first").addClass("selected");
  $("#thumbs img").hover(function() { 
	  $(this).fadeTo(300, 1);  //removes transparency from image when hover over state activated
	  $(this).css("border","1px solid red");  //initiate hover over state
  }, function() {
	  $(this).fadeTo(300, 0.8); //makes image 80% visable on hover out
	  $(this).css("border","1px solid #1F792F");  //initiate hover out state
  });
  
  //Photo Gallery preview box
  $("#thumbs img").click(function(){ //initiate photo transfer from thumbnail viewer to preview box				  
  	  var thumb = $(this).attr("src"); //grab the location of the thumbnail on the server
	  var image = thumb.replace(/t.jpg/, ".jpg"); //replace the generic thumbnail nail - t - to pull in the large image
	  var update = "<img src=\"" + image + "\" alt=\"\" />"; //generate the html code to be injected in the preview box
	  
	  $("#thumbs img").removeClass("selected");
	  $(this).addClass("selected");
      $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
	  	  $(this).removeClass("hidden")
	  }).html(update).addClass("visible").fadeTo(300, 1); //creates the fade out and fade in effect as well as injects the new image
  });
  
  //Previous slideshow button
  $(".previous").click(function(){
	  var current = $(".fullView img").attr("src");
	  var currentImage = current.replace(".jpg", "t.jpg"); //replace the generic thumbnail nail - t - to pull in the large image
	  var previous = $.inArray(currentImage, galleryArray);
	  
	  if(previous == "0" || previous == "-1"){
		  var previousThumb = $("#thumbs img:last").attr("src");
		  var previousFull = previousThumb.replace(/t.jpg/, ".jpg"); 
		  var previousUpdate = "<img src=\"" + previousFull + "\" alt=\"\" />"; 
		  
		  $("#thumbs img").removeClass("selected");
		  $("#thumbs img:last").addClass("selected");
		  $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
		  	  $(this).removeClass("hidden")
		  }).html(previousUpdate).addClass("visible").fadeTo(300, 1); 
	  } else {		  
	      var previousThumb = galleryArray[previous-1];
		  var previousFull = previousThumb.replace(/t.jpg/, ".jpg"); 
		  var previousUpdate = "<img src=\"" + previousFull + "\" alt=\"\" />"; 
		  
		  $("#thumbs img").removeClass("selected");
		  $("#thumbs img").eq(previous-1).addClass("selected");
		  $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
		  	  $(this).removeClass("hidden")
		  }).html(previousUpdate).addClass("visible").fadeTo(300, 1); 
	  }
	  return false;
   });
  
  //Next slideshow button
  $(".next").click(function(){
	  var current = $(".fullView img").attr("src");
	  var currentImage = current.replace(".jpg", "t.jpg"); //replace the generic thumbnail nail - t - to pull in the large image
	  var next = $.inArray(currentImage, galleryArray);
	  
	  if (next == "-1" || next == "0"){
		  var nextThumb = $("#thumbs img").eq(1).attr("src");
		  var nextFull = nextThumb.replace(/t.jpg/, ".jpg"); 
		  var nextUpdate = "<img src=\"" + nextFull + "\" alt=\"\" />"; 
		  
		  $("#thumbs img").removeClass("selected");
		  $("#thumbs img").eq(1).addClass("selected");
		  $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
		  	  $(this).removeClass("hidden")
		  }).html(nextUpdate).addClass("visible").fadeTo(300, 1);
		  
	  } else if (next == $("#thumbs img").length-1) {
		  var nextThumb = $("#thumbs img").eq(0).attr("src");
		  var nextFull = nextThumb.replace(/t.jpg/, ".jpg"); 
		  var nextUpdate = "<img src=\"" + nextFull + "\" alt=\"\" />"; 
		  
		  $("#thumbs img").removeClass("selected");
		  $("#thumbs img").eq(0).addClass("selected");
		  $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
			  $(this).removeClass("hidden")
		  }).html(nextUpdate).addClass("visible").fadeTo(300, 1); 
	  } else {	
	      var nextThumb = galleryArray[next+1];
		  var nextFull = nextThumb.replace(/t.jpg/, ".jpg"); 
		  var nextUpdate = "<img src=\"" + nextFull + "\" alt=\"\" />"; 
		  
		  $("#thumbs img").removeClass("selected");
		  $("#thumbs img").eq(next+1).addClass("selected");
		  $(".fullView").addClass("hidden").fadeTo(300, 0, function(){ 
		  	  $(this).removeClass("hidden")
		  }).html(nextUpdate).addClass("visible").fadeTo(300, 1); 
	  }
	  return false;
   });
});