$(function () {
  var count = 0;
  var img_list = []
  $("#header .banner img").each(function () {
    img_list.push(this);
  });
  
  var tmp_img = new Image();
  tmp_img.src = $(img_list[0]).attr('src');

  tmp_img.onload = function () {
    var len = img_list.length;
    len--;
    var last = img_list[len];
    len--;
    var sec_last = img_list[len];

    setTimeout(function () {
      $(sec_last).css({left: '0px', height: '40px'});
      $(sec_last).show();
      $(sec_last).fadeIn('fast', function () {
        $(this).fadeOut(5000);
      }); 

      $(last).show();
      $(last).css({left: '80px', height: "70px"});
      setTimeout(function () {
        $(last).animate({left: 0, height: '40px'}, 3000, 'swing', function () {
          $(last).fadeOut(5000);
        });
      }, 5000);
    }, 10);

    $(img_list).each(function () { 
      if (this !== last || this !== sec_last) {
        $(this).hide();
      }
      (function (that) {
        var func = function () {
          $(that).css({left: $("#header .banner:first").width() + 10, height: "40px"});
          $(that).hide();
          $(that).fadeIn(5000);
          $(that).animate({left: 95 - ($(that).width()/2), height: "70px"}, 3000, 'swing', function () {
            setTimeout(function () {
              $(that).animate({left:0, height: "40px"}, 3000, 'swing', function () {
                $(that).fadeOut(5000);
                var len = img_list.length;
                len -= 2;
                if (len < 1) len = 1;
                setTimeout(func, len * 7500);
              });
            }, 4000);
          });
        };
        setTimeout(func, count);
      })(this);
      count += 7500;
    });
  };
});


