/********************************************************
	Plugin Name: Directorio UMH
	Version: 1.1
	Plugin URI: 
	Description: Directorio UMH .(Only for WordPress 2.5+)
	Author: Servicios Inform&aacute;ticos UMH
	Author URI: http://www.umh.es/
	Text Domain: wp_directorio_umh
	Domain Path: languages
*********************************************************/

	$post_id = 5;
	$blog_origen = '';
	$my_jquery = jQuery.noConflict();
   
   	function Decodificar (cadena)
   	{
   		var salida=cadena;   		
   		salida = salida.replace(/comillas_dobles/gi,'"');
   		salida = salida.replace(/comilla_simple/gi,"'");
   		salida = salida.replace(/corchete_abre/gi,'[');
   		salida = salida.replace(/corchete_cierra/gi,"]");
   		
   		return salida;
   	}
   
 	function pintarUors(data)
 	{
 		var datos = Decodificar(data);
 		var json = eval('('+datos+')');
 		var uors = json.uors;
		var html = [];
		html.push('<ul class="uors" style="height:400px;overflow:auto;border:1px solid #CCCCCC;">');
		
		for(var i=0; i<uors.length;i++){
			html.push('<li>');
			html.push('<b>'+uors[i].nom_tpuor+'</b>');
			if(uors[i].hijos.length>0)
			{
				html.push('<ul class="hij_uors">');
				var hij_uors = uors[i].hijos;
				for(var j=0; j<hij_uors.length;j++)
				{
					html.push('<li>');
					var cambiado = hij_uors[j].nom_uor.replace('\'',"_");
					html.push('<a href="javascript:void(0);" title="' + hij_uors[j].nom_uor +'" onclick=\'seleccionUOR("'+hij_uors[j].id_uor+'","'+cambiado+'")\' >'+hij_uors[j].nom_uor+' (Personas asociadas: '+hij_uors[j].recuento+')</a>');
					html.push('</li>');
				}
				html.push('</ul>');

			}
			
			html.push('</li>');
		}
		
		html.push('</ul>');

		if(document.getElementById("UorsTree")){
			document.getElementById("UorsTree").innerHTML = html.join('');
		}
 	}

	function seleccionUOR(obj,label)
	{
		if(document.getElementById("cod"))
		{
			document.getElementById("cod").value = obj;
		}
		
		if(document.getElementById("title"))
		{
			document.getElementById("title").value = label.replace('_','\'');
		}
	}

	function enviarDirectorio()
	{
		var directorio_val  = '<div id="directory__' +document.getElementById("cod").value+ '">[directorio cod="'+ document.getElementById("cod").value + '"] </div>';
		top.send_to_editor(directorio_val);
	}
	
	function pintaEmail(obj, elemento, server)
	{
		if(document.getElementById('email'))
		{
			document.body.removeChild(document.getElementById('email'));		
		}

		var X;
		var Y;
		
		if(document.getElementById(obj))
		{
			var objeto = $my_jquery("#"+obj);
			var offset = objeto.offset();
			X = offset.left-40;
			Y = offset.top+18;
		}
		var email = document.createElement('div');
		email.id = 'email';
		email.style.cssText = "position:absolute;top:"+Y+"px;left:"+X+"px;z-index:50;border:2px solid #848484;background-color:white;";
		
		email.innerHTML = '<p style="text-align:center;margin:5px 0px;"><img src="'+server+'?id='+obj+'&blog='+elemento+'&type_blog=blog" /></p><p style="text-align:center;margin:5px 0px;"><a href="javascript:void(0);" onclick="cerrarCorreo(\''+obj+'\');">Cerrar</a>&nbsp;&nbsp;<a href="javascript:void(0);" onclick="muestraAyuda();" >Ayuda</a></p><div id="ayuda_correo" style="display:none;"></div>';
		document.body.appendChild(email);		
	}
	
	function muestraAyuda()
	{
		if(document.getElementById('ayuda_correo'))
		{
			if(document.getElementById('ayuda_correo').style.cssText.toLowerCase().indexOf('display')!=0)
			{
				document.getElementById('ayuda_correo').style.cssText='display: none;';
			}
			else{
				var objeto = $my_jquery("#email");
				var ancho = objeto.innerWidth();
				if(ancho<250)
				{
					document.getElementById('ayuda_correo').style.cssText = 'margin:10px 0px;width:250px;';
				}
				else
				{
					document.getElementById('ayuda_correo').style.cssText = 'margin:10px 0px;width:'+ancho+'px;';
				}
				document.getElementById('ayuda_correo').innerHTML = '<p style="padding:0px 5px;text-align:justify;text-indent:15px;">Para evitar un uso abusivo de las direcciones de correo de la Universidad Miguel Hern&aacute;ndez se almacenar&aacute;n las direcciones IP desde las que se solicitan las visualizaciones de dichas direcciones y se restringir&aacute;n las solicitudes masivas.</p>';			
			}
		}
	}
	
	function cerrarCorreo(obj)
	{
		if(document.getElementById('email'))
		{
			document.body.removeChild(document.getElementById('email'));		
		}
	}
	 			
	function pintarDir(data,obj,blog,server)
	{
		$post_id=obj;
		$blog_origen = blog;
		var datos = Decodificar(data);
 		var json = eval('('+datos+')');
 		//var json = data;
		var directorio = json.directorio;
		var html = [];
		var cargo_Ant='';
		
		if(json.label!='none'){
			html.push('<p style="font-size:14px;font-weight:bold;">'+json.label+'</p>');
		}

		html.push('<ul class="directorio" style="border:1px solid #CCCCCC;margin-bottom:20px;list-style-type:none;">');
								
		for(var i=0; i<directorio.length;i++){

			if(directorio[i]){
				if(directorio[i].personas.length>0){
					html.push('<li style="margin:3px 10px;list-style-type:none;">');
					html.push('<b>'+directorio[i].nom_cargo+'</b>');
					
					html.push('<br />');
					html.push('<ul class="directorio">');
					
					var personas = directorio[i].personas;
					
					for(var j = 0; j<personas.length; j++)
					{
						html.push('<li style="margin:10px 10px;">');
						html.push(personas[j].nombre);
						html.push('<br />');
						
						if(personas[j].email!='5412497282B8FDFE41F65C4B7C04468C')
						{
							html.push('<span style="margin-left:30px;">E-mail: <a id="'+personas[j].email+'__'+directorio[i].id_cargo+'" href="javascript:void(0);" onclick="pintaEmail(\''+personas[j].email+'__'+directorio[i].id_cargo+'\',\''+$blog_origen+'\',\''+server+'\');" ><img src="/wp-content/plugins/wp_directorio_umh/images/email.png" style="border:none;vertical-align:middle;" alt="Pulse para ver la direcci&oacute;n de correo" title="Pulse para ver la direcci&oacute;n de correo"/></a></span>');
							html.push('<br />'); 
						}	

						if(personas[j].extension.length>0)
						{
							html.push('<span style="margin-left:30px;">');
							if(personas[j].extension.length>1)
							{
								html.push('Extensiones: ');
							}
							else
							{
								html.push('Extensi&oacute;n: ');
							}
													
							for(var k=0;k<personas[j].extension.length;k++)
							{
								html.push(personas[j].extension[k].ext);
								html.push(' - ');
							}
													
							html.pop();
							html.push('</span>');
							html.push('<br />');
						}
			
						
						html.push('</li>');
					}
					
					html.push('</ul>');										
					html.push('</li>');
				}
			}
		}
								
		html.push('</ul>');
								
		if(document.getElementById("directorio_"+$post_id)){
			document.getElementById("directorio_"+$post_id).innerHTML = html.join('');
		}
	}

