var status = 0;
var valor_total = '';
var parcelas = 10;

/*FORMATA MOEDA*/
function float2moeda(num) {
	   x = 0;
	   if(num<0) {
	      num = Math.abs(num);
	      x = 1;
	   }
	   if(isNaN(num)) num = "0";
	      cents = Math.floor((num*100+0.5)%100);

	   num = Math.floor((num*100+0.5)/100).toString();

	   if(cents < 10) cents = "0" + cents;
	      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	         num = num.substring(0,num.length-(4*i+3))+'.'
	               +num.substring(num.length-(4*i+3));
	      ret = num + ',' + cents;
	      if (x == 1) ret = ' - ' + ret;return ret;

}

/*GALERIA DE PRODUTOS*/
function ImgPrincipal(img){
    document.getElementById("imgPrincipal").style.background = "url('" + img + "') center no-repeat";
}

/*MONTA SELECT DE PARCELAS*/
function ReturnSelect(valor_total){
    
    var conteudo = "";
    conteudo = conteudo + "<select name='PARCELAS' class='select_parcelas'>\n";
        for(i=1;i<=parcelas;i++){
            var valor = valor_total / i; 
            conteudo = conteudo + "<option value='" + i + "'>" + i + "x de R$" + float2moeda(valor) + "&nbsp;&nbsp;</option>\n";
        }
        conteudo = conteudo + "</select>\n";

    document.getElementById("resultado_parcelas").innerHTML = conteudo;
}


/*ESCOLHA DE FRETE*/
function ChooseShipping(valorfrete, valortotal, tipofrete, acao, prazoentrega){
    
    var valor_total = parseFloat(valorfrete)+parseFloat(valortotal);
    document.getElementById("valor_frete").innerHTML = float2moeda(valorfrete);
    
    if(acao != "cart"){
        
            document.getElementById("hidden_frete_cartao").value = float2moeda(valorfrete);
            document.getElementById("hidden_tipo_frete_cartao").value = tipofrete;
            document.getElementById("hidden_tempo_frete_cartao").value = prazoentrega;
            document.getElementById("hidden_frete_debito").value = float2moeda(valorfrete);
            document.getElementById("hidden_tipo_frete_debito").value = tipofrete;
            document.getElementById("hidden_tempo_frete_debito").value = prazoentrega;
            document.getElementById("hidden_frete_boleto").value = float2moeda(valorfrete);
            document.getElementById("hidden_tipo_frete_boleto").value = tipofrete;
            document.getElementById("hidden_tempo_frete_boleto").value = prazoentrega;
    }else{
        
        document.getElementById("hidden_frete").value = float2moeda(valorfrete);
        document.getElementById("hidden_tipo_frete").value = tipofrete;
        document.getElementById("hidden_tempo_frete").value = prazoentrega;
        
        
    }
    
    
    document.getElementById("valor_total").innerHTML = float2moeda(valor_total);
    
    if(acao != 'cart'){
    ReturnSelect(valor_total);}
    
    document.getElementById("btnComprarCarrinho").disabled = false;
}


/*ESCOLHA TIPO PESSOA CADASTRO*/
function TypeRegister(acao){
    
    if(acao == "pessoa_fisica"){
        document.getElementById("pessoa_fisica").style.display = "block";
        document.getElementById("pessoa_juridica").style.display = "none";
    }else{
        document.getElementById("pessoa_fisica").style.display = "none";
        document.getElementById("pessoa_juridica").style.display = "block";
    }
}

