// JoniJnm.es

function KIDE_iniciar() {
	if (!KIDE_iniciado) {
		KIDE_iniciado = 2;
		document.images['encendido'].src = KIDE_img_encendido;
		KIDE_ajax(1);
		KIDE_sesiones();
	}
}

function KIDE_apagar_encender() {
	if (document.images['encendido'].src == KIDE_img_apagado) {
		document.images['encendido'].src = KIDE_img_encendido;
		KIDE_iniciado = 2;
		if (KIDE_parado) {
			KIDE_parado = false;
			KIDE_ajax(1);
		}
		if (!KIDE_sesiones_parado) {
			KIDE_sesiones();
		}
		KIDE_save_config("iniciado", 2, 1);
	}
	else {
		document.images['encendido'].src = KIDE_img_apagado;
		KIDE_iniciado = 1;
		KIDE_save_config("iniciado", 1, 1);
	}
}

function KIDE_save_config(param, value, dias) {
	if (!dias) {
		if (window.XMLHttpRequest) { ajax = new XMLHttpRequest(); }
		else { ajax = new ActiveXObject("Microsoft.XMLHTTP"); }
		ajax.open('GET', KIDE_file_config + '?param='+param+'&value='+value+"&funcion=save", true);
		ajax.send(null);
	}
	else {
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+dias);
		document.cookie = param+"="+value+"; expires="+exdate.toGMTString()+"; path=/;";
	}
}

function KIDE_ahora() {
	var ya = new Date();
	var m = ya.getMonth() + 1;
	ya = ya.getDate()+"-"+(m < 10 ? "0" : "")+m+" "+ya.getHours()+":"+(ya.getMinutes() < 10 ? "0" : "")+ya.getMinutes()+":"+(ya.getSeconds() < 10 ? "0" : "")+ya.getSeconds();
	return ya;
}

function KIDE_mensaje(id, url) {
	document.getElementById('KIDE_mensaje_perfil').href = url;
	if (KIDE_rango == 1) {
		document.getElementById('KIDE_mensaje_borrar').href = 'javascript:KIDE_borrar('+id+')';
	}
	KIDE_mostrar("KIDE_mensaje", 1);
}

function KIDE_insertNewContent(liName,liText,liUrl,ti,color_text,rango_insert,id) {
	if (KIDE_remplazar(liText," ","") != "") {
		insertO = document.getElementById("chatoutput");
		oLi = document.createElement('div');
		oSpan = document.createElement('span');
		oURL = document.createElement('a');
		oURL.setAttribute('title',ti);
		var nombre_rango;
		if (rango_insert == 3) { 
			nombre_rango = "invitado";
		}
		else if (rango_insert == 2) {
			nombre_rango = "registrado";
		}
		else if (rango_insert == 1) {
			nombre_rango = "administrador";
		}
		else {
			nombre_rango = "especial";
		}
		
		oLi.setAttribute('id', 'KIDE_id_'+id);
		oURL.setAttribute('class', 'KIDE_'+nombre_rango);
		oURL.href = "javascript:void(0)";
		oURL.setAttribute('onclick', 'KIDE_mensaje('+id+', "'+(liUrl ? liUrl : "javascript:void(0)")+'")');
		oName = document.createTextNode(liName);
		oURL.appendChild(oName);
		oSpan.appendChild(oURL);
		oSpan.appendChild(document.createTextNode(':'));
		oLi.appendChild(oSpan);
		oLi.innerHTML += ' <span '+(color_text ? 'style="color:#'+color_text+'"' : "class='texto'")+'">'+KIDE_filter_smilies(liText)+'</span>';
		insertO.insertBefore(oLi, insertO.firstChild);
	}
}

function KIDE_remplazar(txt,r,b) {
	while (txt.indexOf(r) != -1) {
		txt = txt.replace(r,b);
	}
	return txt;
}

/*function KIDE_f_spam(txt) {
	var spam = new Array("www", "http://");
	var i=0;
	var j=spam.length;
	var msg = "No hagas spam";
	while (i<j && txt != msg) {
		if (txt.indexOf(spam[i]) != -1) { txt = msg; }
		i++;
	}
	return txt;
}*/

