// JavaScript Document
HW.Colour = function() {
	if(typeof arguments[0] == 'string' && arguments[0].match(/#[0-9a-fA-F]{3,6}/) && (arguments[0].length == 4 || arguments[0].length == 7)) {
		this.setHex(arguments[0]);
		return;
	}
	else if(arguments.length == 3) {
		this.setRGB.apply(this,arguments);
		return;
	}
	else if(typeof arguments[0] == 'string') {
		this.setString(arguments[0])
		return;
	}
	else if(arguments[0].nodeType == 1) {
		var c = this.bg(arguments[0]);
		if(c.match(/rgb\([0-9 ]+,[0-9 ]+,[0-9 ]+\)/)) {
			c = c.split('rgb(').join('').split(')').join('').split(',');
			this.setRGB.apply(this,c);
		}
		else {
			this.setHex(c);
		}
		return;
	}
}

HW.Colour.prototype = {
	r:0,
	g:0,
	b:0,
	_hex:{
		A:10,B:11,C:12,D:13,E:14,F:15
	},
	_dec:{
		10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'
	},
	setHex:function(hex) {
		hex = hex.replace('#','');
		if(hex.length == 3) {
			var arr = hex.split('');
			var str = '';
			for(var i=0;i<3;i++) {
				str += arr[i]+arr[i];
			}
			this.setHex(str);
			return;
		}
		this.r = this.hex2Dec(hex.substr(0,2));
		this.g = this.hex2Dec(hex.substr(2,2));
		this.b = this.hex2Dec(hex.substr(4,2));
		return this;
	},
	setRGB:function(r,g,b) {
		this.r = r;
		this.g = g;
		this.b = b;
		return this;
	},
	setString:function(str) {
		switch(str.toLowerCase()) {
			case 'red':
				this.setHex('F00');
				break;
			case 'green':
				this.setHex('0F0');
				break;
			case 'blue':
				this.setHex('00F');
				break;
			case 'cyan':
				this.setHex('0FF');
				break;
			case 'magenta':
				this.setHex('F0F');
				break;
			case 'yellow':
				this.setHex('FF0');
				break;
		}
		return this;
	},
	hex2Dec:function(hex) {
		hex = hex.toUpperCase().split('');
		var n = 0;
		for(var i=hex.length-1;i>=0;i--) {
			var m = (isNaN(hex[i])?this._hex[hex[i]]:parseInt(hex[i]))*(Math.pow(16,i));
			n += m;
		}
		return n;
	},
	dec2Hex:function(n,pad) {
		var i = 0,str = [];
		while(n > 0) {
			var mod = n%Math.pow(16,i+1);
			str = (this._dec[mod/Math.pow(16,i)]||mod/Math.pow(16,i)) + str;
			n = n - mod*Math.pow(16,i);
			i++;
		}
		while(str.length < pad) {
			str = '0'+str;
		}
		return str;
	},
	rgb:function() {
		return [this.r,this.g,this.b].join(',');
	},
	hex:function() {
		str = '#';
		str += this.dec2Hex(this.r,2);
		str += this.dec2Hex(this.g,2);
		str += this.dec2Hex(this.b,2);
		return str;
	},
	bg:function(o) {
		if(o.nodeType == 1) {
			if(o.currentStyle) {
				var s = o.currentStyle['backgroundColor'];
			}
			else {
				var s = getComputedStyle(o,'').getPropertyValue('background-color');
			}
			if(!s || s == 'transparent') {
				s = this.bg(o.parentNode);
			}
			return s;
		}
		return '#FFF';
	}
}

HW.Animate.colour = function(c1,c2,t,f,c) {
	var dr,dg,db,r0=c1.r,g0=c1.g,b0=c1.b;
	dr = c2.r - c1.r;
	dg = c2.g - c1.g;
	db = c2.b - c1.b;
	var _f = function(o,v) {
		var col = new HW.Colour(parseInt(r0)+parseInt(v*dr),parseInt(g0)+parseInt(v*dg),parseInt(b0)+parseInt(v*db));
		f(col.hex());
	}
	return new HW.Animator({},0,1,_f,t,c);
}

HW.highlight = function(o,c,t,f0,f1) {
	c = c||'#FF0';
	t = t||1000;
	f0 = f0||500;
	f1 = f1||1000;
	var c0 = new HW.Colour(o);
	var c1 = new HW.Colour(c);
	new HW.Animate.colour(c0,c1,f0,function(v){HW.setStyle(o,{backgroundColor:v});},function(){
		HW.setStyle(o,{backgroundColor:c});
		setTimeout(function(){
			new HW.Animate.colour(c1,c0,f1,function(v){HW.setStyle(o,{backgroundColor:v});},function(){HW.setStyle(o,{backgroundColor:''});});
		},t);
	});
	return o;
}

HW._proto.highlight = function(c,t) {
	return HW.highlight(this,c,t);
}

