// Title: Tigra Fader (customized)
// URL: http://www.softcomplex.com/products/tigra_fader/
// Version: 1.0 (commented source)
// Date: 03/10/2009
// Notes: Original version of this script is free. Visit official site for further details.

function tFader (a_items, a_tpl) {

	// validate parameters and set defaults
	if (!a_items) return alert("items structure is missing");
	if (typeof(a_items) != 'object') return alert("format of the items structure is incorrect");
	if (a_items[a_items.length - 1] == null) return alert("last element of the items structure is undefined");
	if (!a_tpl) a_tpl = [];

	for (var i = 0; i < A_TSLIDEDEFS.length; i += 2)
		if (a_tpl[A_TSLIDEDEFS[i]] == null)
			a_tpl[A_TSLIDEDEFS[i]] = A_TSLIDEDEFS[i + 1];

	// save config parameters in the slider object
	this.a_tpl   = a_tpl;
	this.a_items = a_tpl.random ? tslide_randomize(a_items) : a_items;

	// initialize parameters, assign methods	
	this.n_currentSlide = 0;
	this.f_goto  = tslide_goto;
	this.f_run   = function () { this.b_running = 1; this.f_goto(); };
	this.f_stop  = function () { this.b_running = 0; clearTimeout(this.o_timerS); };
	this.f_fade  = tslide_fade;
	this.f_setOpac = tslide_setOpacity;
	this.f_onClick = tslide_onClick;
	this.f_switchTo = tslide_switchto;

	// register in the global collection	
	if (!window.A_SLIDES)
		window.A_SLIDES = [];
	this.n_id = window.A_SLIDES.length;
	window.A_SLIDES[this.n_id] = this;

	document.write ('<table cellpadding="0" cellspacing="0" border="0"><tr><td valign="top" id="tslide', this.n_id, '_div"',
		(a_tpl['css'] ? ' class="'  + a_tpl['css']    + '"' : ''), ' style="',
		(a_tpl['width']  ? ' width:'  + a_tpl['width']  + 'px;' : ''),
		(a_tpl['height'] ? ' height:' + a_tpl['height'] + 'px;' : ''), '; cursor: pointer; z-index:1"',
		(a_tpl['alt']    ? ' title="'  + a_tpl['alt']  + '"' : ''),
		' ><img src="', this.a_items[0]['img'], '" id="tslide', this.n_id,
		'_img" onclick="A_SLIDES[', this.n_id,
		'].f_onClick()" style="filter:alpha(opacity=100);position:relative; z-index: 1" /></td><td rowspan="2" valign="top">');

	this.a_icons = [];
	for (var i = 0; i < this.a_items.length; i++) {
		document.write ('<img src="', this.a_items[i]['ico'],
		'" class="', a_tpl[i ? 'cssiconor' : 'cssicosel'], '" onclick="A_SLIDES[', this.n_id,
		'].f_onClick(', i, ')" onmouseover="A_SLIDES[', this.n_id,
		'].f_switchTo(', i, ')" onmouseout="A_SLIDES[', this.n_id,
		'].f_run()" id="tslide', this.n_id, '_icon', i, '" />');
		this.a_icons[i] = f_getElement('tslide' + this.n_id + '_icon' + i);
	}
	document.write ('</td></tr><tr><td><div class="', a_tpl['csstext'], '" id="tslide',
		this.n_id, '_txt" style="filter:alpha(opacity=100);">', this.a_items[0]['txt'], '</div></td></tr></table>');

	this.e_divRef = f_getElement('tslide' + this.n_id + '_div');
	this.e_imgRef = f_getElement('tslide' + this.n_id + '_img');
	this.e_txtRef = f_getElement('tslide' + this.n_id + '_txt');
	this.n_currentSlide = 0;

	// exit on old browsers
	if (!this.e_divRef || !this.e_divRef.style || this.a_items.length == 1)
		return;

	// calculate transition variables
	this.n_timeDec = Math.round(this.a_tpl['transtime'] * 1e3 / this.a_tpl['steps']);
	this.n_opacDec = Math.round(100 / this.a_tpl['steps']);

	// preload images if requested
	if (a_tpl.preload) {
		if (!window.A_IMGCACHE)
			window.A_IMGCACHE = [];
		for (var i = 0; i < this.a_items.length; i++) {
			var e_img = new Image();
			e_img.src = this.a_items[i].img;
			window.A_IMGCACHE[window.A_IMGCACHE.length] = e_img;
		}
	}
	
	// run this sucker
	this.f_run();
}

