function colorscale(hexstr, scalefactor) {
/* declared variables first, in order;
  afterwards, undeclared local variables */
  var r = scalefactor;
  var a, i;
  if (r < 0 || typeof(hexstr) != 'string')
    return hexstr;
    hexstr = hexstr.replace(/[^0-9a-f]+/ig, '');
    if (hexstr.length == 3) {
    a = hexstr.split('');
  } else if (hexstr.length == 6) {
    a = hexstr.match(/(\w{2})/g);
  } else
    return hexstr;
  for (i=0; i<a.length; i++) {
    if (a[i].length == 2)
      a[i] = parseInt(a[i], 16);
    else {
      a[i] = parseInt(a[i], 16);
      a[i] = a[i]*16 + a[i];
  }
}

var maxColor = parseInt('ff', 16);

function relsize(a) {
  if (a == maxColor)
  return Infinity;
  return a/(maxColor-a);
}

function relsizeinv(y) {
  if (y == Infinity)
  return maxColor;
  return maxColor*y/(1+y);
}

for (i=0; i<a.length; i++) {
  a[i] = relsizeinv(relsize(a[i])*r);
  a[i] = Math.floor(a[i]).toString(16);
  if (a[i].length == 1)
  a[i] = '0' + a[i];
}
return a.join('');
}

function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function toRGBHex(num)
{
var num = num.replace("rgb(", "");
num = num.replace(")", "");
var decToHex="";
var arr = new Array();
var numStr = new String();
numStr = num;

arr = numStr.split(",");

for(var i=0;i<3;i++)
{
var hexArray = new Array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" );
var code1 = Math.floor(arr[i] / 16);var code2 = arr[i] - code1 * 16;
decToHex += hexArray[code1];
decToHex += hexArray[code2];
}
return (decToHex);
}

function isHex(entry){
validChar='0123456789ABCDEF';   // legal chars
strlen=entry.length;       // test string length
if(strlen<1){return false;}
entry=entry.toUpperCase(); // case insensitive
// Now scan for illegal characters
for(idx=0;idx<strlen;idx++){
  if(validChar.indexOf(entry.charAt(idx))<0){
    return false;}
  } // end scanning
return true;}

var Chooser = Class.create({
	defaults: function() {
		var defaultColour = $('header').getStyle('backgroundColor');
		$('header-choose').setStyle({backgroundColor: defaultColour});
	},

  	header: function(colour) {
  		var colour = (isHex(colour)) ? colour : toRGBHex(colour);
		$('header').setStyle({backgroundColor: '#' + colour + ' !important'});
    	$('header-choose').setStyle({backgroundColor: '#' + colour + ' !important'});
    	var darkColour = colorscale(colour, 0.6);
    	$('container').setStyle({borderColor: '#' + darkColour + ' !important'});
    	$('footer').setStyle({backgroundColor: '#' + darkColour + ' !important'});
    	
    	if($('ithfmon').innerHTML == 1){
    		$('ithfm').setStyle({borderColor: '#' + darkColour + ' !important'});
    	}
    	
    	new Ajax.Request('/ajax.php/chooser/' + colour + '/' + darkColour, {method: 'get'});
  	}
});

var Chooser = new Chooser();