Event.observe(window, 'load', function() {
    var botonTematicas = $('go');
    if(botonTematicas) {
        botonTematicas = botonTematicas.firstDescendant();
        var tematicas = $('block');

        var alturaFinal = (tematicas.scrollHeight ? tematicas.scrollHeight : 194) + 'px';
        botonTematicas.observe('click', function(e) {
            if(tematicas.getHeight() > 140) {
                new Effect.Morph(tematicas, { style: { height: '140px'}, duration: 0.5 });
            } else {
                new Effect.Morph(tematicas, { style: { height: alturaFinal}, duration: 0.5 });
            }
            e.stop();
        });
    }

    var botonVerMasPrograma = $('open');
    if(botonVerMasPrograma) {
        botonVerMasPrograma = botonVerMasPrograma.firstDescendant();
        var datosPrograma = $('block2');
        if(datosPrograma) {
            var textoPrograma = $(datosPrograma.select('.texto_curso')[0]);
            botonVerMasPrograma.observe('click', function(e) {
                //if(datosPrograma.getHeight() > 205) {
                if(datosPrograma.getHeight() > 510) {
                    /* new Effect.Morph(datosPrograma, { style: { height: '178px'}, duration: 0.5 }); */
                    new Effect.Morph(datosPrograma, { style: { height: '500px'}, duration: 0.5 });
                } else {
                    new Effect.Morph(datosPrograma, { style: { height: (textoPrograma.getHeight() + 135) +'px'}, duration: 0.5 });
                }
                e.stop();
            });
        }
    }

    var botonVerMasCentro = $('open');
    if(botonVerMasCentro) {
        botonVerMasCentro = botonVerMasCentro.firstDescendant();
        var datosCentro = $('block3');
        if(datosCentro) {
            var textoCentro = $(datosCentro.select('.texto_curso')[0]);
            botonVerMasCentro.observe('click', function(e) {
                if(datosCentro.getHeight() > 178) {
                    new Effect.Morph(datosCentro, { style: { height: '178px'}, duration: 0.5 });
                } else {
                    new Effect.Morph(datosCentro, { style: { height: (textoCentro.getHeight() + 140) +'px'}, duration: 0.5 });
                }
                e.stop();
            });
        }
    }
});

var _efectoForm = null;
var _efectoCampoForm = null;
function avisarFormulario() {
    var capaForm = $($$('.cuerpo_form')[0]);
    if(!capaForm) {
        return;
    }
    var campos = capaForm.select('input, select, textarea');
    var color = null;
    try {
        color = $($$('.tit')[0]).getStyle('color');
        if(color.indexOf('rgb') != -1) {
            color = color.replace('rgb', '').replace('(', '').replace(')', '');
            color = color.split(',');
            color = '#' +
                parseInt(color[0]).toColorPart() +
                parseInt(color[1]).toColorPart() +
                parseInt(color[2]).toColorPart();
        }
        capaForm.scrollTo();
    } catch(exp) {
        color = '#8EC53F';
    }

    if(_efectoForm) {
        _efectoForm.cancel();
    }

    _efectoForm = new Effect.Highlight(capaForm, {
            startcolor: '#ffff99',
            endcolor: '#ffffff',
            restorecolor: 'transparent',
            afterFinish: function () {
                try {
                    capaForm.setStyle({background : 'transparent url(images/cuerpo_formulario.gif) no-repeat scroll left top'});
                } catch(e){}
            }
        });

    campos.each(function(e) {
        if(_efectoCampoForm) {
            _efectoCampoForm.cancel();
        }
        _efectoCampoForm = new Effect.Highlight(e, {
                duration: 3,
                startcolor: color,
                keepBackgroundImage: true,
                afterFinish: function () {
                    try {
                        e.setStyle({backgroundColor: ''});
                        e.style.backgroundColor = null;
                        e.focus();
                    } catch(exp){}
                }
            }
        );
        e.focus();
        throw $break;
    });
}

