/*   
 * Written April 2000 by David Kim -- KScripts Web Consulting and Design -- unless stated otherwise 
 * Copyright 2000 Kscripts Web Consulting and Design, All rights reserved, please do not copy without consent
 * 
 * Contains a modified version of Flyout menus from the University of Washington Home Page
 * Documentation can be found at
 *     http://www.washington.edu/webinfo/case/flyout/
 */

//menu options
var menuStatus = "on";
var awphd_url = "/";
var expiration_days = 1;
function makeCookie (onoff) {
	expireDate = new Date;
	expireDate.setDate(expireDate.getDate()+expiration_days);
	document.cookie = "menuStatus=" + onoff +";expires="+expireDate.toGMTString()+"; path=" + awphd_url;
}
if (document.cookie != "") {
	menuStatus = document.cookie.split("=")[1]
}	
function menusetting () {
        menuStatus = (menuStatus == "on" ? "off" : "on");
	makeCookie (menuStatus);
	if (document.images) {
		document.images["menustatusimage"].src = (menuStatus == "on" ? menuOff : menuOn);
	}
}
	
function menuimgIn () {
	if (document.images) {
		document.images["menustatusimage"].src = (menuStatus == "on" ? menuOff : menuOn);
	}
}
function menuimgOut () {
	if (document.images) {
		document.images["menustatusimage"].src = menu;
	}		
}

// Reloads window if resized
if(!window.W) {
  window.onresize = resize;
  window.W = window.innerWidth;
  window.H = window.innerHeight;
}

function resize() {
    if (W < window.innerWidth ||
        W > window.innerWidth ||
        H > window.innerHeight ||
        H < window.innerHeight )
    {
        window.history.go(0);
    }
}

