/*
by: Adrian Gyuricska @ Lift Interactive
*/

var w, h;

function resize(){
  var w = $(window).width();
  var h = $(window).height();
  
  $('.photos, .photos li').css({ 'width':w, 'height':h });
};

$(window).resize(function(){
  resize();
});

$(document).ready(function() {
  resize();
  
  $('.photos').each(function(i){
    $(this).before('<div class="nav"><a class="prev-'+i+'">Prev</a><a class="next-'+i+'">Next</a></div>').cycle({
      next: '.next-'+i,
      prev: '.prev-'+i,
      speed: 1000,
      speedIn: 500,
      speedOut: 500,
      timeout: 0,
      slideResize: 0,
      sync: 0
    });
  });
  
  $('nav #gallery').hover(function(){
    $(this).stop();
    $(this).addClass('over').animate({'top':-$(this).children('ul').outerHeight()+4},250);
  },function(){
    $(this).stop();
    $(this).animate({'top':0},250,function(){
      $(this).removeClass('over')
    });
  });
  
  // Hash Tracking with BBQ
  var cache = {
    '': $('#home')
  };
  $(window).bind( 'hashchange', function(e) {
    var url = $.param.fragment();
    
    $('a.current').removeClass('current');
    $('.page:visible').fadeOut();
    
    url && $( 'a[href="#' + url + '"]' ).addClass( 'current' );
    
    if ( cache[ url ] ) {
      cache[ url ].delay(500).fadeIn();
    } else {
      $('#'+url).delay(500).fadeIn();
    }
  })
  
  $(window).trigger( 'hashchange' );  
});

$(document).bind('keydown', 'left', function(){ $('.photos:visible').cycle('prev'); });
$(document).bind('keydown', 'right', function(){ $('.photos:visible').cycle('next'); });
$(document).bind('keydown', 'space', function(){
  if($('header:visible').length > 0){
    $('header, nav, footer, .nav').fadeOut(500);
  }else{
    $('header, nav, footer, .nav').fadeIn(500);
  }
});

$(window).load(function() {
  $('#loading').fadeOut(500, function(){
    $('#container').fadeIn(500);
  });
})
