//-----------------------------------------------------------------------------
// Top level TABLE tag ID attribute
var g_tableId = "dTable"
var g_numCols = 3

// 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 
// "dTableInit()" and "dTableExit()" calls where needed.
//
window.onload = dTableInit
//-----------------------------------------------------------------------------
// main
function dTableInit() {
//	testTABLE()

	buildTABLE(g_tableId, 10)
}
function buildTABLE(refId, numRows) {
	if( document.getElementById(refId) ) {
		alert("Table already on page")
	} else {
		document.body.appendChild( tableObj = document.createElement("TABLE") )
		tableObj.id = "dTable"
		tableObj.appendChild(tbodyObj=document.createElement("TBODY"))
	}
    for (var i=0; i<numRows; i++) {
		addRow(refId, g_numCols)
//alert("row "+(i+1)+" of numrows = "+numRows)
	}
}
function testTABLE() {
	document.body.appendChild(tableObj=document.createElement("TABLE"))
	tableObj.id = "dTable"
	tableObj.appendChild(tbodyObj=document.createElement("TBODY"))
	tbodyObj.appendChild(trObj = document.createElement("TR"))
	trObj.appendChild( tdObj = document.createElement("TD") )
	tdObj.appendChild( document.createTextNode("cell") )
}

function addRow(refId, numCell) {
	refObj = document.getElementById(refId)
 	tbodyObj = refObj.getElementsByTagName("TBODY")
	tbodyObj[0].appendChild( trObj = document.createElement("TR") )
//alert(refId+"  "+numCell+" row "+(i+1))
	for(var i=0; i<numCell; i++) {
		trObj.appendChild( tdObj = document.createElement("TD") )
		tdObj.appendChild( document.createTextNode("cell "+(i+1)) )
	}
}

function removeRow(refId, rowNum) {
	refObj = document.getElementById(refId)
 	tbodyObj = refObj.getElementsByTagName("TBODY")
 	var rows = tbodyObj[0].getElementsByTagName("TR")

	if(rows.length) {
		var rowId = rows.length-1
		var cells = rows[rowId].getElementsByTagName("TD")
		tbodyObj[0].removeChild( rows[rowId] );
	}
}


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()
		}
	}
}








//-----------------------------------------------------------------------------
// 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
}


