// create a folding outline out of nested definition lists, with clickable icons
// Options:
// initial: {String} 'expanded' for fully expanded, 'collapsed' for fully collapsed, 'top' for top-level expanded, rest collapsed. Default: 'top'
// buttons: {Boolean} true to append buttons to expand and collapse the outline. Default: true
// animate: {String|Number|null} the argument to show and hide to set the speed of animation
// Attaches the Outline object to the element, so x=$('dt.whatever').outline()[0] is the object
(function($){

var neuimg = "/images/neutral.png";
var maximg = "/images/maximize.png";
var minimg = "/images/minimize.png";
var head = "DT";
var sub = "DD";
var imgMaster = $.IMG({src: neuimg, className: 'fixPNG'}); // todo: get alt's to work.

$.fn.outline = $.extend(function(opts){
  opts = $.extend({}, arguments.callee['default'], opts);
  return this.each(function(){
    this.outline = new $.fn.outline.Outline(this, opts);
  });
},{ // option sets
  'default': {initial: 'top', buttons: true , animate: null},
  expanded: {initial: 'expanded'},
  collapsed: {initial: 'collapsed'},

  // Constructor
  Outline: function(e, opts){
    // add the images for the leaf items (subs that do not contain heads)
    $(sub, e).filter(function(){return $(head,this).length==0}).each(function(){
      imgMaster.clone().prependTo(this);
    });
    // add images for the headers
    $(head, e).each(function(){imgMaster.clone().prependTo(this)}).
      filter(function(){return $(this).subs().length}). // now get the heads that have children
      each(function(){
        $('> img', this).click(function() {show(this.parentNode,$(this).parent().subs().is(':hidden'))});
        show(this, opts.initial == 'expanded' || opts.initial == 'top' && this.parentNode == e);
      });
    if (opts.buttons){
      $(e).before($.INPUT({type: 'button', value: 'Expand All'}).click(function(){show($(head,e),true)}));
      $(e).before($.INPUT({type: 'button', value: 'Collapse All'}).click(function(){show($(head,e),false)}));
    }

    function show(e, show) {
      if (show){
        $('> img',e).attr({src: minimg, title: 'Collapse'}).css('cursor', 'pointer').end().subs().show(opts.animate);
      }else{
        $('> img',e).attr({src: maximg, title: 'Expand'}).css('cursor', 'pointer').end().subs().hide(opts.animate);
      }
    } // show

  } // constructor
}); // outline

$.fn.subs = function(){
  // find the subs following this
  var ret = [];
  this.each(function(){
    for (var next = this.nextSibling; next != null && next.tagName != head; next = next.nextSibling){
      // only count elements that are in the hierarchy
      if (next.tagName == sub) ret.push (next);
    }
  });
  return $(ret);
} // subs

})(jQuery);

var yi_stl;
if (yi_stl == undefined) yi_stl = {}; // namespace
yi_stl.linkList = function (){ return $.map(document.links, "a.href").join(' '); }

