/************************
 *
 *  Class MateriauxBtn  *
 *
 ***********************/
(function(){
  
 MateriauxBtn = function(elt, parentObj){
    this.btn = elt;
    this.parentObj = parentObj;
    
    this.gammes = [];
    this.initListeners.call(this);
  };

  MateriauxBtn.prototype = {
    
    initListeners : function(){
      var obj = this;
      this.btn.click(function(evt){
        evt.preventDefault();
        if($(this).data('filter-id') == "all"){
          obj.parentObj.firstClick = true;
          obj.parentObj.sortable = false;
          obj.parentObj.turnOnAllButtons.call(obj.parentObj);
          obj.parentObj.sortByCarac.call(obj.parentObj);
        }else{
          
          if(!obj.parentObj.firstClick && obj.btn.parent().hasClass("actif")){
            return 0;
          }

          if(obj.parentObj.sortable){
            obj.parentObj.firstClick = false;
            obj.parentObj.sortable = false;
            obj.btn.parent().addClass("actif");
            obj.parentObj.turnOffOtherButtons.call(obj.parentObj, $(this).attr("id"));
            obj.parentObj.sortByCarac.call(obj.parentObj);
          }
        }
      });
    },
  
  
    getData : function(dataKey){
      return this.btn.data(dataKey);
    },

    getJQueryElement : function(){
      return this.btn;
    }
    
  };

})();
//end class MateriauxBtn





(function(){
  
  CuisineFilter = function(liste_cuisines_id, boutons_prix_selector, boutons_carac_selector){
  
    this.listeCuisines = $(liste_cuisines_id);
    this.boutonsPrix = $(boutons_prix_selector);
    this.boutonsCarac = [];
    this.firstClick = true; 
    var obj = this;
    
    $(boutons_carac_selector).each(function(index, elt){
      var materiauxBtn = new MateriauxBtn($(this), obj);
      obj.boutonsCarac.push(materiauxBtn);
    });
    
    this.sortable = true;
        
    this.gammes = [];
    this.initGammes();
    this.initListeners.call(this);
  };
  
  
  
  CuisineFilter.prototype = {
    
    initListeners : function(){
      var obj = this;
      this.boutonsPrix.each(function(index, elt){
        $(this).click(function(evt){
         evt.preventDefault();
         obj.sortByPrix($(this), obj);
        });
      });
    },
  
    
  initGammes : function(){
    var obj = this;
    this.gammes = $("#slider_gamme .slide");
    this.gammesBtn = [];
    this.gammes.each(function(index, elt){
      var gammeBtn = new GammeButton($(this), function(){
        obj.gammeClickAction(this, obj);
      });
      obj.gammesBtn.push(gammeBtn);
    });
  },
  
  
  gammeClickAction : function(gammeObject, obj){
     if(obj.sortable){
       obj.sortable = false;
       var is_activ = gammeObject.isActiv();
       obj.unselectAllGammes();
       
       if(!is_activ ){
        gammeObject.setIsActiv();
       }else{

       }
       obj.sortByCarac(obj);
     }
  },
  
  
  unselectAllGammes : function(){
    for(var i=0; i < this.gammesBtn.length; i++){
      if(this.gammesBtn[i].isActiv()){
        this.gammesBtn[i].leaveGammeAnimation(this.gammesBtn[i]);
      }
      this.gammesBtn[i].setIsInActiv();
    }
  },
  
 
  /**
   *
   *
   *  GESTION DES FILTRAGES
   *
   */
  
  /**
   *  éteint tous les boutons de filtrage sauf celui qui a été cliqué
   */
  turnOffOtherButtons : function(selectedId){
    $('#filter li a').each(function(index, elt){
      if($(this).attr("id") != selectedId){
        $(this).parent().removeClass("actif");
      }
    });
  },


/**
 *  passe tous les boutons comme actifs
 */
  turnOnAllButtons : function(){
    $('#filter li a').each(function(index, elt){
      $(this).addClass("activ");
      $(this).parent().addClass("actif");
    });
  },


/**
 *
 */
  sortByPrix : function(selectedElt, obj){
    if($(selectedElt).hasClass("activ")){
      // do nothing
    }else{
      obj.sortable = false;
      ToolLoader.addLoader('#'+obj.listeCuisines.parent().attr("id"));
      
      obj.boutonsPrix.each(function(index,elt){ 
        obj.addUnselectBoutonPrix($(this));
      });
      obj.addSelectBoutonPrix(selectedElt);
      
      obj.updateListingWithCriteria(obj);
    }
  },
  
  
  /**
   * déselectionner un bouton de prix
   */
  addUnselectBoutonPrix : function(elt){
    elt.removeClass("activ");
    elt.parent().removeClass("actif");
  },
  
  
  /**
   * selectionner un bouton de prix
   */
  addSelectBoutonPrix : function(elt){
    elt.addClass("activ");
    elt.parent().addClass("actif");
  },
  
  
  

  sortByCarac : function(){
    ToolLoader.addLoader('#'+this.listeCuisines.parent().attr("id"));

    this.updateListingWithCriteria();
  },
  
  
  
  /**
   * récupérer les critères 
   */
  getSelectedCriteria : function(){
     var criteria = [];
    
    this.boutonsPrix.each(function(index, elt){
      if($(this).hasClass("activ")){
        criteria.push("prix="+$(this).data("prix-filter"));
      }
    });
    
    var tabBtnLength = this.boutonsCarac.length;
    for(var i = 0; i < tabBtnLength; i++){
     var filterId = this.boutonsCarac[i].getJQueryElement().data("filter-id");
     
     if(this.boutonsCarac[i].getJQueryElement().parent().hasClass("actif")){
       criteria.push("carac_"+filterId+"=1");
      }else{
       criteria.push("carac_"+filterId+"=0");
      }
    }
     
     var gamme_id = null;
     this.gammes.each(function(index, elt){
      if($(this).hasClass("gamme_activ")){
        var id = $(this).data("gamme-id");
        if(!id){
          return 0;
        }
        gamme_id = id;
      }
    });
    
    if(gamme_id){
      criteria.push("gamme_id="+gamme_id);
    }
     
    return criteria;
  },
  
  
  
  updateListingWithCriteria : function(){
     var url = ROOT_URL + "cuisines.php";
     var datas = [];
     datas.push("action=update_gamme_listing");
     
     var criteria = this.getSelectedCriteria();
     for(var i = 0; i < criteria.length; i++){
       datas.push(criteria[i]);
     }
     var obj = this;
  
     $.ajax({
       url:url,
       data:datas.join('&'),
       type:"post",
       dataType:"html",
       success:function(responseHTML, responseDATA, responseXML){
         obj.listeCuisines.children().remove();
         obj.listeCuisines.append(responseHTML);
         
          obj.sortable = true;
          ToolLoader.removeLoader('#'+obj.listeCuisines.parent().attr("id"));
       },
       error:function(){
         
       }
     });
  }
  
};
  // end prototype
  
})();
// end class CuisineFilter





