/*
	file:		cms_ticker.js
	project:	css2 based ticker for cms-project
	author:		Ueli Leutwyler
	copyright:	insign gmbh
	version:	1.4
	last update:03.06.04
	
	history:
	
	03.06.04	new optional parameter to set tickerposition absolutely. Needed if ticker is already in absoultely positioned element
	07.05.03	if only one news was added, a copy of it is created to grantee seamless scrolling
	19.03.03	faster, slower, setSpeed added
	17.03.03	start
*/

// cmsticker class
function cmsticker(name,width,height,speed,pause,initText,absoluteX,absoluteY)
{
	this.name	= name;
	this.initialized 	= false;
	this.created 		= false;
	this.paused			= true;
	this.turbo			= false;
	if(!document.getElementById) return false;

	this.placer = null;
	this.initText = initText?initText:"loading...";
	this.id		= Math.round(Math.random()*100);
	this.speed  = speed?speed:50;
	this.waittime= pause?(pause*1000):2000;
	this.pos	= 0;
	this.newscounter = 0;
	this.news	= new Array();
	this.width	= width;
	this.height = height;
	this.timeHandler = null;
	this.running = false;
	this.absoluteX = absoluteX?absoluteX:null;
	this.absoluteY = absoluteY?absoluteY:null;
	
	// generate div-tags
	document.write("<div id=\"tickerplacer"+this.id+"\" STYLE=\"width:"+width+"; height:"+height+";\">"+this.initText+"</div>");
	document.write("<div id=\"tickerwindow"+this.id+"\" STYLE=\"position:absolute; left:0; top:0; width:"+width+"px; height:"+height+"px;overflow:hidden;visibility:hidden; z-index:1000\"></div>");
	this.tickerwindow	= document.getElementById("tickerwindow"+this.id);
	this.placer	= document.getElementById("tickerplacer"+this.id);
	if(!this.placer)
	{
		alert("internal error: could not load placer object");
		return false;
	}
	
	if(!this.tickerwindow)
	{
		alert("internal error: could not load ticker_window object");
		return false;
	}
	
	this.created = true;

	// mehthod
	this.start 	= cmsticker_start;
	this.init 	= cmsticker_init;
	this.addNews= cmsticker_addNews;
	this.tick	= cmsticker_tick;
	this.pause	= cmsticker_pause;
	this.stop	= cmsticker_stop;
	this.cont	= cmsticker_cont;
	this.setSpeed = cmsticker_setSpeed;
	this.slower	= cmsticker_slower;
	this.faster = cmsticker_faster;

	return true;
	
}

function cmsticker_faster(amount)
{
	if(!amount) amount = 20;
	this.speed -=amount;
	if(this.speed<1) this.speed=1;
}

function cmsticker_slower(amount)
{
	if(!amount) amount = 20;
	this.speed +=amount;
	if(this.speed>100) this.speed=100;
}


// methods cmsticker
function cmsticker_setSpeed(speed)
{
	this.speed = speed
}

function cmsticker_addNews(id,myobject)
{
	if(id)
		myNews = document.getElementById(id);
	else
		myNews = myobject;
	if(!myNews)
	{
		alert("Die Neuigkeit mit der id '"+id+"' konnte nicht gefunden werden");
		this.initialized=false;
		return false;
	}
	
	this.tickerwindow.appendChild(myNews);
	myNews.style.position="absolute";
	myNews.style.height = this.height;
	myNews.style.width = this.width;
	myNews.style.overflow="hidden";
	myNews.style.visibility="hidden";
	this.news[this.news.length] = myNews;
}

function cmsticker_init()
{
	if(!this.created) return;
	if(this.absoluteX)
		x = this.absoluteX;
	else
		x = findPosX(this.placer);
	
	
	if(this.absoluteY)
		y = this.absoluteY;
	else
		y = findPosY(this.placer);

	
	// set position of tickerwindow according to placer
	this.tickerwindow.style.posTop = y;
	this.tickerwindow.style.posLeft = x;
	this.tickerwindow.style.top = y;
	this.tickerwindow.style.left = x;
	// set visibility of placer and Tickerwindow
	this.tickerwindow.style.visibility = "visible";
	this.placer.style.visibility = "hidden";
	// check if only one news is added and add copy of it if so
	if(this.news.length==1)
	{
		clone = this.news[0].cloneNode(true);
		clone.id +="_clone";
//		this.news[0].insertBefore(clone,this.news[0]);
		this.addNews(null,clone);
	}
	this.initialized = true;

	return true;
}



function cmsticker_start()
{
	if(!this.initialized)
	{
		if(!this.init()) return false;
	}
	if(!this.news[this.newscounter]) return false;;
	// prepare current news
	this.paused = false;
	this.news[this.newscounter].style.posTop = this.height;	
	this.news[this.newscounter].style.top = this.height;	
	this.news[this.newscounter].style.visibility="visible";
	to = this.name+".tick()";
	this.timeHandler=setTimeout(to,this.speed);
	
}

function cmsticker_tick()
{
	this.running = true;
	this.pos+=1;
	if(this.pos>this.height) 
	{
		this.pause();
		return;
	}
	else
	{
		// set layer
		prev = this.newscounter-1;
		if(prev<0) prev=this.news.length-1;
		this.news[this.newscounter].style.posTop=this.height-this.pos ;
		this.news[this.newscounter].style.top =this.height-this.pos;
		this.news[prev].style.posTop=-this.pos ;
		this.news[prev].style.top =-this.pos;
		to = this.name+".tick()";
		this.timeHandler=setTimeout(to,this.turbo?1:this.speed);
	}
}

function cmsticker_pause()
{
	this.paused =true;
	this.turbo=false;
	this.pos=0;
	this.newscounter++;
	if(this.newscounter>=this.news.length) this.newscounter=0;

	this.running = false;
	to = this.name+".start()";
	this.timeHandler=setTimeout(to,this.waittime);
	
}

function cmsticker_stop()
{
	clearTimeout(this.timeHandler);
	this.running = false;
}

function cmsticker_cont()
{
	if(!this.running&&this.initialized) 
	{
		clearTimeout(this.timeHandler);
		if(this.paused)
			this.start();
		else
			this.tick();	
	}
	else if(this.paused)
		this.start();
	else
		this.turbo=true;
}
	

// function to evaluate position of objects

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}