/**
 * jqDivList plugin
 * 
 * This plugin simulates a standard HTML select list,
 * with multiple items visible. 
 * 
 * Created By: Shawn Grover (sgrover@open2space.com)
 * Created On: 29 Aug 2009.
 * License:    Creative Commons Attribution-Share Alike 
 *             (http://creativecommons.org/licenses/by-sa/3.0/)
 * 
*/

(function($) {
  $.fn.extend({
    //we use this method to get the value of the selected elements
    divlistValue: function () {
      var o = $(this).data("options");
      var theval = [];
      $(this).children().each( function () {
        if ($(this).hasClass(o.selectedClass)) {
          theval.push($(this).attr("id"));
        }
      });
      return theval;
    }
  });
  
  $.fn.divlist = function (options) {
    var opts = $.extend({}, $.fn.divlist.defaults, options);
    
    return this.each(function() {
      $this = $(this);
      
      //element specific options
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
      
      //store the options for later use
      $this.data("options", o);
      
      /*
       * click handler - toggle the child element "selected"
       * 
       * Note:  The plugin doesn't care about the structure below the plugin's target element.
       *        The clicked element may be immediately below the plugin target, or it may
       *        be nested deep in other tags.  We need to work up the parents of the clicked element
       *        to find the first child of the plugin target that contains the clicked element.
      */
      $this.click( function (e) {
        if (!o.multiselect) {
          $(this).children().removeClass("selected");
        }
        
        if ($(e.target).parent().attr("id") == $this.attr("id")) {
          if ($(e.target).is("." + o.selectedClass)) {
            $(e.target).removeClass(o.selectedClass);
          }
          else {
            $(e.target).addClass(o.selectedClass);
          }
          
          //we do not stop the event handling here in case there is other processing
          //assigned to the child elements.
        }
        else {
          $(e.target).parents().each( function () {
            if ($(this).parent().attr("id") == $this.attr("id")) {
              if ($(this).is("." + o.selectedClass))
                $(this).removeClass("selected");
              else
                $(this).addClass("selected");
              return false;     //return false stops the .each iterations
            }
          });
        }
      });
      
      /*
        * create the .val() method for the plugin target.
        * this will return the id of the selected element(s).
        * A string is returned if only one element is selected.  Otherwise an array is returned.
      */
      $(this).getValues = function () {
        console.log(this);
        return;
        var theval = [];
        $(this).children().each( function () {
          if ($(this).hasClass(o.selectedClass)) {
            theval.push($(this).attr("id"));
          }
        });
        
        if (theval.length == 1) {
          return theval[0];
        }
        else {
          return theval;
        }
      };

      
    });
  };


  /**
   * default values
  */
   $.fn.divlist.defaults = {
    multiselect: true,
    selectedClass: "selected"
  };

})(jQuery);


