/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* coca::colormap.js                                                        */
/* 'The DHTML Color Calculator' javascript source                           */
/* Copyright (c) 2002 Sebastian Böthin <boethin@math.fu-berlin.de>          */
/*                                                                          */
/* Permission to use, copy, and distribute for non-commercial purposes,     */
/* is hereby granted without fee, providing that the above copyright        */ 
/* notice appear in all copies and that both the copyright notice and       */
/* this permission notice appear in supporting documentation.               */
/*                                                                          */
/* This software is provided "as is" without any expressed or implied       */
/* warranty. The author shall not be liable for any damages suffered by     */
/* users of this software. USE AT YOUR OWN RISK.                            */
/*                                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

// ---------------------------------------------------------------------------
// ImageDiv
function ImageDiv(id, nest) {
	nest = (!nest) ? '': 'document.'+nest+'.'; // needed by NS 4.x
	this.object = document.all ? document.all[id] :
		document.getElementById ? document.getElementById(id) :
		eval(nest+'document.'+id);
	this.css = this.object.style ? this.object.style :
		eval(nest+'document.layers.'+id);
	this.css.visibility = 'visible';
	this.show = ImageDiv_show;
	return this;
}

function ImageDiv_show(v) {
	this.css.zIndex = v ? 20 : 10;
}

// ---------------------------------------------------------------------------
// ColorMap
function ColorMap(id, hues, sats) {
	this.id = id;
	this.hues = hues;
	this.sats = sats;
	this.pos = -1;
	this.divs = new Array(hues);
	for (var i = 0; i < hues; i++) this.divs[i] = new ImageDiv(id+i, id);
	this.update = ColorMap_update;
	this.hsb = ColorMap_hsb;
	this.status = ColorMap_status;
	return this;
}

function ColorMap_update(hue) {
	var a = Math.round(this.hues*hue/360) % this.hues;
	if (a == this.pos) return;
	var old = this.pos;
	this.pos = a;
	this.divs[this.pos].show(true);
	if (old >= 0) this.divs[old].show(false);
}

function ColorMap_hsb(s, t) {
	return new Array(
		Math.round((this.pos + t)*360/this.hues) % 360,
		Math.round((s + 1)*100/this.sats), 100);
}

function ColorMap_status(s, t) {
	var v = this.hsb(s, t);
	return 'hue: '+v[0]+'; saturation: '+v[1];
}
