$(function(){
	var NewsScroller = function(opts){
		var obj      = this;
		var defaults = {};	
		defaults.source_id           = undefined;
		defaults.wrapper             = undefined;
		defaults.navigation_class    = 'news_scroller_widget_navigation';
		defaults.nav_prev_link_class = 'navigation_previous';		
		defaults.nav_prev_link_html  = '&lt;';
		defaults.nav_next_link_class = 'navigation_next';	
		defaults.nav_next_link_html  = '&gt;';
		defaults.status_link_id      = 'status_link';
		defaults.status_html_on      = 'Pause';
		defaults.status_html_off     = 'Play';
		defaults.auto_start          = true;
		// one of: left, right, random
		defaults.behave_as           = 'random';
		defaults.news 			     = [];		
		// 30 seconds of interval
		defaults.interval 		  = 7000;
		defaults.effect_time 	  = 1250;
		
		// interval id defined by setinterval
		this.interval_id = undefined;
		// shown index
		this.shown_index = 0;
		// reproduction stopped by default
		this.status      = false;
		// wether there's a transaction going on
		this.in_transact = false;
		
		// merge options with defaults
		this.opts = $.extend({}, defaults, opts);	
		
		this.show = function(index){
			// too much clicks
			if(obj.in_transact){
				return false;
			}else{
				obj.in_transact = true;
			}
			
			// hide old index
			var next = $('.' + 'js_' + obj.opts.source_id + '_item_' + index);
			
			if(!next.length) return false;
			
			var current = $('.' + 'js_' + obj.opts.source_id + '_item_' + obj.shown_index);
			current.fadeOut(obj.opts.effect_time, function(){
				current.hide();
				next.fadeIn(obj.opts.effect_time);	
				// end of transaction
				obj.in_transact = false;				
			});
			
			obj.shown_index = index;
			
			return true;
		}
		
		this.show_prev = function(){
			var index = obj.shown_index <= 0? obj.opts.news.length - 1: obj.shown_index - 1;
			
			obj.show(index);
		}		
		
		this.show_next = function(){
			var index = obj.shown_index < (obj.opts.news.length - 1)? obj.shown_index + 1: 0;
			
			obj.show(index);
		}		
		
		this.show_random = function(){
			var index = Math.floor(obj.opts.news.length*Math.random());

			// if index is currently shown, show another element
			if(index == obj.shown_index){
				obj.show_random();
				return;
			}else{
				obj.show(index);
			}
		}
		
		this.start_reproduction = function(){	
		
			switch(obj.opts.behave_as){
				case 'left': function timed_update(){ obj.show_prev(); } break;
				case 'right': function timed_update(){ obj.show_next(); } break;
				case 'random': function timed_update(){ obj.show_random(); } break;
			}
			
			obj.interval_id = setInterval(timed_update, this.opts.interval);	
			obj.status      = true;
		}
		
		this.stop_reproduction = function(){
			if(typeof obj.interval_id != 'undefined'){
				clearInterval(obj.interval_id);
				obj.status  = false;				
			}
		}
		
		this.toggle_reproduction = function(){
			if(obj.status){
				obj.stop_reproduction();
				$('#' + obj.opts.status_link_id).html(obj.opts.status_html_off);
			}else{
				obj.start_reproduction();
				$('#' + obj.opts.status_link_id).html(obj.opts.status_html_on);
			}
		}	

		// build from newstool
		if(typeof this.opts.source_id == 'string' && $('#' + this.opts.source_id).length > 0 && this.opts.news.length == 0){
		
			$('#' + opts.source_id).children().each(function(){
				var title = $(this).children('.newsTool_newsLink');
				var description = $(this).children('.newsTool_newsIntroduction');
				var pubdate = $(this).children('.js_pubdate');			
				
				if(!title.length || !description.length){
					return false;
				}
				
				var title = $($(this).children('.newsTool_newsLink').get(0));
				var description = $($(this).children('.newsTool_newsIntroduction').get(0));
				var pubdate = $($(this).children('.newsTool_newsPubDate').get(0));	
				var link = undefined;
				
				// gets url from selected anchor
				if(title.get(0).tagName == 'A'){
					link = title.attr('href');
				}

				obj.opts.news.push({ title: title.html(), description: description.html(), pubdate: undefined, link: link});
				
				// adds a reference class to the element
				$(this).addClass('js_' + obj.opts.source_id + '_item_' + (obj.opts.news.length - 1));	

				$(this).hide();
				
				return true;
			});
			
			obj.show(Math.floor(obj.opts.news.length*Math.random()));
		}		
		
		if(this.opts.news.length){			
			// add navigation
			if(this.opts.news.length > 1){
				var navigation = $(document.createElement('div'));
				navigation.attr('class', this.opts.wrapper + '_navigation');
				
				// previous link
				var prev = $(document.createElement('a'));
				prev.attr('class', this.opts.nav_prev_link_class);
				prev.attr('href', 'javascript:void(0)');
				prev.html(this.opts.nav_prev_link_html);
				prev.click(function(){
					obj.show_prev();
				});
				navigation.append(prev);
				
				var status = $(document.createElement('a'));
				status.attr('id', this.opts.status_link_id);
				status.attr('href', 'javascript:void(0)');
				status.html(this.opts.status_html_on);
				status.click(function(){
					obj.toggle_reproduction();
				});
				navigation.append(status);				
				
				// next link
				var next = $(document.createElement('a'));		
				next.attr('class', this.opts.nav_next_link_class);
				next.attr('href', 'javascript:void(0)');
				next.html(this.opts.nav_next_link_html);
				next.click(function(){
					obj.show_next();
				});
				navigation.append(next);
				
				// append controls to navigation
				$('#' + obj.opts.wrapper).append(navigation);
				
				if(this.opts.auto_start){
					this.start_reproduction();
				}
			}
		}
	}
	
	// extends jquery
	$.extend({
		news_scroller: function(opts){
			var news_scroller = new NewsScroller(opts);
		}
	});
});
