/*
adRotator javascript
*/
function AdRotator(id,time){
	
	/* Attributes */
	this.container = $(id);
	this.ads = container.children('li');
	this.timer = null;
	this.time = time;
	this.currentAd = null;
	
	this.init = function(){
		
		this.currentAd = $(ads.get(0));
		
		ads.removeClass('active').hide();
		this.currentAd.addClass('active').show();
		
		this.timer = setTimeout(this.rotate,this.time);
	}
	
	this.rotate = function(){
	
		this.next();
	
	}
	
	this.prev = function(){
	
		clearTimeout(this.timer); // could be called asynchronously
	
		var prev = currentAd.prev();
		
		if(prev.size() == 0){
			prev = $(ads.get(ads.size()-1));	// start at the end
		}
		
		currentAd.fadeOut('slow').removeClass('active');
		prev.fadeIn('slow').addClass('active');
		
		this.currentAd = prev;
		this.timer = setTimeout(this.rotate, this.time);
	}
	
	this.next = function(){
		
		clearTimeout(this.timer);	// could be called asynchronously
		
		var next = currentAd.next();
		
		if (next.size() == 0){
			next = $(ads.get(0)); // start back at the beginning
		}
		
		currentAd.fadeOut('slow').removeClass('active');
		next.fadeIn('slow').addClass('active');
		
		this.currentAd = next;
		this.timer = setTimeout(this.rotate, this.time);
	};
	
	
	this.init();
	return this;
}
