$(function(){
	new panopticum($('.tslider'), $('a.top-3-back'), $('a.top-3-forw'));
})

var panopticum = function(container, back, forw, options)
{
	if (!$(container).length) return;
	
	this.container = container;
	
	options = options || {};
	
	this.cells = options.cells || 3;
	this.timeout = options.timeout || 5000;
	
	this.table = $(this.container).find('table');
	this.tds = $(this.table).find('td');
	
	if ($(this.tds).length < this.cells) return false;
	
	this.cell = 0; // текущая ячейка

	this.windowResize();
	
	$(this.tds).show();
	
	this.scrollTo();
	
	var pan = this;
	
	this.timer = window.setInterval(function(){
		pan.forw(true);
	}, this.timeout);
	
	$(back).click(function(){
		return pan.back();
	})
	$(forw).click(function(){
		return pan.forw();
	})
	$(window).resize(function(){
		pan.windowResize();
	})
}
panopticum.prototype.width = function()
{
	return $(window).width() - $('#leftColumn').width() - parseInt($('#center').css('padding-left')) - parseInt($('#center').css('padding-right')) - 105;
}
panopticum.prototype.windowResize = function()
{
	this.wtd = (this.width()) / this.cells;
	this.tds = $(this.table).find('td');
	$(this.tds).width(this.wtd);
	$(this.table).width(this.wtd * $(this.tds).length);
	$(this.table).css('margin-left', (-this.cell * this.wtd) + 'px');
	$(this.container).width(this.width());
	//$('.lands_sp_offers').width(this.width());
	//$('.top-3-back, .top-3-forw').css('top', ($('.top-3').height() / 2) + 'px');
}
panopticum.prototype.scrollTo = function(callback)
{
	for (var i = 0; i < $(this.tds).length; i++) {
		if (i == this.cell) $($(this.tds).get(i)).removeClass('lborder-left').addClass('dborder-right');
		else if (i == this.cell + this.cells - 1) $($(this.tds).get(i)).addClass('lborder-left').removeClass('dborder-right');
		else if (i > this.cell && i < this.cell + this.cells - 1) $($(this.tds).get(i)).addClass('lborder-left').addClass('dborder-right');
		else $($(this.tds).get(i)).removeClass('lborder-left').removeClass('dborder-right');
	}
	callback = callback || function(){}
	$(this.table).animate({marginLeft : (-this.cell * this.wtd) + 'px'}, 500, callback);
	return false;
}
panopticum.prototype.forw = function(onTimer)
{
	if (!onTimer) clearInterval(this.timer);
	this.cell++;
	var callback, pan = this;
	if (this.cell > $(this.tds).length - this.cells) {
		$(this.table).find('td:last').after($(this.table).find('td:first').clone());
		this.cell--;
		this.windowResize();
		this.cell++;
		callback = function(){
			$(pan.table).find('td:first').remove();
			pan.cell--;
			pan.windowResize();
		}
	}
	return this.scrollTo(callback);
}
panopticum.prototype.back = function(onTimer)
{
	if (!onTimer) clearInterval(this.timer);
	this.cell--;
	var callback, pan = this;
	if (this.cell < 0) {
		$(this.table).find('td:first').before($(this.table).find('td:last').clone());
		this.cell+= 2;
		this.windowResize();
		this.cell--;
		callback = function(){
			$(pan.table).find('td:last').remove();
			pan.windowResize();
		}
	}
	return this.scrollTo(callback);
}

