(function($){
	$.fn.AutostradeNewsSlider=function(options){
		var settings = $.extend({
			total:	0,
			width:	180,
			timer:	3000,
			height: 100,
			width: 150,
			here: 	0,
			inter: 	''
		}, options);
		return this.each(function() {
			var $this = $(this);
			var $set = settings;
			
			var move = function(){
				$('#AutoNwsCont').animate({'left':'-'+($set.here*$set.width)+'px'},'slow','linear',function(){
					if ($set.here == $set.total){
						$('#AutoNwsCont').css({left:'-0px'});
						$set.here = 0;
					}
					Updater();
				});				
			};
			
			var Next = function(){
				$set.here++;
				move();
			};
			
			var Prev = function(){
				$set.here--;
				move();
			};
			
			var SetBox = function(){
				$this.find('.arrsx,.autocenter,.arrdx').css({height : $set.height+'px'}); 
				$('#AutoNwsCont').css({'position':'relative',width:($set.width*$set.total)+'px'});
			};
			
			var Updater = function(){
				$('#Autopaging').html(($set.here+1)+' di '+$set.total);
			};
			
			var Binder = function(){
				if($set.total > 2){
					$this
						.find('.autocenter')
							.hover(
								function(){clearInterval($this.inter);},
								function(){$this.inter = setInterval(function() { Next(); }, $set.timer);}
							)
							.bind('mousemove',function(){
								clearInterval($this.inter);
							});
					$this.inter = setInterval(function() { Next(); }, $set.timer);
				}
				$this
					.find('.arrsx,.arrdx')
						.hover(
							function(){
								clearInterval($this.inter);
								$(this).css({'background-color':'#ccc'});
							},
							function(){
								$this.inter = setInterval(function() { Next(); }, $set.timer);
								$(this).css({'background-color':'#fff'});
							}
						);
				$this
					.find('.arrsx')
						.unbind('click')
						.bind('click',function(){
							if($set.here > 0){ Prev(); }
						});
				$this
					.find('.arrdx')
						.unbind('click')
						.bind('click',function(){
							if($set.here < $set.total){ Next(); }
						});
				$('.AutoNews,#Autopaging').unbind('click').bind('click',function(){location.href='/redazione/autostrade.php?m_id=4&amp;sm_id=4'});
				
			};
			
			var GetInfo = function(){
				$.ajax({
					type: "GET",
					url: "/FeedXML/Autostrade-traffico.xml",
					dataType: "xml",
					success: function(xml) {
						$('#AutoNwsCont').empty();
						$(xml).find('evt').each(function(){
							$html = "<div style='margin-top:5px; font-weight:bold; width:"+$set.width+"px'>"+$(this).find('titolo').text()+"</div>" +
									"<div style='margin-top:5px; width:"+$set.width+"px'>"+$(this).find('testo').text()+"</div>";
							$div = $('<div></div>').addClass('AutoNews').css({'float':'left','width': $set.width+'px', 'overflow':'hidden', 'height': $set.height+'px'}).html($html).appendTo($('#AutoNwsCont'));
								
								//$('<div></div>').css({'margin-top':'5px;','font-weight':'bold', 'width': $set.width+'px'}).html($(this).find('titolo').text()).appendTo($div);
								//$('<div></div>').css({'margin-top':'5px;','width': $set.width+'px'}).html($(this).find('testo').text()).appendTo($div);
							$set.total++;
						});
						Binder();
						SetBox();
						Updater();
						//Starter();
					}
				});
			};
			
			var Init = function(){
				GetInfo();
			};
			
			$(document).ready(function(){ Init();});
		});
	};
})(jQuery);


