

/*
 ** gestion des listes déroulantes
 **
 */
drop_down_manager = {};

drop_down_manager.init = function(){
  this.dropDownValue = null;
  
  drop_down_manager.tab_drop_down = new Array();

  $(".drop_down_list").each(function(index,elt){
      drop_down_manager.tab_drop_down.push(new DropDownList($(this), index));
  });

  for(var i=0; i < drop_down_manager.tab_drop_down.length;i++){
    drop_down_manager.tab_drop_down[i].titre.click(function(){
        drop_down_manager.check_open_dropdown($(this).attr("id"));
    });

    drop_down_manager.tab_drop_down[i].button_down.click(function(){
        drop_down_manager.check_open_dropdown($(this).prev().attr("id"));
    });
  }
};

/*
 * vérifier si une liste déroulante n'est pas déja ouverte
 */
drop_down_manager.check_open_dropdown = function(indice_elt){
  var indice = parseInt(indice_elt.match('[0-9]+'),10);
  for(var i = 0; i < drop_down_manager.tab_drop_down.length; i++){
    if(i != indice && drop_down_manager.tab_drop_down[i].list_down){
      drop_down_manager.tab_drop_down[i].setSlideUp(drop_down_manager.tab_drop_down[i]);
    }
  }
};


/*
 *
 *  Class DropDownList
 *
 */
(function(){

  DropDownList = function(drop_down_elt, indice){

   this.current_value = "";
   this.list_down = false;
   
   this.form = $(drop_down_elt).parent();
    var id = "drop_down_"+Math.ceil(Math.random()*(new Date().getTime())).toString();

    this.callback = null;
    
    this.dropdown = $('<div class="custom_select" id="'+id+'">');
    this.dropdown.append($('<div class="custom_select_limit">'));
    this.titre = $('<div class="custom_select_selected" id="titre_'+indice+'" ></div>')
    this.dropdown.append(this.titre);
    this.button_down = $('<div class="custom_select_btn">')
    this.dropdown.append(this.button_down);
    this.dropdown.append($('<div class="custom_select_limit">'));
    this.input_field = $('<input type="hidden" value="'+drop_down_elt.val()+'" id="'+drop_down_elt.attr("name")+'" name="'+drop_down_elt.attr("name")+'" />');
    
    this.dropDownValue = drop_down_elt.val();
    
    this.content = $('<div class="custom_select_content">');
    var listing = $("<ul>");
    var selected_titre = "";
    var selected_value = "";
   $(drop_down_elt).children().each(function(index, elt){
      listing.append('<li id="custom_select_content_'+$(this).val()+'">'+$(this).text()+'</li>');
      if($(this).attr("selected")){
        selected_titre = $(this).text();
        selected_value = $(this).val();
      }
   });

   this.titre.text(selected_titre);
   this.current_value = selected_value;
   
   this.listing = listing;
   this.content.append(listing);
   this.dropdown.append(this.content);
   $(this.dropdown).replaceAll($(drop_down_elt));
   this.dropdown.append(this.input_field);
   this.initListener(this);
  };


/*
 *  ajout des écouteurs
*/
  DropDownList.prototype.initListener = function(obj){
    obj.button_down.click(function(evt){
       evt.preventDefault();
       evt.stopPropagation();
       
       if(obj.list_down){
         obj.setSlideUp(obj);
       }else{
         obj.setSlideDown(obj);
      }
    });

   obj.titre.click(function(evt){
       evt.preventDefault();
       evt.stopPropagation();

       if(obj.list_down){
         obj.setSlideUp(obj); 
       }else{
         obj.setSlideDown(obj);
      }
    });

    obj.listing.children().each(function(index, elt){
      $(this).click(function(evt){
       evt.preventDefault();
       evt.stopPropagation();

        var value = $(this).attr("id").replace("custom_select_content_","");
        obj.input_field.val(value);
        obj.dropDownValue = value;
        obj.titre.text($(this).text());
        obj.list_down = false;
        obj.content.slideUp("fast", function(){
        //  if(value != obj.current_value){
            obj.form.submit();
         // }
        });
          if(obj.callback){ 
           obj.callback(obj);
         }
      });
    });
  };


  /*
   * liste qui descend
   */
  DropDownList.prototype.setSlideDown = function(obj){
     obj.dropdown.css({"z-index":"9999"});
     obj.list_down = true;
     obj.content.slideDown();
     $("body, window").click(function(evt){
        obj.setSlideUp(obj);
     });
  };


  /*
   * liste qui remonte
   */
  DropDownList.prototype.setSlideUp = function(obj){
     obj.list_down = false;
     obj.content.slideUp("fast", function(){
        obj.dropdown.css({"z-index":"99"});
     });
  //   $("body").unbind("click");
  };


  DropDownList.prototype.setCallback = function(callback){
    this.callback = callback;
  };



  DropDownList.prototype.getValue = function(){
    var value = this.dropDownValue;

      return value;
  };


  DropDownList.prototype.deleteDropDown = function(){
      this.listing.children().unbind("click");
      this.button_down.unbind("click");
      this.titre.unbind("click");
  };

})();
// end Class DropDownList
