/**
 * jQuery Expandablelist Plugin
 * 
 * Licensed under The MIT License
 * 
 * @version     0.1
 * @since       01.08.2009
 * @author      Naden Badalgogtapeh <n.b@naden.de>
 * @link        http://www.naden.de/blog Demonstration
 * @link        http://www.naden.de/blog Blogpost about this plugin (german)
 * @license     http://www.opensource.org/licenses/mit-license.php MIT 
 * @package     jQuery Plugins
 * @subpackage  Expandablelist
 */ 
 
(function($) {
/**
 * Usage: $('ul').expandablelist({options});
 *
 * @param options All possible attributes
 *
 * Options:
 * ---------------------------------------
 * NAME:        DEFAULT,  DESCRIPTION,                    POSSIBLE VALUES
 *
 * count:       2,        break after X items,            any number > 0
 * expanded:    false,    state of list expanded or not,  true or false
 * moreText:    'more',   text for the more button,       any string
 * lessText:    'close',  text for the less button,       any string
 * moreClassA:  '',       classname for the more <a>,     any classname
 * lessClassA:  '',       classname for the less <a>,     any classname
 * moreClassLi: '',       classname for the more <li>,    any classname
 * lessClassLi: '',       classname for the less <li>,    any classname
 * onClick:     function(item, action, flag) {},  onclick handler,  any callback function
 *
 */
$.fn.expandablelist = function() {

  var settings = $.extend({}, $.fn.expandablelist.defaults, arguments[0] || {});

  if(settings.count == 0) {
    return;
  }

  // build "more" button
  var more = $('<a href=""></a>').text(settings.moreText).addClass(settings.moreClassA).click(function() {

    settings.onClick(this, 'more', 0);

    // show the itemlist
    $('> li', this.parentNode.parentNode).show();

    // hide the "more" button"
    $(this).parent().hide();
    
    settings.onClick(this, 'more', 1);
    
    return false;
  });

  // add "more" button
  var li = $('<li></li>');
  li.addClass(settings.moreClassLi);
  if(settings.expanded) {
    li.hide();
  }
  $('li:nth('+(settings.count-1)+')', this).after(li);
  $('li:nth('+(settings.count)+')', this).append(more);
  
  // build "less" button
  var less = $('<a href=""></a>').text(settings.lessText).addClass(settings.lessClassA).click(function() {

    settings.onClick(this, 'less', 0);
    
    // show the itemlist
    $('> li', this.parentNode.parentNode).show();

    // hide all "li" elements on from given index under parent
    $('> li', this.parentNode.parentNode).filter(':gt(' + settings.count + ')').hide();

    settings.onClick(this, 'less', 1);
    
    return false;
  });

  // add "less" button
  $(this).append($('<li></li>').addClass(settings.lessClassLi));
  $('li:last', this).append(less);

  // do initial hiding  
  if(!settings.expanded) {
    $('> li', this).filter(':gt(' + settings.count + ')').hide();
  }
}

// default settings
$.fn.expandablelist.defaults = {
  count: 2,
  expanded: false,
  moreText: 'more',
  lessText: 'close',
  onClick: function(item, action, flag) {},
  moreClassA: '',
  lessClassA: '',
  moreClassLi: '',
  lessClassLi: ''
};
  
})(jQuery);