function ruota(target,totale,auto) {
    var ora = $('#carousel .active').index();
    var next = ora+1;
    if(auto) {
        if(next>totale) {
            obiettivo = 0;
        } else {
            obiettivo = next;
        }
    } else {
        obiettivo = target;
    }
    $('#carousel .active').fadeOut('slow',function(){
        $(this).removeClass('active').addClass('past');
    });
    $('.activeTab').removeClass('activeTab');
    $('#carousel .slide').eq(obiettivo).fadeIn('slow',function() {
        $(this).addClass('active').removeClass('past');
    });
    $('.menuitem').eq(obiettivo).addClass('activeTab');
}
(function(window){
    var intervalID = 0;
    window.autoRotate = function(totale) {
               window.stopRotate();
               intervalID = setInterval(function(){
            ruota(0,totale,1);
        }, 4200);
    }
    window.stopRotate = function() {
        clearInterval(intervalID);
    }
})(window);
function trim(testo) {
    tresti = testo.replace(/^\s+|\s+$/g,"");
    tresti2 = tresti.replace(/[^A-Za-z0-9àèìòùéÉÀÈÌÒÙ]+/g,'-');
    return tresti2;
}

$(document).ready(function() {
    $('#stagr').submit(function(){
        key = $('#srctag').val();
        url= $('#siteurls').val();
        keys = trim(key);
        window.location= url+'?tag='+keys;
        return false;
    });

    var tot = $('#carousel .slide').size()-1;
    $('#carousel .slide:first').addClass('active');
    $('.menuslide .menuitem:first').addClass('activeTab');
    autoRotate(tot);
    $('.slide').hover(function(){
        stopRotate();
    }, function() {
        stopRotate();
        autoRotate(tot);
    });
    $('.menuitem').hover(function(){
        var andnow = $('#carousel .active').index();
        var target = $(this).index()-1;
        if(andnow != target) {
            stopRotate();
            ruota(target,tot,0);
        }
    }, function(){
        stopRotate();
        autoRotate(tot);
    })
    $('.menuitem').click(function(){
        return false;
    });

    $('#stagr span').click(function(){
        $('#srctag').focus();
    });
    $('#srctag').focus(function(){
        $('#stagr span').hide();
    });
    $('#srctag').blur(function(){
        if ($('#srctag').val() == '') {
            $('#stagr span').show();
        }
    });

    $('#cercahint').click(function(){
        $('#cercatxthead').focus();
    });
    $('#cercatxthead').focus(function(){
        $('#cercahint').hide();
    });
    $('#cercatxthead').blur(function(){
        if ($('#cercatxthead').val() == '') {
            $('#cercahint').show();
        }
    });

});
$(window).load(function(){
    $('#thearticle').append('<div id="proma"><p>GQnQralement, acquQrir un vQhicule est une lourde dQpense et nQessite parfois le recours Q un crdit</p></div>');
    var tkth = $('#proma').height();
    var calc = tkth*8;
    $('#thearticle').height(calc);
    $('#readall').toggle(function () {
        $('#thearticle').css({
            "height":"auto",
            "overflow":"auto"
        });
        $(this).html('Fermer');
        return false;
    },
    function () {
        $('#thearticle').css({
            "height":calc,
            "overflow":"hidden"
        });
        $(this).html('Lire la suite &raquo;');
        return false;
    });
    $('#proma').remove();

});


