
var eButton = {

	lnkCount: 0,

	stateMax: 20,

	initState: false,

	speed: [40, 20], // [ on-off , off-on ]

	color_bg: [ [,,] , [,,] ],

	//TEXT colors
	color_fg_off: '#000', //RGB OFF
	color_fg_on: '#000',  //RGB ON

	iTimerIDs: [],
	iLinkStates: [],
	lnkName: [],
	lnkUrl: [],
	bIsColorizing: [],

	init: function() {

		var objBody = document.getElementsByTagName("body").item(0);

		var obj = document.createElement("div");
		obj.setAttribute('id','extra-button-hover');
		obj.style.display = 'none';
		objBody.insertBefore(obj, objBody.firstChild);

		if (eButton.lnkCount > 0) { //some button exist

			var oStyle = getStyle('link0');
			var color = new RGBColor(oStyle.backgroundColor);
			eButton.color_bg[0][0] = color.r;
			eButton.color_bg[0][1] = color.g;
			eButton.color_bg[0][2] = color.b;
			var textcolor = new RGBColor(oStyle.color);
			eButton.color_fg_off = textcolor.toHex();

			var oStyle = getStyle('extra-button-hover');
			var color = new RGBColor(oStyle.backgroundColor);
			eButton.color_bg[1][0] = color.r;
			eButton.color_bg[1][1] = color.g;
			eButton.color_bg[1][2] = color.b;
			var textcolor = new RGBColor(oStyle.color);
			eButton.color_fg_on = textcolor.toHex();

			eButton.initState = true;

			//for (var i = 0; i < eButton.lnkCount; i++) {
			//	_obj('link'+i).style.visibility = '';
			//}

		}

	},

	DecToHex: function( dec ) {

		dec = Math.floor(Math.abs(dec));
		var hex = "";
		var a = "" + dec;
		a = a.length;
		var h = "0123456789ABCDEF";
		for (n=0; n<a; n++){
			he = h.charAt(dec-Math.floor(dec/16)*16);
			dec = (dec - h.indexOf(he)) / 16;
			hex = he + hex;
		}
		if (hex.charAt(0) == "0") hex = hex.substring(1,hex.length);
		return hex;
	},

	toColorHex: function( color ) {
		var s = "#";
		for (var i = 0; i < 3; i++) {
			s += eButton.DecToHex(color[i]);
		}
		return s;
	},

	doColorize: function( onoff, index ) {

		if (eButton.initState != true || !is_defined('eButton')) return;
		clearTimeout(eButton.iTimerIDs[index]);
		if (eButton.iLinkStates[index] > 10) {
			eButton.iLinkStates[index] = 10;
		} else if (eButton.iLinkStates[index] < 0) {
			eButton.iLinkStates[index] = 0;
		} else {
			var diff = eButton.iLinkStates[index] / eButton.stateMax;
			var color = new Array();
			if (onoff == 1) {
				for (var i = 0; i < 3; i++) {
					var colorDiff = diff * (eButton.color_bg[0][i] - eButton.color_bg[1][i]);
					color[i] = eButton.color_bg[0][i] - colorDiff;
				}
				eButton.iLinkStates[index]++;
			} else {
				for (var i = 0; i < 3; i++) {
					var colorDiff = diff * (eButton.color_bg[0][i] - eButton.color_bg[1][i]);
					color[i] = eButton.color_bg[0][i] - colorDiff;
				}
				eButton.iLinkStates[index]--;
			}
			if (eButton.iLinkStates[index] < 8) {
				_obj('link' + index).style.color = eButton.color_fg_off;
			} else {
				_obj('link' + index).style.color = eButton.color_fg_on;
			}
			_obj('link' + index).style.backgroundColor = eButton.toColorHex( color );
			if (eButton.iLinkStates[index] <= 10 && eButton.iLinkStates[index] >= 0) {
				eButton.iTimerIDs[index] = setTimeout('eButton.doColorize(' + onoff + ', ' + index + ')', eButton.speed[ onoff ]);
			} else {
				if (eButton.iLinkStates[index] > 10) {
					eButton.iLinkStates[index] = 10;
				} else if (eButton.iLinkStates[index] < 0) {
					eButton.iLinkStates[index] = 0;
				}
			}
		}
	},

	linkOn: function( numbah ) {
//		_obj('link' + numbah).style.backgroundColor = '#4D5577';
//		_obj('link' + numbah).style.color = '#F7FBFD';
		eButton.doColorize(1, numbah);
	},

	linkOff: function( numbah ) {
//		_obj('link' + numbah).style.backgroundColor = '#F7FBFD';
//		_obj('link' + numbah).style.color = '#4D5577';
		eButton.doColorize(0, numbah);
	},

	linkGo: function( numbah ) {
		document.location = eButton.lnkUrl[ numbah ];
	},

	add: function( name, url ) {

		eButton.lnkName[ eButton.lnkCount ] = name;
		eButton.lnkUrl[ eButton.lnkCount ] = url;
		eButton.iTimerIDs[ eButton.lnkCount ] = 0;
		eButton.iLinkStates[ eButton.lnkCount ] = 0;
		eButton.bIsColorizing[ eButton.lnkCount ] = false;
		eButton.lnkCount++;

	},

	putHorizontal: function( cc ) {

		document.write("<table class=\"extra-button\">\n");
		document.write("	<tr>\n");
		for (var i = eButton.lnkCount-cc; i < eButton.lnkCount; i++) {
			document.write("<td id=\"link" + i + "\" onmouseover=\"eButton.linkOn('" + i + "');\" onmouseout=\"eButton.linkOff('" + i + "');\" onmousedown=\"eButton.linkGo('" + i + "')\" >" + eButton.lnkName[i] + "</td>\n");
		}
		document.write("	</tr>\n");
		document.write("</table>\n");

	},

	putVertical: function ( cc ) {

		document.write("<table class=\"extra-button\">\n");
		for (var i = eButton.lnkCount-cc; i < eButton.lnkCount; i++) {
			document.write("<tr><td id=\"link" + i + "\" onmouseover=\"eButton.linkOn('" + i + "');\" onmouseout=\"eButton.linkOff('" + i + "');\" onmousedown=\"eButton.linkGo('" + i + "')\" >" + eButton.lnkName[i] + "</td></tr>\n");
		}
		document.write("</table>\n");

	}

}

