/*

		dw_fade.js

		requires dw_core.js or dw_write.js

		

		This code is from Dynamic Web Coding 

    www.dyn-web.com 

    Copyright 2002 by Sharon Paine 

    Permission granted to use this code 

    as long as this entire notice is included.

		

*/



function startFade() {

	this.cur_clr = this.start_clr;	// hold current color

	this.initColorValues();

	this.ctlWrite();

}



function addFadeItem(itm) {

	this.items[this.items.length] = itm;

}



function ctlWrite() {

	if (!this.colors_good) return;

	if (this.ctr < 2) {

		this.writePrep(this.items[this.ctr],this.start_clr);

		this.doFade();

	} 

}



function doFade() {

	var rd = Math.round(getR(this.cur_clr) + this.incR);	

	var grn = Math.round(getG(this.cur_clr) + this.incG);	

	var blu = Math.round(getB(this.cur_clr) + this.incB);

	if (eval(this.curCheck)) {

	// if incremented color values still within range, apply to item

		var clr = "#" +getHx(rd) + getHx(grn) + getHx(blu);

		this.cur_clr = clr;

		if (document.layers) {

			this.writePrep(this.items[this.ctr],clr);

		} else this.css.color = clr;

			setTimeout(this.obj+".doFade()",this.spd);

	} else {

	// if incremented values out of range, set color to destination color,

	// apply to item, stop calls to doFade

		if (this.curCheck == this.destCheck) {

			clr = this.dest_clr;

			this.cur_clr = this.dest_clr;

		} else {

			clr = this.start_clr;

			this.cur_clr = this.start_clr;

		}

		if (document.layers) {

			this.writePrep(this.items[this.ctr],clr);

		} else this.css.color = clr;

		this.ctlFade();

	}

}



function ctlFade() {

	// reverse increments

	this.incR=-this.incR; this.incG=-this.incG; this.incB=-this.incB;

	this.ctr++; this.ctlWrite();

		this.curCheck = this.startCheck;

}



//	IMPORTANT: For this code to work properly the

//	r,g, and b colors for your start and end 

//	color choices all either increase or decrease.

//	(It's ok if one or two remain the same, e.g., 

//	#000000 and #ff0000 would be fine.)

//	Otherwise, you would need to modify the destCheck and

//	startCheck variables below and that would 

//	probably result in some strange "fade"!

//

//	For best results choose start and end colors carefully

//	perhaps using a tool like the color wheel from Paint Shop Pro

//	to choose colors of same hue.

function initColorValues() {

	this.stR = getR(this.start_clr);	this.destR = getR(this.dest_clr);

	this.stG = getG(this.start_clr);	this.destG = getG(this.dest_clr);

	this.stB = getB(this.start_clr);	this.destB = getB(this.dest_clr);

	

	// difference between start and end colors

	var distR = this.destR-this.stR;

	var distG = this.destG-this.stG;

	var distB = this.destB-this.stB;

	// check color choices and build check strings

	if (distR==0 && distG==0 && distB==0) {

		alert("There is no difference between your start and end colors.")

		this.colors_good = false; return;

	} else if (distR>=0 && distG>=0 && distB>=0) {

		this.destCheck = "rd<=this.destR && grn<=this.destG && blu<=this.destB";

 		this.startCheck = "rd>=this.stR && grn>=this.stG && blu>=this.stB";

	} else if (distR<=0 && distG<=0 && distB<=0) {

		this.destCheck = "rd>=this.destR && grn>=this.destG && blu>=this.destB";

		this.startCheck = "rd<=this.stR && grn<=this.stG && blu<=this.stB";

	} else {

		alert("Your color choices will not work.\nSee code comments for instructions.");

		this.colors_good = false; return;

	}

	this.curCheck = this.destCheck;

	this.colors_good = true;

	// get increments for each color (for now)

	var dv = 25;

	this.incR = distR/dv; this.incG = distG/dv; this.incB = distB/dv;

}



// get red, green and blue values from hex color string

function getR(clr) { return parseInt(clr.substr(1,2),16); }

function getG(clr) { return parseInt(clr.substr(3,2),16); }	

function getB(clr) { return parseInt(clr.substr(5,2),16); }



// converts r, g, b portions of color value to hex for

// concatenating back into hex color string

function getHx(cVal) {

	var cHx = cVal.toString(16);

	if (cHx.length==2) { return cHx } 

	else { return cHx ="" + "0" + cHx }

}



fadeObj.prototype.startFade = startFade;

fadeObj.prototype.addItem = addFadeItem;

fadeObj.prototype.ctlWrite = ctlWrite;

fadeObj.prototype.writePrep = writePrep;

fadeObj.prototype.doFade = doFade;

fadeObj.prototype.ctlFade = ctlFade;

fadeObj.prototype.initColorValues = initColorValues;

fadeObj.prototype.ctr = 0;

fadeObj.prototype.spd = 160;		// for setTimeout

fadeObj.prototype.pause = 10;