
var selectedArea = 0;
var colourNames = {};

function selectArea (i) {
	if (selectedArea >= 0) {
		var tr = document.getElementById ('area' + selectedArea + 'Tr');
		tr.className = '';
	}
	var tr = document.getElementById ('area' + i + 'Tr');
	tr.className = 'areaSelected';
	selectedArea = i;
}

function selectColour (name, rgb) {

	if (selectedArea < 0) return;

	var colourTd = document.getElementById ('area' + selectedArea + 'ColourTd');
	colourTd.style.background = rgb;

	var colourNameTd = document.getElementById ('area' + selectedArea + 'ColourNameTd');
	var text = document.createTextNode (name);
	colourNameTd.replaceChild (text, colourNameTd.firstChild);

	colours [selectedArea] = rgb.substr (1);
	colourNames [selectedArea] = name;
	doColours ();
}

function doColours () {
	var s = '000000';
	for (var i = 0; i < 8; i++)
		s = s + colours [i];
	document.painter.viz (s);
}

var inDrag = false, boxOffX, boxOffY, moveX, moveY;

function boxDown (e) {
	inDrag = true;
	var box = document.getElementById ('box');
	boxOffX = e.pageX - box.offsetLeft;
	boxOffY = e.pageY - box.offsetTop;
	setTimeout ("boxUpdate ();", 250);
}

function boxMove (e) {
	if (! inDrag) return;
	moveX = e.pageX - boxOffX;
	moveY = e.pageY - boxOffY;
	//document.box.moveTo (10, 10);
}

function boxUp (e) {
	inDrag = false;
	//var box = document.getElementById ('box');
	//box.moveTo (e.pageX, e.pageY);
}

function boxUpdate () {
	if (! inDrag) return;
	var box = document.getElementById ('box');
	box.style.top = '' + moveY + 'px';
	box.style.left = '' + moveX + 'px';
	setTimeout ("boxUpdate()", 250);
}

function setImageName (name, i) {
	var imageName = document.getElementById ('imageName');
	imageName.innerHTML = name;
	imageName.style.marginLeft = (i*109) + 'px';
	imageName.style.borderBottom = '1px solid #f58113';
}

function unsetImageName () {
	var imageName = document.getElementById ('imageName');
	imageName.innerHTML = '&nbsp;';
	imageName.style.borderBottom = '1px solid #ffffff';
}

function writePrintPage (doc, img, width, height, rels, cols, mask) {
	doc.write (
		'<html>\n' +
		'<head>\n' +
		'<title>Prominent Paints Visualiser</title>\n' +
		'</head>\n' +
		'<link rel="stylesheet" type="text/css" href="vis.css"/>\n' +
		'<body>\n' +
		'<div class="printDiv">\n' +
		'<p><img src="prom.jpg"/></p>\n' +
		'<p class="printHeading">Your chosen colour scheme</p>\n');
	writeApplet (doc, img, width, height, rels, cols, mask);
	doc.write (
		'<table class="printLower" border="0" cellspacing="0" cellpadding="0">\n' +
		'<tr><td class="printAreas">\n');
	writeColours (doc);
	doc.write (
		'</td><td width="100"/><td class="printDetails">\n' +
		'<p>Please visit your nearest Prominent Paints stockist and consult our colour cards to view your chosen colours.</p>\n' +
		'<p>To locate a stockist in your area go to \'find us\' or contact our customer care centre:<br/>\n' +
		'(011) 389-4700<br/>\n' +
		'or email:<br/>\n' +
		'callcentre@prominentpaints.co.za</p>\n' +
		'</td><td width="100"/></tr>\n' +
		'</table>\n' +
		'</div>\n' +
		'</body>\n' +
		'</html>\n');
}

