$(function() {  
  
  $('#navigation li').bind({
    click: function(e) {
      if (!$(this).hasClass('active')) {
        $('#navigation li.active').animate({left: '20'}, 200).removeClass('active');
        $(this).addClass('active');
      
        var show = $($(this).find('a').attr('href'));
        var scrollTo;
        var speed = 600;
        switch (show.attr('id')) {
          case 'about':
            scrollTo = 0;
          break;
          case 'networks':
            scrollTo = $('#about').height() + 20;
            speed = 500;
          break;
          case 'contact':
            scrollTo = ($('#about').height() + $('#networks').height() + 40);
          break;
        }
        if ($('#content .active').attr('id') == 'networks') {
          speed = 500;
        }
      
        var current = $('#content .active').removeClass('active').addClass('inactive');
        show.removeClass('inactive').addClass('active');
        if (!Modernizr.boxshadow) {
          var shadow_height = ($('#body').height() - current.height()) + show.height();
          $('.ie-shadow').stop().css({height: $('.ie-shadow').height() - 60}).animate({ height: shadow_height+'px' }, speed);
        }
        $('#content').stop().animate({ scrollTop: scrollTo, height: show.height() }, speed);
      
        _gaq.push(['_trackEvent', 'Menu', 'Selected', show.attr('id')]);
      }
    }
  });
  
  document.getElementById('workNav').focus();
  
  $(window).resize(function() {
    console.log('resize');
    console.log(document.documentElement.clientWidth +' - ' + $(window).width());
    console.log('#' + $('#content > .active').attr('id'));
    if ($(window).width() > 480) {
      scrollToLocation('#about');
    }
  });
  
  if ($(window).width() > 480) {
    scrollToLocation();
  }
  
  // Styling the unstylable! :)
  if (!Modernizr.boxshadow) {
    var body = $('#body');
    $('<div class="ie-shadow"></div>').css({
      height: body.height()+'px',
      width:  (body.width() - 10)+'px'
    }).appendTo('#container');
  }
});

//CHECK FOR IE6

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion<=6)
    document.write("Hi, your browser is out of date. Please <a href=&quot;http://www.webstandards.org/upgrade/&quot; title=&quot;The Web Standards Project's BROWSER UPGRADE initiative.&quot;>upgrade to a web standards compliant browser</a> for this site to display correctly. Thanks.")
}

function scrollToLocation(force_hash) {
  var hash_tag = window.location.hash;
  if (force_hash != undefined) {
    hash_tag = force_hash;
  }
  var link_accessor = '#navigation li a[href=#about]';
  switch (hash_tag) {
    case '#about':
    case '#networks':
    case '#contact':
      link_accessor = '#navigation li a[href=' + hash_tag + ']';
      $(hash_tag).addClass('active');
      var offset = $(hash_tag).offset().top - $(hash_tag).offsetParent().offset().top;
      $('#content').css({height: $(hash_tag).height() }).scrollTop(offset);
      break;
    default:
        $('#about').addClass('active');
        $('#content').css('height', $('#about').height());
      break;
  }
}