function KIDE_c3() {
	if (ajax3.readyState == 4 && ajax3.status == 200) {
		var txt = document.forms['chatForm']['txt'].value;
		var out = ajax3.responseText;
		out = out.split("-");
		if (out[0] != "Ok" || txt.indexOf("://") != -1 || txt.indexOf("www.") != -1) {
			document.forms['chatForm']['txt'].disabled = true;
			document.forms['chatForm']['txt'].value = KIDE_espera_por_favor;
		}
		else {
			KIDE_insertNewContent(KIDE_name,txt,KIDE_url,KIDE_ahora(),KIDE_color,KIDE_rango,out[1]); 
			document.forms['chatForm']['txt'].value = "";
		}
	}
}

function KIDE_colores_div() {
	if (ajax5.readyState == 4 && ajax5.status == 200) {
		document.getElementById('colores').innerHTML += ajax5.responseText;
	}
}

function KIDE_mostrar_colores() {
	if (!KIDE_mostrar_colores_iniciado) {
		KIDE_mostrar_colores_iniciado = true;
		KIDE_ajax('colores');
	}
	KIDE_mostrar('colores');
}

function KIDE_baneado() {
	if (ajax4.readyState == 4) {
		location.reload();
	}
}

function KIDE_muestraContenido() {
	if (ajax1.readyState == 4 && ajax1.status == 200) {
		var out = ajax1.responseText;
		out = out.split("~");
		KIDE_n = out[0];
		KIDE_recargar(out[1]);
	}
}

function KIDE_set_color(c) {
	KIDE_mostrar('colores', 2);
	KIDE_color = c;
	document.getElementById('txt').style.color = "#"+c;
	document.getElementById('colores').style.display = "none";
	KIDE_save_config("color", c, false);
	if (document.forms['chatForm']['nuevo_nick'].value) {
		KIDE_change_name(document.forms['chatForm']['nuevo_nick'].value);
	}
}

function KIDE_mostrar(id, estado) {
	if (estado) {
		if (estado == 1) {
			document.getElementById(id).style.display = "block";
		}
		else if (estado == 2) {
			document.getElementById(id).style.display = "none";
		}	
	}
	else if (document.getElementById(id).style.display == "block") {
		document.getElementById(id).style.display = "none";
	}
	else {
		document.getElementById(id).style.display = "block";
	}
}

function KIDE_change_name(n) {
	if (n) {
		KIDE_mostrar("colores", 2);
		KIDE_name = n;
		document.getElementById('my_name').innerHTML = n;
		KIDE_save_config("name", n, false);
	}
}

function KIDE_inserta() {
	if (ajax2.readyState == 4 && ajax2.status == 200) {
		var out = ajax2.responseText;
		out = out.split("~");
		if (out[0]) {
			KIDE_insertNewContent(out[0],out[1],out[2],out[3],out[4],out[5],out[6]);
		}
		if (KIDE_f < KIDE_n) {
			KIDE_f++;
			KIDE_ajax(2);
		}
		else {
			if (KIDE_iniciado == 2) {
				setTimeout('KIDE_ajax(1)',KIDE_refresh_time);
			}
			else {
				KIDE_parado = true;
			}
		}
	}
}

function KIDE_recargar(t) {
	if (KIDE_f < KIDE_n) {
		KIDE_f++;
		KIDE_ajax(2);
	}
	else { 
		if (document.forms['chatForm']['txt'].value == KIDE_espera_por_favor) { 
			document.forms['chatForm']['txt'].value = ""; 
			document.forms['chatForm']['txt'].disabled = false;
			document.chatForm.txt.focus();
		}
		if (KIDE_iniciado == 2) {
			setTimeout('KIDE_ajax(1)',KIDE_refresh_time);
		}
		else {
			KIDE_parado = true;
		}
	}
	if (t > 0) {
		KIDE_tiempo(t); 
	}
}

