var Scroller = function() {
	var table = document.getElementById('teaser-table');
	var fake = document.getElementById('teaser-fake');
	this.parentElement = document.getElementById('teaserline');
	this.scrollLeftElement = document.getElementById('tes-scroll-left');
	this.scrollRightElement = document.getElementById('tes-scroll-right');
	if (!(this.parentElement) || !(this.scrollLeftElement) || !(this.scrollRightElement) || !(table) || !(fake)) {
		alert('!!! Не найдены элементы на странице');
		return;
	}
	this.items = new Array();
	var childs = this.parentElement.childNodes;
	for (i=0; i<childs.length; i++){
		if (childs.item(i).nodeName=='DIV')
			if (childs.item(i).className=='item')
				this.items.push(childs.item(i));
	}
	fake.style.display = 'none';
	table.style.display = 'block';
	try {table.style.display = 'table';} catch(err) {};
}
//Scroller.prototype.maxTime = 1.542;
Scroller.prototype.maxTime = 1.365;
Scroller.prototype.maxStep = 12;
Scroller.prototype.step = 0;
Scroller.prototype.minSpace = 18;
Scroller.prototype.itemWidth = 170;
Scroller.prototype.parentWidth = 0;
Scroller.prototype.leftNum = 0;
Scroller.prototype.totalNum = 0;
Scroller.prototype.items = 0;
Scroller.prototype.parentElement = 0;
Scroller.prototype.scrollingLeft = false;
Scroller.prototype.scrollingRight = false;
Scroller.prototype.scrollLeftElement = 0;
Scroller.prototype.scrollRightElement = 0;
Scroller.prototype.criticalSection = false;
Scroller.prototype.timer = 0;
Scroller.prototype.update = function() {
	if ( ++this.step >=this.maxStep) {
		clearInterval(this.timer);
		this.timer = 0;
	}
	
	var X = (1-(this.step / this.maxStep))*this.maxTime;
	var Y = Math.pow(X-0.316,2)-0.1;

		this.parentWidth = this.parentElement.clientWidth;
		this.totalNum = Math.floor((this.parentWidth-this.itemWidth)/(this.itemWidth+this.minSpace))+1;
		var freeSpace = this.parentWidth - this.totalNum*this.itemWidth;
		var space = (freeSpace / (this.totalNum-1))+this.itemWidth;
		
		for (i=0; i<this.items.length; i++)
			this.items[i].style.display = 'none';
		for (i=(this.scrollingLeft?-1:0); i<(this.totalNum+(this.scrollingRight&&(this.step>=this.maxStep)?1:this.scrollingLeft?-1:0)); i++) {
			this.items[i+this.leftNum].style.marginLeft = Math.round(space*(i+(this.scrollingLeft?1:-1))+(Y*space*(this.scrollingLeft?-1:1))).toString()+'px';
			this.items[i+this.leftNum].style.display = 'block';
		}		
		
	
	if (this.step >= this.maxStep) {
		if (this.scrollingLeft) {
			this.scrollingLeft = false;
			this.leftNum--;
			this.items[this.totalNum+this.leftNum].style.display = 'none';
		}
		if (this.scrollingRight) {
			this.scrollingRight = false;
			this.items[this.leftNum].style.display = 'none';
			this.leftNum++;			
		}
		
		if (this.totalNum >= this.items.length) {
			this.totalNum = this.items.length;
			this.leftNum = 0;
			this.scrollLeftElement.style.display = 'none';
			this.scrollRightElement.style.display = 'none';
		} else if (this.totalNum+this.leftNum >= this.items.length) {
			this.leftNum = this.items.length-this.totalNum;
			this.scrollLeftElement.style.display = 'block';
			this.scrollRightElement.style.display = 'none';
		} else if (this.leftNum==0) {
			this.scrollLeftElement.style.display = 'none';
			this.scrollRightElement.style.display = 'block';
		} else {
			this.scrollLeftElement.style.display = 'block';
			this.scrollRightElement.style.display = 'block';
		}
		
		this.parentWidth = 0;
		this.criticalSection = false;
	}
}
Scroller.prototype.scrollLeft = function(el) {
	el.blur();
	if ((!this.criticalSection) && (this.leftNum > 0)) {
		this.criticalSection = true;
		
		this.scrollingLeft = true;		
		this.step = 0;
		this.timer = setInterval('scroller.update();', 20);
	}
}
Scroller.prototype.scrollRight = function(el) {
	el.blur();
	if ((!this.criticalSection) && (this.leftNum < this.items.length-this.totalNum)) {
		this.criticalSection = true;
		
		this.scrollingRight = true;
		this.step = 0;
		this.timer = setInterval('scroller.update();', 20);
	}
}
Scroller.prototype.align = function() {
	if (this.parentWidth != this.parentElement.clientWidth) {
		this.parentWidth = this.parentElement.clientWidth;
		this.totalNum = Math.floor((this.parentWidth-this.itemWidth)/(this.itemWidth+this.minSpace))+1;
		if (this.totalNum >= this.items.length) {
			this.totalNum = this.items.length;
			this.leftNum = 0;
			this.scrollLeftElement.style.display = 'none';
			this.scrollRightElement.style.display = 'none';
		} else if (this.totalNum+this.leftNum >= this.items.length) {
			this.leftNum = this.items.length-this.totalNum;
			this.scrollLeftElement.style.display = 'block';
			this.scrollRightElement.style.display = 'none';
		} else if (this.leftNum==0) {
			this.scrollLeftElement.style.display = 'none';
			this.scrollRightElement.style.display = 'block';
		} else {
			this.scrollLeftElement.style.display = 'block';
			this.scrollRightElement.style.display = 'block';
		}
		var freeSpace = this.parentWidth - this.totalNum*this.itemWidth;
		var space = (freeSpace / (this.totalNum-1))+this.itemWidth;
		
		for (i=0; i<this.items.length; i++)
			this.items[i].style.display = 'none';
			
		for (i=0; i<this.totalNum; i++) {
			this.items[i+this.leftNum].style.marginLeft = Math.round(space*i).toString()+'px';
			this.items[i+this.leftNum].style.display = 'block';
		}
	}
}