;(function($){
  window.BioeCart = {
    init:function(){
      this.Forms.init();
    },
    // gerencia os formularios
    Forms:{
      init:function(){
        this.setupBoxForms();
      },
      setupBoxForms:function(){
        // box de produtos
        $("form.form_box").submit(function(){
          // escopo do form
          var $this = $(this);

          // dados do formulario
          var $data = $(this).serializeArray();

          // bloqueia o box
          var $box = $this.parent().parent();
          $box.block();

          // recebe o retorno
          function getPostReturn(r){
            if(r.error){
              alert(r.message);
              return;
            } else {
              // valida se possui a classe index
              if($this.hasClass("index")){
                window.top.location = '/produtos/rc/?id=' + $data[1].value;
              } else {
                // desbloqueia o form
                $box.unblock();
                // atualiza os valores do topo
                BioeCart.Info.updateTopValues()
              }
            }
          }

          // envia o post
          $.post($.baseURL("carrinho/incluir/?ajax=true"), $data, getPostReturn, "json");
          return false;
        })
      }
    },
    // gerencia as informacoes do carrinho
    Info:{
      updateTopValues:function(){
        // valida se existe o elemento
        if($("div#shopcart").length == 0){
          return;
        }
        $("div#shopcart").text("Aguarde...").load($.baseURL("carrinho_ajax/valores-topo"));
      }
    },
    // GERENCIA O PEDIDO
    Pedido:{
      // valores do pedido
      valor_total:0,
      valor_produtos:0,
      valor_frete:0,
      valor_desconto:0,
      // inicializa com os valores
      init:function(){

      }
    }
  }
  
  // inicia o carrinho
	$(document).ready(function(){BioeCart.init();});

})(jQuery);
