
jQuery.fn.tree = function(options) {

  if(options === undefined || options === null) options = {};
  var open_char = "<img src='/images/minus.png' style='border:none' alt='collapse' title='collapse' />";
  var close_char = "<img src='/images/plus.png' style='border:none' alt='expand' title='expand' />";
  	  
  for(var i = 0; i < this.length; i++) {
    if(this[i].tagName === 'UL') {
      // Make a tree
      jQuery(this[i]).find('li').has('ul').prepend('<span class="close" style="cursor:pointer;">' + close_char + '</span>');
      jQuery(this[i]).find('ul').hide();
      // Click event
      jQuery(this[i]).find('span').live('click', {tree : this[i]}, function(e) {
        if (jQuery(this).attr('class') == 'open') {
          jQuery(this).parent().children('ul').hide('slow');
          jQuery(this).attr('class', 'close');
          jQuery(this).html(close_char);
        } else if (jQuery(this).attr('class') == 'close') {
          jQuery(this).parent().children('ul').show('slow');
          jQuery(this).attr('class', 'open');
          jQuery(this).html(open_char);
        }
      });
    }
  }
}
