var Shop;

$(document).ready(function(){
    if(APPLICATION_ENV == 'development') head.js(JS_PATH+'/less.js'); // load less in dev
    
    // máscaras padrões
    $("input.mask-cpf").mask("999.999.999-99");
    $("input.mask-cep").mask("99999-999");
    $("input.mask-data,input.mask-date").mask("99/99/9999");
    $("input.mask-tel").mask("(99)9999-9999");
    $("input.mask-currency").maskCurrency();
    
    $(".prevalue").each(function(){ $(this).prevalue(); }); // prevalue em campos com a class .prevalue
    
    // arruma classe de menus devido ao zend_navigation substituir a classe quando ativo
    $(".site-menu-top a:last").addClass("last");
    $(".site-menu-user ul li:nth-child(2) a").addClass('last');
    $(".site-menu-categorias li:nth-last-child(2) a").addClass("last");
    $(".site-menu-categorias a:last").addClass("last").addClass("vale-presente");
    
    $("a[href^='#']").live("click",function(e){ // remove click de links vazios (que começam por #)
        e.preventDefault();
    });
    $("a.js-back").live("click",function(e){ // adiciona ação de voltar a links com a classe js-back
        history.back();
    });
    
    if($(".pagination").size()){ // se tiver paginação
        $(".pagination .navigation.left").html("&laquo;"); // arruma texto da navegação
        $(".pagination .navigation.right").html("&raquo;");
        if($(".pagination a").size() <= 1){
            $(".pagination a").hide(); // se tiver somente 1 ou nenhuma página esconde a div
        }
    }
    
    if(SCRIPT) head.js(JS_PATH+'/'+SCRIPT+'.js'); // carrega scripts de páginas
    
    // newsletter
    $('.newsletter-form').submit(function(e){
        e.preventDefault();
        var $form = $(this), $status = $form.find('.status').removeClass('error').html('Enviando...');
        
        $.post(URL+'/index/newsletter.json',$form.serialize(),function(json){
            if(json.error) $status.addClass('error');
            $status.html(json.message);
        },'json');
    });
    $('.newsletter-form input.newsletter-telefone').focus(function(){
        $(this).removeClass('pre');
    }).blur(function(){
        var $t = $(this);
        if($t.val() == ''){
            $t.addClass('pre');
        }
    });
    
    // flash-messages
    if($("#flash-messages").size()){
        var $flash = $("#flash-messages");
        var y = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        $flash.css({top:y+10}).delay(1000).show().click(function(){
            $(this).fadeOut("slow");
        });
    }
});

// Color Hex <-> RGB
function hexToR(h){return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h){return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h){return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h){return (h.charAt(0)=="#") ? h.substring(1,7):h}
function hexToRGB(h){return {'R':hexToR(h),'G':hexToG(h),'B':hexToB(h)}}
function RGBToHex(r,g,b){var dec = r+256*g+65536*b;return dec.toString(16);}

// Math
function getRand(n){n=n||0;return Math.floor(Math.random()*(n+1));}

// ValidateForm
function validadeForm(f){var $f=$(f),err=0;$f.find("input").each(function(i,v){if($(this).data("validate")&&$.trim($(this).val())==""){alert($(this).data("errmsg"));$(this).focus();err++;return false}});if(err==0){$f.submit()}};
