if (typeof Begun == 'undefined') {
	var Begun = {};
}

Begun.Scroller = function(element, options) {
	var _this = this;
	if (!element) {
		return;
	}
	this.element = element;
	var options = options || {};
	this.height = options.height;
	this.banners_count = options.banners_count;
	this.banners_count_coef = options.banners_count_coef;
	this.is_horizontal = options.is_horizontal;
	this.tbody = options.tbody || this.element.getElementsByTagName('tbody')[0];
	this.element.style.top = '0px';
	this.element.style.opacity = '1';
	this.groupsOfTrs = [];
	this.groupsOfTds = [];
	this.currentGroup = 0;
	this.REVISION = '$LastChangedRevision: 30832 $';
	this.SCROLL_TIMEOUT = options.scroll_timeout ? options.scroll_timeout * 1000 : 60000;
	this.element.onmouseover = function() {
		_this.isStopped = true;
	};
	this.element.onmouseout = function() {
		_this.isStopped = false;
	};
}

Begun.Scroller.prototype = {
	start: function() {
		if(this.is_horizontal) {
			this.startHorizontal();
		} else {
			this.startVertical();
		}
	},
	stop: function() {
		this.isStopped = true;
	},
	startHorizontal: function() {
		var _this = this;
		this.tds = this.element.getElementsByTagName('td');
		if(this.tds.length<=this.banners_count) {
			return;
		}
		for(var i=0, l=this.tds.length; i<l; i++) {
			this.tds[i].style.width = 'auto';
		}
		this.banners_in_group = Math.round(this.tds.length/this.banners_count_coef);
		this.devideIntoGroupsHorizontal(0);
		this.makeVisibleOnlyHorizontal(this, 0);
		window.setTimeout(function() {
			_this.fadeIn.apply(_this);
		}, this.SCROLL_TIMEOUT);
	},
	startVertical: function() {
		var _this = this;
		this.trs = this.element.getElementsByTagName('tr');
		if(this.trs.length<=this.banners_count) {
			return;
		}
		this.feedHeight = 0;
		for(var i=0, l=this.trs.length; i<l; i++) {
			this.feedHeight += this.trs[i].clientHeight;
		}
		this.devideIntoGroups(0);
		this.makeVisibleOnly(this, 0);
		window.setTimeout(function() {
			_this.fadeIn.apply(_this);
		}, this.SCROLL_TIMEOUT);
	},
	makeVisibleOnlyHorizontal: function(that, groupNumber) {
		for (var i = 0, l = that.groupsOfTds.length; i < l; i++) {
			var newDisplay = (i === groupNumber?"table-cell":"none");
			if (Begun.Browser.IE && (i === groupNumber)) {
				newDisplay = "";
			}
			for (var j = 0; j < that.groupsOfTds[i].length; j++) {
				that.groupsOfTds[i][j].style.display = newDisplay;
			}
		}
	},
	makeVisibleOnly: function(that, groupNumber) {
		for (var i = 0, l = that.groupsOfTrs.length; i < l; i++) {
			var newDisplay = (i === groupNumber?"table-row":"none");
			if (Begun.Browser.IE && (i === groupNumber)) {
				newDisplay = "";
			}
			for (var j = 0; j < that.groupsOfTrs[i].length; j++) {
				that.groupsOfTrs[i][j].style.display = newDisplay;
			}
		}
	},
	nextStep: function() {
		var _this = this;
		var func = this.is_horizontal ? this.makeVisibleOnlyHorizontal : this.makeVisibleOnly;
		var group_of_banners = this.is_horizontal ? this.groupsOfTds : this.groupsOfTrs;
		if (this.currentGroup < group_of_banners.length - 1) {
			func(this, ++this.currentGroup);
		}
		else {
			this.currentGroup = 0;
			func(this, this.currentGroup);
		}
		window.setTimeout(function() {
			_this.fadeOut.apply(_this);
		}, 100);
	},
	devideIntoGroupsHorizontal: function(index) {
		var tdGroup = [];
		var it = 0;
		for(var i = index, l = this.tds.length; i<l; i++) {
			it++;
			if(it > this.banners_in_group) {
				this.groupsOfTds.push(tdGroup);
				this.devideIntoGroupsHorizontal(i);
				return;
			}
			else {
				tdGroup.push(this.tds[i]);
			}
		}
		this.groupsOfTds.push(tdGroup);
	},
	devideIntoGroups: function(index) {
		var feedHeight = 0;
                var includableFeedHeight = 0;
		var trGroup = [];

		for(var i = index, l = this.trs.length; i<l; i++) {
			feedHeight += this.trs[i].clientHeight;
			if(feedHeight > this.height) {
				this.groupsOfTrs.push(trGroup);
				this.devideIntoGroups(i);
				return;
                        }
                        else {
				trGroup.push(this.trs[i]);
                        	includableFeedHeight = feedHeight;
                        }
		}
		this.groupsOfTrs.push(trGroup);
	},
	fadeIn: function() {
		var _this = this;
		if(this.isStopped) {
			this.element.style.opacity = 1;
			this.element.style.filter = 'alpha(opacity=100)';
			window.setTimeout(function() {
				_this.fadeIn.apply(_this);
			}, 300);
			return;
		}
		if(parseFloat(this.element.style.opacity) > 0) {
			this.element.style.opacity = parseFloat(this.element.style.opacity) - 0.1;
			this.element.style.filter = 'alpha(opacity=' + parseFloat(this.element.style.opacity)*100 + ')';
			window.setTimeout(function() {
				_this.fadeIn.apply(_this);
			}, 70);
		} else {
			this.nextStep();
		}
	},
	fadeOut: function() {
		var _this = this;
		if(parseFloat(this.element.style.opacity) < 1) {
			this.element.style.opacity = parseFloat(this.element.style.opacity) + 0.1;
			this.element.style.filter = 'alpha(opacity=' + parseFloat(this.element.style.opacity)*100 + ')';
			window.setTimeout(function() {
				_this.fadeOut.apply(_this);
			}, 70);
		} else {
			window.setTimeout(function() {
				_this.fadeIn.apply(_this);
			}, this.SCROLL_TIMEOUT);
		}
	}
}