addLoadEvent(eButton.init);


/**
 * A class to parse color values
 * @author Stoyan Stefanov <sstoo@gmail.com>
 */
function RGBColor( color_string ) {

    this.ok = false;

    // strip any leading #
    if (color_string.charAt(0) == '#') { // remove # if any
        color_string = color_string.substr(1,6);
    }

    color_string = color_string.replace(/ /g,'');
    color_string = color_string.toLowerCase();

    // array of color definition objects
    var color_defs = [
        {
            re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
            //example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
            process: function (bits){
                return [
                    parseInt(bits[1]),
                    parseInt(bits[2]),
                    parseInt(bits[3])
                ];
            }
        },
        {
            re: /^(\w{2})(\w{2})(\w{2})$/,
            //example: ['#00ff00', '336699'],
            process: function (bits){
                return [
                    parseInt(bits[1], 16),
                    parseInt(bits[2], 16),
                    parseInt(bits[3], 16)
                ];
            }
        },
        {
            re: /^(\w{1})(\w{1})(\w{1})$/,
            //example: ['#fb0', 'f0f'],
            process: function (bits){
                return [
                    parseInt(bits[1] + bits[1], 16),
                    parseInt(bits[2] + bits[2], 16),
                    parseInt(bits[3] + bits[3], 16)
                ];
            }
        }
    ];

    // search through the definitions to find a match
    for (var i = 0; i < color_defs.length; i++) {
        var re = color_defs[i].re;
        var processor = color_defs[i].process;
        var bits = re.exec(color_string);
        if (bits) {
            channels = processor(bits);
            this.r = channels[0];
            this.g = channels[1];
            this.b = channels[2];
            this.ok = true;
        }

    }

    // validate/cleanup values
    this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
    this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
    this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);

    // some getters
    this.toRGB = function () {
        return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
    }

    this.toHex = function () {
        var r = this.r.toString(16);
        var g = this.g.toString(16);
        var b = this.b.toString(16);
        if (r.length == 1) r = '0' + r;
        if (g.length == 1) g = '0' + g;
        if (b.length == 1) b = '0' + b;
        return '#' + r + g + b;
    }

}
