if(typeof mercury == "undefined") var mercury = new Object();
if(typeof mercury.pluggd == "undefined") mercury.pluggd = new Object();

mercury.pluggd.rotator = {
	container:Object,
	elements:Array,
	navigation:Array,
	time:Number,
	timer:Number,
	current:Number,
	effects:Boolean,
	paused:Boolean,
	changing:Boolean,
	
	init:function( c, t, e, n ){
		this.container = $(c);
		this.time = t;
		this.effects = e;
		this.changing = false;
		
		this.container.style.position="relative";
		this.elements = document.getElementsByClassName('feature', this.container);
		
		if( n )
			this.createNavigation();
		
		this.startRotating();
	},
	
	startRotating : function () {
		this.paused = true;
		this.current = 0;
		this.navigation[this.current].addClassName("current");
		this.play();
	},
	
	swap : function (){
		this.moveCursorForward();
		this.showCurrentElement();
		this.timer = window.setTimeout( mercury.pluggd.rotator.swap.bind(this), this.time * 1000 );
	},
	
	showCurrentElement:function(){
		this.changing = true;
		window.setTimeout( mercury.pluggd.rotator.finishedChanging.bind(this), 1000 );
		
		for( var i = 0; i < this.elements.length; i++ ){
			if( !this.elements[i].style.display || this.elements[i].style.display == "block"){
				this.hideElement(this.elements[i]);
				this.navigation[i].removeClassName("current");
			}
		}
		this.showElement(this.elements[this.current]);
		this.navigation[this.current].addClassName("current");
	},
	
	goTo:function(n){
		if( this.changing )
			return;
		
		if( n > this.elements.length )
			n = this.elements.length;
		else if( n <= 0 )
			n = 1;
		
		if( this.current == n-1 )
			return;
		
		this.current = n-1;
		this.showCurrentElement();
		
		this.pause();
		this.play();
	},
	
	next:function(){
		if(this.current == this.elements.length-1)
			this.goTo(1);
		else
			this.goTo(this.current+2);
	},
	
	previous:function(){
		if( this.current <= 1 )
			this.goTo(this.elements.length);
		else
			this.goTo(this.current);
	},
	
	moveCursorForward:function(){
		this.current = ( ++this.current >= this.elements.length) ? 0 : this.current;
	},
	
	moveCursorBack:function(){
		this.current = ( --this.current < 0 ) ? this.elements.length - 1  : this.current;
	},
	
	pause:function(){
		if(!this.paused){
			this.paused = true;
			window.clearTimeout( this.timer );
		}
	},
	
	play:function(){
		if(this.paused){
			this.paused=false;
			this.timer = window.setTimeout( mercury.pluggd.rotator.swap.bind(this), this.time * 1000 );
		}
	},
	
	showElement:function(e){
		if(this.effects){
			e.style.position="static";
			Effect.Appear(e.id);
		} else {
			e.style.display="block";
		}
	},
	
	hideElement:function(e){
		if(this.effects){
			e.style.position="absolute";
			e.style.top = e.style.left = "0";
			Effect.Fade(e.id);
		} else {
			e.style.display="none";
		}
	},
	
	createNavigation:function(){
		this.navigation = new Array();
		
		var ol = document.createElement("OL");
		ol.id = "featured-nav";
		
		for( var i=0; i<this.elements.length; i++){
			var anc = this.createNavigationLink(i+1);
			ol.appendChild(anc);
		}
		
		$("featured-foot").appendChild(ol);
	},
	
	createNavigationLink:function(n){
		var li = document.createElement("LI");
		li.id="nav"+n;
		
		var anc = document.createElement("A");
		anc.href="javascript:mercury.pluggd.rotator.goTo("+n+");";
		Element.Methods.addClassName(anc, "rotator-anc" );
		if( n == this.elements.length )
			Element.Methods.addClassName(anc,"last");
		
		var spn = document.createElement("SPAN");
		spn.appendChild(document.createTextNode(""+n));
		spn.style.display = "none";
		
		anc.appendChild(spn);
		this.navigation.push( anc );
		li.appendChild(anc);
		
		return li;
	},
	
	finishedChanging:function(){
		this.changing = false;
	}
};

mercury.pluggd.initRotator = function(){
	mercury.pluggd.rotator.init("rotator",15,true,true);
};

Event.observe(window, 'load', mercury.pluggd.initRotator, false);
