$(function(){
    var creditos_oculto = false;
    var height_footer   = $('#footer1').css('height').replace('px', '');
    var tamanio_ventana = iniciar_interfaz_inicio();

    // Mostrar contenedor de noticias
    
    // Mostrar noticias
    var lista = $('.carrusel_noticias ul li');
    var j = 0;
    
    function mostrar_noticias(){lista.eq(j++).fadeIn(250, mostrar_noticias);}
    
    $('.carrusel_noticias').animate({'width': 642}, 500, function() {
        mostrar_noticias();
        $('.creditos').animate({'top': 65}, 500, function(){
            $('#creditos_down').fadeIn(500);
        });
    });
    
    
    // Efectos noticias
    $('#lista_noticias li').hover(
        function(){},
        function(){}
    );
        
    // Centrar noticia
    // Tamanio por defecto
    var noticia_width   = 500;
    var noticia_height  = 450;
    
    var height_disponible   = (tamanio_ventana[1] - height_footer);
    //console.log(height_disponible);
    /*
    if (height_disponible < noticia_height) noticia_height = height_disponible - 20;
    
    if (tamanio_ventana[0] < noticia_width) noticia_width = tamanio_ventana[0] - 20;
*/
    var noticia_left    = (tamanio_ventana[0] - noticia_width) / 2;
    var noticia_top     = (tamanio_ventana[1] - height_footer - noticia_height) / 2;
    
    if (noticia_left < 0) noticia_left = 0;
    if (noticia_top < 0) noticia_top = 0;
    
    $('#noticia').css({top: noticia_top, left: noticia_left});
        
    $('#lista_noticias li.noticia').click(function(e){
        e.preventDefault();
        var id = $(this).attr('id');
        $('#noticia').fadeOut();
        $('#noticia').html('<img src="images/ajax-loader.gif" />');
        $('#noticia').show('slow', function () {
            $(this).animate({'width': noticia_width, 'height': noticia_height}, 1500, function() {
                $(this).load(baseurl + 'noticia/ver/' + id, function () {
                    $('#inner-content-div').slimScroll({
                        height: noticia_height - 70,
                        color: '#676767',
                        size: '10px',
                        alwaysVisible: false
                    });
                });
            });
        });
    })
    
    $('.cerrar_ventana').live('click', function(){
        $('#noticia').fadeOut(500);
    })
   
   $('#creditos_down').click(function(){
       var top = 0;

       if (!creditos_oculto){           
           top = 119;

           $('.creditos').animate({top: top}, 500, function(){
               $('#creditos_down').removeClass('down').addClass('up').html('CRÉDITOS');
               creditos_oculto = true;
           });
       } else {
           top = 65;
           $('#creditos_down').removeClass('up').addClass('down').html('');
           $('.creditos').animate({top: top}, 500, function(){
               creditos_oculto = false;
           });
       }
       
    });
   
    window.onresize = function() {
        tamanio_ventana = actualizar_interfaz_inicio();
        // Actualizar noticia
        if ($('#noticia').is(':visible')){
            height_disponible   = (tamanio_ventana[1] - height_footer);
            
            noticia_left    = (tamanio_ventana[0] - noticia_width) / 2;
            noticia_top     = (tamanio_ventana[1] - height_footer - noticia_height) / 2;

            if (noticia_left < 0) noticia_left = 0;
            if (noticia_top < 0) noticia_top = 0;
            
            $('#noticia').css({top: noticia_top, left: noticia_left});
        }
        return false;
    };
});
