////////////////////////////////////////////////////////////////////////
// stacey.js
//
//  JavaScript routines for collapsing menus based on
//  named objects.
//
//  Some functions and code fragments taken from the Dynamic HTML Guru
//  Resource's Curious Eye tutorial, which states the following:
//
//    "1. You are encouraged to use the code and graphics anyway you
//        want as long as you credit the Dynamic HTML Guru Resource
//        and list our URL (http://www.htmlguru.com) so others may
//        learn."
//
// Richard A. Wells (raw@raw.com)
// Reality And Wonder (http://www.raw.com)
//
// Modification history:
//   2002.05.10.16:57  RAW  initial, test code
//   2002.05.14.16:50  RAW  substantially re-written
//   2002.06.11.13:58  RAW  make showMenu smarter (use the m[][] object)
//   2002.06.12.16:44  RAW  parameterize sC by page
//   2002.09.04.15:06  RAW  shorten function and variable names
//   2002.10.02.16:16  RAW  start adding splitMode support
//   2002.10.21.10:23  RAW  splitMode working;
//                          trying to address Nav 4.x Mac CSS bugs
//
////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////
// Global variables
////////////////////////////////////////////////////////////////////////

/* [From ORA Jscript4 examples]
 * This function parses comma-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in 
 * properties of an object and returns that object.
 */
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);     // Get query string.
    var pairs = query.split(",");                 // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');          // Look for "name=value".
	if (pos == -1) continue;                  // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1);    // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
	// In JavaScript 1.5, use decodeURIComponent() instead of escape()
    }
    return args;                                  // Return the object.
}

// set properties of 'is' to the type of browser we've got
function Is() {
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.omni  = (agent.indexOf('omniweb')!=-1);
    this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.ns2 = (this.ns && (this.major == 2));
    this.ns3 = (this.ns && (this.major == 3));
    this.ns4b = (this.ns && (this.minor < 4.04));
    this.ns4 = (this.ns && ((this.minor > 4.04) && (this.minor < 5)));
    this.ns6 = (this.ns && (this.major > 4));
    this.ie   = (agent.indexOf("msie") != -1);
    this.ie3  = (this.ie && (this.major == 2));
    this.ie4  = (this.ie && (this.major >= 4));
    this.op3 = (agent.indexOf("opera") != -1);
    this.win   = (agent.indexOf("win")!=-1);
    this.mac   = (agent.indexOf("mac")!=-1);
    this.unix  = (agent.indexOf("x11")!=-1);
    this.dom   = document.getElementById;
}

var is = new Is();

// set doc and sty to the right names, based on browser, for looking
// up a style object like this:
//    eval(doc + '.' + objName + sty)
if (is.ns4 || is.omni) {
    doc = "document";
    sty = "";
}
else { // ie4, ns6, etc.
    doc = "document.all";
    sty = ".style";
}

var args = getArgs();

// current<object>name variables
var M = ''; // was currentMenuname
var C = ''; // was currentCopyname
var initialC;
if (args.C) {
    initialC = args.C;
}
else {
    initialC = '';
}
var currentPage = '';

// splitMode - if we're in split mode then menu changes shift us to
// other pages
var splitMode = false;

// winDaughter - daughter window to show notes + copy
var winDaughter = 0;

////////////////////////////////////////////////////////////////////////
// Functions
////////////////////////////////////////////////////////////////////////


function showLayer(layername) {
    var el = eval(layername + "Obj");
    if (document.getElementById) { // DOM-capable
        el.style.visibility = 'visible';
    }
    else { // assume ns4 layers
        el.visibility = 'visible';
    }
}

function hideLayer(layername) {
    var el = eval(layername + "Obj");
    if (document.getElementById) { // DOM-capable
        el.style.visibility = 'hidden';
    }
    else { // assume ns4 layers
        el.visibility = 'hidden';
    }
}

function reallyHideLayer(layername) {
    var el = eval(layername + "Obj");
    if (document.getElementById) { // DOM-capable
        el.style.visibility = 'hidden';
        el.style.left = -1000;
        el.style.top = -1000;
    }
    else { // assume ns4 layers
        el.visibility = 'hidden';
        el.left = -1000;
        el.top = -1000;
    }
}

function sM(menuname, menuonly) { // showMenu
    // only if we aren't already the current menu
    if (M != menuname) {
                // hide previous menu
                if (M.length > 0) hM(M);
                setImageSrc('m' + menuname + 'Obj', menuname, 'i/' + menuname + '.3.gif');
                showLayer('m' + menuname);
                M = menuname;
                if (!menuonly) sC(m[menuname][0], '1');
    }
}

function hM(menuname) {
    setImageSrc('m' + menuname + 'Obj', menuname, 'i/' + menuname + '.1.gif');
    hideLayer('m' + menuname);
    M = '';
}

// showCopy
function sC(copyname, page) {
    // if the daughter window is still open, close it first
    // (to work around Nav 4.7 Mac CSS bug and make main window visible)
    // if (winDaughter && !winDaughter.closed) {
    //    winDaughter.close();
    // }
    if (C != copyname) {
	if (C.length > 0) hideCopy(C, currentPage);
	var targetMenu = cnm[copyname];
	if (splitMode && (targetMenu != M)) {
	    // we have to get over to the target menu page
	    // alert("Need to head over to index_" + targetMenu + ".html");
    	    location = "index_" + targetMenu + ".html?C=" + copyname;
	}
	else {
	    sM(cnm[copyname], 1); // menuonly flag
		setImageSrc('cn' + copyname + 'Obj', copyname, 'i/' + copyname + '.on.gif');
	    showLayer('cn' + (copyname + page));
	}
    }
    else if (currentPage != page) {
	hideLayer('cn' + C + currentPage);
	showLayer('cn' + copyname + page);
    }
    C = copyname;
    currentPage = page;
}

function hideCopy(copyname, page) {
    setImageSrc('cn' + copyname + 'Obj', copyname, 'i/' + copyname + '.off.gif');
    hideLayer('cn' + copyname + page);
    C = '';
    currentPage = '';
}

function setImageSrc(layer, imagename, filename)
{
        if (is.dom || is.omni) {
                // eval(imagename+ '.src = "' + filename + '"');
                document.images[imagename].src = filename;
    } 
        else { // NAV4?
                if (document.layers) { 
            for (i=0; i < document.layers.length; i++) {
                        if (document.layers[i].document.images[imagename]) {
                                        // eval('document.layers["' + layer + '"].document.images["' + imagename + '"].src = "' + filename + '"');
                                document.layers[i].document.images[imagename].src = filename;
                        break;
                        }
                }
            }
    }
}

function daughterwindow(daughterURL, xval, yval){
        winDaughter = window.open(daughterURL, "_daughter", "width="+xval+",height="+yval+",resizable,scrollbars");
        winDaughter.focus();
}

function nameddaughterwindow(daughterURL, xval, yval, winname){
        winDaughter = window.open(daughterURL, winname, "width="+xval+",height="+yval+",resizable=no,scrollbars=no");
        winDaughter.focus();
}

// Macromedia image functions
function sir() { //v3.0 of MM_swapImgRestore
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function si() { //v3.0 of MM_swapImage
  var i,j=0,x,a=si.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

