var is_IE = '\v'=='v';

function headerFade(n,items){
    do { var rand = Math.floor(Math.random()*items); if (rand <= 9) rand = '0'+rand; } while (rand==n);
    divs = $('.header_bg');
    if ($(divs[0]).is(':visible')){
        $(divs[1]).css('backgroundImage','url("http://www.mervilab.com/img/cab0'+rand+'.jpg")').fadeIn(1000);
        $(divs[0]).fadeOut(1000);
    } else {
        $(divs[0]).css('backgroundImage','url("http://www.mervilab.com/img/cab0'+rand+'.jpg")').fadeIn(1000);
        $(divs[1]).fadeOut(1000);
    }

    setTimeout("headerFade('"+rand+"',"+items+")",4000);
}

function sliderNoticias(){
    if ($('#wrap_noticias').scrollLeft() == $('#wrap_noticias ul').width()-275) $('#wrap_noticias').animate({scrollLeft:'0'},1000);
    else $('#wrap_noticias').animate({scrollLeft:'+=275'},400);
}

$(document).ready(function(){
    $('body').addClass('js');
    //if (is_IE) $('body').addClass('ie');

    $('a.blank, a[rel=external]').attr('target','_blank');

    if ($('#p_inicio').length == 1) {
        $('#video a').bind('click',function(){
            $(this).parent().flash({src:'http://www.youtube.com/v/ijG-5-VefvM&hl=es_ES&fs=1&rel=0&hd=0&autoplay=1', width:677, height:404, wmode:'transparent'},{version:8});
            return false;
        });

        var autoscrolling = true;
        $('.infiniteCarousel').infiniteCarousel().hover(
            function(){ autoscrolling = false; },
            function(){ autoscrolling = true; }
        );
        setInterval(function(){ if (autoscrolling) $('.infiniteCarousel').trigger('next'); }, 3000);
    } else {
        headerFade(0,14);
    }

    if ($('#wrap_noticias').length == 1){
        var noticia_slide = true;
        $('#wrap_noticias ul').width(275 * $('#wrap_noticias li').length);
        $('#noticias').hover(
            function(){ noticia_slide = false; },
            function(){ noticia_slide = true; }
        );
        setInterval(function(){ if (noticia_slide) sliderNoticias(); }, 5000);
    }

    if ($('#p_contactar').length == 1){
        $('#mapa-madrid').html('<iframe width="629" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.es/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q=Calle+de+Belmonte+de+Tajo,+76,+Madrid&amp;sll=40.597271,-3.713379&amp;sspn=12.554616,33.815918&amp;ie=UTF8&amp;hq=&amp;hnear=Calle+de+Belmonte+de+Tajo,+76,+28019+Madrid,+Comunidad+de+Madrid&amp;ll=40.393038,-3.71634&amp;spn=0.009806,0.026951&amp;z=15&amp;iwloc=A&amp;output=embed"></iframe>');

        $('#form_consulta').submit(function(){
            emailRegExp = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/
            if ($('#nombre').val() == 'Nombre') falta = true;
            else if ($('#email').val() == 'Correo Electrónico' || !emailRegExp.test($('#email').val())) falta = true;
            else if ($('#consulta').val() == 'Su consulta') falta = true;
            else falta = false;

            if (falta){ alert('Debes rellenar todos los campos correctamente.'); return false; }
            else return true;
        });
    }
});