


///////////////////////////////////////////////
// Copyright 2009-2012 Tksoft Inc..
// All Rights Reserved.
// Author: Troy Korjuslommi
// http://www.tksoft.com/
///////////////////////////////////////////////

var Faders = new Array();


function runFader (elids, fadeinsecs, fadeoutsecs, showtime, count) {
	var fader = new Fader(elids[0], fadeinsecs, fadeoutsecs, showtime);
	Faders.push(fader);
	for (var i = 1; i < elids.length; ++i) 
		fader.addnodeid(elids[i]);
	fader.startfadeout(count);
}

function createFader (elid, fadeinsecs, fadeoutsecs, showtime) {
	var fader = new Fader(elid, fadeinsecs, fadeoutsecs, showtime);
	Faders.push(fader);
	return fader;
}

function fadeOut(elid, fadeoutsecs) {
	var fader = findFaderByElId(elid);
	if (fader == null)
		fader = createFader(elid, 2, fadeoutsecs, 1);
	else
		fader.setOutSecs(fadeoutsecs);
	fader.startfadeout(1);
}

function fadeIn(elid, fadeinsecs) {
	var fader = findFaderByElId(elid);
	if (fader == null)
		fader = createFader(elid, fadeinsecs, 2, 1);
	else
		fader.setInSecs(fadeinsecs);
	fader.startfadein(1);
}

function fadeToggle(elid, fadeinsecs, fadeoutsecs) {
	var fader = findFaderByElId(elid);
	if (fader == null)
		fader = createFader(elid, fadeinsecs, fadeoutsecs, 1);
	if (fader.getOpacity() > 50)
		fader.startfadeout(1);
	else
		fader.startfadein(1);
}

function findFaderByElId(elid) {
	for (var i = 0; i != Faders.length; ++i) {
		if (Faders[i].topnodeid == elid)
			return Faders[i];
	}
	return null;
}



function runXmlFader(topid, xmlurl) {
	try {
		var fadeinsecs = 1; 
		var fadeoutsecs = 1;
		var showtime = 3;
		var fader = createFader(topid, fadeinsecs, fadeoutsecs, showtime);

		var xml = getXMLHttp();
		var url = xmlurl;

		xml.open("GET", url);
		try {
			xml.overrideMimeType("text/xml");
		} catch (ex) {
		}
		xml.onreadystatechange = function() {
			if (xml.readyState == 4) {
				try {
					var body = document.getElementsByTagName("body")[0];
					
					var el = document.createElement("div");
					el.setAttribute("style", "visibility: hidden; position: absolute;");
					body.appendChild(el);
					el.innerHTML = xml.responseText;
					
					var top = null;
					for (var i = 0; i != el.childNodes.length; ++i) {
						if (typeof el.childNodes[i].tagName != "undefined") {
							top = el.childNodes[i];
							break;
						}
					}
					if (top == null)
						throw new Error("The XML fader couldn't load. No content found.");

					var faders = top.childNodes;
					while (faders.length > 0) {
						var found = false;
						var nx = new Array();
						for (var i = 0; i != faders.length; ++i) {
							if (faders[i].tagName == "TD") {
								found = true;
								break;
							}
							if (typeof faders[i].tagName != 'undefined') {
								
								nx = faders[i].childNodes;
							}
						}
						if (found)
							break;
						faders = nx;
					}
					for (var i = 0; i != faders.length; ++i) {
						if (faders[i].tagName == "TD") {
							
							fader.addnode(faders[i].innerHTML);
						}
					}
					fader.startfadeout(0);
				} catch (ex) {
					alert("*** ERROR: " + ex.message);
				}
			}
		}
		xml.send(null);
	} catch (ex) {
		alert("*** ERROR in loading XML fader: " + ex.message);
	}
}




function Fader(elid, fadeinsecs, fadeoutsecs, showtime) {
	this.dynamic = 0;
	this.faderid = Faders.length;
	this.fadeval = 100;
	this.incrin = 10;
	this.incrout = 10;
	this.showtimesecs = 5;
	this.looper = null;
	this.targetobjs = new Array();
	this.curtarget = 0;
	this.topnodeid = elid;

	this.addnodeid(elid);

	this.zerocontent = this.targetobjs[0].innerHTML;

	this.setInSecs(fadeinsecs);
	this.setOutSecs(fadeoutsecs);

	this.showtimesecs = showtime;
}