function writeColours (doc) {
	doc.write (
		'<table class="printSwatches" border="0" cellspacing="0" cellpadding="0">\n');
	for (var i = 0; i < acts.length; i++) {
		doc.write (
			'<tr>\n' +
			'<td class="printSwatch0" style="color:#' + colours [i] + '">&#103;</td>\n' +
			'<td class="printSwatch1">' + areas [i] + '</td>\n' +
			'<td class="printSwatch2">' + colourNames [i] + '</td>\n' +
			'</tr>\n');
		/*doc.write (
			'<table class="printColour" border="0" cellspacing="0" cellpadding="0">\n' +
			'<tr><td height="15"/></tr>\n' +
			'<tr><td class="printColour0" style="background: #' + colours [i] + ';"><img src="topLeft.gif"/></td></tr>\n' +
			'<tr><td class="printColour1">' + areas [i] + '<br/>' + colourNames [i] + '</td></tr>\n' +
			'</table>\n');*/
	}
	doc.write (
		'</table>\n');
	doc.write (
		'<p class="printWarning">\n' +
		'Due to screen resolution and clarity, the colours chosen are meant as\n' +
		'a guide only and are not a true indication of the exact paint colours.\n' +
		'Please consult our colour cards for a more accurate reflection of the\n' +
		'paint colour.</p>\n');
}

function writeApplet (doc, img, width, height, cols, acts, mask) {
	doc.write (
		'<applet name="painter"  code="imageRepaint.class" width="' + width + '" height="' + height + '">\n' +
		'<param name="img" value="' + img + '">\n' +
		'<param name="highlight" value="brighter30"/>\n');
	for (var i = 0; i < acts.length; i++) {
		doc.write (
			'<param name="rel' + (i+1) + '" value="' + cols [i] + '"/>\n' +
			'<param name="act' + (i+1) + '" value="' + acts [i] + '"/>\n');
	}
	for (var i = 0; i < mask.length; i++) {
		doc.write (
			'<param name="d' + maskIdx [i] + '" value="' + mask [i] + '"/>\n');
	}
	doc.write ('</applet>\n');
}

function goPrint (img, width, height) {
	document.open ();
	writePrintPage (document, img, width, height, colours, acts, mask);
	document.close ();
}

var palSelected = -1;

function palButtonOver (i) {
	var palButton = document.getElementById ('palButton_' + i);
	palButton.className = 'palButtonHover';
}

function palButtonOut (i) {
	var palButton = document.getElementById ('palButton_' + i);
	if (palSelected == i) {
		palButton.className = 'palButtonSelected';
	} else {
		palButton.className = 'palButton';
	}
}

function palButtonClick (i) {
	if (i == palSelected)
		return;

	var palDiv = document.getElementById ('palDiv');
	palDiv.innerHTML = '';

	var palButton;
	if (palSelected >= 0) {
		palButton = document.getElementById ('palButton_' + palSelected);
		palButton.className = 'palButton';
	}
	palSelected = i;
	palButton = document.getElementById ('palButton_' + palSelected);
	palButton.className = 'palButtonSelected';
	
	var s;
	switch (i) {
	case 0: s = palMulti (palData ['ncs2']); break;
	case 1: s = palNormal(palData['prom']); break;
	case 2: s = palMulti(palData['PX1']); break;
	case 3: s = palNormal(palData['Praktik1']); break;
	case 4: s = palMulti(palData['Balakryl1']); break;
	case 5: s = palMulti(palData['PXi']); break;
	}
	palDiv.innerHTML = s;
}

function palMulti (dat) {
	var s = '';
	for (var i in dat) {
		d = dat [i];
		s += '<p class="palHead">' + d [0] + '</p>\n';
		for (var j = 1; j < d.length; j++) {
			e = d [j];
			s += '<div class="pal" style="background: ' + e [0] + '"' +
				' onclick="selectColour (\'' + e [1] + '\',\'' + e[0] + '\')"></div>\n';
		}
	}
	return s;
}

function palNormal (dat) {
	var s = '';
	for (var i in dat) {
		d = dat [i];
		s += '<div class="pal" style="background: ' + d [0] + '"' +
				' onclick="selectColour (\'' + d [1] + '\',\'' + d[0] + '\')"></div>\n';
	}
	return s;
}
