// Detecci�n de la versi�n de Internet Explorer
var ieVer=/*@cc_on function(){ switch(@_jscript_version){ case 5.0:return 5; case 5.1:return 5; case 5.5:return 5.5; case 5.6:return 6; case 5.7:return 7; case 5.8:return 8; }}()||@*/0;

// Detecci�n de la versi�n de Gecko (Firefox, Seamonkey, etc)
if (/rv[\:\s](\d+\.\d+)/.test(navigator.userAgent)){//if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
	ffversion=new Number(RegExp.$1);
	if (ffversion>=1.9) {var ffVer = 3;}
	else if (ffversion>=1.8) {var ffVer = 2;}
	else if (ffversion>=1.7) {var ffVer = 1;}
} else {var ffVer = 0;}
var idCal = 'bio_calendar'; // Id del calendario
var quitaCalendario = 'no' // Quitar calendario en IE 6. Opciones: 'si' o 'no'
///////////////////////////////////

document.observe("dom:loaded", combo);

// si se carga una capa de opciones por AJAX despu�s de que haya cargado el DOM, basta con llamar otra vez a la funcion combo();


function combo () {

	lista = $$('div.combo_desplegable div.lista:not(.tr)'); // Listado de opciones
	altura = document.body.scrollHeight;

	//quitaEspacios();

	if (ieVer==0 || ieVer>7) {
		Element.insert($$('body')[0], {bottom: '<div onclick="cierraCombo();" id="comboTranslucida" style="background-color:#000; display:none; filter:alpha(opacity=40); height:'+altura+'px; left:0; opacity:.4; position:absolute; top:0; width:100%;"></div>'});
		$('contenedor').style.zIndex = '0';
	} else {
		Element.insert($$('body')[0], {top: '<div onclick="cierraCombo();" id="comboTranslucida" style="background-color:#000; display:none; filter:alpha(opacity=40); height:'+altura+'px; left:0; opacity:.4; position:absolute; top:0; width:100%;"></div>'});
	}


	for (i=0;i<lista.length;i++) {

		Element.addClassName(lista[i],'tr');
		lista[i].up().setStyle({width: '1px', height: '1px'});
		lista[i].style.display = 'none'; // se oculta listado de opciones

		etiqueta = lista[i].up().previous(); // seleccion de la etiqueta
		anchoEtiqueta = etiqueta.getWidth(); // anchura de la 'etiqueta'
		if (ieVer==6 || ieVer==7) {anchoEtiqueta = etiqueta.next().getWidth();}
		textoEtiqueta = etiqueta.firstChild.nodeValue.split(':')[0].toLowerCase(); // se recoge el texto de la etiqueta, quitando may�sculas y los dos puntos del final

		//lista[i].style.left = '-'+(anchoEtiqueta/10)+'em'; // El P de etiqueta tiene que llevar un ancho fijo, o flotando a la izquierda. QUE NO OCUPE todo el ancho.


		Element.insert(etiqueta, {after: '<p class="interaccion"><span class="seleccionada"></span><a href="#" rel="combo">Seleccionar '+textoEtiqueta+'</a></p>'}); // se inserta el enlace para abrir el combo
		Element.insert(lista[i], {top: '<p class="cerrar"><a href="#" class="cerrar" onclick="cierraCombo();return false;"><img src="/images/ico_cerrar_modal.gif" alt="Cerrar" /></a></p>'}); // inserci�n enlace cerrar
		Element.insert(lista[i], {top: '<p class="titulo"><strong>Seleccionar '+textoEtiqueta+'</strong></p>'}); // inserci�n cabecera combo
		//Element.insert($$('body')[0], {bottom: '<div class="capaCombo" style="display:none;></div>'}); //
	}
	combo_opcionSeleccionada();
	abreCombo();
	hoverCombo();
}

function abreCombo () {
	enlace = $$("a[rel='combo']"); // Enlace para abrir el combo
	lista = $$('div.combo_desplegable div.lista.tr');
	for (i=0;i<lista.length;i++) {
		enlace[i].onclick = function () {
			lista = this.up().next();
			$$('html')[0].style.overflow = 'hidden';
			quitaElementosCombo();
			$('comboTranslucida').style.display = 'block';
			lista.down().style.display = 'block';
			lista.down().style.zIndex = '9999';
			lista.down().id = 'comboActivo';


			if (ieVer==6 || ieVer==7) {
				alturaTopIe = lista.previousSibling.getHeight();
				//lista.down().style.top = '-'+((alturaTopIe+6)/10)+'em';
			}

			return false;
		}
	}

}

