jQuery(document).ready(function($) {
  var History = window.History;
  var click_state = false;
  $("#body-copy").animate({'bottom':'0px'});
  $("#body-headline").animate({'top':'0px'});
  $("#body-gallery").animate({'left':'0px'});
  $("#main-inner").css({'margin-left':$("li.current a").attr('data-left')+'px'});

  $("#nav a, #logo a").click(function(e){
    click_state = true;
    e.preventDefault();
    if (supports_history_api()) {
      History.pushState(null, null, $(this).attr('href'));
    }
    nav_click($(this));
    document.title = $(this).attr('rel');
  });
  var nav_click = function(ele){
    var nav_href = ele.attr('href')+" #content-inner";
    $("#body").animate({'opacity':'0'}, function(){
      $("#hero-slides").cycle('destroy');
      $("#content").load(nav_href, function(rspText, txtStatus, req){
        // Slide in and fade in
        $("#body-headline").css({'top':'-400px'}).animate({'top':'0px'});
        $("#body-copy").css({'bottom':'-400px'}).animate({'bottom':'0px'});
        $("#body-gallery").css({'left':'-400px'}).animate({'left':'0px'});
        $("#body").animate({'opacity':'1'});
        // since the homepage could also be loaded
        page_load();
      });
    });
    $('li.navmenu-item1').removeClass('current');
    ele.parents('li.navmenu-item1').addClass('current');
    $("#main-inner").animate({'margin-left':ele.attr('data-left')+'px'});
  };

  function page_load() {
    // homepage hero cycle
    // docs at: http://jquery.malsup.com/cycle/
    if ($("#hero-slides .hero-slide").length >= 2) {
      $("#hero-slides").cycle({
        fx: 'fade',
        pager: '#hero-nav',
        pagerEvent: 'mouseenter.cycle',
        allowPagerClickBubble: true,
        pauseOnPagerHover: 1,
        pagerAnchorBuilder: function(idx, slide) {
          return '#hero-nav li:eq('+idx+')';
        }
      });
    }
    //Trigger same animate effect on clicking the hero links that link to a page.
    $("#hero-nav a, #hero-slides a").bind('click', function(e) {
      // if the same href exists in the top menu, then trigger that one. Otherwise, just let it slide.
      var href = $(this).attr('href');
      var top_menu_link = $(".navmenu-item1 a[href='"+href+"']");
      if (top_menu_link.length) {
        e.preventDefault();
        top_menu_link.trigger('click');
      }
    });

    $("#tabs").tabs();

    // override the normal cyclepager if this is loaded
    if ($('.content').length) {
      var js_args = {
        width:548,
        thumbnail_height:92,
        thumbnail_width:165,
        height:308 };
      $('.content').cyclepager({
        dimensions:js_args
      });
    }

  }
  page_load();

  function supports_history_api() {
    return History.enabled;
  }
  if (supports_history_api()) { 
    History.Adapter.bind(window, 'statechange', function() {
      if (click_state == false) { // skip over calling nav_click since the click has already called it.
        var State = History.getState();
        // when hitting the back button trigger the page load if in the top menu.
        var top_menu_link = $(".navmenu-item1 a[href='"+State.url+"'], #logo a[href='"+State.url+"']");
        if (top_menu_link.length) {
          nav_click(top_menu_link);
        }
      }
      click_state = false;
    });
  } else {
    // no support window.history
  }

});


