$(function() {
  var home_banner = $(".home_banner");
  var buttons = $(".buttons", home_banner);
  var restart_caption = $(".restart_caption", home_banner);
  var restart_popups = $(".restart_popups", home_banner);
  var restart_button = $(".restart_button", buttons);
  var security_caption = $(".security_caption", home_banner);
  var security_shield = $(".security_shield", home_banner);
  var security_button = $(".security_button", buttons);
  var maintenance_caption = $(".maintenance_caption", home_banner);
  var maintenance_servers = $(".maintenance_servers", home_banner);
  var maintenance_button = $(".maintenance_button", buttons);

  function hide_none(f) { f(); };
  var hide = hide_none;
  var t;

  function show_restart() {
    restart_caption.stop(true)
      .css({ opacity: 0 })
      .show()
      .animate({ opacity: 1 }, 500);
    restart_popups.stop(true)
      .css({ marginTop: -162, marginRight: 684, width: 0, opacity: 'inherit' })
      .show()
      .animate({ marginTop: -207, marginRight: 0, width: 344 }, 500);

    restart_button.stop(true)
      .animate({ opacity: .75 }, 500);
    hide = function (f) {
      restart_caption.stop(true)
	.animate({ opacity: 0 },
		 { duration: 500,
		   complete: function() { restart_caption.hide() } });
      restart_popups.stop(true)
	.animate({ marginTop: -252, marginRight: -684, width: 688, opacity: 0 },
		 { duration: 500,
		   complete: function() { restart_popups.hide() } });
      restart_button.stop(true)
	.animate({ opacity: .25 }, 500);
      hide = hide_none;
      t = setTimeout(f, 150);
    };
  }

  function show_security() {
    security_caption.stop(true)
      .css({ opacity: 0 })
      .show()
      .animate({ opacity: 1 }, 500);
    security_shield.stop(true)
      .css({ marginTop: 0, marginLeft: 126, width: 0, opacity: 0 })
      .show()
      .animate({ marginTop: -153, marginLeft: 0, width: 252, opacity: 1 },
	       { duration: 500,
		 complete: function() { security_shield.css({ opacity: 'inherit' }) } });
    security_button.stop(true)
      .animate({ opacity: .75 }, 500);
    hide = function(f) {
      security_caption.stop(true)
	.animate({ opacity: 0 },
		 { duration: 500,
		   complete: function() { security_caption.hide() } });
      security_shield.stop(true)
	.animate({ marginTop: -306, marginLeft: -126, width: 504, opacity: 0 },
		 { duration: 500,
		   complete: function() { security_shield.hide() } });
      security_button.stop(true)
	.animate({ opacity: .25 }, 500);
      hide = hide_none;
      t = setTimeout(f, 150);
    };
  }

  function show_maintenance() {
    maintenance_caption.stop(true)
      .css({ opacity: 0 })
      .show()
      .animate({ opacity: 1 }, 500);
    maintenance_servers.stop(true)
      .animate({ marginTop: -241, marginRight: -411, width: 760, opacity: 0 }, 0)
      .show()
      .animate({ marginTop: -129, marginRight: 0, width: 380, opacity: 1 },
	       { duration: 500,
		 complete: function() { maintenance_servers.css({ opacity: 'inherit' }) } });

    maintenance_button.stop(true)
      .animate({ opacity: .75 }, 500);
    hide = function (f) {
      maintenance_caption.stop(true)
	.animate({ opacity: 0 },
		 { duration: 500,
		   complete: function() { maintenance_caption.hide() } });
      maintenance_servers.stop(true)
	.animate({ marginTop: -241, marginRight: -411, width: 760, opacity: 0 },
		 { duration: 500,
		   complete: function() { maintenance_servers.hide() } });
      maintenance_button.stop(true)
	.animate({ opacity: .25 }, 500);
      hide = hide_none;
      t = setTimeout(f, 150);
    };
  }


  var banners = new Array(show_restart, show_security, show_maintenance);
  var current = 0;

  function next_banner() {
    hide(function() {
      banners[current % banners.length]();
      if (current < banners.length * 2) {
	current = current + 1;
	t = setTimeout(next_banner, 4000);
      }
    });
  }

  restart_button.click(function(e) {
    e.preventDefault();
    clearTimeout(t);
    hide(show_restart);
  });

  security_button.click(function(e) {
    e.preventDefault();
    clearTimeout(t);
    hide(show_security);
  });

  maintenance_button.click(function(e) {
    e.preventDefault();
    clearTimeout(t);
    hide(show_maintenance);
  });

  $("a", buttons).css({ opacity: .25 });
  buttons.css({ display: 'block' });

  restart_caption.hide();
  restart_popups.hide();

  $(window).load(next_banner);
});
