function tamanhos(url, objHtmlReturn, prod_id, cor_id)
{
    dados = {'prod_id':prod_id, 'cor_id':cor_id};

    $("#"+objHtmlReturn).html('<img src="/site-media/img/ajax-loader.gif">');
    $.ajax({
      type: "POST",
      url: url,
      dataType: "json",
      data: dados,
      success: function(retorno){
	  $("#id_foto").attr('src', retorno.foto);
	  $("#zoom_foto").attr('href', retorno.zoom);
          $("#"+objHtmlReturn).empty();
          $.each(retorno.tamanhos, function(i, item){
	      if (item.id != 0) {
		  link1 = '<a href="javascript:void(0);" onclick="adiciona('+item.id+')">';
		  link2 = '</a>';
	      }
	      else {
		  link1='';
		  link2='';
	      }
	      $("#"+objHtmlReturn).append('<td style="width: 39px;">'+link1+'<img src="'+item.imagem+'" alt="'+item.tamanho+'" title="'+item.tamanho+'" />'+link2+'</td>'); 
          });
	  $("#"+objHtmlReturn).append('<td style="text-align:center;"><a href="#" class="autor ">Tabela de medidas</a></td>');
      }
   });
}

function muda_foto(url)
{
    //$("#id_foto").attr('src', url);
    $("#zoom_foto").attr('href', url);
}

function atualiza_quantidade(url, objHtmlReturn1, objHtmlReturn2, valor, item_id)
{
    dados = {'id':item_id, 'valor':valor};

    $("#"+objHtmlReturn1).html('<img src="/site-media/img/ajax-loader.gif">');
    $.ajax({
      type: "POST",
      url: url,
      dataType: "json",
      data: dados,
      success: function(retorno){
	  $("#carrinho_topo").html(retorno.frase);
          $("#"+objHtmlReturn1).empty();
	  $("#"+objHtmlReturn1).html('R$ '+retorno.preco);
	  $("#"+objHtmlReturn2).val(retorno.qtd);
	  $("#total").empty();
	  $("#total").html('R$ '+retorno.total);
	  muda_total();
      }
   });
}

function completa_endereco(url)
{
    cep = $("#id_cep").val();
    if (cep.length == 8) {
	dados = {'cep': cep};

	$.ajax({
	  type: "POST",
	  url: url,
	  dataType: "json",
	  data: dados,
	  success: function(retorno){
	      $("#id_tipo_logradouro").val(retorno.tipo_logradouro);
	      $("#id_logradouro").val(retorno.logradouro);
	      $("#id_cidade").val(retorno.cidade);
	      $("#id_pais").val("Brasil");
	      $("#id_estado").val(retorno.estado);
	      $("#id_bairro").val(retorno.bairro);
	}
      });
    }
    else {
      	      $("#id_tipo_logradouro").val('');
	      $("#id_logradouro").val('');
	      $("#id_cidade").val('');
	      $("#id_pais").val('');
	      $("#id_estado").val('');
	      $("#id_bairro").val('');
    }
}

function calcula_frete(url, carrinho)
{
    cep = $("#cep_destino").val();
    if (!cep) {
	cep = $("#cep_destino").html();
    }
    if (cep.length < 8) {
	$("#frete_opcoes").hide();
    }
    else {
	dados = {'cep': cep, 'carrinho':carrinho}

	$("#frete_opcoes").show();
	$("#bolinha_rodopiante").show();
        $("#frete_sedex").hide();
        $("#frete_pac").hide();
	$.ajax({
	  type: "POST",
	  url: url,
	  dataType: "json",
	  data: dados,
	  success: function(retorno){
	      $("#frete_sedex").empty();
	      $("#frete_sedex").html('R$ '+retorno.sedex.valor);
	      $("#valor_frete_sedex").val(retorno.sedex.valor);
	      $("#prazo_sedex").empty();
	      $("#prazo_sedex").html('entrega em '+retorno.sedex.prazo+' dia');
	      $("#frete_pac").empty();
	      $("#frete_pac").html('R$ '+retorno.pac.valor);
	      $("#valor_frete_pac").val(retorno.pac.valor);
	      $("#prazo_pac").empty();
	      $("#prazo_pac").html('entrega em '+retorno.pac.prazo+' dia');
	      $("#bolinha_rodopiante").hide();
	      $("#frete_sedex").show();
	      $("#frete_pac").show();
	      muda_total();
	}
      });
    }
}

