$(document).ready(function(){
    $("a[rel*=externo]").attr('target','_blank');
});

function changeCidade(urlHttp,selectEstado,selectCidade) {
    selectEstado = $(selectEstado);
    selectCidade = $(selectCidade);
    $.ajax({
        url:urlHttp,
        type:'GET',
        cache: true,
        dataType:'json',
        data:{
            uf:selectEstado.val()
        },
        success: function(data) {
            selectEstado.val(data.uf.id);
            selectCidade.empty().append('<option value="">Selecione...<\/option>').val('');
            $.each(data.cidades, function(i,item){
                selectCidade.append('<option value="'+i+'">'+item+'<\/option>');
            });
        },
        error: function() {
            selectEstado.val(null);
            selectCidade.empty().append('<option value="">Falha ao carregar, tente novamente...<\/option>');
        },
        beforeSend: function() {
            selectCidade.empty().append('<option value="">Carregando...<\/option>').val('');
        }
    });
}

function none() {
    return false;
}

function removeCart() {
    $('td.quantia input.remover').bind('click', function() {
        var produto = $(this).parents('tr:first');
        if(produto) {
            if(!confirm('Deseja confirmar remoção do produto seguinte do carrinho?\n\n- '+produto.attr('title'))) {
                return false;
            }
            produto.find('input.quantidade').val(0);
            return true;
        }
        return false;
    });
}


function ajeitarTransporte(form) {
    form = $(form);
    var botoes = form.find('button');
    var totalBotoes = 0;
    $.each(botoes, function(i) {
        totalBotoes += $(botoes[i]).width() + 22;

    });
    form.find('div').css('width',totalBotoes).css('margin','auto');
}

function sorteioCC(botoes) {
    botoes = $(botoes);
    botoes.hide();
    var useds = new Array(10);
    var i = 0;
    while(i<10) {
        var sort = Math.floor(Math.random() * 10);
        if(!useds[sort]) {
            botoes.find('a:eq('+i+')').text(sort);
            useds[sort] = true;
            i++;
        }
    }
    botoes.show();
}


jQuery.fn.quickEach = (function() {
    var jq = jQuery([1]);
    return function(c) {
        var i = -1,
        el, len = this.length;
        try {
            while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
        } catch (e) {
            delete jq[0];
            throw e;
        }
        delete jq[0];
        return this;
    };
}());

function escorrerBusca() {
    $('#busca').slidinglabels({
        className:'escorrega',
        topPosition:'5px',
        leftPosition:'5px',
        axis:'y',
        speed:'slow'
    });
}

function juitter() {
    $.Juitter.start({
        total: 3,
        searchType:"fromUser",
        searchObject:"clamar_lingerie",
        nameUser:'text',
        loadMSG:'Carregando Tweets...',
        readMore:null,
        openExternalLinks:"newWindow",
        live:"live-60"
    });
}

function dataRelativa(data) {
    var d = Date.parse(data);
    var dateNow = new Date().getTime() - d;
    var since = {
            sec: dateNow / 1000,
            min: dateNow / 1000 / 60, //1000 / 60
            hour : dateNow / 1000 / 60 / 60, //1000 / 60 / 60
            day : dateNow / 1000 / 60 / 60 / 24, //1000 / 60 / 60 / 24
            year : dateNow / 1000 / 60 / 60 / 24 / 365 //1000 / 60 / 60 / 24 / 365
    }
    var txt = '';

    // Segundos
    if(Math.round(since.sec) == 1){
        txt += "1 segundo";
    }
    else if(since.min < 1.01){
        txt += Math.round(since.sec) + " segundos";
    }

    // Minutos
    else if(Math.round(since.min) == 1){
        txt += "1 minuto";
    }
    else if(since.hour < 1.01){
        txt += Math.round(since.min) + " minutos";
    }

    // Horas
    else if(Math.round(since.hour) == 1){
        txt += "1 hora";
    }
    else if(since.day < 1.01){
        txt += Math.round(since.hour) + " horas";
    }

    // Dias
    else if(Math.round(since.day) == 1){
        txt += "1 dia";
    }
    else if(since.year < 1.01){
        txt += Math.round(since.day) + " dias";
    }

    // Anos
    else if(Math.round(since.year) == 1){
        txt += "1 ano";
    }
    else
    {
        txt += Math.round(since.year) + " anos";
    }

    return txt + ' atrás';
}

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

