//foldopen.js by Elizabeth Marston 11 Jul 2008
//the idea is to fold column items that we don't need.




/* Section 0: Globals, constants and functions I didn't write. */

var DEBUG               =  true;

var FOLD_CLASS          = "fold";
var CONTENT_CLASS       = "fcontent";
var TITLE_CLASS         = "ftitle";
var ANCHOR_CLASS        = "fanchor";
var ANCHOR_CONTENT_U    = "[-]";
var ANCHOR_CONTENT_F    = "[+]";
var HIGHLIGHT_CLASS     = "highlighted";

var KOSHER              =  document.createElement && document.getElementsByTagName;


//from PPK book
function addEventSimple(obj,evt,fn) {
    if (obj.addEventListener) // W3C
        obj.addEventListener(evt,fn,false);
    else if (obj.attachEvent) // Microsoft
        obj.attachEvent('on'+evt,fn);
}


function stopEvent(evnt)
{
    if (evnt.stopPropagation)
    {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }
}

/* Section 1: event handlers and action functions */



function fold_onclick(ns_event)
{
  var the_event = ns_event ? ns_event : window.event; 
  var the_target = the_event.target ? the_event.target : window.event.srcElement;
  var fold = get_fold_from_anchor(the_target);
  if (!KOSHER) return;
  
  
  if (is_folded(fold))
      unfold_fold(fold);
  else
      fold_fold(fold);
      
  stopEvent(the_event);
} ;

function fold_onmouseover(ns_event)
{
  var the_event = ns_event ? ns_event : window.event; 
  var the_target = the_event.target ? the_event.target : window.event.srcElement
  var fold = get_fold_from_anchor(the_target);
  if (!KOSHER) return;

  highlight_fold(fold);
      
};

function fold_onmouseout(ns_event)
{
  var the_event = ns_event ? ns_event : window.event; 
  var the_target = the_event.target ? the_event.target : window.event.srcElement
  var fold = get_fold_from_anchor(the_target);
  
  if (!KOSHER) return;
  
  unhighlight_fold(fold);

};


function highlight_fold(f)
{
      var fs = f.fs;
      fs.old_title_className = fs.title.className;
      fs.title.className    += " " + HIGHLIGHT_CLASS;
}

function unhighlight_fold(f)
{
      var fs = f.fs;
      fs.title.className = fs.old_title_className;
}

    
function fold_fold(f)
{
    var fs = f.fs;
    fs.old_html = fs.content.innerHTML;
    fs.content.innerHTML = '';
    fs.anchor.innerHTML = get_folded_string();
    fs.folded = true;
}

function unfold_fold(f)
{
     var fs = f.fs;
     fs.content.innerHTML = fs.old_html; 
     fs.anchor.innerHTML = get_unfolded_string();
     fs.folded = false;
}

function get_fold_from_anchor(fa)
{
    return fa.parentNode.parentNode;
}


function is_fold(putative)
{
    if (!putative) return false;
    return (putative.className.indexOf(FOLD_CLASS)<0) ? false : true;
}

function is_folded(putative)
{
    if (!putative) return false;
    return putative.fs.folded;
}    

function make_fold_anchor(c)
{
     var fold_a      = document.createElement("a"); 
     fold_a.href       = "javascript:void(0);";
     fold_a.innerHTML  = c;
     fold_a.className   = ANCHOR_CLASS;
     fold_a.onclick = fold_onclick;
     fold_a.onmouseover = fold_onmouseover;
     fold_a.onmouseout = fold_onmouseout;
     
     
     return fold_a;

}

function make_fold_title(anchor)
{
    var fold_title  = document.createElement("div");
    fold_title.className = TITLE_CLASS;
    fold_title.appendChild(anchor);
    return fold_title;
}

function make_fold_content(content_html)
{
    var fold_content= document.createElement("div");

    fold_content.className = CONTENT_CLASS;
    fold_content.innerHTML = content_html;

    return fold_content;
}

function get_folded_string()
{
    return ANCHOR_CONTENT_F;
}

function get_unfolded_string()
{
    return ANCHOR_CONTENT_U;
}

function init_fold(fold)
{
    var fa = make_fold_anchor(get_unfolded_string());
    var ft = make_fold_title(fa);
    var fc = make_fold_content(fold.innerHTML);
    var fs = new Object();
    fold.innerHTML = '';
    fold.appendChild(fc);
    fold.appendChild(ft);
    
    fold.fs = fs;
    fold.fs.anchor = fa;
    fold.fs.title  = ft;
    fold.fs.content= fc;
    fold.fs.folded = false;
    
    fold_fold(fold);
}





function electrify()
{
    if (!KOSHER) return;
    var divs = document.getElementsByTagName("div");
    for (var i=0; i<divs.length; i++)
    {
        if (is_fold(divs[i]))
        {
            init_fold(divs[i]);
        }
    }
            
}

