/* easytoggle.js, by Simon Willison */

var collapsibles = new Array(); // array to store all of the collapsibles
var to_show = 'home';

/*
function style_links(s) {
     var i, links, link;
     links = document.getElementById('navigation').getElementsByTagName('a');
     for (i = 0; (link = links[i]); i++) {
          link.className = (link==s) ? 'selected' : '';
     }
}
*/

function get_source(e) {
     if (typeof e == 'undefined') {
         var e = window.event;
     }
     var source;
     if (typeof e.target != 'undefined') {
         source = e.target;
     } else if (typeof e.srcElement != 'undefined') {
         source = e.srcElement;
     } else {
         return true;
     }
     if (source.nodeType == 3) { // Safari bug-fix
         source = source.parentNode;
     }
     return source;
}

function go_headings(id, show) {
    var imgs = $$('#'+ id + ' h1,#' + id + ' h2,#' + id + ' h3,#' + id + ' h4');
    imgs.each(
        function(img, j) {
            img.className = show ? '' : 'hidden';
        }
    );
}


function et_toggle(e) {
     if ((source = get_source(e))===true)
          return true;
     //style_links(source);
     var id = source.href.split('#')[1];
     var cl = source.className;
     var show_sub = false;

     var in_sub = source.parentNode.parentNode.parentNode.id;
     if ( (cl=='toggle_sub') || (in_sub=='sub_navigation') ) {
          show_sub = true;
     }

     for (j in collapsibles) {
        if ( ( j!=id ) && (collapsibles[j].slideOut) ) {
            if (j=='sub_navigation') {
                if ( show_sub===true ) {
                    continue;
                }
                else {
                    slide_out(j, 1);
                    continue;
                }
            }
            slide_out(j);
        }
     }

     if ( show_sub===true ) {
          var sub_id = 'sub_navigation';
          if ($(sub_id).className=='hidden')
                slide_in(sub_id, 1);
     }

     if (id!=to_show) {
          reset_form();
          slide_in(id);
     }

     to_show = id;

     return false;
}

function hide(id) {
    $(id).className = 'hidden';
}
function show(id) {
    $(id).className = '';
}

function slide_out(r, no_fade) {
    if (collapsibles[r].slideOut) {
        //alert('sliding out ' + r);
        if (!(no_fade==1)) {
            fade(r, 1, 0);
        }
        collapsibles[r].slideOut();
        //go_headings(r, false);
        setTimeout("hide('"+r+"');", 500);
    }
}

function slide_in(p, no_fade) {
    if (collapsibles[p].slideIn) {
        //go_headings(p, true);
        show(p);
        if (!(no_fade==1)) {
            setTimeout("fade('"+p+"', 0, 1);", 1000);
        }
        setTimeout("collapsibles['"+p+"'].slideIn();", 500);
    }
}

function fade(id, start, end) {
    var elem = $(id);
    var myFx = new Fx.Tween(elem);
    myFx.start('opacity', start, end);
}



function et_init_on_off() {
     var link, id, target;
     link = document.getElementById('player').getElementsByTagName('a')[0];
     id = link.href.split('#')[1];
     target = document.getElementById(id);
     link.onclick = et_toggle_on_off;
     target.style.display = 'none';
}


function et_toggle_on_off(e) {
     var source = get_source(e);
     if (source===true)
          return true;
     var id = source.href.split('#')[1];
     var elem = document.getElementById(id);
     elem.style.display = (elem.style.display=='none') ? 'block' : 'none';
     return false;
}


/* Thanks to Scott Andrew */
function addEvent(obj, evType, fn){
     if (obj.addEventListener) {
         obj.addEventListener(evType, fn, true);
         return true;
     } else if (obj.attachEvent) {
         var r = obj.attachEvent("on"+evType, fn);
         return r;
     } else {
      return false;
     }
}




