// A MouseOver object animates a mouse-over div and stops thumping for a time.
function MM_MouseOver(div, controller, thumperController) {
  var myOwner = this;
  this.anim_down = new YAHOO.util.Anim(div, { opacity: { to: 0 } }, .5, YAHOO.util.Easing.easeOut);
  this.anim_up = new YAHOO.util.Anim(div, { opacity: { to: 1 } }, .5, YAHOO.util.Easing.easeOut);
  this.thumperController = thumperController;
  
  YAHOO.util.Event.on(controller, 'mouseout', function() {
    if (myOwner.anim_up) myOwner.anim_up.stop();
    myOwner.anim_down.animate();
    myOwner.thumperController.start(2000);
  });  
  YAHOO.util.Event.on(controller, 'mouseover', function() {
    if (myOwner.anim_down) myOwner.anim_down.stop();
    myOwner.anim_up.animate();
    myOwner.thumperController.stop();
  });
}

// A thumper controls the fade-up/fade-down of a single image (then tells the next image to thump).
function MM_Thumper(div, uptime, pause) {
	var myOwner = this;
  this.timer = undefined;
  
	this.uptime = uptime;
	this.pause = pause;

  this.stopped = true;

  this.stop = function() {
    myOwner.stopped = true;
	  myOwner.anim_up.stop();
	  if (myOwner.timer) clearTimeout(myOwner.timer);
		myOwner.anim_down.animate();
  };

  this.go = function() {
    myOwner.stopped = false;
	  myOwner.anim_up.animate();
  };

  this.anim_up = new YAHOO.util.Anim(div, { opacity: { to: 1 } }, 1, YAHOO.util.Easing.easeOut);
	this.anim_up.onComplete.subscribe(function() {
	    myOwner.timer = setTimeout(function(){ 
	  	    myOwner.anim_down.animate();
	  	    myOwner.timer = undefined;
					if (! myOwner.stopped) {
    	      myOwner.timer = setTimeout(function() {
                if (myOwner.nextThumper) myOwner.nextThumper.go();
	            }, myOwner.pause);
					}
	    }, myOwner.uptime);
	  });

  this.nextThumper = undefined;

  this.anim_down = new YAHOO.util.Anim(div, { opacity: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
	  
	this.setNextThumper = function(thumper) {
    myOwner.nextThumper = thumper;
	};
}

// The ThumperCoordinator lines multiple thumpers up in a loop and has the power to stop them all with a single method call.
function MM_ThumperCoordinator() {
  var myOwner = this;
  this.thumpers = new Array();
  this.addThumper = function(thumper) {
    if (myOwner.thumpers.length > 0) {
			thumper.setNextThumper(myOwner.thumpers[0]);
			myOwner.thumpers[myOwner.thumpers.length - 1].setNextThumper(thumper);
    }
    myOwner.thumpers[myOwner.thumpers.length] = thumper;
  };
  this.timer = undefined;
  this.stop = function() {
  	if (myOwner.timer) {
  		clearTimeout(myOwner.timer);
  		myOwner.timer = undefined;
  	}
    for (var i = 0; i < myOwner.thumpers.length; i++) {
      myOwner.thumpers[i].stop();
    }
  };
  this.start = function(time) {
    myOwner.timer = setTimeout(function(){
    	  myOwner.thumpers[0].go();
    	  myOwner.timer = undefined;
    	}, time);
  };
}

(function() {
  var tc = new MM_ThumperCoordinator();
  tc.addThumper(new MM_Thumper('tb1a', 1000, 100));
  tc.addThumper(new MM_Thumper('tb2a', 1000, 100));
  tc.addThumper(new MM_Thumper('tb3a', 1000, 100));

  new MM_MouseOver('tb1', 'b1-run', tc);
  new MM_MouseOver('tb2', 'b2-run', tc);
  new MM_MouseOver('tb3', 'b3-run', tc);

	tc.start(2000);
})();
