var currentColor = null;
var newColor     = null;
var _color_obj = null;
var t = null;
var redStep = null;
var greenStep = null;
var blueStep = null;

var redStart = null;
var greenStart = null;
var blueStart = null;

var redStop = null;
var greenStop = null;
var blueStop = null;

var opacityStart = null;
var opacityStop = null;
var opacityStep = null;
var opacityCurrent = null;

var numSteps = 50;
var hexChars = "0123456789ABCDEF";

function start_new(obj, startColor, endColor, startOpacity, stopOpacity)
{
	startOpacity = typeof(startOpacity) != 'undefined' ? startOpacity : 100;
	stopOpacity = typeof(stopOpacity) != 'undefined' ? stopOpacity : 100;

	if(startColor == endColor) return;
	currentColor = null;
	newColor     = null;
	_color_obj = null;
	t = null;
	redStep = null;
	greenStep = null;
	blueStep = null;

	redStart = null;
	greenStart = null;
	blueStart = null;

	redStop = null;
	greenStop = null;
	blueStop = null;

	// Анализируем начальный цвет
	RedStr = startColor.slice(1,3);
	GreenStr = startColor.slice(3,5);
	BlueStr = startColor.slice(5);

	RedStr = RedStr.toUpperCase();
	red1 = RedStr.slice(0,1);
	red2 = RedStr.slice(1,2);
	
	GreenStr = GreenStr.toUpperCase();
	green1 = GreenStr.slice(0,1);
	green2 = GreenStr.slice(1,2);
	
	BlueStr = BlueStr.toUpperCase();
	blue1 = BlueStr.slice(0,1);
	blue2 = BlueStr.slice(1,2);
	
	redStart = hexChars.indexOf(red1) * 16 + hexChars.indexOf(red2);
	greenStart = hexChars.indexOf(green1) * 16 + hexChars.indexOf(green2);
	blueStart = hexChars.indexOf(blue1) * 16 + hexChars.indexOf(blue2);
	opacityStart = startOpacity;
	opacityCurrent = startOpacity;

	// Анализируем конечный цвет
	RedStr = endColor.slice(1,3);
	GreenStr = endColor.slice(3,5);
	BlueStr = endColor.slice(5);

	RedStr = RedStr.toUpperCase();
	red1 = RedStr.slice(0,1);
	red2 = RedStr.slice(1,2);
	
	GreenStr = GreenStr.toUpperCase();
	green1 = GreenStr.slice(0,1);
	green2 = GreenStr.slice(1,2);
	
	BlueStr = BlueStr.toUpperCase();
	blue1 = BlueStr.slice(0,1);
	blue2 = BlueStr.slice(1,2);
	
	redStop = hexChars.indexOf(red1) * 16 + hexChars.indexOf(red2);
	greenStop = hexChars.indexOf(green1) * 16 + hexChars.indexOf(green2);
	blueStop = hexChars.indexOf(blue1) * 16 + hexChars.indexOf(blue2);
	opacityStop = stopOpacity;

	redStep = (redStop - redStart)/numSteps;
	greenStep = (greenStop - greenStart)/numSteps;
	blueStep = (blueStop - blueStart)/numSteps;
	opacityStep = (opacityStop - opacityStart)/numSteps;

	_color_obj = obj;
	t=setTimeout("apply_new_color()",1);
}
function apply_new_color()
{
	var str='';
	currentColor = _color_obj.style.backgroundColor;
	var rgbValues;

	if (currentColor.indexOf("rgb") >= 0)
	{
        	var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1, currentColor.indexOf(')'));
        	rgbValues = rgbStr.split(",");
		red = rgbValues[0];
		green = rgbValues[1];
		blue = rgbValues[2];
    	}
	else
	{
		var RedStr = currentColor.slice(1,3);
		var GreenStr = currentColor.slice(3,5);
		var BlueStr = currentColor.slice(5);

		RedStr = RedStr.toUpperCase();
		red1 = RedStr.slice(0,1);
		red2 = RedStr.slice(1,2);
		
		GreenStr = GreenStr.toUpperCase();
		green1 = GreenStr.slice(0,1);
		green2 = GreenStr.slice(1,2);
		
		BlueStr = BlueStr.toUpperCase();
		blue1 = BlueStr.slice(0,1);
		blue2 = BlueStr.slice(1,2);
		
		red = hexChars.indexOf(red1) * 16 + hexChars.indexOf(red2);
		green = hexChars.indexOf(green1) * 16 + hexChars.indexOf(green2);
		blue = hexChars.indexOf(blue1) * 16 + hexChars.indexOf(blue2);

	}
	
	opacityCurrent = 	opacityCurrent + opacityStep;
	if(opacityCurrent > 100) opacityCurrent = 100;
	if(opacityCurrent < 0) opacityCurrent = 0;

	red = Number(red) + redStep;
	if(red > 255) red = 255;
	if(red < 0) red = 0;

	green = Number(green) + greenStep;
	if(green > 255) green = 255;
	if(green < 0) green = 0;

	blue = Number(blue) + blueStep;
	if(blue > 255) blue = 255;
	if(blue < 0) blue = 0;

	var newColor = "#";
        
	newColor += hexChars.charAt(red/16) + hexChars.charAt(red%16);
        newColor += hexChars.charAt(green/16) + hexChars.charAt(green%16);
        newColor += hexChars.charAt(blue/16) + hexChars.charAt(blue%16);

	setOpacity(opacityCurrent);
	_color_obj.style.backgroundColor = newColor;

	if((redStep < 0 && red <= redStop) || (redStep > 0 && red >= redStop)) redStep = 0;

	if((greenStep < 0 && green <= greenStop) || (greenStep > 0 && green >= greenStop)) greenStep = 0;

	if((blueStep < 0 && blue <= blueStop) || (blueStep > 0 && blue >= blueStop)) blueStep = 0;

	if((opacityStep < 0 && opacityCurrent <= opacityStop) || (opacityStep > 0 && opacityCurrent >= opacityStop)) opacityStep = 0;

	if(redStep != 0 || greenStep != 0 || blueStep != 0 || opacityStep != 0)
	{
		t=setTimeout("apply_new_color()",10);
	}

}
function stop(_color)
{
	clearTimeout(t);
	_color_obj.style.backgroundColor = _color;
	setOpacity(opacityStart);
}
function setOpacity(op)
{
	_color_obj.style.opacity = op/100;
	_color_obj.style.MozOpacity = op/100;
	_color_obj.style.KhtmlOpacity = op/100;
	_color_obj.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+op+")"; // Для того чтобы не затереть другие фильтры используем "+="
//	_color_obj.style.filter = "alpha(opacity=" + op + ")";
}