function muda_total()
{
    total = $("#total").html().split("R$")[1];
 
    if ($("#radio_frete_sedex").is(':checked')) {
      frete = $("#frete_sedex").html().split("R$")[1];
    }
    else if ($("#radio_frete_pac").is(':checked')) {
      frete = $("#frete_pac").html().split("R$")[1];
    }
    else {
      frete = '0,0';
    }
    
    total = total.replace('\.', '');
    total = total.replace('\,', '.');
    frete = frete.replace('\.', '');
    frete = frete.replace('\,', '.');
    geral = Number(total) + Number(frete);

    dados = {'num':geral}

    $.ajax({
      type: "POST",
      url: "/formata",
      dataType: "json",
      data: dados,
      success: function(retorno){
	  $("#total_geral").empty();
	  $("#total_geral").html('R$ '+retorno.total);
	  $("#visa_vista1").html('Visa Crédito - à vista - R$ '+retorno.total);
	  $("#visa_prazo2").html('Visa Crédito - 2x - R$ '+retorno.doisx);
	  $("#visa_prazo3").html('Visa Crédito - 3x - R$ '+retorno.tresx);
	  $("#visa_vista2").html('Visa Electron - à vista - R$ '+retorno.total);
    }
    });
}

// $(document).ready(function() {
// calcula_frete('/calculos/frete', $("#carrinho_id").val());
// });

function mostra_comentarios() {
    $("#mais").show();
    $("#link_mais").hide();
}

function esconde_comentarios() {
    $("#mais").hide();
    $("#link_mais").show();
}

function adiciona(mod_id) {
    url = "/adiciona/"+mod_id;

    $.ajax({
      type: "POST",
      url: url,
      dataType: "json",
      success: function(retorno){
	  $("#carrinho_topo").html(retorno.frase);
	  if (retorno.imagem) {
	    $("#img_"+mod_id+"_disp").show();
	    $("#img_"+mod_id+"_indisp").hide();
	  }
	  else {
	    $("#img_"+mod_id+"_indisp").show();
	    $("#img_"+mod_id+"_disp").hide();
	  }
          $("#car_image").attr({
	    src: retorno.foto,
	    alt: retorno.nome,
	    title: retorno.nome
	   });
	  $("#car_name").html(retorno.nome);
	  $("#car_model").html(retorno.modelo);
	  $("#car_value").html(retorno.preco);
	  $("#car").css({position:'absolute', left:$("#itens").position().left+144, top:$("#itens").position().top+45});
	  $("#link_pedido").show();
	  $("#link_pedido_seta").show();
	  $("#car").fadeIn();
	  setTimeout(function(){ $("#car").fadeOut();}, 3000);
    //setTimeout(function(){ $("#car").hide('slow');}, 3000);
      }
   });
}

function envia_amigo() {
    $("#amigo").css({left:$("#envia_amigo").position().left+96, top:$("#envia_amigo").position().top+12});
    $("#mostra_msg").hide();
    $("#amigo").show();
}

function enviar_amigo(p_id) {
    email = $("#id_email_destinatario").val();
    dados = {'email':email, 'id':p_id}
    $.ajax({
      type: "POST",
      url: '/mailamigo',
      dataType: "json",
      data: dados,
      success: function(retorno){
	  if (retorno == 1) {
	      msg = 'Enviado com sucesso';
	  }
	  else {
	      msg = 'Falha no envio';
	  }
	  $("#id_email_destinatario").val('');
	  $("#msg_email").html(msg);
	  $("#mostra_msg").show();
      }
   });
}

function cadastra_mail() {
    email = $("#email_novidade").val();
    dados = {'email':email}

    $.ajax({
      type: "POST",
      url: '/mailcad',
      dataType: "json",
      data: dados,
      success: function(retorno){
	  if (retorno == 1) {
	      msg = '<img src="/site-media/img/certo_16x16.png" /> Cadastrado com sucesso';
	  }
	  else if (retorno == 2) {
	      msg = 'E-mail já cadastrado';
	  }
	  else {
	      msg = 'Falha no cadastro';
	  }
	  $("#email_novidade").val('');
	  $("#email_ok").html(msg);
	  $("#email_ok").show();
	  setTimeout(function(){ $("#email_ok").fadeOut();}, 3000);
      }
  });
}