/* Modernizr 2.0.6 (Custom Build) | MIT & BSD
 * Contains: history
 */
;window.Modernizr=function(a,b,c){function w(a,b){return!!~(""+a).indexOf(b)}function v(a,b){return typeof a===b}function u(a,b){return t(prefixes.join(a+";")+(b||""))}function t(a){j.cssText=a}var d="2.0.6",e={},f=b.documentElement,g=b.head||b.getElementsByTagName("head")[0],h="modernizr",i=b.createElement(h),j=i.style,k,l=Object.prototype.toString,m={},n={},o={},p=[],q,r={}.hasOwnProperty,s;!v(r,c)&&!v(r.call,c)?s=function(a,b){return r.call(a,b)}:s=function(a,b){return b in a&&v(a.constructor.prototype[b],c)},m.history=function(){return!!a.history&&!!history.pushState};for(var x in m)s(m,x)&&(q=x.toLowerCase(),e[q]=m[x](),p.push((e[q]?"":"no-")+q));t(""),i=k=null,e._version=d;return e}(this,this.document);


$.fn.imagesLoaded = function(callback){
  var images = this.find('img');
  var total = images.length;
  images.load(function(){
    if (--total <= 0) callback.call(images, this);
  }).each(function(){
    if (this.complete || this.complete === undefined) {
      var src = this.src;
      this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
      this.src = src;
    }
  });
  return this;
};

function setProject(project) {
  var anchor = $('#project-list a[data-rel='+project+']')[0];
  $.getJSON(anchor.href+'?json', function(data) {
    $('#project').remove();
    $(data.html).hide().prependTo('#main').imagesLoaded(function(){
      $('#project').slideDown().click(function(){
        var next = $('#project-list a[data-rel='+current+']').parent().next('li').find('a[data-rel]');
        next = (next[0]) ? $(next[0]).attr('data-rel') : 'home';
        setState(next);
      });
    });
  });
}

function setState(state) {
  switch (state) {
    case 'home': {
      current = 'home';
      window.history.replaceState({}, "", '/');
      document.title = 'Kate Copeland | Illustration';
      $('#project').slideUp(500, function(){
        $('#main').removeClass('inactive').animate({color: '#000000'}).find('img').stop(true,true).animate({opacity: 1});
        $('#about').addClass('inactive').animate({color: '#757575'}).find('img').stop(true,true).animate({opacity: 0.5});
      });
      break;
    }
    case 'about': {
      current = 'about';
      window.history.replaceState({}, "", '/about/');
      document.title = 'Kate Copeland | About';
      $('#project').slideUp(500, function(){
        $('#about').removeClass('inactive').animate({color: '#000000'}).find('img').stop(true,true).animate({opacity: 1});
        $('#main').addClass('inactive').animate({color: '#757575'}).find('img').stop(true,true).animate({opacity: 0.5});
        $('body').animate({
         scrollTop: $('#about').offset().top 
        });
      });
      break;
    }
    default: {
      if (state !== current) {
        current = state;
        var items = $('#project-list a[data-rel='+state+']');
        if (items.length > 0) {
          document.title = 'Kate Copeland | '+items[0].title;
          $('#project').slideUp(500, function(){
            window.history.replaceState({}, "", '/'+state+'/');
            setProject(state);
          });
        }
      } else {
        $('#about').addClass('inactive').animate({color: '#757575'}).find('img').stop(true,true).animate({opacity: 0.5});
      }
      break;
    }
  }
}

$(document).ready(function(){
  
  if (Modernizr.history) {
    
    if (!document.getElementById('project')) $('<div id="project" />').prependTo('#main');
     setState(current);

     $('img').css({backgroundColor: "#EEEEEE"});
    
    $('#project-list a').click(function(){
      if (current === 'about') setState('home');
      else {
        var state = $(this).attr('data-rel');
        setState(state);
      }
      return false;
    });

    $('#main').click(function(){
      if (current === 'about') setState('home');
    });

    $('#about, #nav li:first a').click(function(){
      if (current !== 'about') setState('about');
      return false;
    });
    
    $('#project-list img').hover(function(){
      if (current !== "about") $(this).stop(true,true).animate({backgroundColor: '#FFFFFF'},"fast");
    },function(){
      if (current !== "about") $(this).stop(true,true).animate({backgroundColor: '#EEEEEE'},"slow");
    });

  }

});
