//===================================================================================================================================
//
// NAME:	FADER OBJECT
// VERSION:	1.0
// AUTHOR:	Keith Salisbury, GlobalBeach 2004
//
// DESCRIPTION: Core class definition, will assign mouseover,mouseout,mousedown events. It will recall the "action" until it has 
//		reached the number of steps specified.
//
// REQUIRES:	None
//
// USAGE:	this is designed as a base class. Sub classes should assign a new instance of this class as their proptotype property
//
// EXAMPLE:	this.prototype = new Fader(this.target,this.steps,this.fadeSpeed);
//
// SUPPORT:	Win IE 6, Win Netscape 7.1 (testing is in progress) 
//
// TODO:	There is still a bug in here to do with defaulting the className, dont have time to fix right now
//
//===================================================================================================================================

var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var mac = (agt.indexOf("mac") != -1);
// Fix Mac IE
if(mac&&is_ie){Array.prototype.push=function(o){this[this.length]=o}};

Fader = function()
{
//	alert("Fader:constructor");
	this.direction = null;
	this.stepCount = 0;
	// Create container to store all the pointers for this set
	if (typeof gFades=="undefined") gFades = {name:'FadesContainer'};
}

Fader.prototype.init = function(target,steps,fadeSpeed)
{
	// get the parametes
	this.target = (target) ? target : null;
	this.steps = (steps) ? steps : 10;
	this.fadeSpeed = (fadeSpeed) ? fadeSpeed : 10;
	
	// bind the object to the element
	this.target.owner = this;
	this.className = (this.target.className!="") ? this.target.className.replace(/\s+/g,'').replace(/\s+/g,'') : "unknown";

	this.debug = findObject(this.target.id+"debug");

	// repoint all the events
	this.target.onmouseover = function(){this.owner.MouseOver()}
	this.target.onmouseout = function(){this.owner.MouseOut()}
	this.target.onmousedown = function(){this.owner.MouseDown()}
	this.target.onmouseup = function(){this.owner.MouseUp()}

	// Create a container for this set
	if (!gFades[this.className]) gFades[this.className] = {name:this.className, items:[], rolloverItem:null, selectedItem:null };
	gFades[this.className].items.push(this);
	this.index = gFades[this.className].items.length-1;

	// Trigger the mouseover event
	this.MouseOver();
}

Fader.prototype.setSelectedItem = function()
{
	this.MouseDown();
}

Fader.prototype.MouseDown = function()
{
	// if there's already a selecteditem, de-select it
	if (gFades[this.className].selectedItem != null || this.index) 
	{
		setTimeout("if (gFades."+this.className+".items["+gFades[this.className].selectedItem+"]!=null) gFades."+this.className+".items["+gFades[this.className].selectedItem+"].MouseOut()",10);
	}
	// set selectedItem to this item
	gFades[this.className].selectedItem = this.index;
}

Fader.prototype.MouseUp = function()
{
	//nothing
}

Fader.prototype.MouseOver = function()
{
	this.direction = 1;
	if (gFades[this.className].rolloverItem != null || this.index){
		setTimeout("if (gFades."+this.className+".items["+gFades[this.className].rolloverItem+"]!=null) gFades."+this.className+".items["+gFades[this.className].rolloverItem+"].fadeOut()",this.fadeSpeed);
	}
	if (gFades[this.className].rolloverItem != this.index){
		gFades[this.className].rolloverItem = this.index;
		// if this item isnt already selected
		if (this.index != gFades[this.className].selectedItem){
			setTimeout("if (gFades."+this.className+".items["+this.index+"]!=null) gFades."+this.className+".items["+this.index+"].fadeIn()",this.fadeSpeed);
		}
	}
}

Fader.prototype.MouseOut = function()
{
	this.direction = -1;
	if (gFades[this.className].rolloverItem==this.index) gFades[this.className].rolloverItem = null;
	// if this item isnt the selectedItem
	if (this.index != gFades[this.className].selectedItem) 
	{
		setTimeout("if (gFades."+this.className+".items["+this.index+"]!=null) gFades."+this.className+".items["+this.index+"].fadeOut()",this.fadeSpeed);
	}
}

Fader.prototype.fadeIn = function()
{
	this.action();
	if (this.stepCount<this.steps && this.direction>0)
	{
		this.stepCount++;
		setTimeout("if (gFades."+this.className+".items["+this.index+"]!=null) gFades."+this.className+".items["+this.index+"].fadeIn()",this.fadeSpeed);
	}
}

Fader.prototype.fadeOut = function()
{
	this.action();
	if (this.stepCount>0 && this.direction<0)
	{
		this.stepCount--;
		setTimeout("if (gFades."+this.className+".items["+this.index+"]!=null) gFades."+this.className+".items["+this.index+"].fadeOut()",this.fadeSpeed*2);
	}
}

Fader.prototype.action = function()
{
	// nothing
}

var FaderClsLoaded=true;


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// FINDOBJECT ELEMENT OBJECT LOCATOR +++++++++++++++++++++++++++++++++++++++++++++++++++++
// AUTHOR:			Macromedia
// MODIFIED:		James Costerton

// ARGUMENTS:		String (s)		Element ID as string
// RETURNS:			Boolean
// VERSION:			1.0

findObject = function (n,d)
{
var p,i,x;if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all)x=d.all[n];for(i=0;!x&&i<d.forms.length;i++)x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)x=findObject(n,d.layers[i].document);
if(!x&&document.getElementById)x=document.getElementById(n);return x;
}


