// Add some easing to hover
function showCategory(){ $(this).find("span").animate({top:"0px"}, 200); }
function hideCateogry() { $(this).find("span").animate({top:"30px"}, 200); }
function moveAboveTop(callback) { 
  callback = callback || function(){}
  if ( window.location.hash.length > 0 ){
    $("#page-wrap").animate({top:"-230px"}, 200, '', callback);
  }
  else {
    callback.call();
    $("#page-wrap").animate({top:"0px"}, 200);
  }
}

function loadContent(newLocationUrl, elementName){
  var contentWrapper = $("#" + elementName + "-wrapper");
  var toLoad = newLocationUrl + ' #' + elementName;
  $("ul.sub-nav a").removeClass("active");
  $("ul.sub-nav a").each(function(){
    if ($(this).attr('href').split('/')[1] == newLocationUrl.split('/')[1]) {
      $(this).addClass('active');
    }
  });
  
  $("ul.image-list a").removeClass("active");
  
  window.location.hash = newLocationUrl;
  moveAboveTop(function(){
    contentWrapper.fadeOut(500, function(){
      contentWrapper.load(toLoad, '', function(){
        contentWrapper.fadeIn(500, function(){
          $("#main-content p.lights").hide();
          $("ul.image-list a[href=" + newLocationUrl + "]").addClass("active");

          $("ul.image-list a").tipsy({gravity:'s'});
          
          var vimeo_id = $("#moogaloop").attr('data-vimeo_id');
          load_vimeo_player(vimeo_id, "wooo");
          Cufon.replace('#main-content p.lights', { fontFamily: 'Whitney' })
          $("#main-content p.lights").show();
        });
      });
    });
  });
}

$.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

function docHeight(){
  return Math.max( $(document).height(), $(window).height(), $("#page-wrap").height(), document.documentElement.clientHeight );
}

$(document).ready(function(){
  
  $("#site-nav a").live("click", function() { loadContent($(this).attr('href'), "content"); $(this).blur(); return false; })
  $(".image-list a").live("click", function() { loadContent($(this).attr('href'), "main-content"); $(this).blur(); return false; })
  
  var hash = window.location.hash.substr(1);
  if( hash.length > 0 ) { loadContent(hash, "content"); }

  $("#site-nav ul.image-nav li a").hover(showCategory, hideCateogry);
  
  $(".lights").live("click", function(){
    $("#shadow").css({"height" : docHeight});
    $("#shadow").fadeToggle();    
  });
});