//function to get RCWs dependent upon the http://www.leg.wa.gov/wsladm/rcw.htm site search query set up
function getRCW (RCWname) { 
	RCWarray = RCWname.split(".");
	var endstring = /\D\s*$/;
	if (RCWarray.length == 3) {
		var title = RCWarray[0];
	    	var titleend = endstring.test(title);
    		if (!titleend) {
			if (title.length == 1) {title = "%20%20%20" + title + "%20";}
			if (title.length == 2) {title = "%20%20" + title + "%20";}
			if (title.length == 3) {title = "%20" + title + "%20";}
		} else {
			if (title.length == 2) {title = "%20%20%20" + title;}
			if (title.length == 3) {title = "%20%20" + title;}
			if (title.length == 4) {title = "%20" + title;}
		}
		var chapter = RCWarray[1];
	    	var chapterend = endstring.test(chapter);
    		if (!chapterend) {
			if (chapter.length == 1) {chapter = "%20%20" + chapter + "%20";}
			if (chapter.length == 2) {chapter = "%20" + chapter + "%20";}
			if (chapter.length == 3) {chapter = chapter + "%20";}
		} else {
			if (chapter.length == 2) {chapter = "%20%20" + chapter;}
			if (chapter.length == 3) {chapter = "%20" + chapter;}
			if (chapter.length == 4) {chapter = chapter;}
		}
		var section = "." + RCWarray[2];
		var URL =
		"http:\/\/search.leg.wa.gov\/wslrcw\/RCW"
		+ title + "%20TITLE\/RCW" + title
		+ "." + chapter + "%20CHAPTER\/RCW" + title 
		+ "." + chapter + section + ".htm";
	}
	if (RCWarray.length == 2) {
		var title = RCWarray[0];
	    	var titleend = endstring.test(title);
    		if (!titleend) {
			if (title.length == 1) {title = "%20%20%20" + title + "%20";}
			if (title.length == 2) {title = "%20%20" + title + "%20";}
			if (title.length == 3) {title = "%20" + title + "%20";}
		} else {
			if (title.length == 2) {title = "%20%20%20" + title;}
			if (title.length == 3) {title = "%20%20" + title;}
			if (title.length == 4) {title = "%20" + title;}
		}
		chaptersection = RCWarray[1].split("-");
		var chapter = chaptersection[0];
	    	var chapterend = endstring.test(chapter);
    		if (!chapterend) {
			if (chapter.length == 1) {chapter = "%20%20" + chapter + "%20";}
			if (chapter.length == 2) {chapter = "%20" + chapter + "%20";}
			if (chapter.length == 3) {chapter = chapter + "%20";}
		} else {
			if (chapter.length == 2) {chapter = "%20%20" + chapter;}
			if (chapter.length == 3) {chapter = "%20" + chapter;}
			if (chapter.length == 4) {chapter = chapter;}
		}
		if (chaptersection[1]) {
			var section = "-" + chaptersection[1];
			var URL =
			"http:\/\/search.leg.wa.gov\/wslrcw\/RCW"
			+ title + "%20TITLE\/RCW" + title
			+ "." + chapter + "%20CHAPTER\/RCW" + title 
			+ "." + chapter + section + ".htm";
		} else {
			var URL =
			"http:\/\/search.leg.wa.gov\/wslrcw\/RCW"
			+ title + "%20TITLE\/RCW" + title
			+ "." + chapter + "%20CHAPTER\/RCW" + title 
			+ "." + chapter + "%20Chapter.htm";
		}
	}
	if (RCWarray.length == 1) {
		var title = RCWarray[0];
	    	var titleend = endstring.test(title);
    		if (!titleend) {
			if (title.length == 1) {title = "%20%20%20" + title + "%20";}
			if (title.length == 2) {title = "%20%20" + title + "%20";}
			if (title.length == 3) {title = "%20" + title + "%20";}
		} else {
			if (title.length == 2) {title = "%20%20%20" + title;}
			if (title.length == 3) {title = "%20%20" + title;}
			if (title.length == 4) {title = "%20" + title;}
		}
		var URL = 
		"http:\/\/search.leg.wa.gov\/wslrcw\/RCW"
		+ title + "%20TITLE/rcw" + title
		+ "%20%20TITLE/rcw" + title 
		+ "%20%20TITLE.htm";
	}
	var windowsettings = "status=yes,scrollbars=yes,resizable=yes,copyhistory=yes,height=500,width=600";
	var RCWWindow = window.open("", "RCW", windowsettings);
	RCWWindow.focus();
        var HTML = '<\!-- The bottom frame of this page is part of the Washington State Legislature web site: http://www.leg.wa.gov/wsladm/rcw.htm -->\n';
	HTML += '<html><head><title>RCW ' + RCWname + '</title></head>\n';
	HTML += '<frameset border=0 bordercolor="ffffff" rows="115,*">\n';
	HTML += '<frame src="/RCWwindow.htm" scrolling=no name="sub"><frame src="' + URL + '" name="main">\n';
	HTML += '</frameset><NOFRAMES><body><P>This page is designed to be viewed by a browser which supports frames.\n';
	HTML += 'If you are reading this text, your browser is unable to support frames.\n';
	HTML += 'To go to the RCW site click here <a href="' + URL + '">' + RCWname + '</a>\n';
	HTML += '</body></NOFRAMES></html>';
	var HTMLwindow = RCWWindow.document;
	HTMLwindow.open();
	HTMLwindow.write(HTML);
	HTMLwindow.close();
	RCWWindow.document.status = 'RCW ' + RCWname + '   http://www.leg.wa.gov/wsladm/rcw.htm';
}

//  Modified version of Flyout menus from the University of Washington Home Page
var d = document;
var doDHTML = 0;

if (d.layers || d.all) {
    doDHTML = 1;

    initFlyLyr ();
    initToShow ();
    initDelay ();

    d.write ("<style type='text/css'>" +
		".flyout { visibility: hidden; position: absolute; " +
		"left: 0; top: 0; }<\/style>");
}

function makeLayer (id, title) {
    if (! doDHTML)
	return;
 
    var a = arguments;
    var numsp = new Array;
    numsp.max = 0;
    for (var j = 2; j < a.length; ++j) {
	numsp[j] = a[j].length;
	a[j] = a[j].replace (/^ +/, '');
	numsp[j] -= a[j].length;
	numsp.max = Math.max (numsp[j], numsp.max);
    }
    var fd = FlyLyr.defs;
    d.write ("<div id='l_" + id + "' class='flyout'>" +  
		"<table cellpadding='5' cellspacing='0' border='0' background='" + //added background
		fd.background + "' bordercolor='" + fd.border + "'>");
    var divstart = "<tr><td><table cellpadding='1' cellspacing='0' " +
		    "border='0'>\n";  // took out title option
    d.write (divstart);
    for (j = 2; j < a.length; ++j) {
	if (! a[j]) {
	    d.write ("<\/table><\/td><\/tr>" + divstart);
	    continue;
	}
	var args = null;
	var i = numsp[j];
	if (i < numsp.max)
	    args = "colspan=" + (numsp.max - i + 1);
	d.write ("<tr>");
	while (i-- > 0)
	    d.write ("<td><font size='1'>&nbsp;&nbsp;<\/font><\/td>");
	d.write (buildCell (a[j], fd.useclass, args) + "<\/tr>\n");
    }
    d.write ("<\/table><\/td><\/tr><\/table><\/div>\n");
    new FlyLyr (id);
}