function KIDE_sm() {
	var txt = document.forms['chatForm']['txt'].value;
	//txt = f_spam(txt);
	if (txt.replace(" ","") != "" && txt) {
		if (txt.indexOf("://") != -1 || txt.indexOf("www.") != -1) {
			KIDE_ajax(4);
		}
		else {
			KIDE_ajax(3);
		}
	}
	if (KIDE_rango == 3) {
		KIDE_anti_flood_spam(txt);
	}
	if (KIDE_iniciado == 1) {
		document.images['encendido'].src = KIDE_img_encendido_1;
		KIDE_iniciado = 2;
		KIDE_parado = false;
		KIDE_ajax(1);
	}
}

function KIDE_anti_flood_spam(txt) {
	if (KIDE_ban[0] != 5) {
		KIDE_ban[0]++;
		var time = new Date();
		time = time.getTime();
		KIDE_ban[KIDE_ban[1]] = time;
		KIDE_ban[1]++;
	}
	else {
		var i;
		for (i=2;i<6;i++) { //EL 6 NO!!
			KIDE_ban[i] = KIDE_ban[i+1];
		}
		var time = new Date();
		time = time.getTime();
		KIDE_ban[6] = time;
		var aux = KIDE_ban[6] - KIDE_ban[2];
		if (aux < 10*1000) {
			document.forms['chatForm']['txt'].value = "";
			document.forms['chatForm']['txt'].disabled = true;
			KIDE_ajax("baneado");
		}
	}
}

function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
} 

function KIDE_borrar(id) {
	if (id > 0) {
		document.getElementById("KIDE_id_"+id).style.display = "none";
		KIDE_mostrar("KIDE_mensaje", 2)
		KIDE_ajax("borrar", id);
	}
	else {
		alert(KIDE_mensaje_borrar);
	}
}

function KIDE_sesiones() {
	if (KIDE_cargar_sesiones && KIDE_iniciado == 2) {
		KIDE_ajax("sesiones");
		setTimeout('KIDE_sesiones()', KIDE_refresh_time_sesion);
	}
	else {
		KIDE_sesiones_parado = true;
	}
}

function KIDE_ajax_sesiones() {
	if (ajax7.readyState == 4 && ajax7.status == 200) {
		document.getElementById('KIDE_usuarios').innerHTML = ajax7.responseText;
	}
}

function KIDE_ajax(tipo, tmp) {
	if (tipo == 1) { 
		ajax1 = nuevoAjax();
		ajax1.onreadystatechange = KIDE_muestraContenido;
		ajax1.open('POST',  KIDE_ajax_url+"&task=reload", true);
		ajax1.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax1.send("reload="+KIDE_f);
	}
	else if (tipo == 2) { 
		ajax2 = nuevoAjax();
		ajax2.onreadystatechange = KIDE_inserta;
		ajax2.open('POST',  KIDE_ajax_url+"&task=get_msg", true);
		ajax2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax2.send("sesion="+KIDE_sesion+"&get="+KIDE_f);
	}
	else if (tipo == 3 || tipo == 4) {
		var send = (tipo == 3) ? "sesion="+KIDE_sesion+"&" : "";
		ajax3 = nuevoAjax();
		ajax3.onreadystatechange = KIDE_c3;
		ajax3.open('POST', KIDE_ajax_url+"&task=insertar", true);
		ajax3.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax3.send(send+"name="+encodeURIComponent(KIDE_name)+"&txt="+encodeURIComponent(KIDE_remplazar(document.forms['chatForm']['txt'].value,"~",""))+"&url="+encodeURIComponent(KIDE_url)+"&rango="+KIDE_rango+"&color="+KIDE_color);
	}
	else if (tipo == "baneado") {
		ajax4 = nuevoAjax();
		ajax4.onreadystatechange = KIDE_baneado;
		ajax4.open('POST', KIDE_ajax_url+"&task=insertar", true);
		ajax4.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax4.send("name=JoniJnm&txt="+KIDE_user_baneado.replace("%name", encodeURIComponent(KIDE_name))+"&url=http://www.jonijnm.es/web/jomsocial/JoniJnm/profile.html&rango=0");
	}
	else if (tipo == "colores") {
		ajax5 = nuevoAjax();
		ajax5.onreadystatechange = KIDE_colores_div;
		ajax5.open('POST', KIDE_ajax_url+"&task=colores", true);
		ajax5.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax5.send(null);
	}
	else if (tipo == "borrar") {
		ajax6 = nuevoAjax();
		ajax6.open('POST', KIDE_ajax_url+"&task=borrar", true);
		ajax6.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax6.send("id="+tmp);
	}
	else if (tipo == "sesiones") {
		ajax7 = nuevoAjax();
		ajax7.onreadystatechange = KIDE_ajax_sesiones;
		ajax7.open('POST', KIDE_ajax_url+"&task=sesiones", true);
		ajax7.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax7.send("sesion="+KIDE_sesion);
	}
}

