///////////////////////////////////////////////////////////////////////////////////////////////////

//adds the class 'js' to the html tag so the 'html .js' style will hide the plain text headers
//we don't want this in document.ready() because it needs to happen right away
document.getElementsByTagName('html')[0].className = 'js';


///////////////////////////////////////////////////////////////////////////////////////////////////

//swaps the text for a graphic with the same name
function textToImage(selector, extension) {
	$(selector).each(function() {
      string = $(this).text();
      filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
      $(this).html('<img src="http://adamstoddard.com/template_assets_v8/images/' + filename + extension+'" alt="' + string + '" />');
    });
    
}

///////////////////////////////////////////////////////////////////////////////////////////////////

//fade in and out the image overlay
function folioRoll() {
	$('a.overlay').hover(
		function () {
			//rollover
			$(this).parent().find("div.info").fadeIn("fast");
	      }, 
		function () {
			//rollout
			$(this).parent().find("div.info").fadeOut("fast");
	      }
	    );
}

///////////////////////////////////////////////////////////////////////////////////////////////////

//show the notes on the home page
function noteClick() {
	$("#home-page a.button").click(function () { 
	$(this).parent().parent().find("div.home-details").animate({opacity: 'toggle'}, "fast"); 
			return false;
	    });
}

///////////////////////////////////////////////////////////////////////////////////////////////////

//fade in and out the image overlay
function navRoll() {
	$("#nav a:not(#nav a.selected, #nav #logo a)").jFade({
			trigger: "mouseover",
			property: 'background',
			start: '1C1C1C',
			end: '0071bc',
			steps: 20,
			duration: 15
		}).jFade({
			trigger: "mouseout",
			property: 'background',
			start: '0071bc',
			end: '1C1C1C',
			steps: 20,
			duration: 15
		});
}


///////////////////////////////////////////////////////////////////////////////////////////////////

//add or removes class from overlay based on image count
function setButtons() {
	$("a.zoom").click(function () { 
		var count = $(this).siblings("a.zoom").length; 
		if (count >= 2) {
			$("div#fancy_outer").addClass("buttons");
		}else{
			$("div#fancy_outer").removeClass("buttons");
		};
    });
}


///////////////////////////////////////////////////////////////////////////////////////////////////

//Google Analytics tracking
function trackPage() {
	try {
		var pageTracker = _gat._getTracker("UA-622471-1");
		pageTracker._trackPageview();
	} catch(err) {}
}


///////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() {
	
	//damn you Internet Explorer!!!
	if($.browser.msie == false) {
		
		//add the overlays to the images
		$('div.info').before("<a class='overlay zoom'></a>");
	
		//add the href to each images overlay
		$('a.folio-pic').each(function(index) {
		    $(this).parent().find('a.overlay').attr({
		      'href': $(this).attr('href')
		    });
		  });

		//add the rel to the overlays that need it
		$('a.folio-pic').each(function(index) {
		    $(this).parent().find('a.overlay').attr({
		      'rel': $(this).attr('rel'),
			  'title': $(this).attr('title')
		    });
			$(this).removeAttr('rel');
		  });
		
	}
	
	//swap the text for images
	textToImage('.info p','.png');
	
	//drop the 'js' class from the html tag now that the text has been replaced
	$('html').removeClass();
	//drop the 'nojs' class from the body tag to stop css rollovers
	$('body').removeClass();
	
	//folio image actions
	folioRoll();
	navRoll();
	noteClick();
	
	//load image viewer
	$("a.zoom").fancybox();
	setButtons();
	
	//track the page with Google Analytics
	trackPage();
	
});

///////////////////////////////////////////////////////////////////////////////////////////////////