var posicao = 0;

var img;
move = function(target){
    posicao = target;
    $("#control_num ul li a").removeClass("current");
    img = new Image();
    img.src = $("#control_num ul li a")[target].rel;
    $("#control_num ul li a")[target].className = "current"
    $("#img_banner").animate({
        opacity: 0
    }, 200, function() {
        if(!img.complete)
        {
            $("#slideshow").addClass("loading");
            $(img).load(function(){
                $("#slideshow img")[0].src = $("#control_num ul li a")[target].rel;
                $("#img_banner").animate({
                    opacity: 1
                },500,function(){
                    $("#slideshow").removeClass("loading")
                    ready = 1
                }
                );
            });
            
        }
        else{
            $("#slideshow img")[0].src = $("#control_num ul li a")[target].rel;
            $("#img_banner").animate({
                opacity: 1
            },500);
        }
        
    });
}
moveto = function(index)
{
    move(index);
    clearTimeout(stoploop);
}
movenext = function (){
    if(posicao == ($("#control_num ul li a").length -1))
    {
        move(0);
    }
    else
    {
        posicao += 1
        move(posicao);
    }
}
moveprev = function (){
    if(posicao == 0)
    {
        move($("#control_num ul li a").length - 1);
    }
    else
    {
        posicao -= 1
        move(posicao);
    }
}
var stoploop
loop = function()
{
    movenext();
    if(!img.complete)
    {
        $(img).load(function(){
           stoploop = setTimeout(loop, 5000);
        });
    }
    else
    {
        stoploop = setTimeout(loop, 5000);
    }
    
}
$(function(){
    $('#control_left').click(function() {
        clearTimeout(stoploop);
        moveprev();
    });

    $('#control_right').click(function() {
        clearTimeout(stoploop);
        movenext();
    });
    stoploop = setTimeout(loop, 5000);
});


function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if (typeof(mail) == "string") {
        if (er.test(mail)) {
            return true;
        }
    }
    else
    if (typeof(mail) == "object") {
        if (er.test(mail.value)) {
            return true;
        }
    }
    else {
        return false;
    }
}
function soNums(e, args){
    if (document.all) {
        var evt = event.keyCode;
    }
    else {
        var evt = e.charCode;
    } 
    var chr = String.fromCharCode(evt);
    if (evt < 20 || (evt > 47 && evt < 58) || (args.indexOf(chr) > -1)) {
        return true;
    }    
    return false;    
}

