$(document).ready(function(){
	//$(".scrollbars").scrollbar();
	$('.accordion_container').accordion();
	
	$('ul#nav > li').mouseenter(function() {
	  $(this).children("ul.drop_menu").slideDown();
	});
	
	$('ul#nav > li').mouseleave(function() {
	  $(this).children("ul.drop_menu").slideUp();
	});
	
	$("ul.drop_menu").mouseleave(function() {
	  $(this).slideUp();
	});

	if($("#intro_page")){
		if($("#intro_page_content").height() <= $("#intro_page").height()){
			 $("#intro_page").height("auto").css("margin-bottom","30px");
			 $("#intro_page").next(".vedi_tutto").hide();
		}
	}
});

function setBacheca(){
	if($("#bacheca").css("display")=='block') {
		$("#bacheca").slideUp("slow");  
		$("#puls_bacheca").addClass('puls_bacheca_apri');
		$("#puls_bacheca").removeClass('puls_bacheca_chiudi').attr("title","Apri la bacheca");
		var status="close";
	} else {
		$("#bacheca").slideDown("slow");  
		$("#puls_bacheca").addClass('puls_bacheca_chiudi');
		$("#puls_bacheca").removeClass('puls_bacheca_apri').attr("title","Chiudi la bacheca");
		var status="open";
	}
	
	$.get("actions.php", {
		action: 'set_menu_status',
		status: status,
		nd: new Date().getTime()
	});
}

function get_bacheca(){
	$.get("get_bacheca.php", {
		nd: new Date().getTime()
		},
	function(d)	{
		if(d){
			$("#cont_bacheca>ul").empty().html(d);
			
			var tot_msg_bacheca=$("#cont_bacheca>ul>li.tot_msg_bacheca").text();
			if(tot_msg_bacheca) {
				$("#notifiche").addClass("on");
				$("#num_notifiche").text(tot_msg_bacheca);
			} else {
				$("#notifiche").removeClass("on");
				$("#num_notifiche").text('0');
			}
			
			var tot_msg_privati=$("#cont_bacheca>ul>li.tot_msg_privati").text();
			if(tot_msg_privati) {
				$("#msg").addClass("on");
				$("#num_messaggi").text(tot_msg_privati);
			} else {
				$("#msg").removeClass("on");
				$("#num_messaggi").text('0');
			}
		}
	});
}

function open_intro(el){
	var h=$("#intro_page").height();
	if(h>150) {
		var new_h="150";
		el.removeClass("leggi_close").empty().text("LEGGI TUTTO");
		
	} else {
		var new_h="auto";
		el.addClass("leggi_close").empty().text("CHIUDI");
	}
	$("#intro_page").css("height",new_h);
}

function open_intro2(el){
	var h=$("#intro_page").height();
	if(h>350) {
		var new_h="350";
		el.removeClass("leggi_close").empty().text("LEGGI TUTTO");
		
	} else {
		var new_h="auto";
		el.addClass("leggi_close").empty().text("CHIUDI");
	}
	$("#intro_page").css("height",new_h);
}

function to_contact(user_id){
	if(user_id){
		$.get("actions.php?action=to_contact&user_id="+user_id+"&nd="+new Date().getTime(), 
			function(d) {
				if(d=="OK"){
					$(".bt_add_user_"+user_id).empty().remove();
					jAlert("Il contatto &egrave; stato aggiunto", 'OK');
				} else jAlert(d, 'Attenzione');
			});	
	}
}

function remove_contact(user_id){
	jConfirm('Rimuovo questo utente dai contatti?', 'Conferma eliminazione', function(r) {
		if(!r) return false;
		else {
			$.get("actions.php?action=remove_contact&user_id="+user_id+"&nd="+new Date().getTime(), 
			function(d) {
				if(d=="OK"){
					$(".bt_remove_user_"+user_id).empty().remove();
					jAlert("Il contatto &egrave; stato rimosso", 'OK');
				} else jAlert(d, 'Attenzione');
			});	
		}
	});
	return false;	
}

function checkTesto() {
 	if(!$('#text_commento').val()) {
		jAlert("Non hai inserito il testo del commento", 'ATTENZIONE');
		return false;
	} else $("#form_commenti").submit();
}

function checkRicerca() {
 	if(!$('#search_key').val()) {
		jAlert("Non hai inserito il termine da ricercare", 'ATTENZIONE');
		$("#search_key").focus();
		return false;
	} 
	if($('#search_key').val() && $('#search_key').val().length<3){
		jAlert("Inserisci almeno tre caratteri", 'ATTENZIONE');
		$("#search_key").focus();
		return false;
	}
}


function notifica_letta(id){
	window.location="actions.php?action=notifica_letta&id="+id
}

function close_msg(id_msg){
	$("#msg_body_"+id_msg).slideUp();	
}

function open_msg(id_msg, is_new){
	$("#msg_body_"+id_msg).slideDown();
	if(is_new){
		$("#ico_msg_"+id_msg).removeClass("new");
		$.get("actions.php?action=msg_letto&id="+id_msg+"&nd="+new Date().getTime());
	}
}

function delete_msg(id_msg, type){
	if(type=="sent") var action="delete_msg_sent";
	else action="delete_msg";
	jConfirm('Elimino questo messaggio?', 'Conferma eliminazione', function(r) {
		if(!r) return false;
		else {
			$.get("actions.php?action="+action+"&id="+id_msg+"&nd="+new Date().getTime(), 
			function(d) {
				if(d=="OK"){
					$("#msg_"+id_msg).empty().remove();
					jAlert("Il messaggio &egrave; stato eliminato", 'OK');
				} else jAlert(d, 'Attenzione');
			});	
		}
	});
	return false;	
}

function delete_disc(id_msg){
	jConfirm('Elimino questa discussione?', 'Conferma eliminazione', function(r) {
		if(!r) return false;
		else {
			$.get("actions.php?action=delete_disc&id="+id_msg+"&nd="+new Date().getTime(), 
			function(d) {
				if(d=="OK"){
					$("#disc_"+id_msg).empty().remove();
					jAlert("La discussione &egrave; stata eliminata", 'OK');
				} else jAlert(d, 'Attenzione');
			});	
		}
	});
	return false;	
}