function hoverCombo () {

	imgCerrar = $$('div.combo_desplegable p.cerrar img');
	for (i=0;i<imgCerrar.length;i++) {
		imgCerrar[i].onmouseover = function () {this.src='/images/ico_cerrar_modal_hover.gif';}
		imgCerrar[i].onmouseout = function () {this.src='/images/ico_cerrar_modal.gif';}
	}


	opcion = $$('div.combo_desplegable div.lista li span');
	for (i=0;i<opcion.length;i++) {
		opcion[i].onmouseover = function () {Element.addClassName(this,'hover');}
		opcion[i].onmouseout = function () {Element.removeClassName(this,'hover');}
		opcion[i].onclick = function () {
			this.previousSibling.checked = true;
			textoSeleccionado = this.firstChild.nodeValue;
			parrafo = $('comboActivo').up().previous().down('span');
			enlaceCambio = parrafo.next();
			textoEnlaceCambio = enlaceCambio.firstChild.nodeValue;
			textoEnlaceCambio = textoEnlaceCambio.replace(/Seleccionar/, "Cambiar");
			Element.remove(enlaceCambio.firstChild);
			Element.insert(enlaceCambio, {top: textoEnlaceCambio});
			if (!Element.empty(parrafo)) {Element.remove(parrafo.down());}
			Element.insert(parrafo, {top: '<strong>'+textoSeleccionado+'</strong>'});
			cierraCombo();
		}
	}
}

function combo_opcionSeleccionada () {

	opSel = $$('div.combo_desplegable li.seleccionado');
	for (i=0;i<opSel.length;i++) {
		span = opSel[i].down('label').down('span');
		span.previousSibling.checked = true;
		textoSeleccionado = span.firstChild.nodeValue;
		parrafo = opSel[i].up().up().up().previous().down('span');
		enlaceCambio = parrafo.next();
		textoEnlaceCambio = enlaceCambio.firstChild.nodeValue;
		textoEnlaceCambio = textoEnlaceCambio.replace(/Seleccionar/, "Cambiar");
		Element.remove(enlaceCambio.firstChild);
		Element.insert(enlaceCambio, {top: textoEnlaceCambio});
		if (!Element.empty(parrafo)) {Element.remove(parrafo.down());}
		Element.insert(parrafo, {top: '<strong>'+textoSeleccionado+'</strong>'});
	}
}

function quitaEspacios () {
	texto = $$('div.combo_desplegable ul label');
	for (i=0;i<texto.length;i++) {
		texto[i].cleanWhitespace();
	}
}

function cierraCombo () {
	if ($('comboActivo')) {
        lista = $('comboActivo');
        lista.style.display = 'none';
        $('comboTranslucida').style.display = 'none';
        $$('html')[0].style.overflow = 'auto';
        $$('html')[0].style.overflowX = 'hidden';
        calculaNumAct();
        ponElementosCombo();
        lista.id = '';

        var listaOpt = document.getElementsByTagName("input");
        var centroActividad = -1;
        for (var t = 0 ; t < listaOpt.length ; t++ ){
            if ( listaOpt[t].type == "radio" && listaOpt[t].checked == true ){
                centroActividad = listaOpt[t].value;
            }
        }
        document.forms["formEnter"].centro.value = centroActividad;

        return false;
	}

}

function quitaElementosCombo () {
	capaCalendario = $(idCal);
	if (!capaCalendario && ieVer==6) {alternaSelectCombo('hidden');}
	if (capaCalendario && ieVer>0 && ieVer <7 && quitaCalendario == 'si') {alternaCalendarioCombo('none');}
	if (ieVer==6 || ieVer==7) {elementosIe('static','none','static');}

}
function ponElementosCombo () {
	capaCalendario = $(idCal);
	if (!capaCalendario) { if (ieVer==6) {alternaSelectCombo('');}}
	if (capaCalendario && ieVer>0 && ieVer <7 && quitaCalendario == 'si') {alternaCalendarioCombo('block');}
	if (ieVer==6 || ieVer==7) {elementosIe('','','relative');}
	return false;
}

function elementosIe (p,d,s) {
	elemento1 = $$('.contenido-actividades *');
	for (i=0;i<elemento1.length;i++) {
		elemento1[i].style.position = p;
	}
	elemento2 = $$('p.imagen');
	for (i=0;i<elemento2.length;i++) {
		elemento2[i].style.display = d;
	}
	elemento3 = $$('div.buscar');
	for (i=0;i<elemento3.length;i++) {
		elemento3[i].style.position = p;
	}
	elemento4 = $$('div.busquedas');
	for (i=0;i<elemento4.length;i++) {
		elemento4[i].style.position = p;
	}
	elemento5 = $$('p.fecha');
	for (i=0;i<elemento5.length;i++) {
		elemento5[i].style.position = s;
	}
	$('migas').style.display = d;
	$$('#cabecera h2')[0].style.display = d;
	$$('div.cabecera-superior')[0].style.display = d;
	$('cabecera').style.position = p;

}

function alternaCalendario (n) {alert(capaCalendario);
	capaCalendario = $(idCal);
	capaCalendario.style.display = n;
}

function alternaSelectCombo (n) {
	$$('select').each(function(node){
		node.style.visibility = n;
	});
}