﻿////////////////////////////////////////////////////////////////////////////////
//
//  slide.js - slide a DOM element between different colors with CSS
//
//  ©2009, Josh Moyer <JMoyer@nodomain.net>.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

var cb, ce;		// color: begin, end
var ee;			// element
var rb, gb, bb;	// RGB begin
var re, ge, be;	// RGB end
var ri, gi, bi;	// RGB increment
var tc, ti, tl;	// timer: count, intervals, length

function scheduleSlides()
{
	setTimeout(slideOne,3000);
	setTimeout(slideTwo,9000);
}

function slideOne()
{
	scheduleSlide(document.getElementById("text"), 153, 255, 102, 3000);
}

function slideTwo()
{
	scheduleSlide(document.getElementById("text"), 0, 0, 0, 3000);
}

function scheduleSlide(e, r, g, b, t)
{
    with (Math)
	{
		if (e.nodeType==null || isNaN(r) || isNaN(g) || isNaN (b) || isNaN(t))
		throw new TypeError("slideColor(): invalid argument type");

		if (max(r, g, b)>255 || min(r, g, b)<0 || t<1)
		throw new RangeError("slideColor(): argument out of range");

		var ca;			// color array
		var dx, dn, da;	// delta: max, min, absolute
		var rd, gd, bd;	// RGB delta

		ee = e; re = r; ge = g; be = b;

		cb = getCSSProperty("color", ee);
		ca = parseCSSColorValue(cb);

		rb = ca[0]; gb = ca[1]; bb = ca[2];

		rd = re - rb; gd = ge - gb; bd = be - bb;   

		dx = max(rd, gd, bd);
		dn = min(rd, gd, bd);
		da = max(abs(dx), abs(dn));

		if (da<t) {tl = t / da; tc = round(t / tl);}
		else {tl = 1; tc = t;}

		if (rd!==0) ri = (rd/tc); else ri = 0;
		if (bd!==0) gi = (gd/tc); else gi = 0;
		if (gd!==0) bi = (bd/tc); else bi = 0;

		ti = 1;

		for (var i=1; i<tc+1; i++)
		{
			setTimeout(timedSlide, round(tl*i));
		}
	}
}

function timedSlide()
{
    with (Math)
	{
		if (ti<tc)
		{
		ce = "rgb(" +
			(rb + round(ri*ti)) + ", " +
			(gb + round(gi*ti)) + ", " +
			(bb + round(bi*ti)) + ")";
		}
		else ce = "rgb(" + re + ", " + ge + ", " + be + ")";

		ee.style.color = ce;
		ti++;
    }
}
