// JavaScript Document
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
jQuery.preLoadImages("/mathenygoldmon/images/loading.gif");


$(document).ready(function() {
						   
	//Resize the slide to fit the it's container proportionally (.portfolioImage)
	function resizeImage(){
		var window_height = $(window).height() - 57;
		var window_width  = $(window).width() - 136;
		var image_width = $(".portfolioImage img.current").width();
		var image_height = $(".portfolioImage img.current").height();
		var height_ratio  = image_height / window_height;
		var width_ratio   = image_width / window_width;
		if (width_ratio > height_ratio) { 
			$(".portfolioImage img.current").css({'width' : window_width,'height' : 'auto'});
		} else if (height_ratio > width_ratio) {
			$(".portfolioImage img.current").css({'width' : 'auto', 'height' : window_height});
		}
		$(".portfolioImage").css({'width' : window_width,'height' : window_height});
	}
	resizeImage();
	$(window).resize(function(){ resizeImage(); });
	//END: Resize slide 	
	
	
					   
						   
	//Nav
	 $('li.portfolio').hover(function() {
		$('li.portfolio .secondary').show("slide", { direction: "left" }, 300);
	 } , function() {
		 $('li.portfolio .secondary').hide();
	 });
	 $('.secondary').hover(function() {
	 	 $('li.portfolio .tirtuary').show("slide", { direction: "left" }, 300);
	 });
	 
	 $('li.projects').hover(function() {
		$('li.projects .secondary').show("slide", { direction: "left" }, 300);
	 } , function() {
		 $('li.projects .secondary').hide();
	 });
	 $('.secondary').hover(function() {
	 	 $('li.projects .tirtuary').show("slide", { direction: "left" }, 300);
	 });
	 //END: Nav
	
	 
	 //Thumbnail Changer 
	 $('.tirtuary a').hover(function() {
		var tn = $(this).attr("class");
		var tn = "<img src=\"images/projects/"+tn+".jpg\" />";
		var thisul = $(this).parents().filter('ul').attr("class").replace("tirtuary ",".");
		$(thisul+' .project_thumbnail').attr("id","iefix");
		//if tn is not loaded display this
		$(thisul+' .project_thumbnail').html("<div class=\"loading\"><img src=\"images/loading.gif\" width=\"16\" height=\"16\" /></div>");
		$(tn).load(function() {
			$(thisul+' .project_thumbnail').html(tn);
	 	});
	 } , function() {
		 $('.project_thumbnail img').hide();
		 $('.project_thumbnail .loading').hide();
	 });
	//END: Thumbnail Changer 
	
	
	//Show and Hide Info Box
	$('.show').click(function() {
		$('#infobox').removeClass('hidden');
		$('.info').show(); 
		$('.hide').show();
		$('.show').hide();
		return false; 
	 });
	
	$('.showhide .hide').click(function() {
		$('.info').hide(); 
		$('#infobox').addClass('hidden');
		$('.hide').hide();
		$('.show').show();
		return false; 
	 });
	//END: Show and Hide Info Box
	
	
	
	//Get the Before Photo (if one exists)
	function showBeforePic() {
  	  var currentSlide = $(".portfolioImage img.current").attr('src');
	  var currentBeforePhoto = currentSlide.replace('.jpg', '_before.jpg');
	  $(".before_photo").html("<img src=\""+currentBeforePhoto+"\" />");
	}
	//End: showBeforePic
	
	
	
	
	
	
	//Switch Project Image
	$(".portfolioImage img").hide();
	$(".portfolioImage img.current").show();
	$('.gallery_links .image_links a').click(function() {
		
		var photo = $(this).attr("id");
		var photo = "#img"+photo;
		//a.current
		if($(this).attr("class") == "current"){
			return false;
		//a.nobefore current
		} else if($(this).attr("class") == "nobefore current"){
			$("#before_photos_box").hide();
			return false;
		//a.nobefore
		} else if($(this).attr("class") == "nobefore"){
			$(".gallery_links .image_links a:not(this)").removeClass("current");
			$(this).addClass("current");
			$(".portfolioImage img:not(photo)").removeClass("current").fadeOut(200);
			setTimeout(function() {
				$(photo).addClass("current").fadeIn(200);
				resizeImage();
				$("#before_photos_box").hide();
			}, 210);
			return false;			
		//a
		} else {
			$(".gallery_links .image_links a:not(this)").removeClass("current");
			$(this).addClass("current");
			$(".portfolioImage img:not(photo)").removeClass("current").fadeOut(200);
			setTimeout(function() {
				$(photo).addClass("current").fadeIn(200);
				resizeImage();
				showBeforePic();
				$("#before_photos_box").show();
			}, 210);
			return false; 
		}
	 });
	 //END: Switch Project Image
	 
	 
	 //Show and Hide Before and After Photos
	 $('#before_photos_box').addClass("hidden");
	 $('.before_photos_showhide').click(function() {
		$('#before_photos_box').toggleClass("hidden");
		if ($('.before_photos_showhide .showhidesign').html() == "+") { $('.before_photos_showhide .showhidesign').html("x");}
	 	else if ($('.before_photos_showhide .showhidesign').html() == "x") { $('.before_photos_showhide .showhidesign').html("+");}
		return false; 
	 });
	 //END: Show and Hide Before and After Photos
	 
	 /*
	 if ($(this).attr("class") == "nobefore"){
		$("#before_photos_box").hide();  
	 } else {
		$("#before_photos_box").show(); 
	 }
	 */
		
	 
});
