// fishfarm: Ticker Object Id: ticker.js,v 2.0 2007/01/04 14:55:38 fisch Exp
// Copyright (C) 2000-2007 fishfarm netsolutions, Thomas Hering & Karsten Dambekalns GbR,
// http://www.fishfarm.de/. All rights reserved.

function Ticker(x,y,width,height) {
  this.name = 'Ticker'+(Ticker.count++);
  this.obj = this.name+'Object';
  eval(this.obj+'=this');
  this.x = x;
  this.y = y;
  this.w = width;
  this.h = height;

  this.bgColor = null;
  this.eventLayerImage = 'fileadmin/templates/main/img/blank.gif';
  this.speed = 200;

  this.contentArray = [];
  this.contentArray[0] = {url:'http://www.fishfarm.de/',message:'(c) fishfarm netsolutions'};
  c = 0;

  this.textSize = 14;
  this.textLetterSpacing = 1;
  this.textColor1 = '#ffc0c0';
  this.textColor2 = '#ff9090';
  this.textColor3 = '#ff0000';
  this.title = '';

  if (is.ns5 && is.geRev<=1) { this.yDif=Math.round((this.h-this.textSize)/2-this.textSize); }
  else { this.yDif=Math.round((this.h-this.textSize)/2); }

  this.build = TickerBuild;
  this.activate = TickerActivate;
  this.over = TickerOver;
  this.out = TickerOut;
  this.down = TickerDown;
  this.run = TickerRun;
  this.start = TickerStart;
  this.stop = TickerStop;
  this.init = TickerInit;

  this.pos = 0;
}

function TickerBuild() {
	if (!this.contentArray[0]) this.contentArray[0] = {url:'',message:''};
	this.textWidth = this.textSize/1.7;
	this.items = [];
	this.itemsE = [];
	for (var c=0;c<this.contentArray.length;c++) {
		this.items[c] = [];
		this.itemsE[c] = {};
		for (var i=0;i<this.contentArray[c].message.length;i++) {
			this.items[c][i] = {};
			this.items[c][i].text = this.contentArray[c].message.slice(i,i+1);
		}
	}
	this.css = 'div {font-size:'+this.textSize+'px; font-family:Courier;}\n';
	this.css += css(this.name,this.x,this.y,this.w,this.h,this.bgColor);
	this.div = '\n<div id="'+this.name+'">\n';
	for (var c=0;c<this.contentArray.length;c++) {
		for (var i=0;i<this.contentArray[c].message.length;i++) {
			this.css += css(this.name+'Content'+c+'Item'+i,0,0,this.textWidth+this.textLetterSpacing,this.h-this.yDif,null,'hidden',null,'color:'+this.textColor3+';');
			this.div += '<div id="'+this.name+'Content'+c+'Item'+i+'">'+this.items[c][i].text+'</div>\n';
		}
		this.css += css(this.name+'Content'+c+'ItemE',0,0,this.w,this.h,null,'hidden',999);
		this.div += '<div id="'+this.name+'Content'+c+'ItemE"><a href="'+this.contentArray[c].url+'" title="'+this.title+'"><img src="'+this.eventLayerImage+'" width="'+this.w+'" height="'+this.h+'" border="0" alt="" /></a></div>\n';
	}
	this.div += '</div>\n';
}

function TickerActivate() {
	this.lyr = new DynLayer(this.name);
	for (var c=0;c<this.contentArray.length;c++) {
		for (var i=0;i<this.contentArray[c].message.length;i++) {
			this.items[c][i].lyr = new DynLayer(this.name+'Content'+c+'Item'+i);
		}
		this.itemsE[c].lyr = new DynLayer(this.name+'Content'+c+'ItemE');
	}
}

function TickerOver() {}
function TickerOut() {}
function TickerDown() {}

function TickerRun() {
	var k=Math.round(this.w/(this.textWidth+this.textLetterSpacing));
	if (this.i==0) { //Start from beginning?
		this.itemsE[c].lyr.show();
		for (var j=0;j<this.contentArray[c].message.length;j++) {
			this.items[c][j].lyr.moveTo(this.w,this.yDif);
			this.items[c][j].lyr.hide();
			this.items[c][j].lyr.css.color=this.textColor1;
			this.items[c][j].location=0;
		}
	}
	for (var j=0;j<=this.i;j++) {
		if (j<this.contentArray[c].message.length) {
			if (this.items[c][j].location==1) this.items[c][j].lyr.css.color=this.textColor2;
			if (this.items[c][j].location==2) this.items[c][j].lyr.css.color=this.textColor3;
			if (j==(this.i-k+2)) this.items[c][j].lyr.css.color=this.textColor2;
			if (j==(this.i-k+1)) this.items[c][j].lyr.css.color=this.textColor1;
			if (j==(this.i-k)) this.items[c][j].lyr.hide();  //fuer Opera, weil der noch kein Clipping kann und man die rauslaufenden Layer noch sehen wuerde
			if (j>(this.i-k)) this.items[c][j].lyr.moveBy(0-(this.textWidth+this.textLetterSpacing),0);
			this.items[c][j].location++;
		}
	}
	if (this.i<this.contentArray[c].message.length) this.items[c][this.i].lyr.show();
	this.i++;
	
	if (this.i==this.contentArray[c].message.length+k) { // End of message?
		this.i = 0;
		this.itemsE[c].lyr.hide();
		if (c < this.contentArray.length-1) c++; // Next message or start from beginning?
		else c = 0;
	}
	this.loop = setTimeout(this.obj+'.run()',this.speed);
}

function TickerStart() {
  this.i=0;
  if (this.isOn==false) { this.loop=setTimeout(this.obj+'.run()',this.speed); this.isOn=true; this.pos=0;}
}

function TickerStop() {
  if (this.isOn==true) { clearTimeout(this.loop); this.isOn=false; }
}

function TickerInit() {
  this.isOn = false;
  this.activate();
  this.start();
}

Ticker.count=0;
