/* Fading for the Homepage */
/* NOTE: There is a handler defined on the BODY element. This is not ideal, but Safari and FF detect this and alter 
   their in-memory page caching behavior that can cause issues with navigating backwards after a fade out. */
// Fade out in ms
var fadeOutDuration = 2000;
// Defines the selector for the links we want to have trigger the fade
var fadeTriggerQuery = "#header a";

function wireFadeOutHandlers() {
	$(fadeTriggerQuery).click(function(event) {
		// the url from the link that we'll append our fade parameter to 
		var url;
		// stop the link's natural behavior
		event.preventDefault();
		url = $(this).attr('href') + '?fade=1';
		// fade out the img and div elements 
		$("img").add($("div")).fadeOut(fadeOutDuration,function(){navigateInternal(url);});
		});
}

function initializeSupersize(e) {
  var len = $(e).find('>*').length;
  $.fn.supersized.options = {  
    navigation: 0,
    slide_counter: 0,
    slide_captions: 0,
    transition: 1,
    slide_interval: 8000,
    slideshow: 1
  }
  if (len <= 1) {
    $.fn.supersized.options.slideshow = 0;
  }
  $(e).supersized();
}

function initializeCufon() {
	// Homepage add style to navigation for cufon
	$(".home #primeNav").addClass('univers');
	Cufon.replace('.home .univers'); 
}

// How this logic got seperated:  http://api.jquery.com/fadeOut/#comment-46545806
function navigateInternal(url) {
	window.location = url;
}

/* Document Ready */
$(document).ready(function() {
	initializeCufon();
	wireFadeOutHandlers();
	initializeSupersize('div#supersize');
	// don't try to follow links created by supersized
	$("a[href=#]").bind("click", function(event){
		event.preventDefault();
	});
});
