/* easytoggle2.js
   - Simon Willison, 5th November 2003
   - See http://simon.incutio.com/archive/2003/11/06/easytoggle
*/

et_addEvent(window, 'load', et_init);

var et_toggleElements = [];
var et_toggleLinks = [];

/* Initialisation */
/* Modified:   - Doug Evenhouse, November 19, 2005
   1) to recognize the 'default' class and show the default panel first
   2) to parse the current page url and set the default panel if one has been asked for 
      via the hash - for example: http://blah.com/html/customercare.html#ccc-panel
   Note: if neither is provided, no panel will be displayed at all
*/
function et_init() {
    var i, link, id, target, first;
    first = false;

    dfltpanel = window.location.href.split('#')[1];
    if (typeof dfltpanel == 'undefined' ) {
        dfltpanel = '';
    }

    for (i = 0; (link = document.links[i]); i++) {
        if (/\btoggle\b/.exec(link.className)) {
            et_toggleLinks[et_toggleLinks.length] = link;
            id = link.href.split('#')[1];
            target = document.getElementById(id);
            et_toggleElements[et_toggleElements.length] = target;
            // alert(target.id + ' ' + dfltpanel);
            if (dfltpanel != '') {
                if (target.id == dfltpanel) {
                    first = true;
                }
            } else {
                if (/\bdefault\b/.exec(link.className)) {
                    first = true;
                }   
            }          
            if (first) {
                first = false;
                /* Add the 'current' class to the link so we know it is selected */
                juggleClass(link, 'current', 1);
            } else {
                target.style.display = 'none';
            }
            link.onclick = et_toggle;
        }
    }
}

function et_toggle(e) {
    /* Adapted from http://www.quirksmode.org/js/events_properties.html */
    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;
    }
    /* For most browsers, targ would now be a link element; Safari however
       returns a text node so we need to check the node type to make sure */
    if (source.nodeType == 3) {
        source = source.parentNode;
    }
    var id = source.href.split('#')[1];
    var elem;
    for (var i = 0; (elem = et_toggleElements[i]); i++) {
        if (elem.id != id) {
            elem.style.display = 'none';
            /* Remove the 'current' class from the link */
            juggleClass(et_toggleLinks[i], 'current', 0);
        } else {
            elem.style.display = 'block';
            /* Add the 'current' class to the link so we know it is selected (if it's not already there)*/
            if (!(checkClass(et_toggleLinks[i], 'current'))) {
               juggleClass(et_toggleLinks[i], 'current', 1);
            }
        }
    }
    return false;
}

/* Thanks to Chris Heilmann for these functions to manage classes
   http://www.evolt.org/article/A_touch_of_class_skinable_Javascript/17/60326/ */
/* checkClass checks if the class c is one of the classes of the object o. */
function checkClass(o,c)
{
 var isClassInObj=o.className.indexOf(c)!=-1?true:false;
 return isClassInObj;
}
/* juggleClass adds the class c to the object o when s is 1, otherwise it removes the class c from the object o. */
function juggleClass(o,c,s)
{
 o.className=s==1?o.className+' '+c:o.className.replace(' '+c,'');	
}

/* Thanks to Scott Andrew */
function et_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;
    }
}