var titolo_base = document.title;
$(function() {
	$("a.new").attr({target:"_new"});
	$("div.news").each(function() {
		var testo = new scrollerPannello(parseInt($("div.testo", this).css("lineHeight")), parseInt($("div.testo_container", this).css("height")));
		$("div.slider.testo_su", this).click(function(){testo.scrolla(-1)});
		$("div.slider.testo_giu", this).click(function(){testo.scrolla(1)});

		testo.open($("div.testo",this))
		$(this).data("scroller", testo);
	});		

	$("div.pannello_container")
		.hide()
		.fadeIn(1000)
	;
});

function schermoNewsObject(inizia_con) {
	var newsCorrente = null;
	var schermo = $("div.schermo");

	this.set = function(c) {
		var nuovaNews;

		if (c==null)
			c = $("ul.lista_news>li").eq(0).attr("class").split(" ")[0];

		condividi.titolo( titolo_base + " - " + $("li."+c+">a>span.hi").text() );
		condividi.url( $("ul.lista_news>li."+c+">a").attr("href") );

		nuovaNews = $("div.news."+c);

		var avviaSlideshow = function() {
			var nuovoSlideshow = nuovaNews.data("slideshow");
			if (nuovoSlideshow === undefined)
				return;
			nuovoSlideshow.set(0);
			nuovoSlideshow.start(6000);
		}

		nuovaNews.data("scroller").top();
	
		$("ul.lista_news>li>a").css({color: "rgb(255,255,255)"})
		$("ul.lista_news>li."+c+">a").css({color: "rgb(0,0,0)"});

		if (newsCorrente !== null)
			schermo.fadeIn(500, function() {
				var vecchioSlideshow = newsCorrente.data("slideshow");
				if (vecchioSlideshow !== undefined)
					vecchioSlideshow.stop();
				newsCorrente.css({display: "none"});
				nuovaNews.css({display: "block"});
				schermo.fadeOut(500, avviaSlideshow);
				
				newsCorrente = nuovaNews;
				$("ul.lista_news>li."+c+">a").css({color: "rgb(0,0,0)"});
			});
		else {
			nuovaNews.css({display: "block"});
			schermo.fadeOut(500, avviaSlideshow);
			newsCorrente = nuovaNews;
		}
		

	}

	if ((inizia_con != null)&&(s = /^(\d+)$/.exec(inizia_con)))
		inizia_con = "news"+parseInt(s[1]);
	else
		inizia_con = null;

	this.set(inizia_con);

	$("ul.lista_news>li>a").hover(function(){
		var cl = $(this).parent().attr("class");
		if (!newsCorrente.hasClass(cl))
			$(this).animate({"color": "rgb(0,0,0)"}, 150);
		return false;
	},function(){
		var cl = $(this).parent().attr("class");
		if (!newsCorrente.hasClass(cl))
			$(this).animate({"color": "rgb(255,255,255)"}, 150);
	}).click(function(){

		var cl = $(this).parent().attr("class").split(" ")[0];
		if (!newsCorrente.hasClass(cl)) {
			schermoNews.set(cl);
		}
		return false;
	});;

	return this;

}

function archivioNewsObject(parti_da) {
	var anno_scelto=null;
	var me=this;
	var $pannello = $("div.pannello.lista_news");
	this.scegli = function(y) {
		if (anno_scelto !== null) {
			$("ul.anni>li."+anno_scelto).css({color: "rgb(255,255,255)"});
		}
		anno_scelto = y;
		$("ul.anni>li."+anno_scelto).css({color: "rgb(0,0,0)"});
		$("ul.lista_news>li").hide();
		$("ul.lista_news>li."+anno_scelto).show();
	};
	var testo = new scrollerPannello(
		parseInt($("ul.lista_news>li", $pannello).css("height")), 
		parseInt($("div.testo_container", $pannello).css("height"))
	);
	$("div.slider.testo_su", $pannello).click(function(){console.log("su");testo.scrolla(-1)});
	$("div.slider.testo_giu", $pannello).click(function(){console.log("giu");testo.scrolla(1)});

	testo.open($("ul.lista_news",$pannello));

	$("ul.anni>li").hover(function(){
		var y=$(this).attr("class");
		if (y != anno_scelto)
			$(this).animate({"color": "rgb(0,0,0)"}, 150);
	}, function(){
		var y=$(this).attr("class");
		if (y != anno_scelto)
			$(this).animate({"color": "rgb(255,255,255)"}, 150);
	}).click(function() {
		me.scegli($(this).attr("class"));
	});

	if (parti_da === undefined)
		this.scegli($("ul.anni>li").eq(0).attr("class"));
	else
		this.scegli("y"+parti_da);

	return this;
}