document.observe("dom:loaded", initCentros);
document.observe("dom:loaded", initAgenda);
document.observe("dom:loaded",initEnviarAmigo)

function onChangetipoCentros() {
	if (this.options[this.selectedIndex]) {		
		var parametros = 'idTipoCentro='+this.options[this.selectedIndex].value;
		new Ajax.Request("/ObraSocial/centros/ajax/os_provincias_tipo_centro",{
				method:'post',
				postBody: parametros,
				onSuccess:setProvincias
				});
	}
	
}

function setProvincias(transport) {
	var selectProvincias = $('fb_centros_provincia');
	
	if (transport.status == 200) {
		var xmlDoc = transport.responseXML;
		var provincias = xmlDoc.getElementsByTagName("provincia");
		if (selectProvincias) {			
			selectProvincias.options.length = 0;
			selectProvincias.options[0] = new Option("Todas",0);
			for (var i=0;i<provincias.length;i++) {				
				selectProvincias.options[i+1] = new Option(provincias[i].firstChild.firstChild.nodeValue,provincias[i].getAttribute("id"));
			}
			ocultarSelectLocalidades();
		}
	}
}


function onChangeProvincias() {
	var selectTipoCentros = $("fb_centros_tipocentro");
	if (this.options[this.selectedIndex] && this.selectedIndex != 0) {		
		var parametros = 'idProvincia='+this.options[this.selectedIndex].value + '&idTipoCentro='+selectTipoCentros.options[selectTipoCentros.selectedIndex].value;
		new Ajax.Request("/ObraSocial/centros/ajax/os_localidades_provincia",{
				method:'post',
				postBody: parametros,
				onSuccess:setLocalidades
				});
	} else {
		ocultarSelectLocalidades();
	}
}

function setLocalidades (transport) {
	var selectLocalidades = $('fb_centros_localidad');
	var selectProvincias = $('fb_centros_provincia');
	
	if (transport.status == 200) {
		var xmlDoc = transport.responseXML;
		var localidades = xmlDoc.getElementsByTagName("localidad");
		if (selectLocalidades) {			
			selectLocalidades.options.length = 0;
			selectLocalidades.options[0] = new Option("Todas",0);
			if (selectProvincias.selectedIndex != 0){
				for (var i=0;i<localidades.length;i++) {				
					selectLocalidades.options[i+1] = new Option(localidades[i].firstChild.firstChild.nodeValue,localidades[i].getAttribute("id"));
				}
				mostrarSelectLocalidades();
			} else {
				ocultarSelectLocalidades();
			}
			
			
		}
	}
}

function mostrarSelectLocalidades() {
	if ($('fb_localidad_p')) {
		$('fb_localidad_p').className="clearfix";
	}
}

function ocultarSelectLocalidades() {
	if ($('fb_localidad_p')) {
		$('fb_localidad_p').className="clearfix oculto";
	}
}

function initCentros() {	
	var selectTipoCentros = $("fb_centros_tipocentro");
	var selectProvincias = $('fb_centros_provincia');
	var capas = $$('#api div.capa');
	var enlaces = $$('#api ul li a');
	
	
	if (selectTipoCentros) {
		selectTipoCentros.observe("change",onChangetipoCentros);				
	}
	
	if (selectProvincias) {
		selectProvincias.observe("change",onChangeProvincias);				
	}
	
	for (var i=0; i<enlaces.length;i++) {
		enlaces[i].id = 'apiEnlace-'+i;
		Event.observe(enlaces[i],'click', function () {
			var indice = (this.id.split("-"))[1];
			var enlacesLi = $$('#api ul li');
			var capas = $$('#api div.capa');
			
			if (enlacesLi.length >= 2 && indice < 2) {
				for (var i=0; i<2;i++) {
					if (i==indice) {
						enlacesLi[i].addClassName("activo");
						capas[i].addClassName("oculto");
					} else {
						enlacesLi[i].removeClassName("activo");
						capas[i].removeClassName("oculto");
					}					
				}	
			}									
		});		
	}
	
	if (enlaces.length >= 2 && capas.length > 1) {
		enlaces[1].parentNode.className="activo ultimo";
		enlaces[0].parentNode.className="";
		capas[0].className= "capa";
		capas[1].className= "capa oculto";
	} else if (enlaces.length < 2 && capas.length > 0) {
		capas[0].className= "capa";
	}
	
}


