var deBannerAMenu = false;
var deMenuABanner = false;

$(document).ready(function(){
    //Hacemos que el contenido llegue hasta el final de la pagina
    if($(window).height() > $('#todo').height())
    {
        $('#pie div.pie').css('height', $(window).height() - $('#todo').height());
        //Este div tiene padding: 8px 0 6px
        $('#pie div.texto').css('height', $(window).height() - $('#todo').height() - 14);
    }
    
    $('a.ventanaNueva').click(function(){
        window.open($(this).attr('href'), '_blank');
        return false;
    });
	
    /*** Menu ***/
    $('li.desplegable').live('mouseover', function(){
        $(this).children('ul:first').css('display', 'block');
    });
	
    $('li.desplegable').live('mouseout', function(){
        $(this).children('ul:first').css('display', 'none');
    });
	
    /*** Formularios ***/
    $('input[type="text"], input[type="password"], textarea').focus(function(){$(this).addClass('focusForm')});
    $('input[type="text"], input[type="password"], textarea').blur(function(){$(this).removeClass('focusForm')});
	
    /*** Colorbox ***/
    $('a[rel="colorbox"]').colorbox({maxWidth: 800, maxHeight: 600});

    /* Web Ford */
    $('a.webFord').colorbox({
        iframe: true,
        width:  $(window).width() - 100,
        height: $(window).height() - 100
    });
	    
    $(window).resize(function(){
        $('a.webFord').colorbox({
            iframe: true,
            width:  $(window).width() - 100,
            height: $(window).height() - 100
        });
    });

    /* Boletín */
    $('a.suscripcionBoletin').colorbox({
        iframe: true,
        width: 440,
        height: 450
    });

    /* Solicitar prueba */
    $('a.solicitarPrueba').colorbox({
        iframe: true,
        width: 440,
        height: 600
    });

    /* Solicitar catálogo */
    $('a.solicitarCatalogo').colorbox({
        iframe: true,
        width: 440,
        height: 600
    });

    /* Flota */
    $('a.flota').colorbox({
        iframe: true,
        width: 392,
        height: 430
    });

    /* Promociones */
    $('a.colorboxPromociones').colorbox({
        maxWidth: 800,
        maxHeight: 600
    });
        
    /* Terminos */
    $('a.terminosCondiciones').colorbox({
        iframe: true,
        width: 800,
        height: 600
    });

    /* Envio de comunicaciones */
    $('a.contactoEnvioComunicaciones').colorbox({
        iframe: true,
        width: 800,
        height: 250
    });
        
    $('a.envioComunicaciones').click(function(){
        parent.$('<a></a>')
        .appendTo(parent.document.body)
        .attr('href', $(this).attr('href'))
        .bind('click', function(){})
        .colorbox({
            width: 800,
            height: 250,
            iframe: true
        })
        .click();
        	
        return false;
    });

    /* Protección de datos personales */
    $('a.contactoProteccionDatos').colorbox({
        iframe: true,
        width: 800,
        height: 370
    });
        
    $('a.proteccionDatos').click(function(){
        parent.$('<a></a>')
        .appendTo(parent.document.body)
        .attr('href', $(this).attr('href'))
        .bind('click', function(){})
        .colorbox({
            width: 800,
            height: 370,
            iframe: true
        })
        .click();
        	
        return false;
    });


// Banner del C-MAX

    $('#cmax').hover(function(){
        $('#cmax').animate({height: '430px'}, 500, null); //abrir el banner
        deMenuABanner = true; //cancela cerrado de banner porque volvemos a él desde el menú
    },function(){
        deBannerAMenu = false;
        saleBanner(); //intención de cerrar el banner
    });

    $('#menu').hover(function(){
        deBannerAMenu = true; //cancela cerrado de banner porque vamos a menú
    },function(){
        deMenuABanner = false;
        saleBanner();//intención de cerrar el banner
    });
    
});

function saleBanner() {
    setTimeout('cierraBanner()', 500); //dejamos tiempo antes de cerrar el banner para saber a dónde se dirige el usuario y después evaluar si se debe o no cerrar el banner
}

function cierraBanner() {
    if (!deBannerAMenu || !deMenuABanner) { //si no vamos al menú, ni volvemos desde el menú al banner, cerramos el banner
        $('#cmax').animate({height: '154px'}, 500, null);
    }
}