var RoatingFeatured = Class.create({
	initialize: function(){
		this.current = null;
		this.items = new Array();
		this.cycleTimer = null;
		Event.observe(window, 'load', this.onLoad.bindAsEventListener(this));
	},
	onLoad: function(pEvent){
		$$('a.rotatingFeatured').each(function(el){
			var item =  new RoatingFeaturedItem(el);
			this.items[this.items.length] = item;
			Event.observe(el, 'click', function(pEvent){
				Event.stop(pEvent);
				if(this.current != null){
					if(this.current == item){
						return 
					}
					this.current.hide();
				}
				this.current = item;
				this.current.show();
				
				if(this.cycleTimer != null){
					clearTimeout(this.cycleTimer);
				}
			}.bindAsEventListener(this));
		}.bind(this));
		
		if(this.items.length > 0){
			this.displayRandom();
			this.cycleTimer = setTimeout(this.cycle.bind(this), 8000);
		}
	},
	displayRandom: function(){
		var rand = Math.ceil(this.items.length*Math.random())-1;
		
		//TODO: Handle if rand is the same as current
		if(this.current != null){
			this.current.hide();
		}
		
		this.current = this.items[rand];
		this.current.show();
	},
	cycle: function(){
		if(this.current != null){
			var i = this.items.indexOf(this.current);
			i++;
			
			if(i > this.items.length-1){
				i = 0;
			}
			
			this.current.hide();
			this.current = this.items[i];
			this.current.show();
		} else {
			this.current = this.items[0];
			this.current.show();
		}
		//this.cycleTimer = setTimeout(this.cycle.bind(this), 5000);
	}
});

var RoatingFeaturedItem = Class.create({
	initialize: function(link){
		this.selected = false;
		this.link = link;
		this.content = $(link.attributes['rel'].nodeValue);
	},
	hide: function(){
		this.selected = false;
		this.link.removeClassName('selected');
			$J(this.content).hide(900);
	},
	show: function(){
		this.selected = true;
		this.link.addClassName('selected');
		$J(this.content).show(900);
	}
});

var RoatingFeaturedObj = new RoatingFeatured();
