$(function(){
  $('#coda-slider-1').codaSlider({
    dynamicArrows: false,
    dynamicTabs: false
  });
  
  $("a[rel=freeform_screenies]").fancybox({
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'opacity'         : false,
    'overlayShow'     : true
  });
  
  $("a[rel=zoecity_screenies]").fancybox({
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'opacity'         : false,
    'overlayShow'     : true
  });
  
  $("a[rel=startnow_screenies]").fancybox({
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'opacity'         : false,
    'overlayShow'     : true
  });
  
  $("a[rel=showtimes_screenies]").fancybox({
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'opacity'         : false,
    'overlayShow'     : true
  });
  
  $("a[rel=ravejoint_screenies]").fancybox({
    'transitionIn'    : 'elastic',
    'transitionOut'   : 'elastic',
    'opacity'         : false,
    'overlayShow'     : true
  });
  
  // Highlight the nav
  select_nav(window.location.hash);
  
  $(window).hashchange( function() {
    select_nav(location.hash);
  });
  
  function select_nav(hash) {
    $("ul#nav li").removeClass("selected");
    
    if (hash == "#2" || hash == "#3" || hash == "#4") {
      $("#our_work").addClass("selected");
    }
    
    if (hash == "#5" || hash == "#6") {
      $("#our_clients").addClass("selected");
    }
    
    if (hash == "#7") {
      $("#our_contact").addClass("selected");
    }
  }
  
  // Key navigation
  $(document.documentElement).keyup(function (event) {
    var direction = null;

    // handle cursor keys
    if (event.keyCode == 37) {
      // go left
      direction = 'prev';
    } else if (event.keyCode == 39) {
      // go right
      direction = 'next';
    }

    if (direction != null) {
      elem = $('.coda-slider-wrapper ul a.current').parent()[direction]().find('a');
      
      if (elem.attr("href") == undefined) {
        
      } else {
        window.location.hash = elem.attr("href");
      }
                  
      $(elem).click();
    }
  });
  
});
