var MyLink;
var Clicked = 0;
var Divs = Array();
var divs = 0;

function ChangeClass(ID, NewClass) {
	if (typeof(document.getElementById(ID)) != 'null') {
		document.getElementById(ID).className = NewClass;
	}
}

function ShowDiv(ID) {
	if (typeof(document.getElementById(ID)) != 'null') {
		document.getElementById(ID).style.display = 'block';
	}
}

function HideDiv(ID) {
	if (typeof(document.getElementById(ID)) != 'null') {
		document.getElementById(ID).style.display = 'none';
	}
}

function ShowHideDiv(ID) {
	for (var ID2 in Divs) {
		if (ID != ID2) {
			Divs[ID2][0] = false;
			HideDiv(Divs[ID2][1]);
		}
	}

	if (typeof(Divs[ID]) == 'undefined') {
		Divs[ID] = new Array(false, ID);
	}

	if (Divs[ID][0]) {
		HideDiv(ID);

		Divs[ID][0] = false;
	} else {
		ShowDiv(ID);

		Divs[ID][0] = true;
	}
}

function SetMyLink(Link) {
	MyLink = Link;
}

function GetMyLink() {
	return MyLink;
}

function GoTo(Link) {
	document.location.href = Link;
}

function GoToWait(Link, Wait) {
	window.setTimeout("GoTo('" + Link + "')", Wait);
}


// VARIABILI
var timerID = 0;
var charready = 0;
var checksum = "empty";
var question = "alive";

/************
 *** LOGO ***
 ************/
function SetLogoLoading() {
	$("#Logo").css( {backgroundImage: "url(http://www.hackroom.org/images/logo_loading.gif)"} );
}

function SetLogoNormal() {
	$("#Logo").css( {backgroundImage: "url(http://www.hackroom.org/images/logo.png)"} );
}

/********************
 *** CICLO VITALE ***
 ********************/
function fshowciclovitale() {
	if($("#ciclovitale").is(":visible")){
		fhidediv("ciclovitale");
	} else {
		fshowdiv("ciclovitale");
		$.post("./ajax/caronte.php", { vai: "OK"}, function (data){
			$("#ciclovitale").html(data);
		} );
	}
}

/*******************************
 *** GESTIONE RICERCA UTENTE ***
 *******************************/
function fcercautente() {
	var utente;
	utente=document.getElementById("cercautente").value;
//	utente=$("#cercautente").text;

	if( utente.length == 0 ) {
		fhidediv("ricercautente");
	} else {
		if($("#ricercautente").is(":hidden")){
			fshowdiv("ricercautente");
		}
		$.post("./ajax/caronte.php", { checknick: utente}, function (data){
			if( data == "NDN") {
				$("#ricercautente").html("<font size='-2' color='#5A9F00'>Nessun utente disponibile</font>");
			} else if( data == "NNN") {
				$("#ricercautente").html("<font size='-2' color='#5A9F00'>Inserisci almeno due lettere.</font>");
			} else {
				$("#ricercautente").html("<font size='-1''>"+data+"</font>");
			}
		} );
	}
}

/******************************
 *** GESTIONE UTENTI ONLINE ***
 ******************************/
function ritardaonline() {
	setTimeout('ritardaonline()',10000);
	$.post("./ajax/caronte.php", { online: question}, function (data){
		if( data == "NDN") {
			$("#useronline").html("<font size='-2' color='#5A9F00'>Nessun&nbsp;utente&nbsp;loggato&nbsp;connesso</font>");
		} else {
			$("#useronline").html(data);
		}
	} );
}

function fshowonline() {
	if($("#useronline").is(":visible")){
		fhidediv("useronline");
		question="alive";
	} else {
		fshowdiv("useronline");
		question="question";
		ritardaonline();
	}
}

/************************
 *** GESTIONE GRAFICA ***
 ************************/
function fshowdiv(riferimento) {
variediv = new Array();
variediv[0]=riferimento;
	switch (riferimento) {
		case "ciclovitale": {
			variediv[1]="ricercautente";
			variediv[2]="useronline";
			break;
		}
		case "ricercautente": {
			variediv[1]="ciclovitale";
			variediv[2]="useronline";
			break;
		}
		case "useronline": {
			variediv[1]="ciclovitale";
			variediv[2]="ricercautente";
			break;
		}
	}
	if($("#"+variediv[1]).is(":hidden")){
		if($("#"+variediv[2]).is(":hidden")){
$("#"+variediv[0]).css( {bottom: (document.getElementById("menubottom").offsetHeight)+"px", right: "1px"});
		} else {
$("#"+variediv[0]).css( {bottom: (document.getElementById("menubottom").offsetHeight)+173+"px", right: "1px"});
		}
	} else {
		if($("#"+variediv[2]).is(":hidden")){
$("#"+variediv[0]).css( {bottom: (document.getElementById("menubottom").offsetHeight)+173+"px", right: "1px"});
		} else {
$("#"+variediv[0]).css( {bottom: (document.getElementById("menubottom").offsetHeight)+"px", right: (document.getElementById(variediv[1]).offsetWidth+2)+"px"});
		}
	}
	$("#"+riferimento).css( {visibility: "visible"} );
	$("#"+riferimento).show("slow");
}

