// JavaScript Document

/* ---------------------------- GLOBALS ---------------------------- */
jQuery(document).ready(function() {	
	
	/* ---------------------------- FOTO SOPRA_TITOLO ---------------------------- */	
	if (jQuery("img.sopra_titolo").attr("src") != undefined) {
		jQuery("img.sopra_titolo").hide();
		var imago = "<img class='sopra_titolo' src='" + jQuery("img.sopra_titolo").attr("src") + "'>";
		if (jQuery("div.componentheading")) {jQuery("div.componentheading").prepend(imago);}
		if (jQuery("td.contentheading")) {jQuery("td.contentheading").prepend(imago);}
	}
	
	/* ---------------------------- News Show Pro GK4 CUSTOMIZATION --------------------------- */	
	jQuery(".nsp_art").each(function(index) {
		jQuery(this).css("width","290px");
		if (jQuery(this).css("clear") == "both") {
			jQuery(this).before('<div style="clear:both;"></div>').css("margin-right","20px");
			}
		}
	);
	jQuery(".news .nsp_info").each(function(index) {jQuery(this).removeClass("fleft");});
	jQuery(".focus .nsp_art").each(function(index) {jQuery(this).css("width","auto");});
	
	// correggo struttura FOCUS
	if (jQuery(".focus .nsp_art img.nsp_image").attr("src") != undefined) {
		jQuery(".focus .nsp_art img.nsp_image").hide();
		var imago = "<img class='lato_titolo' src='" + jQuery(".focus .nsp_art img.nsp_image").attr("src") + "'>";
		jQuery(".focus .nsp_art").prepend(imago);
		jQuery(".focus .nsp_art div").addClass("lato_titolo");
	}
	
	// correggo link read-more
	jQuery(".focus").css("margin","0px");
	jQuery(".nsp_art .nsp_readmore").html(" continua");
	jQuery(".nsp_art .readon").html(" continua");
	jQuery(".news .nsp_readmore").html(" leggi la news");
	jQuery(".news .readon").html(" leggi la news");
	jQuery(".magazines .nsp_readmore").html(" vai al sommario");
	jQuery(".magazines .readon").html(" vai al sommario");
	
	// cambio gli eventuali link con classe popup
	jQuery("a.popup").each(function(index) {
		linko = jQuery(this).attr("href");
		jQuery(this).attr("href","javascript:openpopup('" + linko + "');");		
	});
	
	// tracco le parole del tag cloud
	jQuery("div.aixeena_tags a").each(function(index) {
		titolo = jQuery(this).attr("title");
		jQuery(this).bind("click", function(e) {_gaq.push(['_trackEvent', 'tags-cloud', titolo]);}).attr('target', '_blank');		
	});
	
	// nella versione mobile visualizzo il main menu
	$("div.mobilemenu a").click(function(e) {
		$("#leftcolumn").toggleClass("mobile");
		$("#topmenu ul.menu").toggleClass("mobile");
		$("#topmenu").toggleClass("mobile");
		$("body.blog div.intro2").toggleClass("mobile");
	})
	
	// correggo il link delle foto all'articolo
	jQuery("div.tanbox").each(function(index) {
		linko = $("h3 a", this).attr("href");
		$("a",this).attr("href",linko);		
	});
	
	// toggle homepage menu courses
	$("a.corsibtn").click(function(e) {
		$("ul.corsilist").toggle("slow");
	})
	$("a.progettibtn").click(function(e) {
		$("ul.progettilist").toggle("slow");
	})
	
	// inserisco il link ai nostri post
	$(".nostripost>h3").click(function(e) {
		document.location.href="/i-nostri-post";
	})
	
	// show details posts
	// $(".nostripost>h3").after("<p class='nota'>Da febbraio 2014 la RIISG, Rete Italiana per l'Insegnamento della Salute Globale ha lanciato un diario sul blog Saluteinternazionale.info per discutere pubblicamente sulla formazione in Medicina, capirne i limiti e proporre soluzioni.</p>");
	
	// addthis asincrono
	// addthis.init()
	
	// carico il modulo di salute internazionale
	if ($("div#sipreview").length > 0) {
		$.ajax({
			url : "../moduli/saluteinternazionale/index.php",
			success : function (data,stato) {
				$("div#sipreview").html(data);
				$("div#ajaxloader").hide();
				
				// avvio lo scroll di saluteinternazionale
				conto = $("div.tanscroll").width()/$("div.tanbox").width();
				if (conto == 1) {conto=5;} // FIX IE 10
				$("div.tanscroll").attr('rel',conto);
				$("div.tanscroll").attr('rev',1);
				setInterval(function(){ saluteinternazionale () }, 2000);
				
				// tracco le parole del tag cloud
				$("div.moduletable.ulli div.tanbox").each(function(index) {
					hreffo = $("h3 a", this).attr('href');
					$("a.thumb", this).attr('href', hreffo);
				});
			},
			error : function (richiesta,stato,errori) {
				$("div#sipreview").html('<a href="http://www.saluteinternazionale.info/category/aree/salute-globale/">Vai a sito</a>');
			}
		});	
	}
	
	// se spedisco un commento attivo lo scroll "normale"
	if (window.location.hash == "#comments") {
		$("body.blog div#header").css("position", "absolute");
		$("body.blog #topmenu").css("position", "absolute");
	}
	$("#comments-form-send a").click(function(e) {
		$("body.blog div#header").css("position", "absolute");
		$("body.blog #topmenu").css("position", "absolute");
	})
	
	// aggiorno il link alla privacy
	$("body.en-GB div.coockie_banner a").attr('href','/en/privacy-and-cookies');
	$("body.blog div.coockie_banner a").attr('href','/blog/privacy-e-cookies');
	
	// activate/deactive cookie's popup
	if (getCookie('pisapchevron') != 'off') {$("div.moduletable.eucookie").removeClass("pisapchevron");}
	$("div.moduletable.eucookie a.btn").bind('click touchstart', function () {
		$("div.moduletable.eucookie").toggleClass("pisapchevron");	
		v = $("div.moduletable.eucookie").hasClass("pisapchevron")?'off':'on';
		setCookie('pisapchevron',v,5);
	});
	
	// PALESTRE
	$("body.palestre div.items-row div.item img").each(function(index) {
		$("a",$(this).parent().parent()).attr('id',$(this).attr('alt'));
	})
	$("body.palestre div.items-row div.item img").click(function(e) {
		document.getElementById($(this).attr('alt')).click();
	})
	$("body.palestre div.items-row div.item img").mouseover(function(e) {
		$("a",$(this).parent().parent()).addClass('hover');
	})
	$("body.palestre div.items-row div.item img").mouseout(function(e) {
		$("a",$(this).parent().parent()).removeClass('hover');
	})
	
	// BLOG REGISTRATION
	if ($("label#jform_profile_eafrica-lbl a").length > 0) {$("label#jform_profile_eafrica-lbl a").attr('href', 'http://www.mediciconlafrica.org/blog/la-nostra-voce/libri-e-riviste/eafrica/');}
	if ($("label#jform_profile_newsletter-lbl a").length > 0) {$("label#jform_profile_newsletter-lbl a").attr('href', 'http://www.mediciconlafrica.org/wp-content/uploads/newsletters/mail17957.html');}
	if ($("label#jform_profile_address1-lbl").length > 0) {$("label#jform_profile_address1-lbl").html('Indirizzo:<span class="star">&nbsp;*</span>');}
});

