$(function(){
  
  var buttons = ['who', 'shop', 'basket', 'pay', 'blog', 'facebook'];
  var numbers = ['0','1','2','3','4','5','6','7','8','9'];
  
  $(buttons).each(function(){
      var mouseover_src = '/assets/images/nav-' + this + '-selected.png';
      var mouseout_src = '/assets/images/nav-' + this + '.png';

      // Preload the mouseover images
      $('<img/>')[0].src = mouseover_src;

      if (this != selected_page) {
        $('#' + this + '-link')
          .mouseover(function(){
            $(this).attr("src", mouseover_src);
          })
          .mouseout(function(){
            $(this).attr("src", mouseout_src);
          });
      }else {
        $('#' + this + '-link').attr("src", mouseover_src);
      }      
  });
  
  $(numbers).each(function(){
    $('<img/>')[0].src = '/assets/images/numbers/' + this.toString() + '.png';
    $('<img/>')[0].src = '/assets/images/numbers/' + this.toString() + '-selected.png';
  });
  
  if (selected_page != 'basket') {
    $('#basket-link')
    .mouseover(function(){
        $('#cart-count img').each(function(){
            $(this).attr('src', $(this).attr('src').replace('.png', '-selected.png'));
        });
    })
    .mouseout(function(){
        $('#cart-count img').each(function(){
            $(this).attr('src', $(this).attr('src').replace('-selected', '')); 
        });
    })
  }
  else {
      $('#cart-count img').each(function(){
          $(this).attr('src', $(this).attr('src').replace('.png', '-selected.png'));
      });
  }
  
  if (!document.referrer || document.referrer.toString().match('\/$')) {
    var images_loaded = 0;
    $('html,body').css({overflow: 'hidden'});
    $('#outter').css({opacity: 0});
    $('#loader').show();
    if ($('img.product').length == 0) {
      show_page();
    }
    else {
      $('img.product').one('load', function(){
        ++images_loaded;
        if (images_loaded == $('img.product').length) {
          show_page()
        }
      }).each(function(){
        if (this.complete || this.complete === undefined) $(this).load()
      });
    }
  }

  function show_page() {
    $('#loader').fadeOut('slow', function(){
      $('html,body').css({overflow: 'auto'});
      $('#outter').animate({opacity: 1}, 'slow');
    });
  }
  
});