function fhidediv(riferimento) {
variediv = new Array();
variediv[0]=riferimento;
	switch (riferimento) {
		case "ciclovitale": {
			variediv[1]="ricercautente";
			variediv[2]="useronline";
			break;
		}
		case "ricercautente": {
			variediv[1]="ciclovitale";
			variediv[2]="useronline";
			break;
		}
		case "useronline": {
			variediv[1]="ciclovitale";
			variediv[2]="ricercautente";
			break;
		}
	}
	$("#"+riferimento).hide();
	if($("#"+variediv[1]).is(":visible")){
$("#"+variediv[1]).animate({"bottom": (document.getElementById("menubottom").offsetHeight)+"px", "right": "1px"}, "slow");
		if($("#"+variediv[2]).is(":visible")){
$("#"+variediv[2]).animate({"bottom": (document.getElementById("menubottom").offsetHeight+160+13)+"px", "right": "1px"}, "slow");
		}
	} else if($("#"+variediv[2]).is(":visible")){
$("#"+variediv[2]).animate({"bottom": (document.getElementById("menubottom").offsetHeight)+"px", "right": "1px"}, "slow");
	}
}

/*********************
 *** GESTIONE CHAT ***
 *********************/
function fshowchat() {
	var frase = "";
	if($("#chatframe").is(":visible") ) {
		clearTimeout(timerID);
		$("#chatframe").hide();
		$("#chatuserframe").hide();
//		frase = "0x0002";
//		inviachattext(frase);
	} else {
		$("#msg").css( { disabled: true });
		timer5sec();
		$("#chatframe").css( {bottom: (document.getElementById("menubottom").offsetHeight)+"px", left: "1px", visibility: "visible"});
		$("#chatframe").show();
		$("#chatuserframe").css( {bottom: (document.getElementById("menubottom").offsetHeight)+173+"px", visibility: "visible" });
		$("#chatuserframe").show();
		$("#chattitle").html("<p><span class='txt'><font color='#5A9F00'>Benvenuto "+nickName+"</font></span></p>");
		frase = "0x0001";
		inviachattext(frase);
	}
}

function timer5sec() {
	$.get("./chatty/message.php", { all: "1", check: checksum}, function (data){
		checksum = data.substring(0,32);
		var endlistautenti = data.search(":");
		var listautenti = data.substring(32,endlistautenti);
		var restesto = data.substring(endlistautenti+1);
		charready = 1;
		if(restesto!="NTD") {
			$("#result").html(restesto);
			$("#jpId").play();
			document.getElementById("result").scrollTop = document.getElementById("result").scrollHeight;
		}
		$("#chatonline").html("<font color='#5A9F00'>Utenti in chat:</font><br>"+listautenti);
		$("#msg").css({ disabled: false });
		if($("#chatframe").is(":visible") ) {
			timerID = setTimeout("timer5sec()", 5000);
//		} else {
//			clearTimeout(timerID);
		}
	} );
}

function inviachattext(frase) {
	$.get("./chatty/message.php", { msg: frase }, function (data){
		if(charready == 1) {
			checksum = data.substring(0,32);
			var endlistautenti = data.search(":");
			var listautenti = data.substring(32,endlistautenti);
			var restesto = data.substring(endlistautenti+1);
			charready = 1;
			if(restesto!="NTD") {
				$("#result").append(restesto);
				$("#jpId").play();
				document.getElementById("result").scrollTop = document.getElementById("result").scrollHeight;
			}
			$("#chatonline").html("<font color='#5A9F00'>Utenti in chat:</font><br>"+listautenti);
/*
			checksum = data.substring(0,32);
			var restesto = data.substring(32);
			$("#result").append(restesto);
			document.getElementById("result").scrollTop = document.getElementById("result").scrollHeight;
*/
		}
	} );
}

function keypressed(e){
	if((e.keyCode=='13')||(e==13)){
		$("#msg").focus();
		if(nickName=="guest") {
			$("#chattitle").html("<p><span><font color='#5A9F00'>Benvenuto "+nickName+"</font> <font color='red' size='1'>Devi effettuare il login prima di poter scrivere in chat</font></span></p>");
		} else {
			var frase = $("#msg").val();
			inviachattext(frase);
		}
		$("#msg").val("");
	}
}


