// COLORBAR.JS V3.3
// Re-Created & Developed for SimpleMachineForums (SMF) by Karlbenson aka (Smack!) <URL:http://www.youposted.com/>
// Originally based on a PHPBB hack by mat <mat100.AT.ifrance.com>


// VARIABLES NOT ALREADY SET IN PHP
var cpm = 26; // Colors Per Matrice

// ALTERNATES BETWEEN THE ALT (HOVER TEXT)
function alt_text(something) {

	if (usecolor == 1 && usebgcolor == 1) {
		var button = document.images['colorbtn'];
		if (button.src == cbid + "fontcolor.gif") {
			something.title = insert + " [color="+ something.bgColor + "]" + yourtexthere + "[/color]";
		} else {
			something.title = insert + " [bgcolor="+ something.bgColor + "]" + yourtexthere + "[/bgcolor]";
		}
	} else {
		if (usecolor==1) {
			something.title =  insert + " [color="+ something.bgColor + "]" + yourtexthere + "[/color]";
		} else {
			something.title =  insert + " [bgcolor="+ something.bgColor + "]" + yourtexthere + "[/bgcolor]";
		}
	}
}

// SWITCHES THE BUTTONS
function color_switch(something) {
	if (something.src == cbid + "fontcolor.gif") {
		something.src = cbid + "bgcolor.gif";
		something.alt = changetoinsertcolor;
		something.title = changetoinsertcolor;
	} else {
		something.src = cbid + "fontcolor.gif";
		something.alt = changetoinsertbgcolor ;
		something.title = changetoinsertbgcolor ;
	}
}

// INSERTS THE COLOR/BGCOLOR TAGS
function color(hexcolor) {

	if (usecolor == 1 && usebgcolor == 1) {
		var button = document.images['colorbtn'];
		if (button.src == cbid + "fontcolor.gif") {
			surroundText('[color='+ hexcolor + ']', '[/color]', document.forms.postmodify.message);
		} else {
			surroundText('[bgcolor='+ hexcolor + ']', '[/bgcolor]', document.forms.postmodify.message);
		}
	} else {
		if (usecolor==1) {
			surroundText('[color='+ hexcolor + ']', '[/color]', document.forms.postmodify.message);
		} else {
			surroundText('[bgcolor='+ hexcolor + ']', '[/bgcolor]', document.forms.postmodify.message);
		}
	}
}

// FUNCTIONS TO TURN DECIMALS TO HEX
var base_hexa = "0123456789ABCDEF";

function dec2Hexa(number) {
	return base_hexa.charAt(Math.floor(number / 16)) + base_hexa.charAt(number % 16);
}

function RGB2Hexa(TR,TG,TB) {
	return "#" + dec2Hexa(TR) + dec2Hexa(TG) + dec2Hexa(TB);
}

// THE COLOR MATRICES
col = new Array;
col[0] = new Array(255,-1,255,-1,255,-1); // greyscale: white to black
col[1] = new Array(0,1,0,0,0,0); // black to red
col[2] = new Array(255,0,0,1,0,0); //red fading to yellow
col[3] = new Array(255,-1,255,0,0,0); //yellow fading to green 
col[4] = new Array(0,0,255,0,0,1); //green fading to light blue 
col[5] = new Array(0,0,255,-1,255,0); //light blue fading to blue 
col[6] = new Array(0,1,0,0,255,0); //blue fading to purple
col[7] = new Array(255,0,0,1,255,0); //purple/violet/pink to white

// CREATES ALL THE RGB COLORS
function rgb(cpm){
	for (j=0;j<8;j++){
		for (i=0;i<cpm+1;i++) {
			r = Math.floor(col[j][0]+col[j][1]*i*(255)/cpm);
			g = Math.floor(col[j][2]+col[j][3]*i*(255)/cpm);
			b = Math.floor(col[j][4]+col[j][5]*i*(255)/cpm);
			codehex = r + '' + g + '' + b;
  			document.write('<td bgColor="' + RGB2Hexa(r,g,b) + '" onClick="color(this.bgColor);'
			+ 'document.getElementById(\'colorused\').bgColor = this.bgColor;" onmouseover="alt_text(this);'
			+ 'document.getElementById(\'colorused1\').bgColor=this.bgColor;this.style.cursor=\'pointer\'" '
			+ 'style="height:18px;width:3px" class="colorcell"><!-- Space --></td>');
		}
	}
}

// WRITES THE COLOR BAR TABLE
/* outer table - overall container controlling width */
document.write('<table cellspacing="0" width="450px" cellpadding="0" class="colorbar"><tr><td>')
/* switch between color/bg */

if (usebgcolor==1 && usecolor==1) {
	document.write('<td><a href="javascript:void(0);"><img name="colorbtn" onmouseover="bbc_highlight(this, true);" onclick="color_switch(this);document.forms.postmodify.message.focus;return false;" onmouseout="if (window.bbc_highlight) bbc_highlight(this, false);" src="'+ cbid + 'fontcolor.gif" align="middle" width="23px" height="22px" alt="' + switchbetween + '" title="' + switchbetween + '" style="background-image: url(' + cbid + '/bbc_bg.gif); margin: 0px 2px 0px 0px;" /></a></td>')
}
/* hover + click boxes */
document.write('<td id="colorused" onClick="color(this.bgColor);"  bgColor="#FFFFFF" onMouseOver="this.style.cursor=\'pointer\'" style="border-color:#808080;border-width:1px;border-style:solid;width:12px;height:12px;" class="colorbox">')
document.write('<img src="'+ cbid + 'blank.gif" style="width:18px;height:18px;" class="colorboxspacer" alt="' + insertlastclicked + '"/></td><td width="1px;" style="overflow:hidden;font-size:6px;">&nbsp;</td>')
document.write('<td id="colorused1" bgColor="#000000" onClick="color(this.bgColor);" onMouseOver="this.style.cursor=\'pointer\'" style="border-color:#808080;border-width:1px;border-style:solid;width:12px;height:12px;" class="colorbox">')
document.write('<img src="'+ cbid + 'blank.gif" style="width:18px;height:18px;" class="colorboxspacer"  alt="' + insertlasthovered + '" /></td><td width="1px" style="overflow:hidden;font-size:9px;">&nbsp;</td>')
/* colors in the bar */
document.write('<!--' + rgb(cpm) + '// -->')
document.write('</tr></table>')