function buildCell (str, fc, args) {
    var fileurl = document.location.href;  //added to highlight and take away link to the site in the navbar
    var filename = fileurl.replace (/#.+$/gi, '');
    var filename = filename.replace (/\?.+$/gi, '');
    var filename = filename.replace (/^.+legalmanual\//gi, '');
    var navfile = str.replace(/^.+=/gi, '');
    var navfile = navfile.replace(/^.*\.\.\//gi, '');
    if (navfile == filename && filename != "" && filename != "index.html") {
	str = str.replace(/=.+$/gi, '');
    }
    var retstr = "<td";
    if (args)
	retstr += " " + args;
    var eqpos = str.indexOf ("=");
    if (eqpos > 0)
	retstr += "><a href='" + str.substr (eqpos + 1) + "' class='" + fc + "'>"  //added class for link
        + str.substr (0, eqpos) + "<\/a>";
    else
	retstr += " class='navheading'>" + str; //added navheading class
    return retstr + "<\/td>";
}

function positionLayerNS () {
    var img = this.image;
    var ypos = img.y + img.height;
    this.lyr.moveTo (img.x + img.width - 16, ypos);  //took out layer movement if cut off by window
}

function positionLayer () {
    var img = this.image;
    var xpos = -5;
    var ypos = img.height;  //took out layer movement if cut off by window
    if (FlyLyr.usepos['startX'])
	xpos += FlyLyr.usepos['startX'];
    if (FlyLyr.usepos['startY'])
	ypos += FlyLyr.usepos['startY'];
    for (; img && img.parentElement; img = img.parentElement)
	if (FlyLyr.usepos[img.tagName]) {
	    xpos += img.offsetLeft;
	    ypos += img.offsetTop;
	}
    this.lyr.style.pixelLeft = xpos;
    this.lyr.style.pixelTop = ypos;
    if (this.tbl)
	this.lyr.style.clip = 'rect ( 0px ' + this.tbl.offsetWidth + 'px ' +
				this.tbl.offsetHeight + 'px 0px )';
}

function FlyLyr (id) {
    this.lyr = findObj ('l_' + id);
    if (! d.all) {
        this.lyr.captureEvents (Event.MOUSEOUT | Event.MOUSEOVER);
	this.lyr.bgColor = FlyLyr.defs.background;
    }
    this.lyr.onmouseover = function () {
	FlyLyr.showing.stopdelay ()
	ToShow.stopdelay ();
    };
    this.lyr.onmouseout = function () { mOut () };
    this.id = id;
    if (this.lyr.children && FlyLyr.unmacOffset)
	this.tbl = this.lyr.children[0];
    FlyLyr.lyrs[id] = this;
    for (var a in FlyLyr.defs)
	this[a] = FlyLyr.defs[a];
}

function flyDefs (defs) {
    if (! defs)
	return;
    for (var d in defs)
	FlyLyr.defs[d] = defs[d];
}

function initFlyLyr () {
    FlyLyr.prototype.doHide = function () {
	this.stopdelay ();
	this.realHide ();
	this.image.src = this.outimg;
	FlyLyr.showing = null;
    };
    FlyLyr.prototype.doShow = function () {
	if (! this.image && ! (this.image = findObj (this.id)))
	    return;
	this.position ();
	this.realShow ();
	this.image.src = this.overimg;
	FlyLyr.showing = this;
    };
    FlyLyr.prototype.delayCallback = function () {
	if (this == FlyLyr.showing)
	    this.doHide ();
    };
    FlyLyr.prototype.setdelay = function () {
	this.delay = new Delay (this.timeout, this);
    };
    FlyLyr.prototype.stopdelay = function () {
	if (this.delay)
	    this.delay.stop ();
	return this.delay;
    };

    FlyLyr.lyrs = new Object ();
    FlyLyr.defs = {
	background: '#ffffff',
	titlebackground: '#ffffff',
	border: '#ffffff',
	useclass: 'navheading',
	titleclass: 'navheading',
	overimg: '/default.gif',
	outimg: '/default.gif',
	pause: 0,
	timeout: 500
    }
    FlyLyr.unmacOffset = navigator.platform.indexOf ("Mac") != -1 ? 0 : 16;

    if (d.all) {
    	FlyLyr.prototype.realHide = function () {
    	    this.lyr.style.visibility = 'hidden';
	};
    	FlyLyr.prototype.realShow = function () {
    	    this.lyr.style.visibility = 'visible';
	};
    	FlyLyr.prototype.position = positionLayer;
	var vers = navigator.appVersion.split ("MSIE ");
	vers = vers[vers.length - 1];
	FlyLyr.usepos = {
	    'BODY': 1,
	    'IMG': 1,
	    'TABLE': 1,
	    'TD': 1
	};
	if (parseInt (vers) < 5)
	    FlyLyr.usepos["TR"] = 1;
	else if (! FlyLyr.unmacOffset) {
	    FlyLyr.usepos["startX"] = 8;
	    FlyLyr.usepos["startY"] = 12;
	}
    } else {
    	FlyLyr.prototype.realHide = function () {
    	    this.lyr.visibility = 'hide';
	};
    	FlyLyr.prototype.realShow = function () {
    	    this.lyr.visibility = 'show';
	};
    	FlyLyr.prototype.position = positionLayerNS;
    }
}

function ToShow (lyr) {
    if (! lyr || lyr == FlyLyr.showing)
	return;
    this.lyr = lyr;
    ToShow.stopdelay ();
    ToShow.queued = this;
    this.delay = new Delay (FlyLyr.defs.pause, this);
}

function initToShow () {
    ToShow.stopdelay = function () {
	var q = ToShow.queued;
	if (q) {
	    q.delay.stop ();
	    ToShow.queued = null;
	}
	return q;
    };
    ToShow.prototype.delayCallback = function () {
	ToShow.queued = null;
	if (FlyLyr.showing)
	    FlyLyr.showing.doHide ();
	if (this.lyr)
	    this.lyr.doShow ();
    };
}

function Delay (delay, obj) {
    this.obj = obj;
    var uid = ++Delay.nuid;
    this.timeoutid = setTimeout ('Delay.dispatch (' + uid + ')', delay);
    this.uid = uid;
    Delay.disparr[uid] = this;
}

function initDelay () {
    Delay.prototype.stop = function () {
	clearTimeout (this.timerid);
	delete Delay.disparr[this.uid];
    };
    Delay.dispatch = function (uid) {
	var item = Delay.disparr[uid];
	if (! item)
	    return;
	item.obj.delayCallback ();
	item.stop ();
    };
    Delay.nuid = 0;
    Delay.disparr = new Object;
}

function findObj (n, od) { // was MM_findObj v3.0 from Macromedia Dreamweaver
    var p, i, x;
    if (! od)
	od = d;
    if ((p = n.indexOf ("?")) > 0 && parent.frames.length) {
	od = parent.frames[n.substring (p + 1)].document;
	n = n.substring (0, p);
    }
    if (! (x = od[n]) && d.all)
	x = od.all[n];
    for (i = 0; ! x && i < od.forms.length; i++)
	x = od.forms[i][n];
    for (i = 0; ! x && od.layers && i < od.layers.length; i++)
	x = findObj (n, od.layers[i].document);
    return x;
}

function mIn (id) {
    if (! doDHTML || menuStatus == "off")
    	return;
    var lyr = FlyLyr.lyrs[id];
    if (! lyr)
	return;
    if (lyr == FlyLyr.showing)
	lyr.stopdelay ();
    else
	new ToShow (lyr);
}

function mOut () {
    if (! doDHTML || menuStatus == "off")
	return;
    var lyr = FlyLyr.showing;
    if (! ToShow.stopdelay () && lyr)
	lyr.setdelay ();
}
// end of flyout script from UW homepage

