$(document).ready(function(){
    resizeImageBackground();

    $('div.home-text .block').hide();
    setTimeout('displayBlock()',800);
    
    var winHeightDef=$(window).height()-30; 
    var contentHeight=$('#content').height();   
    $('#content').css('height',(winHeightDef>contentHeight?winHeightDef:contentHeight));
    
    
    $('#newsletter-link').click(function(){
       $('#newsletter-block').slideToggle(); 
    });
    $('#newsletter-input').click(function(){
       var content=$(this);
       if(content.val()=='Newsletter Sign Up!') content.val('');
    });
    
});
$(window).resize(function(){
    resizeImageBackground();
    
    var winHeightDef=$(window).height()-30; 
    var contentHeight=$('#content').height();
    $('#content').css('height',(winHeightDef>contentHeight?winHeightDef:contentHeight));
});

function displayBlock(){
    var obj=null;
    if($('div.home-text .block:visible').length>0){
        obj=$('div.home-text .block:visible').next();
    }
    else{
        obj=$('div.home-text .block:first');
    }
    
    obj.fadeIn(1000,function(){
        if(obj.length==1){
            setTimeout('displayBlock()',500);
        }
    });
}

function resizeImageBackground(){
    // resize de las imágenes de fondo para encajar en pantalla

    // ratio proporcional de la ventana del navegador
    var navWidth = $(window).width();
    var navHeight = $(window).height();
    var navRatio = navWidth / navHeight;

    // ratio proporcional de la imagen
    if ($('.main_image').width() > 1) var picWidth = $('.main_image').width();
    if ($('.main_image').height() > 1) var picHeight = $('.main_image').height();
    var picRatio = picWidth / picHeight;

    // escala resultante
    if (navRatio > picRatio) {
            // escalamos la imagen para ajustarse al ancho del navegador
            var newHeight = (navWidth / picWidth) * picHeight;
            var newWidth = navWidth;
    } else {
            // escalamos la imagen para ajustarse a la altura del navegador
            var newHeight = navHeight;
            var newWidth = (navHeight / picHeight) * picWidth;
    }

    // centramos la imagen
    var newTop = 0 - ((newHeight - navHeight) / 2);
    var newLeft =  0 - ((newWidth - navWidth) / 2);

    // colocamos todas
    $('#image_holder').css({height: navHeight, width: navWidth});
    $('#image_holder').css({visibility:"visible", display:"block"});

    $('#image_holder img').css({height: newHeight, width: newWidth});

    // centro y aledañas...
    $('#image_center').css({top: newTop, left: newLeft});
    $('#image_left').css({top: newTop, left: newLeft-newWidth});
    $('#image_right').css({top: newTop, left: newLeft+newWidth});
    $('#image_up').css({top: newTop-newHeight, left: newLeft});
    $('#image_down').css({top: newTop+newHeight, left: newLeft});
    
    $('#head-menu li').mouseenter(function(){
        var menu=$(this).find('.dropdown-list:hidden');  
        if(menu.length>0) menu.slideDown();
    }).mouseleave(function(){
        var menu=$(this).find('.dropdown-list:visible');
        if(menu.length>0) menu.slideUp();
    });
}
