// JavaScript Document
(function($) {
	$.fn.elNews = function(options) {
		var opts = $.extend({}, $.fn.elNews.defaults, options);
		return this.each(function() {
			var $this 					= $(this);
			$this.opts 					= $.extend({}, opts);
			$.fn.elNews.init($this);
		});
	};
	
	$.fn.elNews.defaults = {
		active: null
	};
	
	
	$.fn.elNews.init = function($this){
		$this.find('li').css({top:'140px'});
		$this.find('li:eq(0)').css({top:'0px'});
		$this.opts.total = $this.find('li').length;
		$this.opts.current = 0;
		$this.opts.interval = null;
		if($this.opts.total > 1){
			$this.opts.next = $('.nextmessage')
			$this.opts.next.insertAfter("#shifter")
			$this.opts.next.click(function(event){
			   $.fn.elNews.next($this,1);
				event.preventDefault();
			});
			$.fn.elNews.startslides($this);
			$this.hover(function(){
				$.fn.elNews.stopslides($this);
			},function(){
				$.fn.elNews.startslides($this);
			})
			$this.opts.next.hover(function(){
				$.fn.elNews.stopslides($this);
			},function(){
				$.fn.elNews.startslides($this);
			})
		}
	};
	
	$.fn.elNews.startslides = function($this, nr){
		$.fn.elNews.stopslides($this);
		$this.opts.interval = setInterval(function(){ $.fn.elNews.next($this,1); },4000);
	}
	$.fn.elNews.stopslides = function($this, nr){
		clearInterval($this.opts.interval);
	}
	$.fn.elNews.next = function($this, nr){
		$newnr = $this.opts.current + nr;
		if($newnr >= $this.opts.total){
			$newnr = 0;	
		}
		$.fn.elNews.show($this, $newnr);
	};
	
	$.fn.elNews.show = function($this, $newnr){
		$this.find('li:eq('+$this.opts.current+')').stop().animate({top:'-200px'},300);
		$this.opts.current = $newnr;
		$this.find('li:eq('+$this.opts.current+')').stop().css({top:'140px'}).animate({top:'0px'},300);
	};
	
})(jQuery);