/*JQUERY*/
jQuery(document).ready(function(){
	
    
       
	//Background Select, Radio and Checkbox
    $('#searchSelect').jqTransform({imgPath:'../images/forms/'});
    $('form#cadastro_news').jqTransform({imgPath:'../images/forms/'});
    $('form#searchSide').jqTransform({imgPath:'../images/forms/'});
    $('form#formCadastro').jqTransform({imgPath:'../images/forms/'});
    $('form#formContato').jqTransform({imgPath:'../images/forms/'});
    $('form#formMarcasSide').jqTransform({imgPath:'../images/forms/'});
    $('#searchSelectFooter').jqTransform({imgPath:'../images/forms/'});
    $('#formCadastroAddress').jqTransform({imgPath:'../images/forms/'});
    $('#formEditar').jqTransform({imgPath:'../images/forms/'});
    $('#formPedidos').jqTransform({imgPath:'../images/forms/'});
    
    
    /*CAROUSEL*/
    jQuery('#areaMarcas').jcarousel({
    	wrap: 'circular'
    });
    
    //Masked
	$("#campoCEP").mask("99999-999", {placeholder:""});
    $("#end_cep").mask("99999-999", {placeholder:""});
    $("#end_telres").mask("(99) 9999-9999", {placeholder:""});
    $("#end_telcel").mask("(99) 9999-9999", {placeholder:""});		
    $("#expiration").mask("99/99", {placeholder:""});
    $("#cli_cpf").mask("999.999.999-99", {placeholder:""});
    $("#cli_cnpj").mask("99.999.999/9999-99", {placeholder:""});
    $("#cli_datanasc").mask("99/99/9999", {placeholder:""});
    $("#conts_telefone").mask("(99) 9999-9999", {placeholder:""});
    $("#conts_celular").mask("(99) 9999-9999", {placeholder:""});
    $("#data_compra_ini").mask("99/99/9999", {placeholder:""});
    $("#data_compra_fim").mask("99/99/9999", {placeholder:""});
    
});

/*Função Pai de Mascaras*/
function Mascara(o,f){
    v_obj=o
    v_fun=f
    setTimeout("execmascara()",1)
}

/*Função que Executa os objetos*/
function execmascara(){
    v_obj.value=v_fun(v_obj.value)
}

/*Função que Determina as expressões regulares dos objetos*/
function leech(v){
    v=v.replace(/o/gi,"0")
    v=v.replace(/i/gi,"1")
    v=v.replace(/z/gi,"2")
    v=v.replace(/e/gi,"3")
    v=v.replace(/a/gi,"4")
    v=v.replace(/s/gi,"5")
    v=v.replace(/t/gi,"7")
    return v
}

/*Função que permite apenas numeros*/
function Integer(v){
    return v.replace(/\D/g,"")
}



/*Funcção do Slider da Home*/

$(document).ready(function() {
			
			//Show the paging and activate its first link
			$(".paging").show();
			$(".paging a:first").addClass("active");
			
			//Get size of the image, how many images there are, then determin the size of the image reel.
			var imageWidth = $(".window").width();
			var imageSum = $(".image_reel img").size();
			var imageReelWidth = imageWidth * imageSum;
			
			//Adjust the image reel to its new size
			$(".image_reel").css({'width' : imageReelWidth});
			
			//Paging  and Slider Function
			rotate = function(){
				var triggerID = $active.attr("rel") - 1; //Get number of times to slide
				var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
			
				$(".paging a").removeClass('active'); //Remove all active class
				$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			
				//Slider Animation
				$(".image_reel").animate({
					left: -image_reelPosition
				}, 500 );
			
			}; 
			
			//Rotation  and Timing Event
			rotateSwitch = function(){
				play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
					$active = $('.paging a.active').next(); //Move to the next paging
					if ( $active.length === 0) { //If paging reaches the end...
						$active = $('.paging a:first'); //go back to first
					}
					rotate(); //Trigger the paging and slider function
				}, 10000); //Timer speed in milliseconds (5 seconds)
			};
			
			rotateSwitch(); //Run function on launch
			
						//On Hover
			$(".image_reel a").hover(function() {
				clearInterval(play); //Stop the rotation
			}, function() {
				rotateSwitch(); //Resume rotation timer
			});	
			
			//On Click
			$(".paging a").click(function() {
				$active = $(this); //Activate the clicked paging
				//Reset Timer
				clearInterval(play); //Stop the rotation
				rotate(); //Trigger rotation immediately
				rotateSwitch(); // Resume rotation timer
				return false; //Prevent browser jump to link anchor
			});
			
		});

