//===================================================================================================================================
//
// NAME:	kFADER OBJECT
// VERSION:	1.2
// 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 kFader(this.target,this.steps,this.fadeSpeed);
//
// HISTORY: 1.0 - Core code write
//			1.1 - Added support for displaying alt tags in the window status, Fixed Mac IE not having Array.push
//			1.2 - Added support for assigning a different style class to the selected item. Just requires a selectedClass="" attribute
//				  on the tag
//			1.3 - Changed the onmouse[action] assignment to an array, so more than one class can exist on one html tag element
//			1.4 - Added support for assigning a different style class to the rollover item. Just requires a rolloverClass="" attribute
//				  on the tag
//			1.5 - Fixed bug for netscrap accesses properties of the html element
//
//===================================================================================================================================

var agt=navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
// Fix Mac IE
if((agt.indexOf("mac")!=-1)&&is_ie){Array.prototype.push=function(o){this[this.length]=o}};

kFader = function()
{
	this.id = "kFader";
	this.direction = null;
	this.stepCount = 0;
	// Create container to store all the pointers for this set
	if (typeof fades=="undefined") fades = {name:'FadesContainer',alt:''};
}

kFader.prototype.init = function(target,steps,fadeSpeed)
{
	// get the parameters
	this.target = (target) ? target : null;
	this.steps = (steps) ? steps : 10;
	this.fadeSpeed = (fadeSpeed) ? fadeSpeed : 10 ;
	
	// set the group to the html class, else stick it in the common group
	this.group = (this.target.className!="") ? this.target.className.replace(/\s+/g,'').replace(/\s+/g,'') : "common";
	// get the alt attribute value
	this.alt = (is_ie) ? ((this.target.alt) ? this.target.alt : " ") : ((this.target.getAttribute("alt")!="")?this.target.getAttribute("alt"):" ");
	// get the className
	this.className = this.target.className;
	// get the selectedClass attribute value
	this.selectedClass = (is_ie) ? ((this.target.selectedClass) ? this.target.selectedClass : " ") : ((this.target.getAttribute("selectedClass")!="")?this.target.getAttribute("selectedClass"):" ");
	// get the selectedClass attribute value
	this.rolloverClass = (is_ie) ? ((this.target.rolloverClass) ? this.target.rolloverClass : " ") : ((this.target.getAttribute("rolloverClass")!="")?this.target.getAttribute("rolloverClass"):" ");

	// Mouse Over
	if (!this.target.mouseOverArray) this.target.mouseOverArray = new Array();
	this.target.mouseOverArray.push({scope:this,method:"MouseOver"});
	this.target.onmouseover = function(){for (var i=0; i<this.mouseOverArray.length; i++) {this.mouseOverArray[i].scope[this.mouseOverArray[i].method]()};return true}
	
	// Mouse Out
	if (!this.target.mouseOutArray) this.target.mouseOutArray = new Array();
	this.target.mouseOutArray.push({scope:this,method:"MouseOut"});
	this.target.onmouseout = function(){for (var i=0; i<this.mouseOutArray.length; i++) {this.mouseOutArray[i].scope[this.mouseOutArray[i].method]()};return true}
	
	// Mouse Down
	if (!this.target.mouseDownArray) this.target.mouseDownArray = new Array();
	this.target.mouseDownArray.push({scope:this,method:"MouseDown"});
	this.target.onmousedown = function(){for (var i=0; i<this.mouseDownArray.length; i++) {this.mouseDownArray[i].scope[this.mouseDownArray[i].method]()};return true}

	// Mouse Up
	if (!this.target.mouseUpArray) this.target.mouseUpArray = new Array();
	this.target.mouseUpArray.push({scope:this,method:"MouseUp"});
	this.target.onmouseup = function(){for (var i=0; i<this.mouseUpArray.length; i++) {this.mouseUpArray[i].scope[this.mouseUpArray[i].method]()};return true}

	// Create a container for this set
	if (!fades[this.group]) fades[this.group] = {name:this.group, items:[], rolloverItem:null, selectedItem:null };
	fades[this.group].items.push(this);
	this.index = fades[this.group].items.length-1;

	// Trigger the mouseover event
	this.MouseOver();
}