/**********************
 *
 * Class GammeButton  *
 *
 *********************/
/**
 * over : actif
 */
(function(){
  
  GammeButton = function(elt, callback){
  
    this.elt = elt;
    this.blockOver = elt.children().first().next();
    this.callback = callback;
    
    this.gammeIsActiv = false;
    
    this.isEntering = false;
    this.isLeaving = false;
    
  //  this.initGammeIsClosed();
    
    this.initListeners.call(this);
  };
  
  
  GammeButton.prototype = {
    
    initGammeIsClosed : function(){
    this.overValues = {
      display:"block",
      height:263,
      top:144
    };
    
    var closedTop = (this.overValues.height + this.overValues.top).toString()+"px";
    var closedHeight = 0;
    
    this.blockOver.css({
      "display":"block",
      "height":closedHeight,
      "top":closedTop
    });
    
  },
  
  
  /**
   * ajout des écouteurs
   */
  initListeners : function(){
    
    if(this.blockOver.hasClass("actif")){
      this.setIsActiv();
    }
    
    var obj = this;
    
    this.elt.click(function(evt){
      evt.preventDefault();
      if(!obj.isLeaving && !obj.isEntering){
        if(!obj.blockOver.hasClass("actif")){
          obj.enterGammeAnimation.call(obj);
        }
        obj.callback();
      }
    });
    
    this.elt.mouseenter(function(evt){ 
      if(!obj.gammeIsActiv){
        if(!obj.isLeaving){
           obj.enterGammeAnimation.call(obj);
        }else{
          obj.isEntering = true;
        }
      }
    });
    
    
    
    this.elt.mouseleave(function(evt){
      if(!obj.gammeIsActiv){
        if(obj.isEntering){
          obj.isLeaving = true;
        }else{
          obj.leaveGammeAnimation.call(obj);
        }
      }
    });
    
  },// end initListeners
  
  
  
  leaveGammeAnimation : function(){
      var animDuration = 200;
      this.blockOver.removeClass("actif");
      var obj = this;
      
      this.blockOver.animate({
          "height": "64px",
          "top": "343px"
        },{
        duration:animDuration,
        easing:"easeOutCirc",
        complete:function(){
          obj.blockOver.css({"background-color":"transparent"});
          obj.showLibelles.call(obj);
          obj.isLeaving = false;
        },
        step:function(now, fx){
           if(obj.isEntering){
            obj.isLeaving = false;
            $(this).stop();
            obj.enterGammeAnimation.call(obj);
          }else{
            obj.blockOver.removeClass("actif");
            obj.hideLibelles.call(obj);
          }
        }
      });
  },
  
  
  enterGammeAnimation : function(){
      var animDuration = 300;
      this.hideLibelles();
      var obj = this;

       this.blockOver.css({"background-color":"#ed7f00"});
       this.blockOver.animate({
          "height": "263px",
          "top": "144px"
        },{
        easing:"easeOutCirc",
        duration:animDuration,
        complete:function(){
          obj.showLibelles.call(obj);
          obj.blockOver.addClass("actif");
          obj.isEntering = false;
        },
        step:function(now, fx){
          if(obj.isLeaving){
            obj.isEntering = false;
            $(this).stop();
            obj.leaveGammeAnimation.call(obj);
          }
        }
      });
  },
  
  
  /**
   *  cache le libellés des gamme
   */
  hideLibelles : function(){
    var id = this.blockOver.attr("id");
    $('#'+id+' h3, #'+id+' p').css({"visibility":"hidden"});
  },
  
  /**
   *  affiche les libellés des gammes
   */
  showLibelles : function(){
    var id = this.blockOver.attr("id");
    $('#'+id+' h3, #'+id+' p').css({"visibility":"visible"});
  },
  
  
  /**
   * retourne true si la classe est selectionnée
   */
  isActiv : function(){
    if(this.elt.hasClass("gamme_activ")){
      return true;
    }
    
    return false;
  },
  
  
  
  /**
   * marque la gamme comme selectionnée
   */
  setIsActiv : function(){
      this.elt.addClass("gamme_activ");
      this.blockOver.addClass("actif");
      this.gammeIsActiv = true;
  },
  
  /**
   * marque la gamme comme non-selectionnée 
   */
  setIsInActiv : function(){
    this.elt.removeClass("gamme_activ");
    this.blockOver.removeClass("actif");
    this.gammeIsActiv = false;
  }
  
  
  };
  // end GammeButton.prototype
  
})();
// end class GammeButton

