$j.Avaya.Ticker = function(options){
	
	var self = this,
		presets = {
			$container : '.ticker-container',
			$item : '.ticker-item',
			$first_item : 0,
			
			$timeout_dur : 5000,
			$direction : 'forward',
			$transitions : {
				duration : 750,
				show : 'fadeIn',
				hide : 'fadeOut'
			},
	
			$man_control : false,
			$man_buttons : {
				forward : '.ticker-forward',
				backward : '.ticker-backward'
			}
		};
	
	this.s = $j.extend(true, presets, options);
	this.current = this.s.$first_item;
	this.transition = null;
	
	
	$j(document).bind('Ticker.setupTransitions', function(){
		if(self.s.$direction == 'forward')
			self.transition = setInterval(function(){ self.showNext(); }, self.s.$timeout_dur);
		else if(self.s.$direction == 'backward')
			self.transition = setInterval(function(){ self.showPrev(); }, self.s.$timeout_dur);
	}).trigger('Ticker.setupTransitions');
	
	
	$j(this.s.$container).find(this.s.$item).hover(
		function(){ clearInterval(self.transition); },
		function(){ $j(document).trigger('Ticker.setupTransitions'); }
	);
	
	
	if(this.s.$man_control){
		$j(this.s.$container).find(this.s.$man_buttons.forward).click(function(e){
			e.preventDefault();
			
			clearInterval(self.transition);
			self.showNext();
			$j(document).trigger('Ticker.setupTransitions');
			
			return false;
		});
		
		$j(this.s.$container).find(this.s.$man_buttons.backward).click(function(e){
			e.preventDefault();
			
			clearInterval(self.transition);
			self.showPrev();
			$j(document).trigger('Ticker.setupTransitions');
			
			return false;
		});
	}
};


$j.Avaya.Ticker.prototype = {
	
	showNext : function(){
		var self = this,
			items = $j(this.s.$container).find(this.s.$item),
			item;
		
		item = items.eq(this.current);
		this.hide(item, function(){
			item.hide();
			
			self.current++;
			if(items.eq(self.current).size() > 0) item = items.eq(self.current);
			else item = self.reset();
			
			self.show(item);
		});
	},
	
	showPrev : function(){
		var self = this,
			items = $j(this.s.$container).find(this.s.$item),
			item;
		
		item = items.eq(this.current);
		this.hide(items.eq(this.current), function(){
			item.hide();
			
			self.current--;
			if($j(self.s.$container).find(self.s.$item).eq(self.current).size() > 0) item = $j(self.s.$container).find(self.s.$item).eq(self.current);
			else item = self.resetLast();
			
			self.show(item);
		});
	},
	
	show : function(el, fn){
		if(typeof fn != 'undefined') el[this.s.$transitions.show](this.s.$transitions.duration, fn.call(this));
		else el[this.s.$transitions.show](this.s.$transitions.duration);
	},
	
	hide : function(el, fn){
		if(typeof fn != 'undefined') el[this.s.$transitions.hide](this.s.$transitions.duration, fn.call(this));
		else el[this.s.$transitions.hide](this.s.$transitions.duration);
	},
	
	reset : function(){
		this.current = this.s.$first_item;
		return $j(this.s.$container).find(this.s.$item).eq(this.current);
	},
	
	resetLast : function(){
		var items = $j(this.s.$container).find(this.s.$item),
			last = items.size() - 1;
		
		this.current = last;
		return items.eq(last);
	}
	
};