//===================================================================================================================================
//
// NAME:	TEXTFADER OBJECT
// VERSION:	1.0
// AUTHOR:	Keith Salisbury, GlobalBeach 2004
//
// DESCRIPTION: Flexible amd Easy to create fading text items. Simply add the onmouseover event and it will do the rest.
//
// REQUIRES:	Fader.js - This class extends the main Fader class.
//
// USAGE:	onmouseover="if(!this.fader){this.fader=new imageFader(this,rolloverColour,steps[,timeoutCount])}"
//
//		this			[object]	the object that contains the instance of imageFader
//		rolloverColour		String		hexidecimal colour string for the colour of the rollover
//		steps			Number		number of steps to use for the fade
//		[timeoutCount]		Number		Time in milliseconds between each step of the fade (Optional)
//
// EXAMPLE:	<a href=# onMouseOver="if(!this.fader){this.fader=new textFader(this,'336666',10)}">
//			TEXT GOES HERE
//		</a>
//
// NOTES:	Assign the anchor to a class to define custom colours, and other standard css properties
//		It will NOT work without style="color:#xxxxxx", or a being assigned a class with color defined
// 		Using class="myGroup" on the anchor tag will also enable groupings for click state
//		Currently the anchor tag only supports the firstChild, ie, you cant have more than text within a link
//
// SUPPORT:	Win IE 6, Win Netscape 7.1 (testing is in progress)
//
//===================================================================================================================================

if (typeof FaderClsLoaded=="undefined") alert("Error: please include the file Fader.js")

textFader = function(target,hexTo,steps,fadeSpeed)
{
	//alert("textFader:constructor");

	// parameters
	this.target = target;
	this.steps = steps;
	this.fadeSpeed = fadeSpeed;

	// Call superclass init
	this.init(this.target,this.steps,this.fadeSpeed);

	// subclass specific properties
	this.hexTo = (hexTo) ? hexTo : "ffffff";
	
	// Get the current colour values
	this.from = new Array(3);
	this.colour = (is_ie) ? ((this.target.style.color!="") ? this.target.style.color : this.target.currentStyle.color) : document.defaultView.getComputedStyle(this.target, "").getPropertyValue("color") 
	if (this.colour)
	{
		this.from[0] = (is_ie) ? parseInt(this.colour.substring(1,3),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[0]);
		this.from[1] = (is_ie) ? parseInt(this.colour.substring(3,5),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[1]);
		this.from[2] = (is_ie) ? parseInt(this.colour.substring(5,7),16) : parseInt(this.colour.substring(4,this.colour.length-1).split(',')[2]);
	}
	
	// Get the target colour values
	this.to = new Array(3);
	this.to[0] = parseInt(this.hexTo.substring(0,2),16);
	this.to[1] = parseInt(this.hexTo.substring(2,4),16);
	this.to[2] = parseInt(this.hexTo.substring(4,6),16);

	// Calculate the increments
	this.inc = new Array(3);
	this.inc[0] = (this.from[0]-this.to[0])/this.steps;
	this.inc[1] = (this.from[1]-this.to[1])/this.steps;
	this.inc[2] = (this.from[2]-this.to[2])/this.steps;

	// Calculate the step values
	this.colours = new Array (this.steps)
	this.val = new Array(3);
	for (var i=0;i<=this.steps;i++)
	{
		this.val[0] = (this.from[0]<this.to[0]) ? this.from[0]+(this.inc[0]*i)*-1 : this.from[0]-(this.inc[0]*i);
		this.val[1] = (this.from[1]<this.to[1]) ? this.from[1]+(this.inc[1]*i)*-1 : this.from[1]-(this.inc[1]*i);
		this.val[2] = (this.from[2]<this.to[2]) ? this.from[2]+(this.inc[2]*i)*-1 : this.from[2]-(this.inc[2]*i);
		this.val[0] = parseInt(this.val[0]);
		this.val[1] = parseInt(this.val[1]);
		this.val[2] = parseInt(this.val[2]);
		this.colours[i] = (is_ie) ? this.dec2hex(this.val[0]) + this.dec2hex(this.val[1]) + this.dec2hex(this.val[2]) : this.val[0] + "," + this.val[1] + "," + this.val[2];
	}
	
	// Destory unnecessary things
	this.from = null;
	this.to = null
	this.inc = null;
	this.val = null;
	this.colour = null;
	
}

textFader.prototype = new Fader();

textFader.prototype.action = function()
{
	this.colour = (is_ie) ? "#" + this.colours[this.stepCount] : "rgb(" + this.colours[this.stepCount] + ")";
	if (this.colour!=null){
		this.target.style.color = this.colour;
	}
}

textFader.prototype.dec2hex = function(fig)
{
//	alert("textFader:dec2hex");
	var divFig=Math.floor(fig/16)
	var modFig=Math.floor(fig % 16)
	var divFig=this.hexify(divFig)
	var modFig=this.hexify(modFig)
	return divFig+modFig
}

textFader.prototype.hexify = function (fig)
{
//	alert("textFader:hexify");
	if (fig==15) return "f"
	if (fig==14) return "e"
	if (fig==13) return "d"
	if (fig==12) return "c"
	if (fig==11) return "b"
	if (fig==10) return "a"
	if (fig<10) return ""+fig // use "" first to convert number to string
}

