//-----------------------------------------------------------------------------
// Top level UL tag ID attribute
var g_listId = "dList"

// The onload and onunload events are defined in this js file.
// If this script needs to work with others on the page that
// share these events, comment out the next 2 lines and include 
// "dListInit()" and "dListExit()" calls where needed.
//
window.onload = dListInit
window.onunload = dListExit
//-----------------------------------------------------------------------------
// main
function dListInit() {
	parseUL(g_listId,getListCookie("dynamicList"))
}
function dListExit() {
	setListCookie("dynamicList", getState(g_listId), 365)
}
function parseUL(refId, state) {
	var sT = new Array("collapsed-normal", "collapsed-hover", "collapsed-down", "expanded-normal", "expanded-hover", "expanded-down")
	for(k=0; k<document.styleSheets.length; k++) {
		if(document.styleSheets[k].cssRules) {
			styles = document.styleSheets[k].cssRules
		} else {
			styles = document.styleSheets[k].rules
		}
		for(i=0; i<styles.length; i++) {
			for(j=0; j<sT.length; j++) {
				if(styles[i].selectorText.match(sT[j])) {	
					bI = styles[i].style.backgroundImage
					sT[j] = bI.substring(bI.indexOf('(')+1,bI.indexOf(')'))
				}
			}
		}
	}
	refObj = document.getElementById(refId)
 	var entries = refObj.getElementsByTagName("UL")
	if(!state) state=""
	var item = state.split("~")
    for (i=0,c=1; i<entries.length; i++) {
		entries[i].id = refId+"UL"+i
		entries[i].style.display = "block"
		pRef = entries[i].parentNode
		if(pRef.childNodes[0].nodeName == "A") {
			ulText = pRef.childNodes[0].childNodes[0].nodeValue
		} else {
			ulText = pRef.childNodes[0].nodeValue
		}
		divObj = document.createElement("DIV")
		pRef.replaceChild(divObj, pRef.childNodes[0])
		cBtn = new bugButton(divObj , sT[0], sT[1], sT[2])
		cBtn.buttonObj.hide()
		eBtn = new bugButton(divObj , sT[3], sT[4], sT[5])
		var cmd = "toggleUL('"+entries[i].id+"','"+cBtn.buttonObj.id+"','"+eBtn.buttonObj.id+"');"
		cBtn.setCallFunc(cmd)
		eBtn.setCallFunc(cmd)
		divObj.appendChild( aObj=document.createElement("A") )
		aObj.href = "#"
		aObj.appendChild( document.createTextNode(" "+ulText) )
		aObj.onclick = new Function(cmd+"this.blur();return false;")
		if(item[c]!=i) {
			cBtn.buttonObj.show()
			eBtn.buttonObj.hide()
			entries[i].style.display="none"
		} else {
			c++
		}
	}
}
function toggleUL(refId,cId,eId) {
	refObj = document.getElementById(refId)
	cObj = document.getElementById(cId)
	eObj = document.getElementById(eId)
	if(refObj.style.display=="block") {
		refObj.style.display="none"
		cObj.show()
		eObj.hide()
	} else {
		refObj.style.display="block"
		eObj.show()
		cObj.hide()
	}
}
function collapseAll() {
	refObj = document.getElementById(g_listId)
 	var entries = refObj.getElementsByTagName("UL")
    for (i=0; i<entries.length; i++) {
		if(entries[i].style.display == "block") {
			pRef = entries[i].parentNode
			pRef.childNodes[0].childNodes[2].onclick()
		}
	}
}
function expandAll() {
	refObj = document.getElementById(g_listId)
 	var entries = refObj.getElementsByTagName("UL")
    for (i=0; i<entries.length; i++) {
		if(entries[i].style.display == "none") {
			pRef = entries[i].parentNode
			pRef.childNodes[0].childNodes[2].onclick()
		}
	}
}
function getState(refId) {
	refObj = document.getElementById(refId)
    var entries = refObj.getElementsByTagName("UL")
	var state = ""
    for (i=0; i<entries.length; i++) {
		if(entries[i].style.display == "block") {
			state += "~"+i
		}
	}
	return state
}
function setListCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function getListCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null
}
//-----------------------------------------------------------------------------
// bugBase
function bugBase() {}
window.bugimus = new bugBase()
//-----------------------------------------------------------------------------
// bugImageSwap
bugBase.prototype.attachBIS = function() {
	this.BISnum = 0
	this.BISobj = new Array()
}
window.bugimus.attachBIS()
function imageSwapper() {
	this.objname = "window.bugimus.BISobj[" + window.bugimus.BISnum + "]"
	window.bugimus.BISobj[window.bugimus.BISnum++] = this
	this.imageindex = -1
	this.numimages = 0
	this.img = new Array()
}
imageSwapper.prototype.showimage = function( placement, imagenum ) {
	if( imagenum<this.numimages ) document.getElementById(placement).src=this.img[imagenum].src;
	this.imageindex=imagenum;
}
imageSwapper.prototype.loadimage = function( source ) {
	this.img[this.numimages] = new Image()
	this.img[this.numimages].src = source
	this.numimages++
}
//-----------------------------------------------------------------------------
// bugButton
bugBase.prototype.attachBBB = function() {
	this.BBBnum = 0
	this.BBBobj = new Array()
	this.BBB_NORMAL = 0
	this.BBB_HOVER = 1
	this.BBB_DOWN = 2
}
window.bugimus.attachBBB()
function bugButton(refObj, normalImage, hoverImage, downImage) {
	this.objname = "window.bugimus.BBBobj[" + window.bugimus.BBBnum + "]"
	window.bugimus.BBBobj[window.bugimus.BBBnum++] = this
	this.buttonObj = document.createElement("IMG")
	this.buttonObj.id = this.objname
	this.buttonObj.alt = ""
	this.buttonObj.title = ""
	this.buttonObj.src = normalImage
	this.buttonObj.show = function showoDiv() { this.style.display = "" }
	this.buttonObj.hide = function hideoDiv() { this.style.display = "none" }
	refObj.appendChild( this.buttonObj )
	this.bisOver = new imageSwapper()
	this.bisOver.loadimage(normalImage)
	if (hoverImage) {
		this.bisOver.loadimage(hoverImage)
	} else {
		this.bisOver.loadimage(normalImage)
	}
	if (downImage) {
		this.bisOver.loadimage(downImage)
	} else {
		this.bisOver.loadimage(normalImage)
	}
	this.buttonObj.onmouseout = this.buttonObj.onmouseup = function() {
		this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
	}
	this.buttonObj.onmouseover = function() {
		this.parentObj.buttonObj.style.cursor = "pointer"
		this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_HOVER)
	}
	this.buttonObj.onmousedown = function() {
		this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_DOWN)
	}
	this.buttonObj.onmouseup = function() { 
		this.parentObj.bisOver.showimage(this.parentObj.objname, window.bugimus.BBB_NORMAL)
		if(this.parentObj.externalCall) setTimeout(this.parentObj.externalCall, 0)
	}
	this.buttonObj.parentObj = this
}
bugButton.prototype.setCallFunc = function(funcname) { 
	this.externalCall = funcname
}


