/*
	name   : viscolor.js
	date   : 2001 May 13
	author : Amanda Gunther
	www    : http://home.earthlink.net/~redbird77
	email  : redbird77@earthlink.net

	This script is basically the same as multi.js (the script used to make
	multicolor text fades).  It is modified to produce colors for Winamp's
	viscolor.txt.  It also incorporates some of colkit.js.

	Use this script however you want.  Improve upon it, slice it, dice it,
	whatever..., but try to leave the some comments and credits intact.
*/

var m_pos, m_htm = '';

var FIELD_MAX = 16, SPECTRUM_ANALYZER = 0, OSCILLOSCOPE = 1;

function make_viscolor(f)
{
	var c, list, sp = '', s = '';

	f.taResult.value = '';

	// ADD VISUALIZATION BACKGROUND COLOR
	c = hex_to_srgb(f.tBG.value);
	for (var i = 0, sp = ''; i < FIELD_MAX - c.length; i++) sp += ' ';
	s += c + sp + '\/\/ ln0 = background\n';

	// ADD VISUALIZATION DOTS COLOR
	c = hex_to_srgb(f.tDots.value);
	for (var i = 0, sp = ''; i < FIELD_MAX - c.length; i++) sp += ' ';
	s += c + sp + '\/\/ ln1 = background dots\n';

	m_htm = '<table border="0" cellpadding="3" cellspacing="0">\n'

	// ADD SPECTRUM ANALYZER COLORS
	list = (f.oSchemeSA[0].checked) ? f.selPresetsSA.options[f.selPresetsSA.selectedIndex].value : f.tColorsSA.value

	if (!is_valid(list))
	{
		alert('Invalid spectrum analyzer color list.  Could not create complete viscolor.txt.');
		f.tColorsSA.focus();
		return;
	}

	s += multi_viscolor(list, SPECTRUM_ANALYZER)

	// ADD OSCILLOSCOPE COLORS
	list = (f.oSchemeOS[0].checked) ? f.selPresetsOS.options[f.selPresetsOS.selectedIndex].value : f.tColorsOS.value

	if (!is_valid(list))
	{
		alert('Invalid oscilloscope color list.  Could not create complete viscolor.txt.');
		f.tColorsOS.focus();
		return;
	}

	s += multi_viscolor(list, OSCILLOSCOPE)

	m_htm += '<\/table>';

	// ADD SPECTRUM ANALYZER PEAK DOTS COLOR
	c = hex_to_srgb(f.tPeak.value);
	for (var i = 0, sp = ''; i < FIELD_MAX - c.length; i++) sp += ' ';
	s += c + sp + '\/\/ ln23 = analyzer peak dots\n';

	f.taResult.value = s;
}

function multi_viscolor(list, vis_type)
{
	m_htm += '<tr>\n<td colspan="2"><b>' + ((vis_type == SPECTRUM_ANALYZER) ? 'Spectrum Analyzer' : 'Oscilloscope') + '<\/b><\/td>\n<\/tr>\n';

	// would like to pass to two_color by reference, but js only by value so must use a global var
	m_pos = (vis_type == SPECTRUM_ANALYZER) ? 2 : 18;

	var len = (vis_type == SPECTRUM_ANALYZER) ? 16 : 5;

	var offset = m_pos - 1;

	var vis_txt = '', color_count = 0, chunk_count = 0, chunks = new Array();

	var colors = (list.toLowerCase()).split('|');

	// The number of chunks will always be equal to the number of colors - 1.
	// All of the chunks will not be the same size though.  For example, if
	// the len parameter is 58 and you want to use 5 colors, then
	// quotient = 14 and remainder = 2.  The chunks are all initially 14
	// with the remainder, 2, being split between the last 2 chunks.
	var num_chunks = colors.length - 1;

	var quotient  = Math.floor(len / num_chunks);
	var remainder = len % num_chunks;

	for (var i = 0; i < num_chunks; i++) chunks[i] = quotient + (i >= (num_chunks - remainder) ? 1 : 0);

	for (var i = 0; i < len; i += chunks[chunk_count++])
	{
		vis_txt += two_color_viscolor(chunks[chunk_count], colors[color_count], colors[color_count + 1], vis_type, offset);

		color_count++;
	}

	return vis_txt;
}

