//
// Global bugColorPicker management -- BCP (Bugimus Color Picker)
//

bugBase.prototype.attachBCP = function() {
	this.BCPnum = 0
	this.BCPobj = new Array()
	this.BCPselected = 0
	
	this.BCPcolorRange = new Array(
		"#000", "#003", "#006", "#009", "#00C", "#00F", "#030", "#033", "#036", "#039", "#03C", "#03F",
		"#060", "#063", "#066", "#069", "#06C", "#06F", "#090", "#093", "#096", "#099", "#09C", "#09F", 
		"#0C0", "#0C3", "#0C6", "#0C9", "#0CC", "#0CF", "#0F0", "#0F3", "#0F6", "#0F9", "#0FC", "#0FF", 
		"#300", "#303", "#306", "#309", "#30C", "#30F", "#330", "#333", "#336", "#339", "#33C", "#33F", 
		"#360", "#363", "#366", "#369", "#36C", "#36F", "#390", "#393", "#396", "#399", "#39C", "#39F", 
		"#3C0", "#3C3", "#3C6", "#3C9", "#3CC", "#3CF", "#3F0", "#3F3", "#3F6", "#3F9", "#3FC", "#3FF", 
		"#600", "#603", "#606", "#609", "#60C", "#60F", "#630", "#633", "#636", "#639", "#63C", "#63F", 
		"#660", "#663", "#666", "#669", "#66C", "#66F", "#690", "#693", "#696", "#699", "#69C", "#69F", 
		"#6C0", "#6C3", "#6C6", "#6C9", "#6CC", "#6CF", "#6F0", "#6F3", "#6F6", "#6F9", "#6FC", "#6FF", 
		"#900", "#903", "#906", "#909", "#90C", "#90F", "#930", "#933", "#936", "#939", "#93C", "#93F", 
		"#960", "#963", "#966", "#969", "#96C", "#96F", "#990", "#993", "#996", "#999", "#99C", "#99F", 
		"#9C0", "#9C3", "#9C6", "#9C9", "#9CC", "#9CF", "#9F0", "#9F3", "#9F6", "#9F9", "#9FC", "#9FF", 
		"#C00", "#C03", "#C06", "#C09", "#C0C", "#C0F", "#C30", "#C33", "#C36", "#C39", "#C3C", "#C3F", 
		"#C60", "#C63", "#C66", "#C69", "#C6C", "#C6F", "#C90", "#C93", "#C96", "#C99", "#C9C", "#C9F", 
		"#CC0", "#CC3", "#CC6", "#CC9", "#CCC", "#CCF", "#CF0", "#CF3", "#CF6", "#CF9", "#CFC", "#CFF", 
		"#F00", "#F03", "#F06", "#F09", "#F0C", "#F0F", "#F30", "#F33", "#F36", "#F39", "#F3C", "#F3F", 
		"#F60", "#F63", "#F66", "#F69", "#F6C", "#F6F", "#F90", "#F93", "#F96", "#F99", "#F9C", "#F9F", 
		"#FC0", "#FC3", "#FC6", "#FC9", "#FCC", "#FCF", "#FF0", "#FF3", "#FF6", "#FF9", "#FFC", "#FFF" );

}
window.bugimus.attachBCP()

//
// Local bugSwarm functionality
//

