function Scroller()
{
	this.iTimeout = 20;
	
	this.iWidth = 0;
	this.iRemoveOldElements = 0;
	
	this.jScrollers = null;
	this.bScrolling = false;
	
	this.fBeforeRemoveOldCallback = null;
	
	this._scroll = function()
	{
		var lScroller = this;
		this.bScrolling = true;

		this.jScrollers.each(function() {
			var jScroller = j(this);
			var slide = -parseInt(jScroller.parent().parent().css('margin-left'));

			if(slide<=10) {
				jScroller.parent().parent().css('margin-left', 0);
				if(lScroller.iRemoveOldElements) {
					var jChildren = jScroller.children('td[uid!="0"]');
					for(var i=0; i<lScroller.iRemoveOldElements; i++) {
						if(lScroller.fBeforeRemoveOldCallback) lScroller.fBeforeRemoveOldCallback(jChildren.eq(jChildren.length-i-1));
						jChildren.eq(jChildren.length-i-1).remove();
					}
				}

				if(scroller.load_callback_after) scroller.load_callback_after();
				return;
			}
	
			iMove = (Math.sin(slide/lScroller.iWidth*Math.PI) + 0.2)*10;
			jScroller.parent().parent().css('margin-left', -slide+iMove);
		});
	
		if(parseInt(this.jScrollers.eq(0).parent().parent().css('margin-left'))<0) window.setTimeout("scroller._scroll()", this.iTimeout);
		else this.bScrolling = false;
	}
	
	this.scroll = function(iWidth, sScollerJSelector, iTimeout, iRemoveOldElements)
	{
		if(this.bScrolling || !iWidth || !sScollerJSelector || !(this.jScrollers = j(sScollerJSelector))) return;

		this.iWidth = iWidth;
		this.iTimeout = iTimeout ? iTimeout : 20;
		if(iRemoveOldElements) this.iRemoveOldElements = iRemoveOldElements;
		
		this.bScrolling = true;
		window.setTimeout("scroller._scroll()", this.iTimeout);
	}
	
	this.load_callback_before;
	this.load_callback_after;
	this.sAreaJSelector;
	this.iScrollTimeout;

	this.prepandURL = function(sAreaJSelector, sLoadingUrl, load_callback_before, load_callback_after, before_removeold_callback, iScrollTimeout, xml, data)
	{
		if(this.bScrolling) return;

		this.fBeforeRemoveOldCallback = before_removeold_callback;
		
		var lScroller = this;
		j("#_scroller_load_container").remove();
		var jLoaded = j("body").append('<div id="_scroller_load_container" style="position:absolute; top:-2000px;"><table><tr id="_scroller_loaded_html"></tr></table></div>');
		
		this.load_callback_before = load_callback_before;
		this.load_callback_after = load_callback_after;
		this.sAreaJSelector = sAreaJSelector;
		this.iScrollTimeout = iScrollTimeout

		if(data) scroller._load_callback(data);
		else {
			if(xml) jQuery.get(sLoadingUrl, null, scroller._load_callback, "xml");
			else j("#_scroller_loaded_html").load(sLoadingUrl, null, scroller._load_callback);
		}
	}
	
	this._load_callback = function (data)
	{
		if(scroller.load_callback_before) scroller.load_callback_before(data);
		
		var jLoadedArea = j("#_scroller_loaded_html");
		var iLoadedWidth = jLoadedArea.width();
		var iNewTdCount = j("#_scroller_loaded_html > td").length;

		if(iLoadedWidth) {
			j(scroller.sAreaJSelector).each(function() {
				j(this).parent().parent().css('margin-left', -iLoadedWidth);	
				j(this).prepend(jLoadedArea.html());
				j("#_scroller_load_container").remove();
			});
				
			scroller.scroll(iLoadedWidth, scroller.sAreaJSelector, scroller.iScrollTimeout, iNewTdCount);
		}
	}
}

var scroller = new Scroller();