function initAgenda() {	
	var selectTipoCentros = $("fb_agenda_tipocentro");
	var selectProvincias = $('fb_agenda_provincia');
	
	if (selectTipoCentros) {
		selectTipoCentros.observe("change",onChangeAgendaTipoCentros);				
	}
	
	if (selectProvincias) {
		selectProvincias.observe("change",onChangeAgendaProvincias);				
	}
}


function initEnviarAmigo () {
	
	if ($('enviarAmigo')) {
		enlace = $('enviarAmigo');
		formulario0 = enlace.parentNode.parentNode.getElementsByTagName('form')[0];
		formulario0.id = 'fEnvioAmigo';
		formulario = $('fEnvioAmigo');
		formulario.style.display = 'none';
		
		enlace.onclick = function () {
			
			if (formulario.style.display == 'block') {
				formulario.style.display = 'none';
			}
			else if (formulario.style.display == 'none') {
				formulario.style.display = 'block';
			}
			
			return false;
			
		}
	}
	
}

function onChangeAgendaTipoCentros() {
	if (this.options[this.selectedIndex]) {		
		var parametros = 'idTipoCentro='+this.options[this.selectedIndex].value;
		new Ajax.Request("/ObraSocial/centros/ajax/os_provincias_eventos_tipo_centro",{
				method:'post',
				postBody: parametros,
				onSuccess:setAgendaProvincias
				});
	}
	
}

function setAgendaProvincias(transport) {
	var selectProvincias = $('fb_agenda_provincia');
	
	if (transport.status == 200) {
		var xmlDoc = transport.responseXML;
		var provincias = xmlDoc.getElementsByTagName("provincia");
		if (selectProvincias) {			
			selectProvincias.options.length = 0;
			selectProvincias.options[0] = new Option("Todas",0);
			for (var i=0;i<provincias.length;i++) {				
				selectProvincias.options[i+1] = new Option(provincias[i].firstChild.firstChild.nodeValue,provincias[i].getAttribute("id"));
			}
			ocultarSelectLocalidades();
		}
	}
}


function onChangeAgendaProvincias() {
var selectTipoCentros = $("fb_agenda_tipocentro");
	if (this.options[this.selectedIndex]) {		
		var parametros = 'idProvincia='+this.options[this.selectedIndex].value+'&idTipoCentro='+selectTipoCentros.options[selectTipoCentros.selectedIndex].value;
		new Ajax.Request("/ObraSocial/centros/ajax/os_localidades_provincia_eventos",{
				method:'post',
				postBody: parametros,
				onSuccess:setAgendaLocalidades
				});
	}	
}

function setAgendaLocalidades (transport) {
	var selectLocalidades = $('fb_agenda_localidad');
	var selectProvincias = $('fb_agenda_provincia');
	
	if (transport.status == 200) {
		var xmlDoc = transport.responseXML;
		var localidades = xmlDoc.getElementsByTagName("localidad");
		if (selectLocalidades) {			
			selectLocalidades.options.length = 0;
			selectLocalidades.options[0] = new Option("Todas",0);
			if (selectProvincias.selectedIndex != 0){
				for (var i=0;i<localidades.length;i++) {				
					selectLocalidades.options[i+1] = new Option(localidades[i].firstChild.firstChild.nodeValue,localidades[i].getAttribute("id"));
				}
				if (localidades.length > 0) {
					mostrarSelectLocalidades();
				}
			} else {
				ocultarSelectLocalidades();
			}
			
			
		}
	}
}