//  funzione che scrolla saluteinternazionale
function saluteinternazionale() {
	conto = parseInt($("div.tanscroll").attr('rev'));
	if (conto == parseInt($("div.tanscroll").attr('rel'))) {
		$("div.tanscroll").attr('rev',1);
		// $("div.tanscroll").css('marginLeft','0');
		$("div.tanscroll").animate({marginLeft: '0'}, 200);
	} else {
		$("div.tanscroll").attr('rev',conto+1);
		$("div.tanscroll").animate({marginLeft: '-=230px'}, 500);
	}
}

//  funzione che apre una popup
function openpopup(linko) {
	window.open(linko,'mywindow','status=0,menubar=0,resizable=1,scrollbars=1,width=600,height=650');
}

//  funzione che verifica che una stringa sia scritta in forma di 'numero'
function isnumeric(sText)
{
	var ValidChars = "0123456789.,";
	var IsNumber=(sText.length > 0);
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {IsNumber = false;}
	}
	if (IsNumber) {
		var nv = sText.split(",");	
		if (nv.length > 2) {IsNumber = false;}
		var np = sText.split(".");	
		if (np.length > 2) {IsNumber = false;}
		var pp = sText.indexOf(".");
		var pv = sText.indexOf(",");
		if ((pp > -1) && (pv >-1) && ((pv < (pp + 4)) || (pv > (pp + 4)) || (pp > pv))) {IsNumber = false;}
	}
	
	return IsNumber;
}

// funzione per gestire la visualizzazione dei tooltip degli appuntamenti
function tooltip_calendar (j) {
	stato = jQuery('#tt' + j).is(':visible');
	jQuery('.tooltip').hide();
	jQuery('.mod_events_daylink').css("background-color", "").css("color", "");
	if (!stato) {
		jQuery('#tt' + j).show();
		jQuery('#dd' + j).css("background-color", "#FFFFFF").css("color", "#86012C");
	}
}

function setCookie(cname,cvalue,exdays) {
	var d = new Date();
	d.setTime(d.getTime()+(exdays*24*60*60*1000));
	var expires = "expires="+d.toGMTString();
	var domains = "domain=gruppomarcato.it";
	var paths = "path=/";
	document.cookie = cname + "=" + cvalue + "; " + expires + "; " + paths;
}

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
	  var c = ca[i].trim();
	  if (c.indexOf(name)==0) return c.substring(name.length,c.length);
	}
	return "";
}