function enviarBusqueda() {
    var f = $('formBusqueda');

    if(f.busqueda.value.blank()) {
        avisarCampoObligatorio(f.busqueda, JS_ALERT_INTRODUZCA_PALABRA);
        return false;
    }

    f.submit();
    
    return true;
}

var _efectoCampoObligatorio1 = null;
var _efectoCampoObligatorio2 = null;
function avisarCampoObligatorio(campo, msj) {
    var padre = $(campo.parentNode);
    campo = $(campo);
    if(msj) {
        alert(msj);
    }
    if(_efectoCampoObligatorio1) {
        _efectoCampoObligatorio1.cancel();
    }
    if(_efectoCampoObligatorio2) {
        _efectoCampoObligatorio2.cancel();
    }
    _efectoCampoObligatorio1 = new Effect.Highlight(padre, {
        afterFinish: function() {
            try {
                padre.setStyle({backgroundColor: ''});
                padre.style.backgroundColor = '';
            } catch(e) {}
        }
    });
    _efectoCampoObligatorio2 = new Effect.Pulsate(campo, {
        afterFinish: function() {
            campo.setOpacity(1);
        }
    });
    try {
        campo.focus();
        campo.scrollTo();
    } catch(e) {}
}


function mostrarVtnEmergente(url, nombre, ancho, alto) {
    window.open(url, nombre,'top=100,left=100,width=' + ancho + ',height=' + alto + ',resize=1,scrollbars=1');
}

function mostrarPrivacidad(url) {
    mostrarVtnEmergente(url ? url : '/politica_privacidad.html', 'privacidad', 420, 450);
}

function mostrarAvisoLegal(url) {
    mostrarVtnEmergente(url ? url : '/aviso_legal.html', 'aviso_legal', 420, 450);
}

function mostrarFaqsCentros(url) {
    mostrarVtnEmergente(url ? url : '/faqs_centros.html', 'faqs_centros', 560, 450);
}

function mostrarFaqsUsuarios(url) {
    mostrarVtnEmergente(url ? url : '/faqs_usuarios.html', 'faqs_usuarios', 560, 450);
}

function mostrarAnadirOfertaFormativa() {
    mostrarVtnEmergente('/cursos/anadir_oferta_formativa.html', 'anadir_oferta_formativa', 957, 663);
}

function guardarFavoritos(){
    try { /* IE */
        window.external.AddFavorite(URL_DOMINIO, DOMINIO);
    } catch(e) {}
    try { /* Mozilla */
        window.sidebar.addPanel(DOMINIO, URL_DOMINIO, '');
    } catch(e) {}
}

function enviarFormSolicitudSugeridos() {
    var f = $('formSolicitudesSugeridas');
    var idsCursos = [];
    $A(f['curso_id[]']).each(function(c) {
        if(c.checked) {
            idsCursos.push(c.value);
        }
    });
    if(!idsCursos.length) {
        alert(JS_ALERT_SELECCIONAR_CURSO);
        $('listaCursosSugeridos').highlight();
        return false;
    }

    var onError = function() {
        $('listaCursosSugeridos').update('<div class="error">'+JS_ALERT_ENVIANDO_SOLICITUDES+'</div>');
        setCargando(false);
    }

    var onSuccess = function(r) {
        try {
            var resultado = r.evalJSON();
            if(!resultado.error) {
                $('listaCursosSugeridos').update('<div class="confirmacion">'+JS_ALERT_SOLICITUD_ENVIADA+'</div>');
                setCargando(false);
            } else {
                onError();
            }
        } catch(e) {
            onError();
        }
    }
    
    try {
        setCargando(true, JS_ALERT_ESPERE_ENVIANDO_SOLICITUD);
    } catch(e) {}

    $('btnEviarSolicitudSugeridos').onclick = function() { return false; };

    var peticion = new JMLib.Ajax.Sender();
    peticion.setForm(f);
    peticion.setOnSuccess(onSuccess);
    peticion.setOnError(onError);

    peticion.send();

    return true;
}