$(function() {
    $('#botonMas').live('click', function() {
       $('#enlacesOcultos').css('display','block');
       $('.marcoMas').css('display','none');
       return false;
    });
    $('#botonMenos').live('click', function() {
       $('#enlacesOcultos').css('display','none');
       $('.marcoMas').css('display','block');
       return false;
    });

    setTimeout(function() {$('body').css('background-color','#000')}, 500);
    // para min-height en IE
    $('.tituloCuerpo').each(function() {
       if ( $(this).height() < 38 )
           $(this).height(38);
    });

    $('.botonListadoProyectos').overlay({
      mask:'#fff',
      modal:true,
      top:'center'
    });
    $('.lupaAmpliacion').live('click',function() {
        $( '#' + $(this).attr('rel') ).parents('a:first').trigger('click');
        return false;
    });
    $('.ampliarImagen').live('click',function() {
        $( '#' + $(this).attr('rel') ).parents('a:first').trigger('click');
        return false;
    });
    $('.botonSubmenu')
        .live('click',function(e) {document.location = $(this).attr('href');return false;})
        .live('mousedown',function() {
            $(this)
                .css('background-position','0px -104px')
                .css('color','white')
                .css('font-weight','900');
        })
        .live('mouseup',function() {
            $(this)
                .css('background-position','0px -52px')
                .css('color','#535353')
                .css('font-weight','normal');
        })
        .live('mouseenter',function() {
            $(this)
                .css('background-position','0px -52px')
                .css('color','#535353')
                .css('font-weight','normal');
        })
        .live('mouseleave',function() {
            $(this)
                .css('background-position','0px 0px')
                .css('color','#535353')
                .css('font-weight','normal');
        });
    $('.botonListaGris')
        .live('click',function(e) {document.location = $(this).attr('href');return false;})
        .live('mouseenter',function() {
            $(this)
                .css('background-position','54px -50px')
                .css('color','white');
            $(this).children('.botonListaGrisIcono')
                .css('background-position','0px -50px')
        })
        .live('mouseleave',function() {
            $(this)
                .css('background-position','54px 0px')
                .css('color','black');
            $(this).children('.botonListaGrisIcono')
                .css('background-position','0px 0px')
        });
    $('.botonListadoSubmenu')
        .live('click',function(e) {
            $(this)
                .toggleClass('botonListadoSubmenuActivo')
            if ( $(this).hasClass('botonListadoSubmenuActivo') )
                $(this).next('.listadoSubmenuDespliegue').slideDown('normal');
            else
                $(this).next('.listadoSubmenuDespliegue').slideUp('normal');
            return false;
        })
    $('.botonListaGrisDesplegable')
        .live('click',function(e) {
            $('.botonListaGrisDesplegable.botonListaGrisDesplegableActiva').not(this)
                .each(
                    function() {
                        $(this)
                            .removeClass('botonListaGrisDesplegableActiva')
                            .next('.listaGrisDespliegue').slideUp('normal');
                        $(this)
                            .css('background-position','54px 0px')
                            .css('color','black')
                            .children('.botonListaGrisIcono')
                            .css('background-position','0px 0px');
                    });
            $(this)
                .toggleClass('botonListaGrisDesplegableActiva')
                .children('.botonListaGrisIcono')
                    .css('background-position','0px -50px');

            if ( $(this).hasClass('botonListaGrisDesplegableActiva') )
                $(this).css('background-position','54px -100px').next('.listaGrisDespliegue').slideDown('normal');
            else
                $(this).css('background-position','54px -50px').next('.listaGrisDespliegue').slideUp('normal');
            return false;
        })
        .live('mouseenter',function() {
            if ( !$(this).hasClass('botonListaGrisDesplegableActiva')) {
                $(this)
                    .css('background-position','54px -50px')
                    .css('color','white')
                    .children('.botonListaGrisIcono')
                    .css('background-position','0px -50px');
            }
        })
        .live('mouseleave',function() {
            if ( !$(this).hasClass('botonListaGrisDesplegableActiva')) {
                $(this)
                    .css('background-position','54px 0px')
                    .css('color','black')
                    .children('.botonListaGrisIcono')
                    .css('background-position','0px 0px');
            }
        });
    precargarImagenes();
    enmarcarFotos();
    setTimeout(fotoEmergente, 2000);
//    fotoEmergente();

});

var precargas = new Array();
function precargarImagenes() {
    var i = 0;
    for ( i = 0; i < 5; i++ )
        precargas[i] = new Image();
    i = 0;
    precargas[i++].src = "imagenes/submenuDesplegableSuperior.gif";
    precargas[i++].src = "imagenes/submenuDesplegableCentro.gif";
    precargas[i++].src = "imagenes/submenuDesplegableInferior.gif";
    precargas[i++].src = "imagenes/flechaCabeceraActiva.jpg";
    precargas[i++].src = "imagenes/flechaPieActiva.jpg";

}

var imagenPrecarga = new Array();
function enmarcarFotos() {
    $('.enmarcada[id]').each(function() {
        var i = $(this).attr('id');
        imagenPrecarga[i] = new Image();
        imagenPrecarga[i].onload = function() {
            var $imagen = $('.enmarcada[id=' + $(this).val() + ']');
            var $marco = $imagen.parents('div:first');
            $imagen.css('display','block');
            var padding = 0;
            if ( ( $marco.height() / $marco.width() ) > ( this.height / this.width ) ) {
                $imagen.css('height', $marco.css('height'));
                $imagen.css('position','relative');
                padding = ( ( ( $imagen.width() * $marco.height() / $imagen.height() ) - $marco.width() ) / 2 );
                if ( !isNaN( padding ))
                    $imagen.css('left', '-' + padding + "px");
            }
            else {
                $imagen.css('width', $marco.css('width'));
                $imagen.css('position','relative');
                padding = ( ( ( $imagen.height() * $marco.width() / $imagen.width() ) - $marco.height() ) / 2 );
                if ( !isNaN(padding) )
                    $imagen.css('top', '-' + padding + "px");
            }
        }
        $(imagenPrecarga[i]).val($(this).attr('id'));
        imagenPrecarga[i].src = $(this).attr('src');
    });
}
function fotoEmergente() {
    $('a[rel^="prettyphoto"]').prettyPhoto(
                {theme:'light_square',
                    show_title:false,
                    horizontal_padding: 18,
                    social_tools:false,
                    autoplay: false
                });
}

