$.fn.transitionGeneral = function() {
  $(document)
    .attr('title', $('#pageTitle', this).text())
    .find('.content')
    .attr('class', '')
    .attr('class', $('.content', $data).attr('class'));
  return this;
}

$.fn.levelTransition = function() {
  var $link = this;
  $.get( '/ajax'+ $link.attr('href'), function(data) {
    $data = $(data);
    $data.relativeTransition('hide');
    $link.removeClass('unfocused').addClass('selected').parent().siblings('li').children('a').addClass('unfocused');
    $newSubsection = $link.siblings('ul');
    $currentSubsection = $('#categoryNavigation li ul:visible');
    if( $newSubsection[0] != $currentSubsection[0] ) {
      $('a', $newSubsection).removeClass('selected');
      $('#contentHeader .location .section').fadeOut(500,function() { $(this).html( $('#contentHeader .location .section', $data ).html() ).fadeIn(); });
      if( $currentSubsection.length ) {
        $('#categoryNavigation li ul:visible').animate({'margin-top':0,'height':'0px','opacity':0},1000, function() {
          $(this).hide().css({'margin-top':'1em', 'height':'auto'});
        });
      }
      height = $newSubsection.outerHeight(false)+'px';
      $newSubsection.css({'margin-top':0,'height':0,'opacity':0}).show().animate({'margin-top':'1em','height':height,'opacity':1 }, 1000);
    } else {
      $('a', $currentSubsection).removeClass('selected');
    }
    $('#categoryContent').fadeOut(500, function() {
      $data.transitionGeneral().relativeTransition('show');
      $(this).html( $data.find('#categoryContent') ).fadeIn(500).bindAnchors();
      $(document).trigger('pageLoad');
      application.resizeContent( function() { $(document).trigger('pageReady'); });
    });
  });
}

$.fn.sectionTransition = function() {
  var $link = this;
  $.get( '/ajax'+ $link.attr('href'), null, function(data) {
    $data = $(data);
    $data.relativeTransition('hide');
    $link.removeClass('unfocused').addClass('selected').parent().siblings('li').children('a').removeClass('selected unfocused')
    $('#contentHeader .location .section').fadeOut(500,function() { $(this).html( $('#contentHeader .location .section', $data ).html() ).fadeIn(); });
    $subsection = $('#categoryNavigation ul:visible');
    if( $subsection.length ) {
      $('#categoryNavigation ul ul:visible').animate({'margin-top':0,'height':0,'opacity':0},1000, function() {
        $(this).hide().css({'height':'auto'});
      });
    }
    $('#categoryContent').fadeOut(500, function() {
      $data.transitionGeneral().relativeTransition('show');
      $(this).html( $data.find('#categoryContent') ).fadeIn(500).bindAnchors();
      $(document).trigger('pageLoad');
      application.resizeContent( function() { $(document).trigger('pageReady'); });
    });
  });
}

$.fn.subsectionTransition = function() {
  var $link = this;
  $.get( '/ajax'+ $link.attr('href'), null, function(data) {
    $data = $(data);
    $link.addClass('selected').parent().siblings().find('a').removeClass('selected');
    $link.parent().parent().prev().children('a').removeClass('selected');
    $data.transitionGeneral().relativeTransition('hide');
    $('#categoryContent').fadeOut(500, function() {
      $data.relativeTransition('show');
      $(this).html( $data.find('#categoryContent') ).fadeIn(500).bindAnchors();
      $(document).trigger('pageLoad');
      application.resizeContent( function() { $(document).trigger('pageReady'); });
    });
  });
}

$.fn.relativeTransition = function( state ) {
  var $relative = $('#contentHeader .relative');
  if( state == 'hide'&& $relative.is(':visible') ) {
    $relative.fadeOut(500);
  } else if( state == 'show' && $('#contentHeader .relative', this).text() ) {
    $relative.show().html( $('#contentHeader .relative', this).html() ).bindAnchors();
  }
}

$.fn.bindAnchors = function() {
  if( !($.browser.msie && $.browser.version < 8) ) {
    $("a[href^=/]", this).unbind('click.transistion').bind('click.transition', function(event) {
      $(document).trigger('pageLeave');
      event.preventDefault();
      window.location = '#'+$(this).attr('href');
      if( $(this).hasClass('file') ) {
        var newWindow = window.open( $(this).attr('href'), '_blank');
        newWindow.focus();
      } else if( $(this).hasClass('subsection') ) {
        $(this).subsectionTransition();
      } else if( $(this).hasClass('section') ) {
        $(this).sectionTransition();
      } else if( $(this).hasClass('level') ) {
        $(this).levelTransition();
      } else {
        application.transition( $(this).attr('href') );
      }
    });
  }
  return this;
}

var application = new function() {
  
  var init = function() {
    $(document).bind('pageLoad', function() {
      $('.colorbox').colorbox({width:'50%', height:'80%', iframe:true});
    });
    $(document).trigger('pageLoad').bindAnchors();
    $(window).load( function() {
      $(window).resize( function() {
        resizeContent( function() { $(document).trigger('pageResize'); });
      });
      resizeContent( function() { $(document).trigger('pageReady'); });
    });
  }

  var transition = function(target) {
    window.location = '#'+target;
    $.get( '/ajax'+target, null, function(data) {
      $('#footer').fadeOut(750);
      $('#content > div').fadeOut(1000, function() {
        $strip = $('<div>')
        .css({
          backgroundColor:'#808080',
          height:'4px',
          margin:'4px auto',
          width:'20px'
        })
        .prependTo('#content').slideDown(300, function() {
          $(this).animate({
            width:'900px'},
            1000,
            function() {
              $(this).fadeOut(300, function() {
                $(this).remove();
                $('#footer').fadeIn(500);
                $data = $(data);
                $data.transitionGeneral();
                $('#content')
                  .html('')
                  .hide()
                  .append( $data )
                  .find('.scrollArea')
                  .height(0);
                $(document).trigger('pageLoad');
                $('#content').show().bindAnchors();
                resizeContent( function() { $(document).trigger('pageReady'); });
              });
            }
          );
        });
      });     
    });    
  }

  var resizeContent = function(callback) {
    $('.content .scrollArea').animate({
        height: getContentHeight() + 'px'
      }, 400, function() {
        if(typeof callback == 'function'){
          callback.call();
        }
      }
    );
  }

  var getContentHeight = function() {
    height = $(window).height() - ( $('.contentHeader').outerHeight(true) + $('#contentHeader').outerHeight(true) + $('#header').outerHeight(true) + $('#footer').outerHeight(true) );
    if( $('.scrollArea').children().outerHeight(true) < height ) {
      return $('.scrollArea').children().outerHeight(true)
    } else {
      return height;
    }
  }

  return {
    init: init,
    resizeContent: resizeContent,
    transition: transition
  }
 
}
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17195604-1']);
_gaq.push(['_trackPageview']);
(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready( function() {
  application.init();
});