function KIDE_pressedEnter(event,tipo,value) {
	var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (theCode == 13) {
		if (!tipo) {
			KIDE_sm();
		}
		else if (tipo == 1) {
			KIDE_change_name(value);
		}
		return false;
	} 
	else { 
		return true;
	}
}

function KIDE_tiempo(t) {
	var time = new Date();
	time = time.getTime();
	t = Math.floor((time/1000) - t);
	var mes = Math.floor(t/60/60/24/30);
	var dia = Math.floor((t - mes*60*60*24*30)/60/60/24); 
	var hora = Math.floor((t - mes*60*60*24*30 - dia*60*60*24)/60/60);
	var minuto = Math.floor((t - mes*60*60*24*30 - dia*60*60*24 - hora*60*60)/60);
	var segundo = Math.floor(t - mes*60*60*24*30 - dia*60*60*24 - hora*60*60 - minuto*60);
	var out = "";

	if (mes) {
		out += mes+" ";
		if (mes != 1) { out += KIDE_lang[1]; }
		else { out += KIDE_lang[0]; }
		if (mes < 4 && dia) {
			out += " "+dia+" ";
			if (dia != 1) { out += KIDE_lang[3]; }
			else { out += KIDE_lang[2]; }
		}
	}
	else if (dia) { 
		out += dia+" "; 
		if (dia != 1) { out += KIDE_lang[3]; }
		else { out += KIDE_lang[2]; }
		if (dia < 3 && hora) {
			out += " "+hora+" ";
			if (hora != 1) { out += KIDE_lang[5]; }
			else { out += KIDE_lang[4]; }
		}
	}
	else if (hora) {
		out += hora+" ";
		if (hora != 1) { out += KIDE_lang[5]; }
		else { out += KIDE_lang[4]; }
		if (hora < 6 && minuto) { 
			out += " "+minuto+" ";
			if (minuto != 1) { out += KIDE_lang[7]; }
			else { out += KIDE_lang[6]; }
		}
	}
	else if (minuto) {
		out += minuto+" ";
		if (minuto != 1) { out += KIDE_lang[7]; }
		else { out += KIDE_lang[6]; }
		if (minuto < 10 && segundo) {
			out += " "+segundo+" ";
			if (segundo != 1) { out += KIDE_lang[9]; }
			else { out += KIDE_lang[8]; }
		}
	}
	else { 
		out += segundo+" "; 
		if (segundo != 1) { out += KIDE_lang[9]; }
		else { out += KIDE_lang[8]; }
	}

	document.getElementById('tiempo').innerHTML=out; 

}

function KIDE_insertSmile(text) {
	var textarea = document.forms['chatForm']['txt'];
	textarea.value += " "+text;
	textarea.focus(textarea.value.length - 1);
}
		
function KIDE_filter_smilies(s) {
	for (var i = 0; i < KIDE_smilies.length; i++) {
		s = KIDE_remplazar(s, KIDE_smilies[i][0], '<img src="' + KIDE_smilies[i][1] + '" class="wp-smiley" />');
	}
	return s;
}