//------------------------------------------
// ticker.js - v1.0
// Author: Gareth Davies

// Netscape 6/7, Mozilla, Phoenix, compatibility added by Inigo Surguy 03Mar2003

//------------------------------------------
 
function Ticker (id) {
	//--------------  General Properties -------------- 
	this.instanceName = id;
	this.interval = 3000;
	this.counter = 0;
	this.paused = false;
	this.pauseTime = 500;
	this.created = false;
	this.item = 0;
	//-------------- Fields -------------- 
	this.left = 10;
	this.top = 10;
	this.height = 20;
	this.width = 300;
	this.bgColor = "#ffffff";
	this.fgColor = "#000000";
	this.fonts = "arial, helvetica, sans-serif";
	this.fontSize = "2";
	this.items = new Array();
	this.css = "";
	this.first = true;
	this.fadeSteps = 10;
}

//-------------- Define Prototypes -------------- 
Ticker.prototype.start = fn_start;
Ticker.prototype.stop = fn_stop;
Ticker.prototype.restart = fn_restart;
Ticker.prototype.pause = fn_pause;
Ticker.prototype.init = fn_init;
Ticker.prototype.drawItem = fn_drawItem;
Ticker.prototype.addItem = fn_addItem;
Ticker.prototype.getItemsCount = fn_getItemsCount;
Ticker.prototype.hideAll = fn_hideAll;
Ticker.prototype.hide = fn_hideLayer;
Ticker.prototype.show = fn_showLayer;
Ticker.prototype.fadeIn = fn_fadeInLayer;
Ticker.prototype.fadeOut = fn_fadeOutLayer;
Ticker.prototype.setOpacity = fn_setOpacity;
Ticker.prototype.getOpacity = fn_getOpacity;
Ticker.prototype.getCurrentItem = fn_getCurrentItem;

//-------------- Define Methods -------------- 
function fn_getItemsCount() {
	return this.items.length;
}

function fn_getCurrentItem() {
	return this.layerNames[this.item];
}

function fn_addItem(item) {
	this.items[this.getItemsCount()] = item;
}

function fn_init() {
	this.layerNames = new Array();
	
	for (var i = 0; i < this.getItemsCount(); i++) {
		this.layerNames[i] = this.instanceName + "_layerItem" + i;
		this.drawItem(this.layerNames[i], this.items[i]);
		this.created = true;
	}
	this.hideAll();
	this.first = true;
}

function fn_drawItem(name, item) {
	var cssStart = ""
	var cssEnd = "";
	
	if (this.css != "")	{
		cssStart = '<span class="' + this.css + '">';
		cssEnd = '</span>';
	}
	else {
		cssStart = '<font face="' + this.fonts + '" size="' + this.fontSize + '">';
		cssEnd = '</font>';
	}
	
	if (document.getElementById) {
		var thisObj;	
		thisObj = getObject(createLayer(name));
		thisObj.style.position = "absolute";
		thisObj.style.left = this.left + "px";
		thisObj.style.top = this.top + "px";
		thisObj.style.width = this.width + "px";
		thisObj.style.height = this.height + "px";
		//thisObj.style.backgroundColor = this.bgColor;
		thisObj.style.filter = "alpha(opacity=0)";
		// Following line added by Inigo
		thisObj.style.MozOpacity = "0";
		thisObj.innerHTML = cssStart + item + cssEnd;
	} 
	else if (document.all) {
		document.open();
		document.write('<div id="' + name +'" style="position:absolute;left: ' + this.left + 'px; top: ' + this.top + 'px; width: ' + this.width + 'px; height: ' + this.height + 'px; background: ' + this.bgColor + '">');
		document.write(cssStart + item + cssEnd);
		document.write('</div>');
		document.close();	
	}  
	else  {
		var num = this.top - this.height
		document.open();
		document.write('<layer name="' + name +'" width="' + this.width +'" height="' + this.height +'" top="' + num +'" left="' + this.left + '">');
		document.write(cssStart + item + cssEnd);
		document.write('</layer>');
		document.close();			
	}
}

function fn_hideAll() {
	for (var i = 0; i < this.getItemsCount(); i++) {
		this.hide(this.layerNames[i]);
	}
	
	this.item = 0;
}

function fn_hideLayer(name) {
		hideLayer(name);
}

function fn_showLayer(name) {
		showLayer(name);
}

function fn_fadeOutLayer() {
	// Following line added by Inigo
	if (document.getElementById && (document.all || isMozillaBrowser())) {
//	if (document.getElementById && document.all) {
		var rate = 100 / this.fadeSteps
		this.stop();
		this.setOpacity(this.getCurrentItem(), this.getOpacity(this.getCurrentItem()) - rate);
		
		if (this.getOpacity(this.getCurrentItem()) <= 0) {
			this.hide(this.getCurrentItem());
			this.restart();
		}
		else
			setTimeout(this.instanceName + ".fadeOut();",(this.pauseTime / 2)/ this.fadeSteps);
	}
	else {
		this.hide(this.getCurrentItem());
		this.restart();
	}
}

function fn_fadeInLayer() {
	// Following line added by Inigo
	if (document.getElementById && (document.all || isMozillaBrowser())) {
// 	if (document.getElementById && document.all) {
		var rate = 100 / this.fadeSteps
		this.stop();
		
		if (this.getOpacity(this.getCurrentItem()) <= 0 || this.getOpacity(this.getCurrentItem()) < 0)
			this.show(this.getCurrentItem());
			
		if (this.getOpacity(this.getCurrentItem()) < 100)
			this.setOpacity(this.getCurrentItem(), this.getOpacity(this.getCurrentItem()) + rate);
		
		if (this.getOpacity(this.getCurrentItem()) >= 100 || this.getOpacity(this.getCurrentItem()) > 100) {
			this.show(this.getCurrentItem());
			this.restart();	
		}
		else
			setTimeout(this.instanceName + ".fadeIn();",(this.pauseTime / 2)/ this.fadeSteps);
	}
	else {
		this.show(this.getCurrentItem());
		this.restart();
	}
}

function fn_setOpacity(layer, op) {
	var layerObj = getObject(layer);
	layerObj.style.filter = "alpha(opacity=" + op + ")";
	// Following line added by Inigo
 	layerObj.style.MozOpacity=op/100;

	this.opacity = op;
}

function fn_getOpacity(layer) {
	var layerObj = getObject(layer);
	// Following line added by Inigo
	if (layerObj.style.MozOpacity) { return layerObj.style.MozOpacity * 100; }

	var tmp = layerObj.style.filter;
	var op1 = tmp.replace("alpha(opacity=","");
	var opacity = op1.replace(")","");
	
	return parseInt(opacity);
}

function fn_restart() {
	this.stopped = false;
}

function fn_stop() {
	this.stopped = true;
}

function fn_start() {
	if (this.created && !this.stopped) {
		// show next item
		this.item++;
		if (this.item == this.getItemsCount())
			this.item = 0;
			
		if (this.first) {
			this.item = 0;
			this.first = false;
		}
			
		this.fadeIn();
		setTimeout(this.instanceName + ".pause();", this.interval);
	}
}

function fn_pause() {
	if (this.created && !this.stopped) {
		this.fadeOut();
		setTimeout(this.instanceName + ".start();", this.pauseTime);
	}
}

function doTickerDraw () {
	ticker.init();
	ticker.start();
}

// Following function added by Inigo
function isMozillaBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    return ((userAgent.indexOf("gecko")!=-1) && (userAgent.indexOf("safari")==-1));
}

