
is_IE = '\v'=='v';
email_reg = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/

function load_proy(data){
    $('#our_work').append(data);
    li = $('#our_work').find('li.loaded');

    li.find('a').hover(
        function(){ this.over=true; e=this; setTimeout("if(e.over) $(e).stop().animate({'marginTop':'-43px'},200);",200); },
        function(){ this.over=false; $(this).stop().animate({'marginTop':'0px'},200); }
    );

    $('#our_work').animate({height:'+='+(Math.ceil(li.length/3) * 226)},1000,function(){
        li.fadeIn().removeClass('loaded');
    });

    if ($('.endpage').length > 0) $('#div_more').hide();
    else {
        $('#div_more a').show().parent().removeClass('loading');
        $('#div_more .loading').hide();
    }
}

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

    //hover con fade
    if (!is_IE){
        $('h1 a')
            .each(function(){ $(this).html('<span>'+$(this).text()+'</span>') })
            .hover(
                function(){ $(this).find('span').stop().animate({'opacity':1},200); },
                function(){ $(this).find('span').stop().animate({'opacity':0},1200); }
            );

        $('.lateral a')
            .each(function(){ $(this).html('<span>'+$(this).text()+'</span>') })
            .hover(
                function(){ $(this).find('span').stop().animate({'opacity':1},200); },
                function(){ $(this).find('span').stop().animate({'opacity':0},200); }
            );
    }

    //mosaico de proyectos hover
    $('#our_work a').hover(
        function(){ this.over=true; e=this; setTimeout("if(e.over) $(e).stop().animate({'marginTop':'-43px'},200);",200); },
        function(){ this.over=false; $(this).stop().animate({'marginTop':'0px'},200); }
    );

    //destacados slider
    $('#outstanding').codaSlider();
    outstanding_hover = false;
    $('#outstanding').hover(function(){ outstanding_hover=true; }, function(){ outstanding_hover=false; });
    setInterval("if (!outstanding_hover) $('.scrollButtons.right').click();",8000);

    //movimiento cabecera
    $('#top_bg ul').jparallax({linkResponse:false,yparallax:false,frameDuration:40,mouseport:$('#page')},{},{xtravel:-1},{},{},{xtravel:-1});

    //select de tags, servicios
    if ($('#tag_submenu').length == 1) $('#tag_order').selectReplace({onchange:function(){
        $('#tag_submenu .list').hide();
        $('#'+$('#tag_order').val()+'_list').fadeIn(400);
    }});

    $.localScroll({filter:'.lateral a.top, #menu a'});

    //cargando mas proyectos con ajax
    if ($('#div_more').length > 0){
        $('#div_more a').click(function(){
            $(this).hide().parent().addClass('loading');
            $('#div_more .loading').fadeIn();

            var url = this.href.split('/');
            for (i=0; i<url.length; i++) if (url[i]=='work') j = i+1;
            var n = $('#our_work li').length;

            if (url[j] == 'pag') data = {'action':url[j], 'items':n};
            else data = {'action':url[j], 'value':url[j+1], 'items':n};

            $.get('mod_proyectos_ajax.php',data,load_proy);

            return false;
        });
    }

    //i love
    $('#love a').click(function(){
        $.get('mod_proyectos_ajax.php',{'action':'ilove','value':$(this).attr('href').substr(1)},function(data){
            if (!isNaN(data) && data > 0){
                $('#love').find('span').html(data);
                $('#love').find('a').replaceWith('<span>milgracias</span>');
            } else $('#love').find('a').replaceWith('<span title="Se ha producido un error">arrea!</span>');
        });

        return false;
    });

    //newsletter
    $('#accept').prettyCheckboxes();
    $('#newsletter .txt')
        .focus(function(){ if(this.value == 'your@email.com') this.value=''; })
        .blur(function(){ if(this.value == '') this.value='your@email.com'; });

    $('#newsletter').submit(function(){
        email = $('#newsletter .txt').val();
        if (email == 'your@email.com'){ alert('Escribe tu dirección de email.'); return false; }
        else if (!email_reg.test(email)){ alert('Escribe una dirección de email válida.'); return false; }
        else if (!$('#accept').is(':checked')){ alert('Debes aceptar los Términos.'); return false; }
        else return true;
    });

    $('a.blank').attr('target','_blank');
});
