var ticker = Class.create({ 
	t: '', 
	faderT: '',
	appearT: '',
	initialize: function(container, options) {  
	
		this.container = container;  
		
		this.options = Object.extend(options || {},{   
			item_frequency: 4000,
			item_pause: 'newsTickerPause',
			item_play: 'newsTickerPlay'
		});
		
		this.current = 0;
		this.current_item = this.container[this.current]; 
		this.startTick();  
	},  
	
	startTick: function() { 
	
		this.container.each(function(item, index) {  
			if (index > 0) {
            	item.hide();  
			}
		});  
		this.t = setTimeout(this.onTick.bind(this), this.options.item_frequency);  
	},  
	
	onTick: function() {
	
		var self = this; 
		var time = 0;

		if (this.current_item != null) {
			this.current_item.hide();
			time = 0;
		}
		
		this.faderT = setTimeout(function(){
			self.current += 1;
			self.current_item = self.container[self.current%self.container.length]; 
			self.current_item.show();
			self.appearT = setTimeout(function(){
				self.t = setTimeout(self.onTick.bind(self), self.options.item_frequency);
			}, 1050)
		}, time);   
	},
		
	stopTick: function() {
	
		$(this.options.item_play).show();
		clearTimeout(this.t);
		clearTimeout(this.faderT);
		$(this.options.item_pause).hide();
		return false;
	},
	
	restartTick: function() {
	
		$(this.options.item_play).hide();
		this.t = setTimeout(this.onTick.bind(this), this.options.item_frequency); 
		$(this.options.item_pause).show();
		
		return false;
	},
	
	next: function() {
	
		clearTimeout(this.t);
		clearTimeout(this.faderT);
		clearTimeout(this.appearT);
		
		current_item = this.container[this.current%this.container.length]; 	
		current_item.hide();
		
		this.current_item = this.container[(this.current+1)%this.container.length]; 
		this.current_item.show();
		this.current++;
		
		if ($(this.options.item_play).style.display != "") {
			this.t = setTimeout(this.onTick.bind(this), this.options.item_frequency); 
		}
		
		return false; 
	},
	
	prev: function() {
	
		clearTimeout(this.t);
		clearTimeout(this.faderT);
		clearTimeout(this.appearT);
		
		current_item = this.container[this.current%this.container.length]; 
		current_item.hide();
		
		this.current = this.current-1 < 0 ?this.container.length: this.current;
		
		this.current_item = this.container[(this.current-1)%this.container.length]; 
		this.current_item.show();
		this.current--;
		
		if ($(this.options.item_play).style.display != "") {
			this.t = setTimeout(this.onTick.bind(this), this.options.item_frequency); 
		}
		return false; 
	}
});
