var application = new function() {
  
  var init = function() {
    bindAnchors();
  }

  var bindAnchors = function() {
    $("a[href^=/]").bind('click.transition', function(event) {
      event.preventDefault();
      href = $(this).attr('href');
      delay = 230 / ($('#content .panels').length + 1);
      $('#content .panels > div:last').cascadeUp( delay, function() {
        $('#content .navigation').cascadeUp( delay, function() {
          $('#content .navigation, #content .panels > div').show();
          window.location = href;
        });
      });
    });
  }
  
  return {
    init: init,
    bindAnchors: bindAnchors
  }
 
}

$.fn.cascadeUp = function(delay, callback) {
  this.animate( { 'margin-top': '-24px', 'margin-bottom': '24px', 'opacity': 0 }, delay, function() {
      $(this).css({ 'visibility': 'hidden', 'opacity': 1 });
  });
  $prev = this.prev();
  if( $prev.length ) {
    setTimeout( function() {
      $prev.cascadeUp(delay, callback);
    }, delay );
  } else if(typeof callback == 'function'){
    setTimeout( function() { callback.call(); }, delay );
  } 
}

$.fn.cascadeDown = function(delay, callback) {
  this.css({ 'visibility': 'visible', 'opacity': 0 });
  this.animate( { 'margin-top': '0px', 'opacity': 1 }, delay );
  $next = this.next();
  if( $next.length ) {
    setTimeout( function() {
      $next.cascadeDown(delay, callback);
    }, delay );
  } else if(typeof callback == 'function'){
    setTimeout( function() { callback.call(); }, delay );
  } 
}

$(document).ready( function() {
  application.init();
});

