﻿var r0=g0=b0=r1=g1=b1=0; //start/end of interval
var per=20;
var rev=false;
var h="0123456789ABCDEF";
var backup=new Array();
var mousein;
var bgc;
var bgco;

function HexaColor(i)
{
  i = i%per;
  if (rev==true && (i>per/2)) i = per-i;
  r = r0+Math.round( (r1-r0)/per*i );
  g = g0+Math.round( (g1-g0)/per*i );
  b = b0+Math.round( (b1-b0)/per*i );
  return h[(r-r%16)/16]+h[r%16]+h[(g-g%16)/16]+h[g%16]+h[(b-b%16)/16]+h[b%16];
}

function RandomColor()
{
 c='';
 while(c.length<6)
  c+= h[Math.floor(Math.random()*16)];
  return c;
}

function Colorize(c0,c1,period,repeat,reverse,bgc0,bgco0,bgcm)
{
	bgc=bgc0; /* a background color */
	bgco=bgco0; /* a:hover background color */
	var menus = document.getElementsByClassName('menu_top');
	this.rev = reverse;
	this.per = (period<1)? 1:0+period; //alert(this.per);
	p=0;
	for(var i=0;i<1 /*menus.length*/;i++)
	{
		//alert(i);
		if (bgcm)
			menus[i].style.backgroundColor = '#'+bgc;
		else
			menus[i].style.removeProperty("background-color");
		var ax= menus[i].getElementsByTagName('a');
		for (var j=0;j<ax.length;j++)
		{
			var text = backup[p]; p++; // var text=ax[j].innerHTML;
			var length = text.length;
			var result = '';
			var periodstep=0;
			if (c0=='default' || c1=='default')
			{
				ax[j].innerHTML = text;
				break;
			}
			this.r0= parseInt(c0.substring(0,2),16);
			this.g0= parseInt(c0.substring(2,4),16);
			this.b0= parseInt(c0.substring(4,6),16);
			this.r1= parseInt(c1.substring(0,2),16);
			this.g1= parseInt(c1.substring(2,4),16);
			this.b1= parseInt(c1.substring(4,6),16);
			//alert(''+r0+','+g0+','+b0+' - '+r1+','+g1+','+b1+text);
			for (var k=0; k<length; k++)
			{
				result += '<span style="color:#'+HexaColor(reverse?2*k:k)+'">'+text[k]+'</span>';
				if (k%per==per-1)
				{
					if (repeat==0) //use random color
					{
						this.r0= this.r1;
						this.g0= this.g1;
						this.b0= this.b1;
						nc1 = RandomColor();
						this.r1= parseInt(nc1.substring(0,2),16);
						this.g1= parseInt(nc1.substring(2,4),16);
						this.b1= parseInt(nc1.substring(4,6),16);
						//alert(r0+','+g0+','+b0);
					}
					periodstep++;
					if (repeat>0 && periodstep==repeat)
					{
						if (this.rev)
						{
							this.r1= this.r0;
							this.g1= this.g0;
							this.b1= this.b0;
						}
						else
						{
							this.r0= this.r1;
							this.g0= this.g1;
							this.b0= this.b1;
						}
					}
				}
			}
			ax[j].innerHTML = result;
			//alert(ax[j].className);
			if (ax[j].className && (ax[j].className.indexOf('selected',0)>=0))
			{
					ax[j].style.backgroundColor = '#'+bgco;
			}
			else
			{
				ax[j].style.backgroundColor = '#'+bgc;
				//remove (possible) previous listeners-useful only for admin menu
				if (ax[j].removeEventListener)
					ax[j].removeEventListener('mouseover', mouseoverMenuEvent, false);// all browsers except IE before version 9
				else if (ax[j].detachEvent)
					ax[j].detachEvent('onmouseover',mouseoverMenuEvent);// IE before version 9
				if (ax[j].removeEventListener)
					ax[j].removeEventListener('mouseout',mouseoutMenuEvent,false);// all browsers except IE before version 9
				else if (ax[j].detachEvent)
					ax[j].detachEvent('onmouseout',mouseoutMenuEvent);// IE before version 9
				//add listeners
				if (ax[j].addEventListener)
					ax[j].addEventListener('mouseover',mouseoverMenuEvent,false);// all browsers except IE before version 9
				else if (ax[j].attachEvent)
					ax[j].attachEvent('onmouseover',mouseoverMenuEvent);// IE before version 9
				if (ax[j].addEventListener)
					ax[j].addEventListener('mouseout',mouseoutMenuEvent,false);// all browsers except IE before version 9
				else if (ax[j].attachEvent)
					ax[j].attachEvent('onmouseout',mouseoutMenuEvent);// IE before version 9
			}
			//alert('done');
		}
	}
}

function mouseoverMenuEvent()
{
	this.style.backgroundColor='#'+bgco;
}


function mouseoutMenuEvent()
{
	this.style.backgroundColor='#'+bgc;
}

function Admin()
{
	Colorize(document.getElementById('color0').value,
		document.getElementById('color1').value,
		document.getElementById('period').value,
		document.getElementById('repeat').value,
		document.getElementById('reverse').checked,
		document.getElementById('bgcolor').value,
		document.getElementById('bgcoloro').value,
		document.getElementById('bgcolorm').checked);
}

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{ 
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
				oldonload();
			func();
		}
	} 
}
//http://www.webreference.com/programming/javascript/onloads/

