var ofrW = 118;
var ofrInterval = 2500;
var extendMaxa = 5;
var ofrTotal = 0;

fx.Background = Class.create();
fx.Background.prototype = Object.extend(new fx.Base(), {
	initialize: function(el, options) {
		this.el = $(el);
		this.setOptions(options);
		this.now = 0;
	},
	increase: function() {
		this.el.style.marginLeft = this.now + "px";						
	},
	scroll: function(howMuch){
			
		this.clearTimer();

		// calculate the width of elements, including padding/border								
		var scroll_limit = this.el.offsetWidth - this.el.parentNode.offsetWidth + (ofrW);
		
		//if (((this.now + howMuch) <= 0) && ((this.now + howMuch) >= -(Math.abs(howMuch) + scroll_limit))) {
		if (((this.now + howMuch) <= 0) && ((this.now + howMuch)>=-(Math.abs(howMuch) + (ofrTotal*ofrW)))){
			this.custom(this.now, this.now + howMuch);
		}else{
			//this.custom(this.now, 0);

			if((this.now==0)&&howMuch>0){
				this.el.style.marginLeft = -(ofrTotal*ofrW);
				this.now = -(ofrTotal*ofrW);
			}else{
				this.el.style.marginLeft = -ofrW;
				this.now = -ofrW;
			}
			this.custom(this.now, this.now + howMuch);
		}
		//clearInterval(autoScr);
		//autoScr = setInterval(autoScroll, ofrInterval);
	}
});


function autoScroll(){
	scrollerEffect.scroll(-ofrW);
}

function stp(){
	clearInterval(autoScr);
}

function srt(){
	autoScr = setInterval(autoScroll, ofrInterval);
}


function initScroller() {
	if(document.getElementById('roller')){
		var tds = document.getElementById('roller').getElementsByTagName('TD');
		maxa = tds.length - extendMaxa;
		if(maxa>5){
			for (var i = 0; i < tds.length; i++) {
				tds[i].id = 'content' + (i+1);
				if(i>=(tds.length-extendMaxa)){
					tds[i].innerHTML = tds[(i-(tds.length-extendMaxa))].innerHTML;
				}else{
					ofrTotal = i+1;
				}
			}

			//Apply pause when mouseover
			var ahrefs = document.getElementById('roller').getElementsByTagName('A');
			for (var i = 0; i < ahrefs.length; i++) {
				ahrefs[i].onmouseover = function (){
					//stp();
				}
				ahrefs[i].onmouseout = function (){
					//srt();
				}
			}

			//Show scrollerarrows and apply manual scrolling events
			leftBtnObj = document.getElementById('leftbtn');
			rightBtnObj = document.getElementById('rightbtn');

			leftBtnObj.onmousedown = function (){
				scrollerEffect.scroll(ofrW);
				this.blur();
				return false;
			}
			leftBtnObj.onclick = function (){
				return false;
			}

			rightBtnObj.onmousedown = function (){
				scrollerEffect.scroll(-ofrW);
				this.blur();
				return false;
			}
			rightBtnObj.onclick = function (){
				return false;
			}

			leftBtnObj.style.display = 'block';
			rightBtnObj.style.display = 'block';

	
			//Run autoscroller effect
			scrollerEffect = new fx.Background('roller', {duration: 300});
			//autoScr = setInterval(autoScroll, 2500);


		}else{
			rightBtnObj = document.getElementById('rightbtn');
			rightBtnObj.style.display = 'block';
		}
	}
}


addEvent(window, 'load', initScroller);
