//<![CDATA[

/**
 * Activa los pop-ups de los enlaces con rel="external", y centra los pop-ups en pantalla.  
 * @author usaelraton.com
 * @param {int} ancho
 * @param {int} alto
 */
function activarPopups(ancho, alto)
{
	if (ancho > screen.width) { ancho = screen.width;}
	if (alto > screen.height) { alto = screen.height;}
	var x = (screen.width - ancho) / 2;
	var y = ((screen.height - alto) / 2) - 30;
	$("a[rel=external],div#textos a").click(function(e) // Activamos el popup al hacer clic.
	{
		window.open(this.href, "Externo", "width=" + ancho + ",height=" + alto + ",left=" + x + ",top=" + y + ",resizable=yes,scrollbars=yes");
		return false;
	});
}

/**
 * Pone el foco en el primer input, textarea o select del documento
 * @author usaelraton.com
 */
function ponerFocoEnPrimerCampo()
{
    var etiquetas = document.getElementsByTagName("label");
    var campos;
    var tipoDeCampo;
    var focoPuesto = false;
    var i = 0;

    if (etiquetas.length > 0) // hay etiquetas en el documento
    {
        campos = etiquetas[0].childNodes; // obtenemos los hijos de la primera etiqueta
        /* Recorremos los hijos de la etiqueta hasta encontrar un input, y le asignamos el foco */
        while ((i < campos.length) && (focoPuesto == false))
        {
            tipoDeCampo = campos[i].nodeName.toLowerCase();
            if ((tipoDeCampo == "input") || (tipoDeCampo == "textarea") || (tipoDeCampo == "select"))
            {
                campos[i].focus();
                focoPuesto = true;
            }
            i++;
        }
    }
}

/**
 * Busca las imágenes insertadas en Administra y les activa el thickbox
 * @author usaelraton.com
 */
function activarImagenes()
{
	$("img.galeria").each(function()
	{
		$(this).wrap("<a class='thickbox' href='" + $(this).attr("src").replace("miniatura", "ampliacion") + "'></a>");
	});
	
	$(".thickbox").not("[href*=inline]").removeAttr("rel");
	$(".thickbox").not("[href*=inline]").attr("rel", "galeria");
}

/**
 * Busca los vídeos pre-insertados en Administra y los reemplaza por el vídeo original
 * @author usaelraton.com
 */
function activarVideos()
{
	$("img.video").each(function()
	{
		$(this).wrap('<div style="text-align:center;"></div>');
		
		var ancho = $(this).attr("width");   
		var alto = $(this).attr("height");
		var ruta = decodeURI($(this).attr("title"));
		
		$(this).replaceWith(
			"<object type='application/x-shockwave-flash' data='" + ruta + "' width='" + ancho + "' height='" + alto + "'>" +
			"<param name='movie' value='" + ruta + "' />" +
			"</object>"
		);
	});
	
	$("object").wrap("<div style='text-align:center;'></div>");
}

$(document).ready(
	function(e)
	{
		//ponerFocoEnPrimerCampo();
		activarPopups(800,600);
		activarImagenes();
		activarVideos();
		$(".fecha").append(' <acronym title="Central European Time (Hora de Europa Central)">C.E.T.</acronym>');
		$("a[rel=external]").each(function() { if ($(this).children("img").length > 0) { $(this).removeAttr("rel"); } });
	}
);

//]]>
