window.qtyStreamNews = 0;
window.qtyGeoNews = 0;
window.voted = new Array();
window.visited = new Array();


$(document).ready(function() {
	if(window.idsezione!="" && !isNaN(window.idsezione)) {
			focusOn(window.idsezione);
	}
	if(window.id!="") {
			related(window.id);
			relatedVideo(window.id);
	}
	
	if( $("#calendario_container").length ) { 
		if(window.calendar!="" && window.section!="" && window.calendar!="undefined" && window.section!="undefined") {
			calendario(window.calendar,window.section);
		}
	}
	
	$("#overlay_video").overlay({ top: 92, closeOnClick: true, mask: "#FFFFFF", close: ".close",onClose: function(){   $("#overlay_video_content").html("");   } });
	$("#overlay_image").overlay({ top: 92, closeOnClick: true, mask: "#FFFFFF", close: ".close",onClose: function(){}});
	$(".close").click(function(){   $("#overlay_video_content").html("");   });
	
	if( $("#stream").length ) { 
		$(function()
		{
			$('#stream').jScrollPane(
				{
					autoReinitialise: true,
					showArrows: true
				}
			);
		});
		setTimeout('streamNews()',100);
		window.timerStream = window.setInterval('streamNews()',20000);	
	}
	if( $("#geo_news").length ) { 
		setTimeout('geoNews()',100);
		window.timerNews = window.setInterval('geoNews()',20000);	
	}	
});
function geoNews() {
	var id = window.lastNewsId;
	$.ajax({
		url: '/ajax/geonews/',
		type: 'get',
		data: 'id='+id,
		dataType: 'json',
		success: function(json) {
			if (json['success']) {
				window.lastGeoNewsId = json['id'];
				window.qtyGeoNews = parseInt(window.qtyGeoNews) + parseInt(json['num']);
				
				$("#geo_news").prepend( json['html'] );
				
			}
			$("#geo_news").children().removeClass("invisibile");	
			$("#geo_news").children().fadeIn("fast");
		}
	});
}

function focusOn(id) {
	if( $("#fo_expander").length ) { 
		var start = $("#fo_expander").val();
	} else {
		var start = 0;
	}
	$("#focuson_container").css("opacity","0.3");
	$.ajax({
		url: '/ajax/box-focus-on/',
		type: 'get',
		data: 'id='+id+'&s='+start,
		dataType: 'json',
		success: function(json) {
			if (json['success']) {
				if(json['extend']) {
					$("#focuson_container").html( json['html'] );
					$("#focuson_container").css("opacity","1");
					$("#fo_expander").val( json['new_start'] );
				} else {
					$("#focus-on").html( json['html'] );
				}
			}
		}
	});
}

function invia() {
	var subject = $("#subject").val();
	if(subject=="" || subject.length<10) {
		alert("Prego specifica un 'Oggetto' valido per il messaggio");
		return false;
	}
	var mess = $("#messaggio").val();
	if(mess=="" || mess.length<50) {
		alert("Il testo del messaggio deve essere di almeno 50 caratteri");
		return false;
	}	
	var email = $("#email").val();
	if(email=="") {
		alert("Prego specificare una mail valida");
		return false;
	}	
	$("#form").submit();
}

function display(div) {
			$(".newsHolders").removeClass("focus");
			$(".newsSmall").fadeIn("fast");
			$(".newsBig").fadeOut("fast");
			$("#pre_"+div).fadeOut("fast");
			$("#news_"+div).fadeIn("fast");
			$("#holder_"+div).addClass("focus");

	if( !contains(window.visited,div) ) {	
	
		$.ajax({
			url: '/ajax/visita/',
			type: 'get',
			data: 'id=' + div,
			dataType: 'json',
			success: function(json) {
				if (json['success']) {
					
					//qui incrementare array
					window.visited.push( div );
					
				}
			}
		});
	
	}
}
function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}

function voto(voto,id) {
	
	if( !contains(window.voted,id) ) {
	
		$.ajax({
			url: '/ajax/voto/',
			type: 'get',
			data: 'id=' + id + '&v=' + voto,
			dataType: 'json',
			success: function(json) {
				if (json['success']) {
					$("#voto_"+id).html( json['html'] );
					
					//qui incrementare array
					window.voted.push( id );
					
				}
			}
		});
	} else {
		$("#voto_"+id).html( "HAI GI&Agrave; VOTATO QUESTO ARTICOLO!" );
	}
}
function filtra() {
	//$("#stream").children().fadeOut("fast");
	$('input[type=checkbox]').each(function () {
	           if (this.checked) {
	           		fadeInCategory( $(this).val() );
				} else {
	           		fadeOutCategory( $(this).val() );
				}
	});
}
function fadeInCategory(id) {
	$("#stream").children().each(function () {
	        if ( $(this).attr("categoria")==id ) {
	        	$(this).fadeIn("fast");
			}
	});
}
function fadeOutCategory(id) {
	$("#stream").children().each(function () {
	        if ( $(this).attr("categoria")==id ) {
	        	$(this).fadeOut("fast");
			}
	});
}
function streamNews() {
	var id = window.lastNewsId;
	$.ajax({
		url: '/ajax/stream/',
		type: 'get',
		data: 'id=' + id,
		dataType: 'json',
		success: function(json) {
			if (json['success']) {
				window.lastNewsId = json['id'];
				window.qtyStreamNews = parseInt(window.qtyStreamNews) + parseInt(json['num']);
				//alert(window.qtyStreamNews);
				
				$("#stream").prepend( json['html'] );
				
				
				
				
			}
			$("#stream").children().removeClass("invisibile");	
			$("#stream").children().fadeIn("fast");
			filtra();
		}
	});
}

