$('html').addClass('js');

$(document).ready(function() {
  var currentPage = location.pathname;
  var sidebarLeft = 650;
  
  var transitionOut = function() {
    $('a').removeClass('current-page');
    $('#sidebar').stop(true, true).css({
      left: sidebarLeft
    }).animate({
      opacity: 'hide',
      left: sidebarLeft + 50
    }, 'slow', function() {
      $('#content').stop(true, true).fadeOut('slow');
    });
  };
  var transitionIn = function() {
    var buildPage = function() {
      $('#topic-icon img').stop(true, true).css({
        display: 'block',
        opacity: 0.01,
        width: 6,
        height: 6,
        paddingTop: 33,
        paddingLeft: 35
      }).animate({
        opacity: 1,
        width: 76,
        height: 72,
        paddingTop: 0,
        paddingLeft: 0
      }, 500);
      $('#sidebar').stop(true, true).css({
        left: sidebarLeft + 50
      }).animate({
        opacity: 'show',
        left: sidebarLeft
      }, 500);
      
      $('.home-section .icon img').each(function(index) {
        $(this).bind('transitionIn', function() {
          $(this).stop(true, true).css({
            display: 'block',
            opacity: 0.01,
            width: 8,
            height: 7,
            paddingTop: 55,
            paddingRight: 58,
            paddingLeft: 58
          }).animate({
            opacity: 1,
            width: 124,
            height: 117,
            paddingTop: 0,
            paddingRight: 0,
            paddingLeft: 0
          }, 250, function() {
            $('.home-section img').eq(index + 1).trigger('transitionIn');
          });
        });
      });
      $('.home-section img:first').trigger('transitionIn');
    };

    $('#content').fadeIn('slow', buildPage);
  };
  
  var setupPage = function() {
    transitionIn();

    $('a[href="' + currentPage + '"]').addClass('current-page');
  
    $('.headlines').each(function() {
      var $container = $(this);
      var url = $container.find('a').attr('href');
      
      $container.html('<div class="loading"></div>');
      $.get(url, function(data) {
        var $ul = $('<ul></ul>');
        $container.html($ul);
        $(data).find('item').each(function() {
          var $item = $(this);
          $('<li><a href="' + $item.find('link').text() + '">' + $item.find('title').text() + '</a></li>').appendTo($ul);
        });
      });
    });
  };
  
  $.history.init(function(hash) {
    if (hash == '') {
      hash = location.pathname;
    }
    if (currentPage == hash) {
      setupPage();
    }
    else {
      currentPage = hash;
      
      transitionOut();

      $('#content').load(currentPage, function() {
        setupPage();
      });
    }
  });
  
  // $('a[href^=/]').click(function() {
  //   var $link = $(this);
  //   if (location.pathname == '/') {
  //     $.history.load($link.attr('href'));
  //     $link.blur();
  //   }
  //   else {
  //     location.href = '/#' + $link.attr('href');
  //   }
  //   return false;
  // });
  
  $('#contact').ajaxForm({
    target: '#body',
    beforeSubmit: function() {
      var valid = true;
    
      $('#contact').find('input.required').each(function() {
        var $field = $(this);
        if ($field.val() == '') {
          $field.addClass('error');
          $field.parent()
            .animate({marginLeft: -5}, 100)
            .animate({marginLeft: 0}, 100)
            .animate({marginLeft: -5}, 100)
            .animate({marginLeft: 0}, 100)
            .animate({marginLeft: -5}, 100)
            .animate({marginLeft: 0}, 100);
          valid = false;
        }
        else {
          $field.removeClass('error');
        }
      });
    
      return valid;
    }
  }).bind('form-submit-notify', function() {
    $('#body').html('<div class="loading"></div>');
  });
});

