// Color Gradient (31-Mar-2006)
// by Vic Phillips http://www.vicsjavascripts.org.uk
// modifief by Petrov Sergey (20-Fev-2007)

// Progessively Change Text Color between two colors.

// Application Notes

// The script would normally be activated by a MouseOver event
// e.g.
// <span style="color:#000000;background-color:#FFFFFF"
// onmouseover="CngColor(this,['#000000','#FFFFFF']);"
// onmouseout="CngColor(this);"
// > Some Text </span>

// where zxcCngColor(*Object*,*GroupID*,'*Color1*','*Color2*');" :
// *Object* = the object or unique ID name of the element to change (object or sting)
// *GroupID* = the unique ID of group element to change
// '*Color*' = an array defining the initial & finish text colors (array or null if not required)
// field 0 = the HEX value of the initial text color (string HEX value)
// field 1 = the HEX value of the finish text color (string HEX value)

//
// The mouseout call CngColor(*Object*); will reverse the color Change

// All variable, function etc. names are prefixed with 'zxc' to minimise conflicts with other JavaScripts
// These charactors are easily changed to charactors of choise using global find and replace.

// Tested with IE6 and Mozilla FireFox

// Functional Code - No Need to Change

var zxcsteps = 20;	// Количество переход от цвета к цвету
var zxcpause;	// Пауза между переходами
var zxcpause_in = 1;	// Пауза между переходами в прямую сторону
var zxcpause_out = 20;	// Пауза между переходами в обратную сторону

var zxcColCnt=0;

function CngColor(zxcobj,zxcgroup,zxccol){
	if (typeof(zxcobj)=='string') {
		zxcobj=document.getElementById(zxcobj);
	}
	if (typeof(zxcgroup)=='number') {
		for (i = 0; i < array.length; i++) {
            if(!array[i]) return;
			if (array[i][0] == zxcgroup) {
				//alert(array[i]);
				id = 'i'+i;
				zxcobj = document.getElementById(id);

				if (!zxccol) {
				    zxcobj.style.borderBottom = "none";
                    zxcpause = zxcpause_out;
                }
				else {
				    if (array[i][1] == 0) zxcobj.style.borderBottom = "2px solid "+zxccol[1];
				    else zxcobj.style.borderBottom = "2px dashed "+zxccol[1];
                    
                    zxcpause = zxcpause_in;
                }
                
				if (!zxcobj.cngcolor) {
					zxcobj.cngcolor= new zxcOOPCol(zxcobj,zxccol);
					zxcobj.colcng=false;
				}
				clearTimeout(zxcobj.cngcolor.to)
				if (zxcobj.colcng){
					zxcobj.colcng=false;
				} else {
					zxcobj.colcng=true;
				}
				zxcobj.cngcolor.changeColor();
			}
		}
	} else {
		if (!zxcobj.cngcolor) {
			zxcobj.cngcolor= new zxcOOPCol(zxcobj,zxccol);
			zxcobj.colcng=false;
		}
		clearTimeout(zxcobj.cngcolor.to)
		if (zxcobj.colcng){
			zxcobj.colcng=false;
		} else {
			zxcobj.colcng=true;
		}
		zxcobj.cngcolor.changeColor();
	}
}

function zxcOOPCol(zxcobj,zxccol){
	this.obj=zxcobj;
	this.ref = 'zxccngcol'+zxcColCnt;
	window[this.ref]=this;
	this.to=null;
	this.cnt=0;
	if (zxccol){
		this.colary=new Array(zxcsteps);
		zxcGradientColors(zxccol[0],zxccol[1]||zxccol[0],this.colary);
	}
	zxcColCnt++;
}

zxcOOPCol.prototype.setTimeOut= function(zxcf,zxcd){
	this.to=setTimeout("window."+this.ref+"."+zxcf,zxcd);
}

zxcOOPCol.prototype.changeColor=function(){
	if (this.colary){
		this.obj.style.color=this.colary[this.cnt];
	}
	if (this.obj.colcng){
		this.cnt++;
	} else {
		this.cnt--;
	}
	if ((this.obj.colcng&&this.cnt<this.colary.length)||(!this.obj.colcng&&this.cnt>0)){
		this.setTimeOut("changeColor();", zxcpause);
	}
}


function zxcGradientColors(zxcc1,zxcc2,zxcary) {
	zxclen=zxcary.length;
	zxcc1=zxcc1.replace('#','');
	zxcc2=zxcc2.replace('#','');

	var zxcr=zxcHexToInt(zxcc1.substring(0,2));
	var zxcg=zxcHexToInt(zxcc1.substring(2,4));
	var zxcb=zxcHexToInt(zxcc1.substring(4,6));
	var zxcr2=zxcHexToInt(zxcc2.substring(0,2));
	var zxcg2=zxcHexToInt(zxcc2.substring(2,4));
	var zxcb2=zxcHexToInt(zxcc2.substring(4,6));
	var zxcrstep=Math.round((zxcr2-zxcr)/zxclen);
	var zxcgstep=Math.round((zxcg2-zxcg)/zxclen);
	var zxcbstep=Math.round((zxcb2-zxcb)/zxclen);
	for (zxc0=0;zxc0<zxclen-1;zxc0++){
		zxcary[zxc0]="#"+zxcIntToHex(zxcr)+zxcIntToHex(zxcg)+zxcIntToHex(zxcb);
		zxcr+=zxcrstep; zxcg+=zxcgstep; zxcb+=zxcbstep;
	}
	zxcary[zxclen-1]='#'+zxcc2;
}

function zxcIntToHex(zxcn){
	if (zxcn>255){
		zxcn=255;
	}
	var zxcresult=zxcn.toString(16);
	if (zxcresult.length==1){
		zxcresult="0"+zxcresult;
	}
	return zxcresult;
}

function zxcHexToInt(zxchex){
	return parseInt(zxchex,16);
}
