Function.prototype.bind = function(object) {
    var method = this
    return function() {
        return method.apply(object, arguments) 
    }
}



function VPA_vscroller(name,current_cell_index,show_cells,smooth)
{
	this.singleton_name=name;
	this.name=name;
	this.show_cells=show_cells;
	this.offset=0;
	this.smooth=smooth;
	this.timer=null;
	this.velocity=3;
	this.obj=document.getElementById(this.name);
	if (!this.obj) return false;
	this.scroll_obj=this.obj.getElementsByTagName('table')[0];
	this.scroll_obj_left=this.scroll_obj.offsetLeft;
	this.cells=this.scroll_obj.rows[0].cells.length;
	this.cell_first=this.scroll_obj.rows[0].cells[0];
	this.cell_last=(this.cells>0) ? this.scroll_obj.rows[0].cells[this.cells-1] : 0;
	this.current_cell_index=current_cell_index;
	this.cell_current=this.scroll_obj.rows[0].cells[this.current_cell_index];
	this.left_link_cell=this.obj.rows[0].cells[0];
	this.left_link=this.obj.rows[0].cells[0].getElementsByTagName('a')[0];
	this.left_link.setAttribute('obj', this.singleton_name);
	this.right_link_cell=this.obj.rows[0].cells[2];
	this.right_link=this.obj.rows[0].cells[2].getElementsByTagName('a')[0];
	this.right_link.setAttribute('obj', this.singleton_name);
	if (this.cells<=show_cells)
	{
		this.left_link_cell.style.visibility='hidden';
		this.right_link_cell.style.visibility='hidden';
	}
	
	this.set_cell=function (cell)
	{
		
		if (cell>=0 && this.cells-cell>=this.show_cells)
		{
			this.current_cell_index=cell;
			this.cell_current=this.scroll_obj.rows[0].cells[this.current_cell_index];
			this.right_link_cell.style.visibility=(this.current_cell_index+this.show_cells==this.cells) ? 'hidden' : 'visible';
			this.left_link_cell.style.visibility=(this.cell_current==this.cell_first) ? 'hidden' : 'visible';
			this.left=-this.cell_current.offsetLeft;
			this.offset=this.scroll_obj.offsetLeft;
			this.slow_motion(Math.abs(this.offset-this.left)/2);
		}
	}
	

	this.slow_motion=function (start_delta)
	{
		this.start_delta=start_delta;
		this.timer=window.setInterval(this.tick.bind(this),5);
	}
	
	this.tick=function()
	{
		if (this.offset!=this.left)
		{
			var dl=this.offset-this.left;
			if (this.smooth)
			{
				var delta=Math.abs(dl);
				this.velocity+=delta>this.start_delta ? 1 : -1;
			}
			var desc=(dl>=0) ? this.velocity : -this.velocity;
		
			this.offset-=desc;
			if ((desc>0 && this.offset<this.left) || (desc<0 && this.offset>this.left))
			{
				this.offset=this.left;
			}
			this.scroll_obj.style.left=this.offset+'px';
		}
		else
		{
			this.scroll_obj.style.left=this.offset+'px';
			window.clearInterval(this.timer);
		}
	}

	this.left_link.onclick=function ()
	{
		this.set_cell(this.current_cell_index-1);
	}.bind(this)

	this.right_link.onclick=function ()
	{
		this.set_cell(this.current_cell_index+1);
	}.bind(this)

	this.set_cell(this.current_cell_index);
}

VPA_vscroller.prototype.toString=function() { return 'Object VPA_vscroller\n\nDeveloped by Andrey Pahomov (andrey.pahomov@gmail.com)';};