function showVideo(url) {
	
	$("#overlay_video_link").attr("href",url);
	
	$("#overlay_video").overlay().load();
	
	
	var html ="<iframe style='border:none' noscroll src='"+url+"&autoplay=1&rel=1' width='600' height='450' frameborder='0' allowfullscreen></iframe>";
	$("#overlay_video_content").html(html);
	
	/*
	// inizializzazione player
	projekktor( '#overlay_video_content', { volume: 0.5, playerFlashMP4: 'js/plug_video/jarisplayer.swf', playerFlashMP3: 'js/plug_video/jarisplayer.swf', _width: 600, _height: 450, _autoplay: true, controls: true, disablePause: false, _loop: true, debug: false, flashStreamType: 'http', useYTIframeAPI: false } );
	// nome variabile player
	name_player = projekktor('#overlay_video_content');
	var playlist = [ { 0:{ src: ''+ url +'', type: 'video/youtube'} } ];
	name_player.setFile(playlist);
	*/
	
	centerOnScreen($("#overlay_video"));
}
function zoom() {
	$("#overlay_image").overlay().load();
	centerOnScreen($("#overlay_image"));
}
function zoomTopic(img) {
	$("#topic_img").attr("src",img);
	$("#overlay_image").overlay().load();

	centerOnScreen($("#overlay_image"));
}
function centerOnScreen(div){
		var documentWidth = $(window).width();
        var documentHeight = $(window).height();
        var overlayWidth = $(div).width();
		var overlayHeight = $(div).height();
		var leftValue = (documentWidth-overlayWidth)/2;
        var topValue = (documentHeight-overlayHeight)/2;
        $(div).css("top",topValue);
		$(div).css("left",leftValue);
}
	
function related(id) {
	if(id && id!="undefined") {
		$.ajax({
			url: '/ajax/related/',
			type: 'get',
			data: 'id=' + id,
			dataType: 'json',
			success: function(json) {
				if (json['success']) {
					$('#related').html(json['html']);
				}	
			}
		});
	}
}
function calendario(datax,sez) {
	
	//alert(datax+" "+sez);
	
	
	$('#calendario_container').html('<div style="text-align:center;padding:5px">caricamento calendario&nbsp;&nbsp;<img src="/images/loading.gif" valign="absmiddle" /></div>');
	
		//$('#calendario_container').html('');
	
		$.ajax({
			url: '/ajax/calendario/',
			type: 'get',
			data: 'd=' + datax + '&s=' + sez,
			dataType: 'json',
			success: function(json) {
				if (json['success']) {
					$('#calendario_container').html(json['html']);
				}	
			}
		});
	
}
function relatedVideo(id) {
	if(id && id!="undefined") {
		$.ajax({
			url: '/ajax/relatedvideo/',
			type: 'get',
			data: 'id=' + id,
			dataType: 'json',
			success: function(json) {
				if (json['success']) {
					$('#related_video').html(json['html']);
				}	
			}
		});
	}
}
function checkSearch() {
	$("#q").removeClass("searchkeyerror");
	var k = $("#q").val();
	if(k=="Cerco..." || k=="") {
		$("#q").addClass("searchkeyerror");
		return false;
	}
	$("#ricerca").submit();
}
function showRicercaDetail() {
	var div = $("#ricercadetail_content");
	var altezzaMax = $("#inforicerca").scrollHeight;
	if(div.css("display")=="none") {
		div.css("display","");
		$("#inforicerca").animate({ height: altezzaMax } );
		$("#ricercadetail").html('<a href="" onclick="showRicercaDetail();return false" style="color:#000;text-decoration:none"><b>[</b>-<b>]</b></a>');
	}else{
		$("#inforicerca").animate({height: "15px"});
		div.css("display","none");
		$("#ricercadetail").html('<a href="" onclick="showRicercaDetail();return false" style="color:#000;text-decoration:none"><b>[</b>+<b>]</b></a>');
	}
	
}
function showLegalDetail() {
	var div = $("#legadetail_content");
	var altezzaMax = $("#infolegal").scrollHeight;
	if(div.css("display")=="none") {
		div.css("display","");
		$("#infolegal").animate({ height: altezzaMax } );
		$("#infolegal_link").html('&rsaquo;&nbsp;<a href="" onclick="showLegalDetail();return false" style="color:#000;text-decoration:none">legal &amp; copyright <b>[</b>-<b>]</b></a>&nbsp;&nbsp;');
	}else{
		$("#infolegal").animate({height: "15px"});
		div.css("display","none");
		$("#infolegal_link").html('&rsaquo;&nbsp;<a href="" onclick="showLegalDetail();return false" style="color:#000;text-decoration:none">legal &amp; copyright <b>[</b>+<b>]</b></a>&nbsp;&nbsp;');
	}
	
}
function submenu(id){
	$(".menu_sub_sezioni").css("display","none");
	$("#submenu-"+id).css("display","");
	
}
function selectAll(id)
{
    $("#"+id).focus();
    $("#"+id).select();
}
