/***************************************************************************************
        Nested list collapsing script written by Mark Wilton-Jones - 21/11/2003
Version 2.2.0 - this script takes existing HTML nested UL or OL lists, and collapses them
            Updated 13/02/2004 to allow links in root of expanding branch
                  Updated 09/09/2004 to allow state to be saved
          Updated 07/10/2004 to allow page address links to be highlighted
Updated 28/11/2004 to allow you to force expand/collapse links to use just the extraHTML
****************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details and a demo of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use
_________________________________________________________________________
****************************************************************/

var openLists = [], oIcount = 0;
function compactMenu(oID,oAutoCol,oPlMn,oMinimalLink) {
    if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return; }
    var baseElement = document.getElementById( oID ); if( !baseElement ) { return; }
    compactChildren( baseElement, 0, oID, oAutoCol, oPlMn, baseElement.tagName.toUpperCase(), oMinimalLink && oPlMn );
}
function compactChildren( oOb, oLev, oBsID, oCol, oPM, oT, oML ) {
    if( !oLev ) { oBsID = escape(oBsID); if( oCol ) { openLists[oBsID] = []; } }
    for( var x = 0, y = oOb.childNodes; x < y.length; x++ ) { if( y[x].tagName ) {
        //for each immediate LI child
        var theNextUL = y[x].getElementsByTagName( oT )[0];
        if( theNextUL ) {
            //collapse the first UL/OL child
            theNextUL.style.display = 'none';
            //create a link for expanding/collapsing
            var newLink = document.createElement('A');
            newLink.setAttribute( 'href', '#' );
            newLink.onclick = new Function( 'clickSmack(this,' + oLev + ',\'' + oBsID + '\',' + oCol + ',\'' + escape(oT) + '\');return false;' );
            //wrap everything upto the child U/OL in the link
            if( oML ) { var theHTML = ''; } else {
                var theT = y[x].innerHTML.toUpperCase().indexOf('<'+oT);
                var theA = y[x].innerHTML.toUpperCase().indexOf('<A');
                var theHTML = y[x].innerHTML.substr(0, ( theA + 1 && theA < theT ) ? theA : theT );
                while( !y[x].childNodes[0].tagName || ( y[x].childNodes[0].tagName.toUpperCase() != oT && y[x].childNodes[0].tagName.toUpperCase() != 'A' ) ) {
                    y[x].removeChild( y[x].childNodes[0] ); }
            }
            y[x].insertBefore(newLink,y[x].childNodes[0]);
            y[x].childNodes[0].innerHTML = oPM + theHTML.replace(/^\s*|\s*$/g,'');
            theNextUL.MWJuniqueID = oIcount++;
            compactChildren( theNextUL, oLev + 1, oBsID, oCol, oPM, oT, oML );
} } } }
function clickSmack( oThisOb, oLevel, oBsID, oCol, oT ) {
    if( oThisOb.blur ) { oThisOb.blur(); }
    oThisOb = oThisOb.parentNode.getElementsByTagName( unescape(oT) )[0];
    if( oCol ) {
        for( var x = openLists[oBsID].length - 1; x >= oLevel; x-=1 ) { if( openLists[oBsID][x] ) {
            openLists[oBsID][x].style.display = 'none'; if( oLevel != x ) { openLists[oBsID][x] = null; }
        } }
        if( oThisOb == openLists[oBsID][oLevel] ) { openLists[oBsID][oLevel] = null; }
        else { oThisOb.style.display = 'block'; openLists[oBsID][oLevel] = oThisOb; }
    } else { oThisOb.style.display = ( oThisOb.style.display == 'block' ) ? 'none' : 'block'; }
}
function stateToFromStr(oID,oFStr) {
    if( !document.getElementsByTagName || !document.childNodes || !document.createElement ) { return ''; }
    var baseElement = document.getElementById( oID ); if( !baseElement ) { return ''; }
    if( !oFStr && typeof(oFStr) != 'undefined' ) { return ''; } if( oFStr ) { oFStr = oFStr.split(':'); }
    for( var oStr = '', l = baseElement.getElementsByTagName(baseElement.tagName), x = 0; l[x]; x++ ) {
        if( oFStr && MWJisInTheArray( l[x].MWJuniqueID, oFStr ) && l[x].style.display == 'none' ) { l[x].parentNode.getElementsByTagName('a')[0].onclick(); }
        else if( l[x].style.display != 'none' ) { oStr += (oStr?':':'') + l[x].MWJuniqueID; }
    }
    return oStr;
}
function MWJisInTheArray(oNeed,oHay) { for( var i = 0; i < oHay.length; i++ ) { if( oNeed == oHay[i] ) { return true; } } return false; }
function selfLink(oRootElement,oClass,oExpand) {
    if(!document.getElementsByTagName||!document.childNodes) { return; }
    oRootElement = document.getElementById(oRootElement);
    for( var x = 0, y = oRootElement.getElementsByTagName('a'); y[x]; x++ ) {
        // location is populated only by get, phpPostString is made by php into javascript var .... compare post with string, too
        if( y[x].getAttribute('href') && !y[x].href.match(/#$/)   &&  compareRealAddress(oRootElement.id, y[x])  ) {
            y[x].className = (y[x].className?(y[x].className+' '):'') + oClass;
            if( oExpand ) {
                oExpand = false;
                for( var oEl = y[x].parentNode, ulStr = ''; oEl != oRootElement && oEl != document.body; oEl = oEl.parentNode ) {
                    if( oEl.tagName && oEl.tagName == oRootElement.tagName ) { ulStr = oEl.MWJuniqueID + (ulStr?(':'+ulStr):''); } }
                stateToFromStr(oRootElement.id,ulStr);
} } } }

function compareRealAddress(rootElement, oOb) { 
    // global var phpPostStringArr;
    var searchval = '';
    var searchkey = '';
    var testkey = '';
    var found = 0;
    var one = oOb.search;
    var loopcount = 0; // outer loop
    var realcount = 0; // match count
    if (! one)  {
        // alert('ob ' + one);
        return false;  
    }
    // next line: keep from_lang for vocabulary, delete it for read
    if (rootElement == 'rss') {  one = one.replace(/&from_lang=english&to_lang=[A-Za-z]+/,'&from_lang=english'); }
    one = one.replace(/old_cfile=[A-Za-z]*&?/,'');
    one = one.replace(/^\/?index.php\?/,'');
    one = one.replace(/^\?/,'');
    var oneArr = parseQueryString(one);
    // vars I don't care about
    var two = phpPostStr;
    if (! two ) { 
        return false; // are not the same
    } else  {
        two = two.replace(/^\/?index.php\?/,'');
        two = two.replace(/^\?/,'');
        // alert('my thing ' + two);
    }
    var twoArr = parseQueryString(two);
    // vars I don't care about
    for ( searchkey in oneArr ) {
        loopcount = loopcount + 1;
        searchval = oneArr[searchkey];  // simple
        for ( testkey in twoArr ) {
            // alert(loopcount + ") " + searchkey + " " + searchval + ";" + testkey + " " + twoArr[testkey] );
            if (testkey == searchkey) {
                found = 1;
                if (twoArr[testkey] == searchval) { realcount++;  delete twoArr[testkey]; break; }
                else return false;
            }
        }
        if (! found ) return false;  // was in link, is not in post or get
        else found = 0;  // start over for new searchkey
    }
    if (realcount == loopcount) return true;
    else alert('found a pot of gold at the bottom of a rainbow');
}

function parseQueryString ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

