HomepageModule={};

FeaturedContent = {
	content: [],
	extraFeaturedContent: null,
	thumbnails: [],
	thumbnailContainer: null,
	currentPosition: 0,
	init: function(element) {
		this.content[0] = element;
		this.thumbnails = YAHOO.util.Dom.getElementsByClassName('banner_thumbnail', 'a', element);
		for (var i=0;i<this.thumbnails.length;i++) {
			this.thumbnails[i].setAttribute('position', i);
		}
		this.thumbnailContainer = this.thumbnails[0].parentNode;
	},
	initExtraContent: function(element) {
		this.extraFeaturedContent = element;
		var banners = YAHOO.util.Dom.getElementsByClassName('banner_container', 'div', element);

		for (var i=0;i<banners.length;i++) {
			this.content[i+1] = banners[i];
		}
		
	},
	select: function(event, element) {
		if (event) {
			YAHOO.util.Event.stopEvent(event);
		}
		if (this.animationTimer) {
			this.animationTimer.cancel();
		}
		var position = element.getAttribute('position');
		var currentContent = this.content[this.currentPosition];
		currentContent.parentNode.replaceChild(this.content[position], currentContent);
		this.content[position].appendChild(this.thumbnailContainer);
		YAHOO.util.Dom.removeClass(this.thumbnails[this.currentPosition], 'current');
		YAHOO.util.Dom.addClass(this.thumbnails[position], 'current');
		this.currentPosition = position;
	},
	animateNext: function() {
		var position = (this.currentPosition + 1)%this.content.length;
		var newContent = this.content[position];
		var currentContent = this.content[this.currentPosition];
		
		YAHOO.util.Dom.setStyle(newContent, 'opacity', '0');
		currentContent.appendChild(newContent);
		newContent.appendChild(this.thumbnailContainer);
		YAHOO.util.Dom.removeClass(this.thumbnails[this.currentPosition], 'current');
		YAHOO.util.Dom.addClass(this.thumbnails[position], 'current');
		this.currentPosition = position;
		
		var anim = new YAHOO.util.Anim(newContent, {
			opacity: { to: 1 }
		}, 0.5);
		anim.animate();
		anim.onComplete.subscribe(function() {
			currentContent.parentNode.replaceChild(newContent, currentContent);
		});
	},
	beginAnimation: function(element) {
		this.animationTimer = YAHOO.lang.later(5000, this, this.animateNext, null, true);
	}
};

Overlord.assign({
	minion: 'featured_content',
	load: FeaturedContent.init,
	scope: FeaturedContent,
	order: 0
});

Overlord.assign({
	minion: 'featured_content:extra',
	load: FeaturedContent.initExtraContent,
	scope: FeaturedContent,
	order: 1
});

Overlord.assign({
	minion: 'featured_content:thumbnail',
	click: FeaturedContent.select,
	scope: FeaturedContent,
	order: 2
});

Overlord.assign({
	minion: 'featured_content:animate',
	load: FeaturedContent.beginAnimation,
	scope: FeaturedContent
});

