/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* coca::coca.js                                                            */
/* 'The DHTML Color Calculator' javascript source                           */
/* Copyright (c) 2002 Sebastian B�hin <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.                            */
/*                                                                          */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* requires                                                                 */
/*   coca::wait.js                                                          */
/*   coca::convert.js                                                       */
/*   coca::forms.js                                                         */
/*   coca::show.js                                                          */
/*   coca::colorset.js                                                      */
/*   coca::colormap.js                                                      */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

// ---------------------------------------------------------------------------
// 'The DHTML Color Calculator' main code
// ---------------------------------------------------------------------------
// globals

var tm = 0;
var loaded = false;
var waitfor;
var holdmsg = 'Please hold while loading...';
var hsb, rgb, cmy; // TripleForm
var rgbshow; // ColorShow
var colormap; // ColorMap
var barshow; // BarShow
var numofbars;
var hexform, setsform;
var sets; // ColorSet[]
var numofsets;
var currentset;
var increasedelay, increasestep;
var defaultcolors = new Array('#ffffff', '#ff0000', '#00ff00', '#0000ff');
var defaultstatus = 'The DHTML Color Calculator';

var perform = false;

// ---------------------------------------------------------------------------
// init
window.defaultStatus = holdmsg;
window.onload = init;

function init() {
	numofsets = 4;
	numofbars = 18;
	increasedelay = 1;
	increasestep = 2;
	hsb = new TripleForm('HSB', 'hsb', 360, true, 100, false, 100, false);
	rgb = new TripleForm('RGB', 'rgb', 255, false, 255, false, 255, false);
	cmy = new TripleForm('CMY', 'cmy', 255, false, 255, false, 255, false);
	barshow = new BarShow('bar', numofbars);
	colormap = new ColorMap('map', 24, 8);
	rgbshow = new ColorShow('rgbshow');
	hexform = new FormObject('HEX');
	sets = new Array(numofsets);
	for (var i = 0; i < numofsets; i++)
		sets[i] = new ColorSet('crect'+i, defaultcolors[i]);
	waitfor = new WaitObject('waitfor');
	setcurrent(0);
	setstatus(true);
	tm = setTimeout('waituntilloaded()', 1000);
}

function waituntilloaded() {
	clearTimeout(tm);
	if (!document.all || document.body) {
		tm = setTimeout('start()', 500);
		return;
	}
	loaded = false;
	tm = setTimeout('waituntilloaded()', 500);
}

function start() {
	clearTimeout(tm);
	var d = new Date();
	// document.LogForm.submit();
	loaded = true;
	window.defaultStatus = defaultstatus;
	waitfor.show(false);
}

// ---------------------------------------------------------------------------
// status

function setstatus(def) {
	var st = def || sets[currentset].undolength() == 0 ? defaultstatus :
		'undo buffer contains '+sets[currentset].undolength()+' colors.';
	window.status = st;
}

// ---------------------------------------------------------------------------
// update functions

function updatebyHSB(save) {
	var v = hsb2rgb(hsb.get());
	rgb.put(v);
	cmy.put(invertcolor(v));
	update(rgb2hex(v), save);
}

function updatebyRGB(save) {
	var v = rgb.get();
	hsb.put(rgb2hsb(v));
	cmy.put(invertcolor(v));
	update(rgb2hex(v), save);
}

function updatebyCMY(save) {
	var v = invertcolor(cmy.get());
	hsb.put(rgb2hsb(v));
	rgb.put(v);
	update(rgb2hex(v), save);
}

function updatebyHEX(save) {
	var v = hex2rgb(hexform.get(0));
	hsb.put(rgb2hsb(v));
	rgb.put(v);
	cmy.put(invertcolor(v));
	update(hexform.get(0), save);
}

function update(h, save) {
	hexform.put(0, h);
	rgbshow.update(h);
	barshow.update(h);
	colormap.update(hsb.get()[0]);
	bodycolor(h);
	sets[currentset].update(h, save);
}

// ---------------------------------------------------------------------------
// events

function resetclicked() {
	if (!loaded) return false;
	for (var i = numofsets - 1; i >= 0; i--) {
		sets[i].update(defaultcolors[i], true);
		setcurrent(i);
		document.Sets.elements[i].checked = true;
	}
}

function undoclicked() {
	if (!loaded) return false;
	if (sets[currentset].undo()) {
		initset();
		setstatus(false);
	}
	else window.status = 'undo buffer is empty!';
}

function redoclicked() {
	if (!loaded) return false;
	if (sets[currentset].redo()) {
		initset();
		setstatus(false);
	}
	else window.status = 'redo buffer is empty!';
}

function currentclicked(s) {
	if (!loaded) return false;
	setcurrent(s);
}

function mergewithclicked(s) {
	if (!loaded) return;
	if (s == currentset) return;
	var mby = document.Merge.mergeby.selectedIndex;
	var t = Math.round(document.Merge.mergeby.length/2);
	var f = mby < t ? 1/(t - mby) : mby > t ? mby - t + 1 : 0;
	sets[currentset].update(
		mergecolors(sets[currentset].color, sets[s].color, f), true);
	initset();
}

function invertrgbclicked() {
	if (!loaded) return;
	rgb.put(invertcolor(rgb.get()));
	updatebyRGB(true);
}

function inverthueclicked() {
	if (!loaded) return;
	var v = hsb.get();
	hsb.put(new Array((v[0] + 180) % 360, v[1], v[2]));
	updatebyHSB(true);
}

function areaclicked(s, t) {
	if (!loaded) return;
	hsb.put(colormap.hsb(s, t));
	updatebyHSB(true);
}

function areamover(s, t) {
	if (!loaded) return false;
	window.status = colormap.status(s, t);
	return true;
}

function barclicked(i) {
	if (!loaded) return false;
	hexform.put(0, barshow.get(i));
	updatebyHEX(true);
}

function barmover(b) {
	if (!loaded) return false;
	window.status = barshow.status(b);
	return true;
}

function hexchanged() {
	if (!loaded) return;
	var v = hex2rgb(hexform.get(0));
	if (!v) {
		hexform.put(0, rgb2hex(rgb.get()));
		return;
	}
	updatebyHEX(true);
}

// ---------------------------------------------------------------------------
// sets

function setcurrent(s) {
	currentset = s;
	initset();
	with (document) {
		Sets.elements[currentset].checked = true;
		Controls.current.selectedIndex = currentset;
		Merge.mergewith.selectedIndex = (currentset + 1) % numofsets;
	}
}

function initset() {
	var v = hex2rgb(sets[currentset].color);
	rgb.put(v);
	hsb.put(rgb2hsb(v));
	cmy.put(invertcolor(v));
	update(sets[currentset].color, false);
}

// ---------------------------------------------------------------------------

function settitle(id, title) {
	var elem = document.getElementById ?
		document.getElementById(id) :
		document.all ? document.all[id] : null;
	if (elem == null) return;
	elem.title = title;
}

function bodycolor(h) {
	if (document.getElementsByTagName) {
		document.getElementsByTagName('body')[0].style.backgroundColor = h;
	}
	else document.bgColor = h;
}

function dummy() {} // href-dummy needed by Opera :-/
