// Imposta se è sulla lista dei messaggi
var listMsg = true;
// Imposta se i messaggi sono visibili
var cmsg = false;
// Imposta nuovi messaggi
var newMsg;
// Tempo di attesa per il refresh dei messaggi
var tempo_original = 7000;
var tempo = tempo_original;
// Titolo pagina
var titolo = document.title;

function msgMenu() {
	// Attiva il menu del messaggio Aperto
	$("#cmsg_ln_close").click(function() {
		waiting();
		refreshMsg();
		return false;
	});
	$("#cmsg_ln_reply").click(function() {
		listMsg = false;
		var action = $(this).attr('href').substr(1).replace(".","&dest=");
		$(this).removeAttr('href');
		$(".cmsg_full p").append('<div class="cmsg_reply"></div>');
		if($("#cmsg_ogg").html().substr(0,4) != "Re: ") {
			var subjRe = "Re: "+$("#cmsg_ogg").html();
		} else {
			var subjRe = $("#cmsg_ogg").html();
		}
		var formReply = '<form id="cmsg_send_reply" method="post" action="ajax.php">';
		formReply += 'Oggetto:<br />';
		formReply += '<input type="text" value="'+subjRe+'"/><br />';
		formReply += 'Testo:<br />';
		formReply += '<textarea name="cmsg_reply_text"></textarea>';
		formReply += '<input type="submit" class="cmsgbtn" value="Invia">';
		formReply += '</form>';
		$(".cmsg_reply").html(formReply);
		$("textarea[name=cmsg_reply_text]").focus();
		
		$('#cmsg_send_reply').submit(function() {
			// Invia la risposta
			var reply_subj = escape($.trim($("input:first").val()));
			var reply_text = escape($.trim($("textarea:first").val()));
			if(reply_subj+reply_text != "") {
				var par = action+"&subj="+reply_subj+"&testo="+reply_text;
				$.ajax({
  				type:"POST", url:"msgsend.php", data:par+"&ajax=1",  
     			success: function(response) {
     				waiting("Messaggio Inviato");
       			var refresh = setTimeout(refreshMsg, 800);
       			//var refresh = setTimeout(refreshMsg, tempo);
					}
    		});	
			}
			return false;
		});
		return false;
	});
	$("#cmsg_ln_trash").click(function() {
		var msgid = $(this).attr('href').substr(1);
		waiting();
		$.ajax({
      type:"POST", url:"ajax.php", data:msgid+"&ajax=1",  
      success: function(response) {
        refreshMsg();
      }
    });
		return false;
	});
}

function msgOpen() {
	// Apre un messaggio
	$(".cmsg_open").click(function() {
		//Toglie tutti messaggi
		waiting();
		var msgid = $(this).attr('href').substr(1);
		$.ajax({
      type:"POST", url:"ajax.php", data:"msgid="+msgid+"&ajax=1",  
      success: function(response) {
        $("#cmsg_main").html(response);
        listMsg = false;
        msgMenu();
      }
    });
    return false;
	});
}

function refreshMsg() {
	// Aggiorna la lista dei messaggi
	$.ajax({
		type:"POST", url:"ajax.php", data:"msgaction=list&ajax=1",
		success: function(response) {
			$("#cmsg_main").html(response);
			msgOpen();
			listMsg = true;
			contaMessaggi(true);
		}
	});
}

function waiting(text) {
	if(text == null) text = "Caricamento..";
	var cmsgHeight = $("#cmsg_main").height();
	var confirmText = '<div style="line-height:'+cmsgHeight+'px;';
	confirmText += "text-align:center;";
	confirmText += "font-size: 15px;font-weight:bold;"
	confirmText += '">'+text+'</div>';
	$("#cmsg_main").html(confirmText);
}

function toggleClass(classe){
	if(!$("#cmsg_toggle").hasClass(classe)) {
		$("#cmsg_toggle").removeClass();
		$("#cmsg_toggle").addClass(classe);
	}
}

function contaMessaggi(block) {
	// Conta i nuovi messaggi
	//if(listMsg && cmsg && !block) alert(newMsg);
	$.ajax({
		type:"POST", url:"ajax.php", data:"msgaction=count&tm="+tempo,
		success: function(response) {
			if(response != newMsg) {
				newMsg = response;
				if(listMsg && cmsg && !block) refreshMsg();
			}
			if(response > 0) {
				$("#cmsg_toggle").text(response);
				toggleClass("cmsg_toggle_new");
				if(response == 1) $("#cmsg_text").text("Nuovo messaggio");
				else $("#cmsg_text").text("Nuovi messaggi");
				document.title = "("+response+") "+titolo;
			} else {
				$("#cmsg_toggle").text("");
				toggleClass("cmsg_toggle_active");
				$("#cmsg_text").text("Nessun nuovo messaggio");
				document.title = titolo;
			}
		}
	});
}

function contaMessaggiAuto() {
	// Cerca nuovi messaggi ciclicamente
	contaMessaggi();
	setTimeout(contaMessaggiAuto, tempo);
	increaseTime();
}

function increaseTime() {
	if(tempo < 180000) {
		tempo += 2000;
	}
}

$(document).ready(function() {
	//tempo = tempo_original;
	if($("#cmsg_container").size()) setTimeout(contaMessaggiAuto, tempo);
	//if($("#cmsg_container").size()) cycleMsg = setInterval("contaMessaggi()", 5000);
	// Imposta se il mouse è sopra il div dei messaggi
	var mousein = false;
	
	$("#cmsg_container").hide();
	
	$("#cmsg_toggle, #cmsg_text").mouseover(function() {
		mousein = true;
	});
	$("#cmsg_toggle, #cmsg_text").mouseout(function() {
		mousein = false;
	});
	$("#cmsg_toggle, #cmsg_text").click(function() {
		//$("#cmsg_container").toggle();
		if(!cmsg) {
			$("#cmsg_container").show();
			cmsg = true;
			if(listMsg) {
				waiting();
				refreshMsg();
			}
		} else {
			cmsg = false;
			$("#cmsg_container").hide();
		}
		return false;
	});
	
	$("#cmsg_container").hover(function() {
		// Mouse sul div dei messaggi
		mousein = true;
	}, function() {
		mousein = false;
	});
	
	$(document).mouseup(function() {
		if(!mousein) {
			$("#cmsg_container").hide();
			cmsg = false;
		}
	});
	msgOpen();
	
	// Resetta il tempo di attesa se si muove il mouse
	$(document).mousemove(function() {
		tempo = tempo_original;
	});
});