function tslide_switchto (n_slide) {
		this.f_stop();
		this.a_icons[this.n_currentSlide].className = this.a_tpl['cssiconor'];
		this.n_currentSlide = n_slide % this.a_items.length;
		this.a_icons[this.n_currentSlide].className = this.a_tpl['cssicosel'];
		this.e_imgRef.src = this.a_items[this.n_currentSlide]['img'];
		this.e_txtRef.innerHTML = this.a_items[this.n_currentSlide]['txt'];
		this.f_setOpac(100);
}

function tslide_goto (n_slide, b_now) {

	// skip if specified slide is already displayed
	if (n_slide == this.n_currentSlide)
		return;

	this.n_nextSlide = (n_slide == null ? this.n_currentSlide + 1 : n_slide) % this.a_items.length;
	if (this.n_nextSlide == this.n_currentSlide)
		return;

	this.e_divRef.style.backgroundImage = "url('" + this.a_items[this.n_nextSlide]['img'] + "')";

	// cancel any scheduled transitions	
	if (this.o_timerS)
		clearTimeout(this.o_timerS);
	if (this.o_timerT)
		clearTimeout(this.o_timerT);

	// schedule transition
	if (b_now)
		this.f_fade();
	else
		this.o_timerS = setTimeout('A_SLIDES[' + this.n_id + '].f_fade()', this.a_tpl['slidetime'] * 1e3);
}

function tslide_fade (n_opacity) {
	// new transition
	if (n_opacity == null) {
		this.e_txtRef.innerHTML = this.a_items[this.n_nextSlide]['txt'];
		n_opacity = 99;
		this.f_setOpac(99);
		this.a_icons[this.n_currentSlide].className = this.a_tpl['cssiconor'];
	}
	n_opacity -= this.n_opacDec;
	// end of transition
	if (n_opacity < 0) {
		this.f_setOpac(0);
		this.n_currentSlide = this.n_nextSlide;
		this.e_imgRef.src = this.a_items[this.n_currentSlide]['img'];
		this.f_setOpac(100);
		this.n_nextSlide = null;
		this.a_icons[this.n_currentSlide].className = this.a_tpl['cssicosel'];
		return this.b_running ? this.f_run() : null;
	}
	// set transparency
	this.f_setOpac(n_opacity);

	// cycle
	this.o_timerT = setTimeout('A_SLIDES[' + this.n_id + '].f_fade(' + n_opacity + ')', this.n_timeDec);
}

function tslide_onClick (n_slide) {
	if (n_slide == null)
		n_slide = this.n_currentSlide;
	window.location = this.a_items[n_slide]['href'];
}

function tslide_randomize (a_source) {
	var n_index,
		a_items = [];
	while (a_source.length) {
		n_index = Math.ceil(Math.random() * a_source.length) - 1;
		a_items[a_items.length] = a_source[n_index];
		a_source[n_index] = a_source[a_source.length - 1];
		a_source.length = a_source.length - 1;
	}
	return a_items;
}

// cross-browser opacity
var s_uaApp  = navigator.userAgent.toLowerCase();
if (s_uaApp.indexOf('opera') != -1 || s_uaApp.indexOf('safari') != -1)
	window.tslide_setOpacity = function (n_opacity) {
		this.e_imgRef.style.opacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('gecko') != -1)
	window.tslide_setOpacity = function (n_opacity) {
		this.e_imgRef.style.MozOpacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('msie') != -1)
	window.tslide_setOpacity = function (n_opacity) {
		try {
			this.e_imgRef.filters.alpha.opacity = n_opacity;
		} catch (e) {};
	};
else
	window.tslide_setOpacity = null;

f_getElement = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
	
	
// defaults
var A_TSLIDEDEFS = [
	'steps', 40,
	'css', '',
	'transtime', 0.5,
	'slidetime', 2
];
