ProjectRotator = Class.create({
	initialize: function(element, options){
		Object.extend(this, pH8.Mixin.needsPages);
		Object.extend(this, pH8.Mixin.needsTemplates);
		this.element = $(element);
		this.options = Object.extend({
			rotationtime: 5,
			maxShow: 2,
			templates: {}
		},options || {});
		this.waitForTemplates(this.options.templates,this.onTemplatesLoaded.bind(this));
		this.waitForPages(this.onPagesLoaded.bind(this));
	},
	onPagesLoaded: function(){
		this.pagesLoaded = true;
		this.pages = new pH8.Pages();
		this.onReady();
	},
	onTemplatesLoaded: function(){
		this.templatesLoaded = true;
		this.onReady();
	},
	onReady: function(){
		if(this.pagesLoaded && this.templatesLoaded){
			this.addEventHandlers();
			this.list = this.element.select('li.rollover').inGroupsOf(this.options.maxShow);
			this.start();
		}
	},
	start: function(){
		if(!this.interval){
			this.index = 0;
			this.interval = setInterval(this.rotate.bind(this), (this.options.rotationtime * 1000));
		}
	},
	rotate: function(){
		new Effect.Fade(this.element, {
			afterFinish: function(){
				this.nextStep();
				new Effect.Appear(this.element);
			}.bind(this)
		});
	},
	delegates: {
		mouseout: {
			'li.rollover *': function(){
				this.start();
			}
		},
		mouseover: {
			'li.rollover *': function(){
				this.stop();
			}
		}
	},
	addEventHandlers: function(){
		this.element.observe('mouseout', Event.delegate(this.delegates.mouseout).bindAsEventListener(this));
		this.element.observe('mouseover', Event.delegate(this.delegates.mouseover).bindAsEventListener(this));
	},
	nextStep: function(){
		this.list[this.index].each(function(element){
			if(element){
				element.addClassName('hidden');
			}
		}.bind(this));
		
		this.index = this.index + 1;
		
		if((this.index + 1)  > this.list.size()){
			this.index = 0;
		}
		this.list[this.index].each(function(element){
			if(element){
				element.removeClassName('hidden');
			}
		}.bind(this));
	},
	stop: function(){
		clearInterval(this.interval);
		this.interval = null;
	},
	showError: function(transport){
	}
});