// 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($){

// These three images need to be the same size or IE can't handle it.
var neuimg = "/images/bullet.gif";
var maximg = "/images/expand.gif";
var minimg = "/images/collapse.gif";
var head = "DT";
var sub = "DD";
var imgMaster = $('<img class="fixPNG"/>').attr('src', neuimg); // todo: get alt's to work.

$.expr[':']['hasSubs'] = function(a) { return $(a).subs().length}; // custom selector

$.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){
    me = this;
    this.expand = function(){ show($(head,e).filter(':hasSubs'),true) };
    this.collapse = function(){ show($(head,e).filter(':hasSubs'),false) };

    // add the images for the leaf items (subs that do not contain heads) that do not already have an image
    $(sub, e).filter(function(){return $(head,this).length==0 && $('>img', this).length==0}).each(function(){
      imgMaster.clone().prependTo(this);
    });
    // add images for the headers
    $(head, e).each(function(){imgMaster.clone().prependTo(this)}).
      filter(':hasSubs'). // 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(me.expand));
      $(e).before($('<input type="button" value="Collapse All">').click(me.collapse));
    }

    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(' '); }