function two_color_viscolor(len, clr1, clr2, vis_type, offset)
{
	var col_txt, comment = '', vis_txt = '', sp = '';

	if (len == 0) return vis_txt;

	var c1 = hex_to_rgb(clr1), c2 = hex_to_rgb(clr2);

	var inc = new Array();

	// calculate individual increments for red, green and blue
	for (var i = 0; i < 3; i++)
	{
		if (len - 1) inc[i] = (c2[i] - c1[i]) / (len - 1);
		else inc[i] = c2[i];
	}

	for (var i = 0; i < len; i++)
	{
		col_txt = fix(c1[0]) + ',' + fix(c1[1]) + ',' + fix(c1[2]);

		// build table preview
		m_htm += '<tr>\n<td bgcolor="' + rgb_to_hex(c1) + '" height="15" width="35"><br><\/td>\n';
		m_htm += '<td><font color="' + rgb_to_hex(c1) + '">' + col_txt + '<\/font><\/td>\n<\/tr>\n';

		// 255,255,255 = 11 chars max in a 16 char field

		for (var k = 0, sp = ''; k < FIELD_MAX - col_txt.length; k++) sp += ' ';

		comment = sp + '\/\/ ln' + m_pos + ' = ' + ((vis_type == SPECTRUM_ANALYZER) ? 'spec' : 'osc') + ' ' + (m_pos - offset);

		vis_txt += col_txt + comment + '\n';

		m_pos++;

		for (var j = 0; j < 3; j++) c1[j] += inc[j];
	}

	return vis_txt;
}

function is_valid(list)
{
	var c = (list.toLowerCase()).split('|');
	var h = '0123456789abcdef';
	var valid  = true;

	// must have at least 2 colors
	if (c.length < 2) valid = false;

	for (var i = 0; (i < c.length) && valid; i++)
	{
		// if (c[i].search(/(^[\da-f]{6})$/i) < 0) valid = false
		if (c[i].length != 6) valid = false;

		for (var j = 0; j < c[i].length; j++)
			if (h.indexOf(c[i].charAt(j)) < 0) valid = false;
	}

	return valid;
}

function pop_win()
{
	var s = ''

	s += '<html>\n<head>\n<title>Preview<\/title>\n'
	s += '<style type="text\/css">\nbody,td,p{font-family: Verdana, Arial, sans-serif}\n<\/style>\n'
	s += '<\/head>\n<body>\n<p><b>Here is a preview of your Spectrum Analyzer and Oscilloscope.<\/b><\/p>\n' + m_htm
	s += '\n<p><a href="javascript:;" onClick="self.close()">Close this window<\/a><\/p>\n<\/body>\n<\/html>'

	var w = window.open('', 'winPreview', 'width=500,height=500,resizable=yes,scrollbars=yes,screenX=0,screenY=0,top=0,left=0')
	w.document.write(s)
	w.document.close()
}

function fix(num)
{
	if (num < 0) return 0;
	if (num > 255) return 255;

	return Math.floor(num);
}

function rgb_to_hex(c)
{
	var h = (c[0] << 16 | c[1] << 8 | c[2]).toString(16);

	while (h.length < 6) h = '0' + h;

	return '#' + h;
}

// h is always passed as a string here
function hex_to_srgb(h)
{
	h = parseInt(h, 16);
	return (h >> 16) + ',' + ((h >> 8) & 0xff) + ',' + (h & 0xff)
}

function hex_to_rgb(h)
{
	var r;

	h = parseInt(h, 16)

	r = new Array(h >> 16, (h >> 8) & 0xff, h & 0xff)

	return r;
}