/* 
 * Buscador de Recetas Malloa
 * 
 */

var _esMovePag = false;

$().ready(function(){
  $("input[type=checkbox]").attr("checked",false).parent().parent().css({"color":"#fff"});
  $("input[type=checkbox]").click(function(){
      $.ajax({
          url: "jxIngredientes.php"
          ,type: "post"
          ,data: $("input[type=checkbox]:checked").serializeArray()
          ,success: function(_data){
              if($("input[type=checkbox]:checked").length<1){
                  $("input[type=checkbox]").show().parent().parent().css({"color":"#fff"});
                  return true;
              }
              $("input[type=checkbox]").each(function(){
                  if(_data.indexOf(","+$(this).val()+",")==-1) {
                      $(this).hide().parent().parent().css({"color":"#bbb"});
                  }
                  else $(this).show().parent().parent().css({"color":"#fff"});
              });
              if($("input[type=checkbox]:hidden").length == $("input[type=checkbox]").length)
                  $("input[type=checkbox]").show().parent().parent().css({"color":"#fff"});
              return true;
          }
          ,error: function(_data){}
      });
  });

  $("#btnBuscaRecetas").live("click",function(){
      if($("input[type=checkbox]:checked").length<2) return false;
      if(!_esMovePag){
          $("#boxIngredientes,#boxRecetas").hide();
          $("#boxAnimacion").show();
      }
      _esMovePag = false;
      $.ajax({
          url: "jxResultadoAplicacion.php"
          ,type: "post"
          ,data: $("#frmRecetas").serializeArray()
          ,success: function(_data){
              //$("#boxAnimacion").hide();
              $("#boxRecetas").html(_data);
              return false;
          }
          ,error: function(_data){}

      });
      
      return false;
  });

  $("a.carga_relacionada").live("click",function(){

      //$("#boxIngredientes,#boxRecetas").hide();
      //$("#boxAnimacion").show();
      $("#_rID").val($(this).attr("rel"));
      $.ajax({
          url: "jxResultadoAplicacion.php"
          ,type: "post"
          ,data: $("#frmRecetas").serializeArray()
          ,success: function(_data){
              $("#boxRecetas").html(_data);
              return false;
          }
          ,error: function(_data){}

      });

      return false;
  });

  $("#btnVolverIngredientes").live("click",function(){
      // Reestablecer valores
      $("#_p").val(1);
      $("#_rID").val(0);


      $("#boxAnimacion,#boxRecetas").hide();
      $("#boxRecetas").html("");
      $("#boxIngredientes").show();
      return false;
  });

  $("a.movePag").live("click",function(){
      var _MPag = Number($("span.max_recetas_encontradas").text());

      if(_MPag < 2) return false;

      if($(this).attr("rel")=="right") $("#_p").val(Number($("#_p").val()) + 1);
      if($(this).attr("rel")=="left") $("#_p").val(Number($("#_p").val()) - 1);

      if(Number($("#_p").val()) < 1) $("#_p").val(_MPag);
      if(Number($("#_p").val()) > _MPag) $("#_p").val(1);
      
      _esMovePag = true;
      $("#_rID").val(0);

      $("#btnBuscaRecetas").click();

      return false;
  });

  var _verRecetaUrl = window.location.toString().indexOf("/#/receta/");

  if(_verRecetaUrl > -1){
      var _verRecetaID = window.location.toString().substring(_verRecetaUrl + 10);
      verReceta(_verRecetaID);
      return false;
  }

});

function ocultar_animacion(){
  $("#boxAnimacion").hide();
  $("#boxRecetas").show();
}

function verReceta(_idR){
      _esMovePag = false;
      $("#boxIngredientes,#boxRecetas").hide();
      $("#boxAnimacion").show();
      $.ajax({
          url: "jxResultadoAplicacion.php"
          ,type: "post"
          ,data: {_rID: _idR}
          ,success: function(_data){
              $("#boxRecetas").html(_data);
              return false;
          }
          ,error: function(_data){}

      });

      return false;
}