kFader.prototype.setSelectedItem = function()
{
	this.MouseDown();
}

kFader.prototype.reset = function ()
{
	// remove any settings
	//alert("reset????");
}

kFader.prototype.MouseDown = function()
{
	var selectedItem = fades[this.group].selectedItem
	if (selectedItem != this.index){
		// if there's already a selected item, de-activate it
		if (selectedItem != null){this.__setTimeout(selectedItem,"MouseOut()",10)};
		// set selectedItem to this item
		fades[this.group].selectedItem = this.index;
		// set the shared rollout status property
		fades.alt = this.alt;
		if (this.selectedClass)
		{
			this.reset();
			this.target.className = this.className + " " + this.selectedClass;
		}
	}
}

kFader.prototype.MouseUp = function()
{
	//nothing
}

kFader.prototype.MouseOver = function(isDelayed)
{
	this.direction = 1;
	var rolloverItem = fades[this.group].rolloverItem;
	if (rolloverItem != this.index){
		// if there's already a rollover item de-activate it
		if (rolloverItem != null){this.__setTimeout(rolloverItem,"MouseOut()",10)};
		// set the status bar text
		this.__setStatus(this.alt);
		// set common rollover pointer to this guy
		fades[this.group].rolloverItem = this.index;
		// if this item isnt already selected
		if (this.index != fades[this.group].selectedItem){
			// start the css fades
			this.__setTimeout(this.index,"fadeIn()",this.fadeSpeed);
			// set the rollover style
			if (this.rolloverClass)
			{
				this.reset();
				this.target.className += " " + this.rolloverClass;
			}
		}
	}
	if (this.mouseOutID)
	{
		clearTimeout(this.mouseOutID);
	}
}

kFader.prototype.fadeIn = function()
{
	//document.getElementById("debug").innerHTML += "<br>fadeIn";
	this.action();
	if (this.stepCount<this.steps && this.direction>0)
	{
		this.stepCount++;
		this.__setTimeout(this.index,"fadeIn()",this.fadeSpeed);
	} else if (this.stepCount==this.steps)
	{
		this.fadeInComplete();
	}

}

kFader.prototype.MouseOut = function(isDelayed)
{
	//document.getElementById("debug").innerHTML += "<br>"+this.id+".prototype.MouseOut index["+this.index+"]";
	if (this.MouseOutDelay&&!isDelayed)
	{
		if (fades[this.group].rolloverItem==this.index) fades[this.group].rolloverItem = null;
		this.mouseOutID = this.__setTimeout(this.index,"MouseOut(true)",this.MouseOutDelay);
		return false;
	}
	this.direction = -1;
	if (fades[this.group].rolloverItem==this.index) fades[this.group].rolloverItem = null;
	this.__setStatus(fades.alt);
	// if this item isnt the selectedItem
	if (this.index != fades[this.group].selectedItem)
	{
		this.__setTimeout(this.index,"fadeOut()",this.fadeSpeed);
		if (this.target.className != this.className)
		{
			this.target.className = this.className;
		}
	}
}

kFader.prototype.fadeOut = function()
{
	//document.getElementById("debug").innerHTML += "<br>fadeOut()";
	this.action();
	if (this.stepCount>0 && this.direction<0)
	{
		this.stepCount--;
		this.__setTimeout(this.index,"fadeOut()",this.fadeSpeed*2);
	} else if (this.stepCount==0)
	{
		this.fadeOutComplete();
	}
}

kFader.prototype.__setTimeout = function (item,func,delay)
{
	return setTimeout("{fades."+this.group+".items["+item+"]."+func+"}",delay);
}

kFader.prototype.__setStatus = function (string)
{
	if(string!=null) window.status=string;
}

kFader.prototype.action = function()
{
	// nothing
	//document.getElementById("debug").innerHTML += "<br>kFader action"+this.stepCount;
}

kFader.prototype.fadeInComplete = function()
{
	// nothing
	//document.getElementById("debug").innerHTML += "<br>FadeInComplete";
}

kFader.prototype.fadeOutComplete = function()
{
	//document.getElementById("debug").innerHTML += "<br>FadeOutComplete";
	// nothing
}

var kFaderClsLoaded=true;
