/************************
 *
 *  UrlRewritingManager *
 *
 ***********************/
(function(){
  
  UrlRewritingManager = function(url){
    this.PATTERN_URL_ELECTRO = /^electromenager-/i;
    this.PATTERN_URL_ELECTRO_STR = "^electromenager";
    this.PATTERN_URL_PRESSE = /^revue-de-presse/i;
    this.PATTERN_URL_PRESSE_STR = "^revue-de-presse";

    this.PATTERN_URL_ACTU = /^actualite/i;
    this.PATTERN_URL_ACTU_STR = "^actualite";

    this.PATTERN_URL_DESTOCK = /^destockages/i;
    this.PATTERN_URL_DESTOCK_STR = "^destockages";


    this.PATTERN_URL_NOUS = /qui-sommes-nous-\//i;
    this.PATTERN_URL_LOWCOST = /les-plus-du-low-cost\//i;
    this.PATTERN_URL_CONSEILS = /conseils-et-outils\//i;

    this.PATTERN_URL_FINITIONS = /^cuisine\/finitions\//i;
    this.PATTERN_URL_FINITIONS_STR = "^cuisine/finitions";

    this.module = null;
    this.datas = [];
    this.matching = false;
    this.url = url;
    
    this.validator_func_name = null;
    
  };
  
  
  /**
   *
   */
  UrlRewritingManager.prototype.getDatas = function(){
    return {module:this.module, datas:this.datas};
  };
  
  /**
   *
   */
  UrlRewritingManager.prototype.urlIsMatch = function(){
    return this.matching;
  };
  
  
  /**
   *
   *  récupérer le validator
   *
   */
  UrlRewritingManager.prototype.initPatternValidator = function(){ 
    if(this.url.match(this.PATTERN_URL_PRESSE)){
      this.validator_func_name =  this.presseValidator;
      this.module = "presse";
      
      this.matching = true;
      return true;
    }
    
     if(this.url.match(this.PATTERN_URL_ELECTRO)){
      this.validator_func_name =  this.electroValidator;
      this.module = "electromenager";
      this.matching = true;

      return true;
    }
    
    if(this.url.match(this.PATTERN_URL_NOUS) 
      || this.url.match(this.PATTERN_URL_LOWCOST) 
      || this.url.match(this.PATTERN_URL_CONSEILS)
     ){
      this.validator_func_name =  this.rubriqueValidator;
      this.module = "contenu";
      this.matching = true;

      return true;
    }
    
    
   if(this.url.match(this.PATTERN_URL_ACTU)){
      this.validator_func_name =  this.actualiteValidator;
      this.module = "actualites";
            this.matching = true;

      return true;
    }
    
    
    if(this.url.match(this.PATTERN_URL_DESTOCK)){
      this.validator_func_name =  this.destockageValidator;
      this.module = "destockage";
      this.matching = true;

      return true;
    }
    
    
    if(this.url.match(this.PATTERN_URL_FINITIONS)){ 
      this.validator_func_name =  this.finitionsValidator;
      this.module = "cuisines";
      this.matching = true;

      return true;
    }

    return false;
  };
  
  /**
   *
   */
  UrlRewritingManager.prototype.getDatasForAjaxNavigation = function(){
    if(this.validator_func_name){
       this.matching = this.validator_func_name();
    }
  };
  
  
  
  
  /********************
   *                  *
   *  Les validators  *
   *                  *
   *******************/
  
  
  UrlRewritingManager.prototype.finitionsValidator = function(){
    this.datas.push("action=display_rubrique_load");
    var pattern = this.PATTERN_URL_FINITIONS_STR + "/([0-9]+)-[a-zA-Z0-9\-]+.html$";
    var pattern_finitions = new RegExp(pattern, "i");
    var url_matching = this.url.match(pattern_finitions);
    if(url_matching){
      this.datas.push("rub_id="+url_matching[1]);
      return true;
    }
    
    return false;
  };
  
  
  
 /**
  * destockage
  */ 
  UrlRewritingManager.prototype.destockageValidator = function(){ 
    this.datas.push("action=display_destockage_load");
    var pattern_home = new RegExp(this.PATTERN_URL_DESTOCK_STR + "/([0-9]+)/[a-zA-Z0-9\-]+.html$", "i");
    var url_matching = this.url.match(pattern_home);
   if(url_matching){ 
      this.datas.push("region_id="+url_matching[1]);
      return true;
    }

    return false;
  };
  
  
  /**
   *
   */
   UrlRewritingManager.prototype.actualiteValidator = function(){
    this.datas.push("action=listing_actualite_load");
    var pattern_home = new RegExp(this.PATTERN_URL_ACTU_STR + ".html$", "i");
    var url_matching = this.url.match(pattern_home);
    if(url_matching){ 
      return true;
    }
    
    var pattern_pagin = new RegExp(this.PATTERN_URL_ACTU_STR + "-([0-9]+).html$", "i");
    url_matching = this.url.match(pattern_pagin);
    if(url_matching){ 
      this.datas.push("page="+url_matching[1]);
      return true;
    }
    
    var pattern_rub = new RegExp(this.PATTERN_URL_ACTU_STR + "/([0-9])/[0-9a-zA-Z]+.html$", "i");
    url_matching = this.url.match(pattern_rub);
    if(url_matching){ 
      this.datas.push("actuTypeId="+url_matching[1]);
      return true;
    }
    
    var pattern_rub_pagin = new RegExp(this.PATTERN_URL_ACTU_STR + "/([0-9])/[0-9a-zA-Z]+-([0-9]+).html$", "i");
    url_matching = this.url.match(pattern_rub_pagin);
    if(url_matching){ 
      this.datas.push("actuTypeId="+url_matching[1]);
      this.datas.push("page="+url_matching[2]);
      
      return true;
    }

    return false;
   };
    
  
  /**
   *
   *  rubrique
   *  
   */
  UrlRewritingManager.prototype.rubriqueValidator = function(){
    var pattern_text = "^([a-zA-Z0-9-]+)";
    
    var pattern_outils = new RegExp( pattern_text + "/12/([0-9]+)/([a-zA-Z0-9-]+).html$", "i");
    var url_matching = this.url.match(pattern_outils);
    if(url_matching){
      this.datas.push("action=display_outils_load");
      this.datas.push("rub_id="+url_matching[2]);
      return true;
    }
    
    var pattern_qui = new RegExp(pattern_text + "/14/([0-9]+)/([a-zA-Z0-9-]+).html$", "i");
    url_matching = this.url.match(pattern_qui);
    if(url_matching){
      this.datas.push("action=display_qui_sommes_nous_load");
      this.datas.push("rub_id="+url_matching[2]);
      return true;
    }
    
    var pattern_low = new RegExp(pattern_text + "/15/([0-9]+)/([a-zA-Z0-9-]+).html$", "i");
    url_matching = this.url.match(pattern_low);
    if(url_matching){
      this.datas.push("action=display_low_cost_load");
      this.datas.push("rub_id="+url_matching[2]);
      return true;
    }
    
    return false;
  };
  
  
  
  
  /**
   *
   *  electroménager
   *  
   */
  UrlRewritingManager.prototype.electroValidator = function(){
    this.datas.push("action=display_pack_electromenager_load");
    
    var pattern_home = new RegExp(this.PATTERN_URL_ELECTRO_STR + "([a-zA-Z\-]+)([0-9]+).html$", "i");
    var url_matching = this.url.match(pattern_home);
    if(url_matching){
      this.datas.push("rub_id="+url_matching[2]);
      return true;
    }
    
    return false;
  };
  
  

/**
 * presse
 */  
  UrlRewritingManager.prototype.presseValidator = function(){
    this.datas.push("action=presse_load");
    
    var pattern_home = new RegExp(this.PATTERN_URL_PRESSE_STR + ".html$", "i");
    var url_matching = this.url.match(pattern_home);
    if(url_matching){
      return true;
    }
    
    var pattern_rub = new RegExp(this.PATTERN_URL_PRESSE_STR + "/([0-9]+)/([a-zA-Z\-]+).html$", "i");
    url_matching = this.url.match(pattern_rub);
    if(url_matching){
      this.datas.push("rubrique="+url_matching[1]);
      return true;
    }
    
    var pattern_home_pagin = new RegExp(this.PATTERN_URL_PRESSE_STR + "-([0-9]+).html$", "i");
    url_matching = this.url.match(pattern_home_pagin);
    
    if(url_matching){
      this.datas.push("page="+url_matching[1]);
     return true;
    }
    
    
    var pattern_rub_pagin = new RegExp(this.PATTERN_URL_PRESSE_STR + "/([0-9]+)/([a-zA-Z\-]+)([0-9]+).html$", "i");
    url_matching = this.url.match(pattern_rub_pagin);
    if(url_matching){
      this.datas.push("page="+url_matching[3]);
      this.datas.push("rubrique="+url_matching[1]);
      return true;
    }
    
    return false;
  };
  
  
})();
// end Class UrlRewritingManager
