var activeAdIndex;
var adGroups = new Array();
var timeID;

function adGroup(buttonEl, adEl, nextButtonEl) {
	this.button = document.getElementById(buttonEl);
	this.nextButton = document.getElementById(nextButtonEl);
	this.ad = document.getElementById(adEl);
	YAHOO.util.Dom.setStyle(this.ad, "opacity", 0);

	this.activate = function() {
		this.button.className = 'active';
		activeAdIndex = buttonEl;
		this.opacity = 1;
		YAHOO.util.Dom.setStyle(this.ad, "opacity", 1);
		timeID = setTimeout("adGroups['"+buttonEl+"'].nextButton.onclick()",5000);
	}
	this.button.onclick = rotateAds;
	
	function rotateAds() {
		var thisGroup = adGroups[this.id];
		if(activeAdIndex != this.id && thisGroup.opacity == 0) {
			clearTimeout(timeID);
			timeID = setTimeout("adGroups['"+buttonEl+"'].nextButton.onclick()",5000);
			adGroups[activeAdIndex].animate();
			adGroups[activeAdIndex].button.className = '';
			thisGroup.animate();
			thisGroup.button.className = 'active';
			activeAdIndex = this.id;
			thisGroup.opacity = 1;
		}
		return false;
	}
	this.opacity = 0;
	this.animate = function() {
		var animTo = this.opacity == 0 ? 1 : 0;
		if(animTo == 1) {
			this.ad.style.display = 'block';
		}
	    var anim = new YAHOO.util.Anim(this.ad, {opacity: { to: animTo }});
		anim.onComplete.subscribe(function() {
			var thisGroup = adGroups[this.getEl().id];
			thisGroup.opacity = animTo;
			if(animTo == 0) {
				thisGroup.ad.style.display = 'none';
			}
		});
		anim.animate();
	}
	adGroups[adEl] = this;
}

	
YAHOO.util.Event.onDOMReady = function() {

}
