$(document).ready(function() {

   speed_initialWait = 100; // pause before animation starts to allow image some time to to pre-load
   speed_fadeInLogo = 800; // fade in the logo
   speed_waitOnLogo = 1200; // wait with the logo on screen
   speed_fadeOutLogo = 1000; // fade out the logo
   speed_fadeInMainContent = 250; // fade in the main content
  
  $mainElements = $("#bd,#hd,#ft").hide();
  $welcome = $("#welcome");
  $welcomeImage = $('#welcome img');
  $welcome.css({opacity:"0"}).show(); //try and force image to load

  //if no home hash, we assign it within this argument, for simplicity
  if( '#home' == window.location.hash || !(window.location.hash='#home')){
    //we use the home anchor to detect whether the page has been entered directly, or on a subsequent click
    if ( $mainElements.is(":hidden") ) {
      $welcome.stop().hide();
      $mainElements.stop().show();
    }
  }
  // we test that the image has loaded before playing the animation
  // temporarily ignoring the readyState and complete as an alternative is
  // needed for Mozilla
  else if(1==1 || this.complete || 'complete' == this.readyState) {
       $welcome.animate({opacity:"0"},speed_initialWait,function(){ //initial pause to allow image to load
          $(this).animate({opacity:"1"},speed_fadeInLogo, function(){ // fade in welcome logo
            $(this).animate({opacity:"1"},speed_waitOnLogo, function(){ // pause with logo on screen
              $(this).fadeOut(speed_fadeOutLogo, function(){ //fade logo out
                $mainElements.fadeIn(speed_fadeInMainContent); //fade the rest back in.
              });
            });
          });
        });

     } else {
    $welcomeImage.bind('load readystatechange', function(e) { 
      if (this.complete || ('complete' == this.readyState &&  'readystatechange' == e.type)) {

        //webkit trigger event fix <--this seemed to cause problems to me
        var src = this.src; 
        this.src = '#'; 
        this.src = src;

        $welcome.animate({opacity:"0"},speed_initialWait,function(){ //initial pause to allow image to load
           $(this).animate({opacity:"1"},speed_fadeInLogo, function(){ // fade in welcome logo
             $(this).animate({opacity:"1"},speed_waitOnLogo, function(){ // pause with logo on screen
               $(this).fadeOut(speed_fadeOutLogo, function(){ //fade logo out
                 $mainElements.fadeIn(speed_fadeInMainContent); //fade the rest back in.
               });
             });
           });
         });

       } 
    })
  }

  // capture impatient user click
  $("html").click(function(){
    if ( $mainElements.is(":hidden") ) {
      $welcome.stop().hide();
      $mainElements.stop().fadeIn();
    }
  });
  /**/


    //hover buttons
    $('#logos')
      .find('h2 a')
      .removeClass('css_fade')
      .append('<span class="js_fade"></span>')
      .each(function(){
        //cache the new span
        var $span = $('> span.js_fade', this).css('opacity',0);
        $(this).hover(function(){
          //on hover
          $span.stop().fadeTo(200,1);
        }, function(){
          //off hover
          $span.stop().fadeTo(400,0);
        })
      });
});