String.prototype.trim = function() {
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}

function slideBanner(sel, max, tx, links) {	
	this.el = $$(sel)[0];
	this.els = [this.el];
	this.xpos = [(this.el.getStyle('background-position').split(' ')[0]).toInt()];
	this.ypos = [(this.el.getStyle('background-position').split(' ')[1]).toInt()];
	this.max = max;
	this.pos = 0;
	//Math.floor(Math.random()*max);

	this.dir = 1;
	this.wd = 191;
	this.tx = tx;
	this.txel = $('banner_text');
	this.tm = 0;
	this.intid = null;
	this.startid = null;

	this.links = links;
	this.link = this.links[0];

	this.shifttime = 300000;
	this.stoptime = 7000; // stoptime + shifttime = real stoptime!

// init elements to scroll bg
	var bp = '';
	var elements = this.el.getElements('div');
	for (var i = 0; i < elements.length; i++) {
		bp = elements[i].getStyle('background-position');
		if (!$chk(bp)) continue;
		this.els[this.els.length] = elements[i];
		var bs = bp.split(' ');
		this.xpos[this.xpos.length] = bs[0].toInt();
		this.ypos[this.ypos.length] = bs[1].toInt();
	}

// init buttons
	$('banner_right').addEvent('click', (function(e) {
		e.stop();
		this.pause();
		if (this.pos < (this.max -1)) this.run(1);
		else this.run((-1)*this.pos);
		if (this.startid) this.startid = $clear(this.startid);
		this.startid = this.start.delay(this.stoptime, this);
	}).bind(this));

	$('banner_left').addEvent('click', (function(e) {
		e.stop();
		this.pause();
		if (this.pos > 0) this.run(-1);
		else this.run(this.max-1);
		if (this.startid) this.startid = $clear(this.startid);
		this.startid = this.start.delay(this.stoptime, this);
	}).bind(this));


// init text
	this.txel.set({
		'styles': {
			'width' : this.wd-18+'px',
			'overflow' : 'hidden'
		}
	});

	var td = '<td align="center" class="banner_headline" style="width: '+this.wd+'px;overflow:hidden;">';

	this.txscroll = new Element('div', {
		'styles' : {
			'margin-left' : this.tm+'px'
		}, 
		'html' : '<table style="width:'+(this.tx.length * this.wd)+'px"><tr>'+td+this.tx.join('</td>'+td)+'</td></tr></table>'
	}).inject(this.txel);

// init link thing 
	this.el.addEvent('click', (function() {
		window.location.href = this.link;
	}).bind(this));

	this.el.setStyle('cursor', 'pointer');
// start
	this.start();
	
	this.run(Math.floor(Math.random()*max));
}

slideBanner.prototype.start = function() {
	this.intid = this.run.periodical(this.shifttime, this);
}

slideBanner.prototype.run = function(tdir) {
	if (typeof(tdir) == 'undefined') tdir = this.dir;

	for (var i = 0; i < this.els.length; i++) {
		this.xpos[i] += (this.wd*tdir*(-1));
		this.els[i].tween('background-position', this.xpos[i]+'px '+this.ypos[i]+'px');
	}

	this.tm += (this.wd*tdir*(-1));

	this.txscroll.tween('margin-left', this.tm+'px');

	this.pos += tdir;
	
	this.link = this.links[this.pos];
	if (this.pos >= (this.max - 1)) { 
		this.dir = -1;
	} else if (this.pos <= 0) {
		this.dir = 1;
	}
}

slideBanner.prototype.pause = function() {
	this.intid = $clear(this.intid);
}
/*
function banner_start() {
	var settings = {
		tl: { radius: 4 },
		tr: { radius: 4 },
		bl: { radius: 4 },
		br: { radius: 4 },
		antiAlias: true,
		autoPad: false,
		validTags: ['div']
	};
	
	var newCornersObj = new curvyCorners(settings, 'banner');
	newCornersObj.applyCornersToAll();
	sb = new slideBanner('div.banner', 3, ['dudl1 ganz viel lang', 'dudl2', 'dudl3'], ['#dudl1', '#dudl2', '#dudl3']); // (container, elementanzahl, texte, links)
	
}
*/


