var Slider = {
  jumpToPhoto: function() {
    $('.container').hide();
    $('.photo-slide').show();
    $('body').css("overflow", "hidden").css("height", "auto");
    $('#slide-back-button').css("visibility", "visible");
  },
  slideToHome: function() {
    console.log('home');
    $('.container').show();
    $('body').css("overflow", "visible").css("height", "100%");
    $('.photo-slide').fadeOut(800);
    $('#slide-back-button').stop().fadeOut();
  },
  slideToPhoto: function() {
    console.log('photo');
    $("body").css("overflow", "hidden").css("height", "auto");
    $('.photo-slide').fadeIn(800, function() {
      $('.container').hide();
      $('#slide-back-button').hide().css("visibility", "visible").fadeIn(2000);
    });
  }
};

var StateManager = {
  doneOnce: false,
  init: function(state) {
    if(state === "photography") {
      window.history.replaceState(state, state, $('#background-switch').attr('href'));
    }
    window.onpopstate = function(e) {
      if(e.state === 'photography') {
        Slider.slideToPhoto();
      } else if((e.state === 'home' || e.state === null) && StateManager.doneOnce) {
        Slider.slideToHome();
      }
      StateManager.doneOnce = true;
    };
  },
  back: function() {
    window.history.back();
  },
  pushState: function(one, two, three) {
    window.history.pushState(one, two, three);
  }
};

var App = {
  init: function(state) {
    var container = $('.container').first();
    if(container.hasClass("initial-hide")) {
      container.fadeIn();
    }

    if(state === "photography") {
      Slider.jumpToPhoto();
    }

    StateManager.init(state);

    $('#background-switch').click(function(e) {
      if(Modernizr.history) {
        StateManager.pushState("photography", "photography", $('#background-switch').attr('href'));
      }
      Slider.slideToPhoto();
      e.preventDefault();
    });

    $('#slide-back-button a').click(function(e) {
      if(state !== 'error') {
        if(Modernizr.history) {
          StateManager.pushState("home", "home", $('#slide-back-button a').attr('href'));
        }
        Slider.slideToHome();
        e.preventDefault();
      }
    });

    this.checkCompatibility();
  },

  checkCompatibility: function() {
    if(!Modernizr.history || !Modernizr.rgba || !Modernizr.fontface || !Modernizr.cssgradients || !Modernizr.flexbox || !Modernizr.textshadow) {
      $('#incompatibility').html([
        'Your browser isn’t capable of displaying this page in it’s full glory.',
        'For best results, upgrade to the latest <a href="http://google.com/chrome">Google Chrome</a>',
        'or <a href="http://apple.com/safari">Apple Safari<a>.'
      ].join(' ')).fadeIn();
    }
  }
};

/*
LatestTweet.fetch('jamfour', function() {
  $('#twitter-item').tipTip({
    content: '<p>' + LatestTweet.getText() + '</p><a href="' + LatestTweet.getURL() + '">' + LatestTweet.getTime() + '</a>',
    defaultPosition: "left"
  });
});
*/

