var ContentRotator = Class.create({
	
	data: [],
	
	CONTAINER_ID: 'intro', // Element#ID
	
	SELECTOR: '#intro .element .textElement',
	
	rotate: true,
	speed: 5,
	current: 0,
	count: null,
	
	initialize: function(){
		
		if( !$('adminbar') ){ 
		
			cr_container = new Element('div', {'id':'cr_container','class':'cr-item-container'});
			cr_content = new Element('div', {'id':'cr_content','class':'cr-item-content'});
			cr_navigation = new Element('ul', {'id':'cr_navigation','class':'cr-navigation'});
					
			$$(this.SELECTOR).each( function(item,index){ 
				
				if( item.up('.element').previous('.container') ){
					
					var content = new Element('div', {'class':'content-item','id':'content_'+index}).update(item.innerHTML);
					
					var a = new Element('a', {'href':'javascript:;','rel':'content_'+index,'class': (index==0?'current':'') } ).update( item.up('.element').previous('.container').down('.title').innerHTML );
					
					Event.observe(a, 'mouseover', this.bindMouseOver.bindAsEventListener(this) );
					Event.observe(a, 'mouseout', this.bindMouseOut.bindAsEventListener(this) );
					
					cr_content.insert(content);
					cr_container.insert(cr_content);
					cr_navigation.insert(new Element('li').update(a));
				}
				
			}, this);
			
			$(this.CONTAINER_ID).update('');
			$(this.CONTAINER_ID).insert(cr_container);
			$(this.CONTAINER_ID).insert(cr_navigation);
			
			$$('.cr-item-container .content-item').each( function(item,index){ 
				if( index!=0 )
					item.hide();
			});
			
			this.count = $$('.cr-navigation li a').length;

			this.showItem($$('.cr-navigation li a')[0]);
			
			if(this.rotate==true)
				setTimeout('CR.autoRotate()',this.speed*1000);
		}
		
	},
	
	bindMouseOver: function(event){
		
		this.rotate = false;
		this.showItem(Event.element(event));
		
	},
	
	bindMouseOut: function(event){
		
		this.rotate = true;
		
	},
	
	showItem: function(element){
		
		this.current = element.rel.replace(/content_/,'');

		$$('.cr-navigation li a').each( function(link_item){ 
				
			if( link_item.rel != element )
				link_item.className=''; 
		
		});
		
		$$('.cr-item-container .content-item').each( function(content_item){ 
			if( element.rel != content_item.id ){
				content_item.hide();
			}
		},element);
		
		new Effect.Appear(element.rel, {duration: 0.4} );
		element.addClassName('current');
		
		
	},
	
	showNext: function(){
		
		if( this.current == (this.count-1) )
			this.current = -1;
		
		this.showItem( $$('.cr-navigation li a')[++this.current] );
	},
	
	autoRotate: function(){
		
		if( this.rotate == true )
			this.showNext();

		setTimeout('CR.autoRotate()',this.speed*1000);
		
	}

});


document.observe("dom:loaded",function(){ CR = new ContentRotator(); });