// version 0.1.0 2007-10-04
/* ************************************************************
	Object oriented color class
**************************************************************/
function classColors() {
	function color() {				// Min and max values etc. for r,g,b, instantiated later on
		this.min = 0;		// Min value
		this.max = 255;	// Max value
		this.diff = 11;		// Difference
		this.color = 0;			// Current color
	}

	function setHideColor(r, g, b) {		// Set the color to use when hiding
		this.c[0].min = r;
		this.c[1].min = g;
		this.c[2].min = b;
	}

	function setShowColor(r, g, b) {		// Set the color to use when showing
		this.c[0].max = r;
		this.c[1].max = g; 
		this.c[2].max = b;
	}
	
	function hide() {				// Hide (use hide color)
		for(counter=0; counter<3; counter++)
			this.c[counter].color = this.c[counter].min;
		this.current = 0;
	}

	function show() {				// Show (use show color)
		for(counter=0; counter<3; counter++)
			this.c[counter].color = this.c[counter].max;
		this.current = this.steps;
	}

	function setSteps(numSteps)	{		// Number of steps to use when fading
		for(counter=0; counter<3; counter++)
			this.c[counter].diff = Math.round((this.c[counter].max - this.c[counter].min)/numSteps);
		this.steps = numSteps;
		this.hide();	// Use hide color by default
		return this.c[0].diff;
	}
	
	function fadeToShow() {				// Fade to visible (show color)
		if(this.current==this.steps)	// If already at maximum return 0
			return 0;
		for(counter=0; counter<3; counter++) {
			if(Math.abs(this.c[counter].max - this.c[counter].color) > this.c[counter].diff*2)
				this.c[counter].color += this.c[counter].diff;
			else
				this.c[counter].color = this.c[counter].max;
			if(this.c[counter].color < 0)
				this.c[counter].color = 0;
		}
		this.current++;					// Go to next color
		return 1;
	}
	
	function fadeToHide() {				// Fade to invisible (hide color)
		if(this.current==0)	// If already at minimum return 0
			return 0;
		for(counter=0; counter<3; counter++) {
			if(Math.abs(this.c[counter].color - this.c[counter].min) > this.c[counter].diff*2)
				this.c[counter].color -= this.c[counter].diff;
			else
				this.c[counter].color = this.c[counter].min;
			if(this.c[counter].color > 255)
				this.c[counter].color = 255;
		}
		this.current--;					// Go to next color
		return 1;
	}

	this.c = new Array(new color, new color, new color);	// Create three color objects
	this.current = 0;	// The current position of the color pointer
	this.steps = 24;	// Number of steps to go through when changing color
	this.setHideColor = setHideColor;	// Set the color to use when hiding
	this.setShowColor = setShowColor;	// Set the color to use when showing
	this.hide = hide;	// Hide now
	this.show = show;	// Show now
	this.setSteps = setSteps;	// Set how many ssteps to go through
	this.fadeToShow = fadeToShow;	// fadeToShow color one step
	this.fadeToHide = fadeToHide;	// fadeToHide color one step
}


/* ************************************************************
	
	Class definition for  the text fader class
	Andreas Wileur 2006-Aug-11
	
	Instantiation:
		var newsfader = new classFader(instanceName, newsArray);
	where instancename = is the name of the new object and newsArray is a simple array of newsitems
		
	
	The following properties and methods are accessible from outside:
	
		.faderClass = "faderObject";
		.faderDivClass = "faderDiv";
		
		.fadeTime = 50;
		.displayTime = 1000;
		.hidetime = 100;

		.colors.setHideColor(255, 255, 255);
		.colors.setShowColor(0, 0, 0);
		.colors.hide();
		.colors.show();
		.colors.setSteps(number of steps);
		.startFade(24);
		
*************************************************************/
function classFader(instanceName, newsArray) {	
	var direction = 1;	
	function fade() {
		if(direction) {
			if(this.colors.fadeToShow()) {
				// colorset is the new color in rgb format
				colorset = "rgb("+this.colors.c[0].color+","+this.colors.c[1].color+","+this.colors.c[2].color+")";
				// Final version, now we only change the color of the link. Works in all browsers, and requires less CSS
				document.getElementById("fader_link"+thing).style.color= colorset;

				// Call fade in fadeTime ms from now			
				this.timer = setTimeout(this.name+".fade()",fadeTime); 
			} else {
				direction = 0;
				this.timer = setTimeout(this.name+".fade()",displayTime); 
				//document.getElementById("debug").value=this.timer;
			}
		} else {
			if(this.colors.fadeToHide()) {
				// colorset is the new color in rgb format
				colorset = "rgb("+this.colors.c[0].color+","+this.colors.c[1].color+","+this.colors.c[2].color+")";
				document.getElementById("fader_link"+thing).style.color= colorset;
				// Call fade in fadeTime ms from now
				this.timer = setTimeout(this.name+".fade()",fadeTime); 
			} else {
				document.getElementById("fader_div"+thing).style.display="none";
				newsCount++;
				if(newsCount==newsMax)
					newsCount=0;
				thing = newsCount;
				document.getElementById("fader_div"+thing).style.display="block";
				direction = 1;
				this.timer = setTimeout(this.name+".fade()",hidetime); 
			}
		}
	}
	
	function startFade() {
		for(a = 0; a < newsMax; a++) {
			// Create one div for each item
			document.write("<div id='fader_div"+a+"' onmouseover='clearTimeout("+this.name+".timer)' onmouseout='"+this.name+".fade()' class='"+this.faderDivClass+"' style='display: none;'>"+newsArray[a]+"</div>");
			// Immediately set color to background
			colorset = "rgb("+this.colors.c[0].min+","+this.colors.c[1].min+","+this.colors.c[2].min+")";
			document.getElementById("fader_link"+a).style.color= colorset;
		}
		//thing = "fader"+newsCount;
		thing = newsCount;
		document.getElementById("fader_div"+thing).style.display="block";

		this.fade();
	}

	// Thing will contain the name of the current div we are working with
	var thing;

	// Setup changeColorInClass
	//this.faderClass = "faderObject";
	this.faderDivClass = "faderDiv";
	var cssRules, stylesheet, rule;
	

	// Setup news array and timings
	newsMax = newsArray.length;
	fadeTime = 50;
	displayTime = 2000;
	hidetime = 100;
	this.timer=0;
	// Randomize start item
	newsCount = Math.floor(Math.random()*newsMax);

	// Create an instance of the classColor object
	this.colors = new classColors;
	this.colors.setHideColor(255,255,255);
	this.colors.setShowColor(0,0,0);
	this.colors.setSteps(24);

	// Make functions accessible
	this.name = instanceName;
	this.fade = fade;
	this.startFade = startFade;
}