// Define colorpicker object
function colorPicker(x,y,ref) {
	this.objname = "window.bugimus.BCPobj[" + window.bugimus.BCPnum + "]"
	this.objnum = window.bugimus.BCPnum
	window.bugimus.BCPobj[window.bugimus.BCPnum++] = this

	dbg = "#ccc"	// div bkgnd color
	pdg = 3			// div padding
	dbs = 2			// div border size
	allspace = 2*pdg + 2*dbs

	rows = 12		// table rows
	cols = 18		// table columns
	tdw = 6			// table td width
	tdh = 10		// table td height
	tcs = 1			// table cell spacing
	tbg = "#000"	// table bkgnd color

	w = cols*(tdw+tcs) + tcs 
	h = rows*(tdh+tcs) + tcs + 2*(20)

//nodrag	this.bddobj = new dragMe(x,y,w,h,dbg)
//nodrag	this.bddobj.obj.BCPnum = this.objnum

	// Improve drag window border style
//nodrag	with(this.bddobj.obj) {
//nodrag		style.backgroundColor = dbg
//nodrag		style.border = "outset "+dbs+"px #fff"
//nodrag		style.padding = pdg+"px"
//nodrag		style.overflow = "hidden"
//nodrag		clip(0, getw()+allspace, geth()+allspace, 0)
//nodrag	}

//	this.bddobj.obj.style.border = "solid 1px " + tbg
//	this.bddobj.obj.style.padding = pdg + "px"

	// create container for color picker goodies
//	document.body.appendChild( this.container = document.createElement("DIV"))
	ref.appendChild( this.container = document.createElement("DIV"))
	this.container.objnum = this.objnum
	this.container.setx = setoX
	this.container.sety = setoY
	this.container.setw = setoWidth
	this.container.seth = setoHeight

	// pretty up the container div
	with(this.container) {
//		style.position = "absolute"
		style.backgroundColor = dbg
		style.border = "outset "+dbs+"px #fff"
		style.padding = pdg+"px"
	}

//	this.container.setx(x)
//	this.container.sety(y)
	this.container.setw(w)
	this.container.seth(h)
	
	// create table element with properties
	tableObj = document.createElement("TABLE")
	with(tableObj) {
		cellSpacing = tcs
		cellPadding=0
		border=0
		width = w
	}
	tableObj.style.backgroundColor = tbg
	// create tbody and append to table
	tbodyObj = document.createElement("TBODY")
	tableObj.appendChild(tbodyObj)
	// create td element with properties
	tdObj = document.createElement("TD")
	with(tdObj.style) {
		width = tdw
		height = tdh
	}

	// create each tr and append tds according to row col count,
	// as each cell is cloned set to next colorRange value
	var colorIndex=0
	for(j=0; j<rows; j++) {
		trObj = document.createElement("TR")
		for(i=0; i<cols; i++) {
			trObj.appendChild( cellObj = tdObj.cloneNode(true) )
			cellObj.style.backgroundColor = bugimus.BCPcolorRange[colorIndex++]
		}
		tbodyObj.appendChild( trObj.cloneNode(true) )
	}
	// attach completed table to dragMe object
//nodrag	this.bddobj.obj.appendChild( tableObj.cloneNode(true) )
	// attach completed table to container div

	this.container.appendChild( tableObj.cloneNode(true) )

	// attach function to each table cell for color viewing
	for(j=0; j<rows; j++) {
		for(i=0; i<cols; i++) {
			with(this.container.childNodes[0].childNodes[0]) {
				childNodes[j].childNodes[i].onmouseover = function() { 
					bugimus.BCPobj[bugimus.BCPselected].viewColor(this) 
				}
				childNodes[j].childNodes[i].onmousedown = function() { 
					bugimus.BCPobj[bugimus.BCPselected].setColor(this) 
				}
			}
		}
	}

	this.container.appendChild( this.colorViewObj = document.createElement("DIV") )
	with(this.colorViewObj.style) {
		backgroundColor = "transparent"
		border="solid 1px " + tbg
		width = w - 2*tcs
		height = 15 + "px"
		marginTop = 3 + "px"
		textAlign = "center"
	}
	this.colorViewObj.appendChild( document.createTextNode("- - -") )

	this.container.appendChild( this.colorSetObj = this.colorViewObj.cloneNode(true) )

	this.container.onmouseover = function() {
		bugimus.BCPselected = this.objnum
	}

}

colorPicker.prototype.viewColor = function(obj) { 
	cpColor = obj.style.backgroundColor
	this.colorViewObj.style.backgroundColor = cpColor
	this.colorViewObj.style.color = BCU_makeLegible(BCU_threeToSix(cpColor))
	this.colorViewObj.childNodes[0].nodeValue = BCU_threeToSix(cpColor).toUpperCase()
}

colorPicker.prototype.setColor = function(obj) { 
	cpColor = obj.style.backgroundColor
	this.colorSetObj.style.backgroundColor = cpColor
	this.colorSetObj.style.color = BCU_makeLegible(BCU_threeToSix(cpColor))
	this.colorSetObj.childNodes[0].nodeValue = BCU_threeToSix(cpColor).toUpperCase()
	// call external function for this event, usually to set items to chosen color
	if(this.externalCall) setTimeout(this.externalCall, 0)
}

colorPicker.prototype.getColor = function() { 
	return this.colorSetObj.style.backgroundColor
}

colorPicker.prototype.setCallOnSet = function(funcname) { 
	this.externalCall = funcname
}




