$(document).ready(function(){

  var directload = true;
  var slowin = true;
  $('article').css("opacity", "0");
  $('article .images img').css("opacity", "0");

  var w = parseInt( $(window).width() );
  var leftshift = ( w - 950) / 2;
  if ( leftshift < 15 ) { leftshift = 15; }
  $('header').css("left", leftshift+"px");
  $('nav').css("left", leftshift+"px");
  $('article').css("left", leftshift+"px");

  $(window).resize(function(){
    w = parseInt( $(window).width() );
    leftshift = ( w - 950) / 2;
    if ( leftshift < 15 ) { leftshift = 15; }
    $('header').css("left", leftshift+"px");
    $('nav').css("left", leftshift+"px");
    $('article').css("left", leftshift+"px");
  });

  var delay = 2000;
  $('aside a').animate({ opacity: 0, }, 0);
  $('aside a').each(function() {
      $(this).delay(delay).animate({ opacity: 1, }, 500);
      delay += 100;
  });

  $(window).bind( 'hashchange', function(e) {
    var url = $.param.fragment();
    url = url.substring(1);
    $( 'a.selected' ).removeClass( 'selected' );
    url && $( 'a[id="' + url + '"]' ).addClass( 'selected' );
    if ( url ) {
      if ( url == "about" ) {

        $('article').animate({ opacity: 0, }, 250, function() {
          $('article').load('/pages/about/page.html', function(response, status, xhr) {
          if (status == "error") {
          } else {
  
            $('article #about').css("opacity", "0");
            $('article #about img').css("opacity", "0");
  
            $('article').animate({ opacity: 1, }, 250, function() {
              $('article #about').animate({ opacity: 1, }, 250, function() {
                var delay = 250;
                $('article #about img').each(function() {
                  $(this).delay(delay).animate({ opacity: 1, }, 250);
                  delay += 250;
                });
              });
            });
  
          }
          });
        });

      } else if ( url == "contact" ) {

        $('article').animate({ opacity: 0, }, 250, function() {
          $('article').load('/pages/contact/page.html', function(response, status, xhr) {
          if (status == "error") {
          } else {
  
            $('article #contact').css("opacity", "0");
            $('article #contact div').css("opacity", "0");
  
            $('article').animate({ opacity: 1, }, 250, function() {
              $('article #contact').animate({ opacity: 1, }, 250, function() {
                var delay = 0;
                $('article #contact div').each(function() {
                  $(this).delay(delay).animate({ opacity: 1, }, 250);
                  delay += 250;
                });
              });
            });
  
          }
          });
        });
        
      } else {
        
        $('article').animate({ opacity: 0, }, 250, function() {
          $('article').load('/pages/'+url+'/page.html', function(response, status, xhr) {
          if (status == "error") {
          } else {
  
              $('#tabbuttons li').click(function(){
                var newtab = this.id.substring(4);
                if ( newtab != tab ) {
                  $('ul#tabbuttons li.selected' ).removeClass( 'selected' );
                  $('ul#tabbuttons li#open'+newtab ).addClass( 'selected' );
    
                  $('#tab'+tab).animate({ opacity: 0, }, 250, function() {
                    $('article .tabs img').css("opacity", "0");
    
                    $('#tab'+newtab).animate({ opacity: 1, }, 250, function() {
                      var delay = 0;
                      $('#tab'+newtab+' img').each(function() {
                        $(this).delay(delay).animate({ opacity: 1, }, 250);
                        delay += 250;
                      });
                    });
                    tab = newtab;
    
                  });
                }
  
              }) ;
  
            if ( slowin ) {
  
              $('article .images').css("opacity", "0");
              $('article .images img').css("opacity", "0");
  
              $('article .tabs img').css("opacity", "0");
              $('article .tabs').css("opacity", "0");
              $('article #tabbuttons').css("opacity", "0");
  
              $('article #about').css("opacity", "0");
              $('article #about img').css("opacity", "0");
  
              $('article #contact').css("opacity", "0");
              $('article #contact div').css("opacity", "0");
  
              var tab = 1;
              var tabcount = parseInt( $('.tab').length );
              if ( tabcount > 0 ) { var aretabs = true; } else { var aretabs = false; }
              var tabh = parseInt( $('#tab1').height() );
              $('article .tabs').css("height", (tabh+10)+"px");
  
              $('article').animate({ opacity: 1, }, 250, function() {
  
                if ( aretabs ) {
  
                  $('article .tabs').animate({ opacity: 1, }, 250, function() {
                    var delay = 250;
                    $('article .tabs #tab1 img').each(function() {
                      $(this).delay(delay).animate({ opacity: 1, }, 250, function() {
                        $('article #tabbuttons').animate({ opacity: 1, }, 500);
                      });
                      delay += 250;
                    });
                  });
  
                } else {
  
                  $('article .images').animate({ opacity: 1, }, 250, function() {
                    var delay = 250;
                    $('article .images img').each(function() {
                      $(this).delay(delay).animate({ opacity: 1, }, 250);
                      delay += 250;
                    });
                  });
  
                }
  
              });
  
  
              slowin = false;
  
            } else {
  
              $('article').animate({ opacity: 1, }, 250);
  
            }
  
          }
        });

        if (directload) {
          var hashidpos = $("#"+url).offset().top;
          var scrollto = parseInt( hashidpos ) - 140;
          $('html,body').animate({ scrollTop: scrollto, }, 2500);
          directload = false;
        }
        });
      }
    } else {

      $('article').animate({ opacity: 0, }, 250, function() {
        $('article').load('/pages/about/page.html', function(response, status, xhr) {
        if (status == "error") {
        } else {

          $('article #about').css("opacity", "0");
          $('article #about img').css("opacity", "0");

          $('article').animate({ opacity: 1, }, 250, function() {
            $('article #about').animate({ opacity: 1, }, 250, function() {
              var delay = 250;
              $('article #about img').each(function() {
                $(this).delay(delay).animate({ opacity: 1, }, 250);
                delay += 250;
              });
            });
          });

        }
        });
      });

    }
  });

  $(window).trigger( 'hashchange' );

  $(".thumb").click(function(){
    var thumbid = this.id;
    window.location.hash = "/"+thumbid;
    slowin = true;
    return false;
  }) ;

  $("nav ul li a").click(function(){
    var thumbid = this.id;
    window.location.hash = "/"+thumbid;
    slowin = true;
    return false;
  }) ;

  
});
