/**
* @Copyright Copyright (C) 2011 - JoniJnm.es
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

kide.mensaje = function(name, id, url, ti, sesion, rango, img) {
	this.html('KIDE_mensaje_username', name);
	this.$('KIDE_mensaje_username').className = "KIDE_"+kide.rangos[rango];
	this.html('KIDE_tiempo_msg', ti);
	this.$('KIDE_mensaje_img').src = img;
	if (this.site == "com") {
		if (sesion != this.sesion && this.in_array(sesion, this.sids)) {
			this.show("KIDE_mensaje_privado_span", true);
			this.$('KIDE_mensaje_privado').href = 'javascript:kide.privado_nuevo("'+sesion+'", "'+name+'", '+rango+', "'+img+'")';
		}
		else 
			this.show("KIDE_mensaje_privado_span", false);
	}
	if (url) {
		this.$('KIDE_mensaje_perfil').href = url;
		this.show("KIDE_mensaje_perfil_span", true);
		this.$('KIDE_mensaje_img_enlace').href = url;
		this.$('KIDE_mensaje_img_enlace').target = '_blank';
		this.css('KIDE_mensaje_img', 'cursos', 'pointer');
	}
	else {
		this.show("KIDE_mensaje_perfil_span", false);
		this.$('KIDE_mensaje_img_enlace').href = 'javascript:void(0)';
		this.$('KIDE_mensaje_img_enlace').target = '';
		this.css('KIDE_mensaje_img', 'cursor', 'default');
	}
	if (this.rango == 1 || sesion == this.sesion) {
		this.show('KIDE_mensaje_borrar_span', true);
		this.$('KIDE_mensaje_borrar').href = 'javascript:kide.borrar('+id+')';
	}
	else
		this.show('KIDE_mensaje_borrar_span', false);
	if (this.rango == 1) {
		this.show('KIDE_mensaje_banear_span', false);
		if (sesion != '0' && rango != 1) {
			this.show('KIDE_mensaje_banear_span1', true);
			this.$('KIDE_mensaje_banear').onclick = function() { kide.banear(sesion, 'mensaje'); }
		}
		else
			this.show('KIDE_mensaje_banear_span1', false);
	}
	this.show("KIDE_mensaje", true);
};
kide.mostrar_usuario = function(name, rango, sesion, url, img) {
	this.html('KIDE_usuario_name', name);
	this.$('KIDE_usuario_name').className = "KIDE_"+this.rangos[rango];
	this.$('KIDE_usuario_img').src = img;
	if (this.site == "com") {
		if (sesion != this.sesion) {
			this.show("KIDE_usuario_privado_span", true);
			this.$('KIDE_usuario_privado').href = 'javascript:kide.privado_nuevo("'+sesion+'", "'+name+'", '+rango+', "'+img+'")';
		}
		else 
			this.show("KIDE_usuario_privado_span", false);
	}
	if (url) {
		this.$('KIDE_usuario_perfil').href = url;
		this.show("KIDE_usuario_perfil_mostrar", true);
		this.$('KIDE_usuario_img_enlace').href = url;
		this.$('KIDE_usuario_img_enlace').target = '_blank';
		this.css('KIDE_usuario_img', 'cursor', 'pointer');
	}
	else {
		this.show("KIDE_usuario_perfil_mostrar", false);
		this.$('KIDE_usuario_img_enlace').href = 'javascript:void(0)';
		this.$('KIDE_usuario_img_enlace').target = '';
		this.css('KIDE_mensaje_img', 'cursor', 'default');
	}
	if (this.rango == 1) {
		this.show('KIDE_usuario_banear_span', false);
		if (sesion != '0' && rango != 1) {
			this.show('KIDE_usuario_banear_span1', true);
			this.$('KIDE_usuario_banear').onclick = function() { kide.banear(sesion, 'usuario'); } 
		}
		else
			this.show('KIDE_usuario_banear_span1', false);
	}
	this.show("KIDE_usuario", true);
};
kide.insertNewContent = function(name,text,url,ti,color,rango,id,sesion,yo,hora,img) {
	if (text.replace(/ /g, "") != "") {
		var c = color.length>0 ? 'color="#'+color+'"' : 'class="KIDE_dc_'+this.rangos[rango]+'"';
		var div = this.$('KIDE_msgs');
		var nodo = document.createElement('div');
		var insertO = this.$("KIDE_output");
		var s_hora;
		nodo.setAttribute('id', 'KIDE_id_'+id);
		if (this.show_hour == 'both' || this.site == this.show_hour)
			s_hora = '<span class="KIDE_msg_hour">'+hora+'</span> ';
		else
			s_hora = '';
		nodo.innerHTML = s_hora+'<span style="cursor: pointer" title="'+ti+'" class="KIDE_'+this.rangos[rango]+'" onclick="kide.mensaje(\''+name+'\', '+id+', \''+url+'\', \''+ti+'\', \''+sesion+'\', '+rango+', \''+img+'\')">'+name+'</span>: <span '+c+'>'+this.filter_smilies(text)+'</span>';

		if (this.order == 'bottom') {
			this.insertAfter(nodo, insertO.lastChild);
			div.scrollTop = div.scrollHeight;
		}
		else
			insertO.insertBefore(nodo, insertO.firstChild);
		if (!yo && this.sound == 1) 
			this.play_msg_sound();
	}
};
kide.privado_insertar = function(sesion, name, text, color, rango, yo) {
	if (text.replace(/ /g, "") != "") {
		var alias = this.privado_getAlias(sesion);
		var insertO = this.$("privado_full_"+alias);
		var oDiv = document.createElement('div');
		var c = color.length>0 ? 'color="#'+color+'"' : 'class="KIDE_dc_'+this.rangos[rango]+'"';
		oDiv.innerHTML = '<span class="KIDE_'+this.rangos[rango]+'">'+name+'</span>: <span '+c+'>'+this.filter_smilies(text)+'</span>';
		if (this.order == 'bottom') {
			this.insertAfter(oDiv, insertO.lastChild);
			insertO.scrollTop = insertO.scrollHeight;
		}
		else
			insertO.insertBefore(oDiv, insertO.firstChild);
		if (!yo) {
			if (this.privado_abierto != alias)
				this.css('KIDE_privados_avatar_'+alias, 'border', '2px solid red');
			if (this.sound == 1) 
				this.play_msg_sound();
			if (this.privado_cerrado(alias))
				this.privado_abrir_cerrar(alias);
		}
	}
};
kide.privado_minimizar = function(alias) {
	if (this.privado_abierto == alias) {
		this.show("privado_full_"+alias, false);
		this.privado_abierto = -1;
		this.show("privados_full_div", false);
	}
	else {
		if (this.privado_abierto != -1) 
			this.show("privado_full_"+this.privado_abierto, false);
		this.$("privados_txt").disabled = this.privados[alias].stoped;
		var nombre = this.privados[alias].name;
		this.show("privado_full_"+alias, true);
		this.show("privados_full_div", true);
		this.css('KIDE_privados_avatar_'+alias, 'border', '1px solid black');
		this.$("privados_full_minimizar").href = "javascript:kide.privado_minimizar("+alias+")";
		this.$("privados_full_cerrar").href = "javascript:kide.privado_abrir_cerrar("+alias+")";
		this.html("privados_full_title", nombre);
		this.$('privados_txt').focus();
		if (this.order == 'bottom')
			this.$("privado_full_"+alias).scrollTop = this.$("privado_full_"+alias).scrollHeight;
		this.privado_abierto = alias;
	}
};
kide.privado_crear = function(sesion, name, rango, img) {
	var alias = this.privados.length;
	this.privados[alias] = {
		sid: sesion,
		opened: true,
		stoped: false,
		name: name
	};
	var insertO = this.$("privado_full_x");
	var struct = document.createElement('div');
	struct.setAttribute('id', 'privado_full_'+alias);
	struct.setAttribute('class', 'privado_full');
	struct.innerHTML = "<span></span>";
	insertO.insertBefore(struct, insertO.firstChild);
	
	insertO = this.$("privados_mini");
	struct = document.createElement('div');
	struct.setAttribute('id', 'privados_mini_'+alias);
	struct.setAttribute('class', 'privados_mini');
	struct.innerHTML = '<span id="privados_mini_name_'+alias+'" style="cursor:pointer" class="KIDE_'+this.rangos[rango]+'" onclick="kide.privado_minimizar('+alias+')">'+name+'</span><br /><img alt="" onclick="kide.privado_minimizar('+alias+')" style="cursor:pointer" id="KIDE_privados_avatar_'+alias+'" class="KIDE_avatar" src="'+img+'" style="border:1px solid black" />';
	insertO.insertBefore(struct, insertO.firstChild);
	
	this.show("privados_mini_div", true);
};
kide.insert_sesion = function(row) {
	var div = document.createElement('div');
	div.setAttribute('style', 'cursor:pointer');
	div.setAttribute('class', row.getAttribute('class'));
	div.onclick = function() { kide.mostrar_usuario(row.getAttribute('name'), row.getAttribute('rango'), row.getAttribute('sesion'), row.getAttribute('profile'), row.getAttribute('img')) };
	div.innerHTML = row.getAttribute('name');
	this.$('KIDE_usuarios').insertBefore(div, this.$('KIDE_usuarios').firstChild);
}
kide.privado_update_name_opened = function (name) {
	this.html("privados_full_title", name);
}
kide.privado_update_name_mini = function (alias, name) {
	this.html("privados_mini_name_"+alias, name);
}