Fader.prototype = {
	setOpacity : function(node, val) {
		if (val < 0)
			val = 0;
		else if (val > 100)
			val = 100;

		

      var filterval = "alpha(opacity=" + val + ", style=0, finishopacity=" + val + ")";
      var opval = (val > 0) ? val/100 : 0;
      if (node.style.filter)
      	node.style.filter = filterval;
      else
      	node.style.opacity = opval;
      for (var i = 0; i != node.childNodes.length; ++i) {
         if (typeof node.childNodes[i].tagName != "undefined") {
            if (node.childNodes[i].style.filter)
 	            node.childNodes[i].style.filter = filterval;
            else 
               node.childNodes[i].style.opacity = opval;
         }
      }

		
	}
	, getOpacity : function() {
		return this.fadeval;
	}
	, setInSecs : function(fadeinsecs) {
		if (fadeinsecs > 0) {
			if (fadeinsecs > 5)
				this.incrin = 4;
			else if (fadeinsecs > 3)
				this.incrin = 6;
			else
				this.incrin = 10;
			this.intervalin = (fadeinsecs*1000) / this.incrin;
		}
		else {
			this.incrin = 100;
			this.intervalin = 0;
		}
	}
	, setOutSecs : function(fadeoutsecs) {
		if (fadeoutsecs > 0) {
			if (fadeoutsecs > 5)
				this.incrout = 4;
			else if (fadeoutsecs > 3)
				this.incrout = 6;
			else
				this.incrout = 10;
			this.intervalout = (fadeoutsecs*1000) / this.incrout;
		}
		else {
			this.incrout = 100;
			this.intervalout = 0;
		}
	}
	, startfadeout : function (count) {
		if (this.intervalout <= 0)
			this.fadeval = 0;
		if (this.dynamic == 1) {
			this.setOpacity(this.targetobjs[0], this.fadeval);
		}
		else {
			this.setOpacity(this.targetobjs[this.curtarget], this.fadeval);
		}
		if (this.looper != null) {
			clearInterval(this.looper);
		}
		
		if (this.intervalout > 0) {
			this.looper = setInterval("Faders[" + this.faderid + "].fadeout(" + count + ")", this.intervalout);
		}
	}
	, startfadein : function(count) {
		var cur = this.curtarget;
		this.curtarget = (++this.curtarget >= this.targetobjs.length) ? 0 : this.curtarget;
		if (this.intervalin <= 0) {
			this.fadeval = 100;
		}
		
		if (this.dynamic == 1) {
			this.setOpacity(this.targetobjs[0], this.fadeval);
			if (this.targetobjs.length > 1) {
				if (this.curtarget == 0)
					this.targetobjs[0].innerHTML = this.zerocontent;
				else
					this.targetobjs[0].innerHTML = this.targetobjs[this.curtarget].innerHTML;
				
			}
		}
		else {
			this.setOpacity(this.targetobjs[this.curtarget], this.fadeval);
		}
		if (this.looper != null) {
			clearInterval(this.looper);
		}
		
		if (this.intervalin > 0) {
			this.looper = setInterval("Faders[" + this.faderid + "].fadein(" + count + ")", this.intervalin);
		}
	}
	, fadeout : function(count) {
		this.fadeval -= this.incrout;
		if (this.dynamic == 1)
			this.setOpacity(this.targetobjs[0], this.fadeval);
		else 
			this.setOpacity(this.targetobjs[this.curtarget], this.fadeval);

		if (this.fadeval <= 0) {
			if (this.looper != null)
				clearInterval(this.looper);
			this.looper = null;
			if (count == 0 || --count > 0)
				setTimeout("Faders[" + this.faderid + "].startfadein(" + count + ")", 100);
		}
	}
	, fadein : function(count) {
		this.fadeval += this.incrin;
		if (this.dynamic == 1)
			this.setOpacity(this.targetobjs[0], this.fadeval);
		else 
			this.setOpacity(this.targetobjs[this.curtarget], this.fadeval);
		if (this.fadeval >= 100) {
			if (this.looper != null)
				clearInterval(this.looper);
			this.looper = null;
			if (count == 0 || --count > 0)
				setTimeout("Faders[" + this.faderid + "].startfadeout(" + count + ")", this.showtimesecs*1000);
		}
	}
	, addnode : function (data) {
		this.dynamic = 1;
		var topsibling = this.targetobjs[0];
		
		var node = document.createElement("td");
		node.setAttribute("id", "fader_" + this.targetobjs.length);
		
		node.innerHTML = data;
		
		this.targetobjs.push(node);
		
	}
	, addnodeid : function (nodeid) {
		var el = document.getElementById(nodeid);
		this.setOpacity(el, 0);
		this.targetobjs.push(el);
	}
};



