/* ttree.js

javascript component of ttree config nested fieldset widget
Copyright (c) 2007-2009, JJW  - orig: 4/28/07 sheldon treetest18, ajax & dom hide/show, js incl

Requirements:
  1) JJW version of ajaz 1K DOM library
  2) Behaviour
*/

stat = gE ('statusdiv');
trace = 0;

function gf (n)  // get corresponding fieldset for the input tag (n)
{ return gE ('fs_' + n.id);
}

function log (s)
{ if (trace)
  { stat = gE ('statusdiv');
    stat.innerHTML += s + '<br>';
  }
}

function unchk (n)  // uncheck all INPUT nodes under this one
{ var kids = n.getElementsByTagName ('INPUT');
  var x,i;

  for (i=0; i<kids.length; i++)
  { x = kids [i];

    if (x.nodeType==1 && x.checked)
    { x.checked = false;
      x.defaultChecked = false;
    }
  }
}

function hide (n)  // uncheck INPUT node and hide corresponding fieldset
{ n.checked = false;
  n.defaultChecked = false;
  var f = gf(n)

  if (f)
  { log ('hiding: ' + n.value)
    f.style.display = 'none';
  }

  return f;
}

function hideall (n)  // hide node's corresponding fieldset, and uncheck all nested INPUTs
{ f = hide (n);  // also returns corresponding fieldset, if present

  log ('hideall:' + n.id + ' ' + f)

  if (f)
  { var kids = f.getElementsByTagName ('INPUT');
    var i;

    for (i=0; i<kids.length; i++)
      hide (kids [i])
  }
}

function hp (n)  // hide INPUT radio peers of this node
{ var sibs = d.getElementsByName (n.name);
  log ('sibs.length: ' + sibs.length);

  var x,i;

  for (i=0; i<sibs.length; i++)
  { x = sibs [i]

    log ('sib ' + i + ': ' + x.type + ' ' + x.value + ' ' + x.nodeType);

    if (x.nodeType==1 && x.name == n.name && x.value != n.value)
      hideall (x);
  }
}


var ttree_rules = {
  '.nested': function(e)
  { e.onchange = function()
    { log ('changing: ' + this.id + ' ' + this.type + ' ' + this.checked + ' ' + this.defaultChecked);

      var f = gf (this);

      if (this.checked)
      { if (f)      // presence of fieldset implies nested (!)
          f.style.display = "";
        if (this.type.toLowerCase() == 'radio')
        { hp (this);
          this.defaultChecked = true;  // ischecked works, but requires more state mgmt
        }
      }
      else  // only gets hit for checkboxes
      { log ('calling hideall')
        hideall (this);
      }
    };

    e.onclick = function()
    { log ('clicking: ' + this.id + ' ' + this.type + ' ' + this.checked + ' ' + this.defaultChecked);

      if (this.type.toLowerCase() == 'radio')
        if (this.checked)
          if (this.defaultChecked)
          { this.defaultChecked = false;
            this.checked = false;
            e.onchange();
          }
          else
            this.defaultChecked = true;
        else
          this.defaultChecked = false;
    }
  },

  '.tooltip' : function(e)
  { e.onmouseover = function()
    { T (ajax.gets (this.href));
    };

    e.onmouseout = function()
    { T() }
  },

  '#expandall': function(e)
  { e.onclick = function()
    { tags = gT('fieldset');
      for (i=0; i<tags.length; i++) 
        //this.value += tags [i].nodeName;
        tags [i].style.display = 'none';
    }
  },
}

Behaviour.register (ttree_rules);

Behaviour.addLoadEvent (function() { log ('unchecking all');
                                     unchk ($('ttree'));
                                   })

