$(document).ready(function(){
	$('a#linkNews').click(function(){
		caricaLinkMenu('newsDescr.php');
		return(false);
	});
	$('a#linkPlus').click(function(){
		caricaLinkMenu('listPlus.php');
		return(false);
	});
	$('a#linkInfo').click(function(){
		caricaLinkMenu('infoDescr.php');
		return(false);
	});
	$('#txtRicerca').focus(function(){
		$('#txtRicerca').css('color', 'black');
		if($('#txtRicerca').val()=="ricerca.."){
			$('#txtRicerca').val("");
		};
	});
	$('#txtRicerca').blur(function(){
		$('#txtRicerca').css('color', 'grey');
		if($('#txtRicerca').val()==""){
			$('#txtRicerca').val("ricerca..");
		};
	});
	$('#txtRicerca').keypress(function(e){
		var code=(e.keyCode ? e.keyCode : e.which);
		if(code==13)avviaRicerca();
	});

	setTimeout(renderGPlusOne(), 100);
	setTimeout(renderIFrame(), 100);
});

function avviaRicerca(){
	if($('#txtRicerca').val() != "" && $('#txtRicerca').val() != "ricerca.."){
		caricaLinkMenu('newsDescr.php', $('#txtRicerca').val());
	}
}

function renderGPlusOne(){
	//google plus
	$('#gPlusOneHead').html('<div class="g-plusone" data-annotation="inline" data-width="250" data-href="http://www.dennybiasiolli.com/"></div>');
	window.___gcfg = {lang: 'it'};
	var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
	po.src = 'https://apis.google.com/js/plusone.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	//twitter
	var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
	po.src = '//platform.twitter.com/widgets.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
}

function renderIFrame(){
	var art = $("div.corpoArticolo");
	art.find("iframe").each(function() {
		if($(this).data("src"))
			$(this).attr('src', $(this).data("src"));
	});
}

function caricaLinkMenu(pagina, ricerca){
	$('#corpoPagina').slideUp('slow', function(){
		$.post(pagina, {ricerca: ricerca}, function(data){
			$('#tblCorpo').html(data);
			$('#corpoPagina').slideDown('slow', function(){
				//scroll('#corpoPagina');
				return(false);
			});
		});
	});
}

function selectTag(tag){
	$('#corpoPagina').slideUp('slow', function(){
		$.post('newsDescr.php', {tag: tag}, function(data){
			$('#tblCorpo').html(data);
			$('#corpoPagina').slideDown('slow', function(){
				scroll('#corpoPagina');
			});
		});
	});
}

function selectCateg(categoria){
	$('#corpoPagina').slideUp('slow', function(){
		$.post('newsDescr.php', {cat: categoria}, function(data){
			$('#tblCorpo').html(data);
			$('#corpoPagina').slideDown('slow', function(){
				scroll('#corpoPagina');
			});
		});
	});
}

function selectPage(pagina, categoria, tag, ricerca){
	$('#corpoPagina').slideUp('slow', function(){
		$.post('newsDescr.php', {pag: pagina, cat: categoria, tag: tag, ricerca: ricerca}, function(data){
			$('#tblCorpo').html(data);
			$('#corpoPagina').slideDown('slow', function(){
				scroll('#corpoPagina');
			});
		});
	});
}

function selectPost(id, scorri, posizione){
	$('#corpoPagina').slideUp('slow', function(){
		$.post('newsDescr.php', {articolo: id}, function(data){
			$('#tblCorpo').html(data);
			$('#corpoPagina').slideDown('slow', function(){
				if(scorri){
					scroll(posizione);
				}
				else{
					scroll('#corpoPagina');
				}
			});
		});
	});
}

function commenta(id){
	$.post('newsDescr.php', {articolo: id, commenta: 1}, function(data){
		$('#tblCorpo').html(data);
		scroll('#fineFormCommenti');
		$('#txtNome').focus();
	});
}
function mipiace(id, voto){
	$.post('mipiace.php', {azione: 'vota', sezione: 'news', idSezione: id, voto: voto}, function(data){
		//$('#rss_mipiace_'+id).html(data);
	});
}
function gMipiace(obj) {
	var h;
	h = obj.href;
	h = h.replace('http://www.dennybiasiolli.com/', '');
	h = h.replace('?', '');
	h = h.replace('sezione=news', '');
	h = h.replace('articolo=', '');
	h = h.replace('lang=it', '');
	h = h.replace('lang=en', '');
	h = h.replace('&', '');
	mipiace(h, (obj.state=='on' ? 1 : 0))
}

//gestisce lo scrolling per ogni click nel menu
function scroll(hh){$.scrollTo(hh || 0, 1000, 'easeOutBack');return(false);}



