var scrollposition = 0;
var _steps = [489];
var liPerDivs = [1];
var __steps = [489];
var _fix = [0];
var scrollIds = ['box'];
var scrollIsRepeat = [true];
var scrollButtonIds = {'box':['btn-prev-index','btn-next-index']};

var scrollObjs = [];

function initScroll()
{
	for(var i=0; i < scrollIds.length; i++)
	{
		var ul = document.getElementById(scrollIds[i]);
		if(ul)
		{
			var lis = ul.getElementsByTagName('li');
			var reminder = (lis.length) % liPerDivs[i];
			if(reminder > 0)
			{
				for(var j=0; j< liPerDivs[i]-reminder; j++)
				{
					var _li = document.createElement('li');
					_li.className = "no-bg";
					var __li = ul.appendChild(_li);
				}
			}
			
			var handle = document.getElementById('handles');
			if(handle)
			{
				handle.innerHTML = "";
				_handlers = [];
				total_cnt = lis.length
				for(var j=0; j < lis.length; j++)
				{
					var _li = document.createElement('li');
					var __li = handle.appendChild(_li);	
					var _a = document.createElement('a');
					var __a = __li.appendChild(_a);
					if(j == 0) __a.className = "active";
					__a.innerHTML = j + 1;
					__a.href = "javascript:;";
					__a._i = j;
					__a._obj_i = i;
					__a.onclick = function(){
						if(_to) clearTimeout(_to);
						if(scrollObjs[this._obj_i] && scrollObjs[this._obj_i].handlers)
						{
							for(var j=0; j < scrollObjs[this._obj_i].handlers.length; j++)
							{
								scrollObjs[this._obj_i].handlers[j].className = '';
							}
							this.className = "active";
							_step = this._i - scrollObjs[this._obj_i]._currstep;
							if(this._i == 0 && scrollObjs[this._obj_i]._currstep == total_cnt - 1)
							{
								_next(this._obj_i,1);	
								scrollObjs[this._obj_i]._currstep = 0;
							}
							else if(this._i == total_cnt - 1 && scrollObjs[this._obj_i]._currstep == 0)
							{
								_prev(this._obj_i,1);
								scrollObjs[this._obj_i]._currstep = total_cnt - 1;
							}
							else if(_step > 0)
							{
								_next(this._obj_i,_step);
							}
							else
							{
								_prev(this._obj_i,-1*_step);								
							}
							_to = setTimeout('_switch()',5000);
						}						
					}
					_handlers[_handlers.length] = __a;
				}				
			}
			
			var lis = ul.getElementsByTagName('li');
			total_cnt = lis.length;
			
			var ulw = (total_cnt/liPerDivs[i]) * _steps[i] + _fix[i];

			if(ulw > ul.parentNode.parentNode.offsetWidth)
			{
				ul.parentNode.id = "slide-div-" + i;
				ul.parentNode.style.position = "absolute";
				ul_offsetWidth = ul.offsetWidth;		
				ul.parentNode._ul_offsetWidth = ul_offsetWidth;
				
				ul.parentNode.mooeffect = new Fx.Styles( ul.parentNode.id, {wait:true,duration: 600,
								
							onStart: function(){
							},
							onComplete: function(){
														
								if(this.element.offsetLeft > this.element._start)
								{
									this.set({'left': this.element._end});
								}
								else if(this.element.offsetLeft < this.element._end)
								{
									this.set({'left': this.element._start});					
								}
							}		
					
					});
					
				ul.innerHTML = ul.innerHTML + ul.innerHTML + ul.innerHTML;
		
				ul.parentNode.mooeffect.set({'left': ul.parentNode.offsetLeft -1 * ulw });
		
				ul.parentNode._start = ul.parentNode.offsetLeft;
				ul.parentNode._end = ul.parentNode.offsetLeft - ulw +  __steps[i];
				
				if(scrollButtonIds[scrollIds[i]])
				{
					var aprev = document.getElementById(scrollButtonIds[scrollIds[i]][0]);
					if(aprev)
					{
						aprev.href = 'javascript:;';
						aprev._index = i;
						aprev.onclick = function(){
							_prev(this._index,1);
						}
					}
					var anext = document.getElementById(scrollButtonIds[scrollIds[i]][1]);
					if(anext)
					{
						anext.href = 'javascript:;';
						anext._index = i;
						anext.onclick = function(){
							_next(this._index,1);
						}
					}
				}
				
				ul._isrepeat = false;
				ul._currstep = 0;
				
				if(scrollIsRepeat[i])
				{
					ul._isrepeat = scrollIsRepeat[i];
				}				
				scrollObjs[i] = ul;
				scrollObjs[i].handlers = _handlers;
			}
		}
	}

	_to = setTimeout('_switch()',2000);

}

function _next(_index,step)
{
	var ul = scrollObjs[_index];
	if(ul)
	{
		scrollObjs[_index]._currstep = scrollObjs[_index]._currstep + step;
		var __step = _steps[_index];
		if(__steps[_index]) __step = __steps[_index];
		if(ul._isrepeat)
		{	
			ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft - step*__step]});
		}
		else
		{
			if(ul.parentNode.offsetLeft > ul.parentNode._end)
			{
				ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft - step*__step]});
			}
		}
	}
}

function _prev(_index,step)
{
	var ul = scrollObjs[_index];
	if(ul)
	{
		scrollObjs[_index]._currstep = scrollObjs[_index]._currstep - step;
		var __step = _steps[_index];
		if(__steps[_index]) __step = __steps[_index];
		if(ul._isrepeat)
		{
			ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft + step*__step]});
		}
		else
		{
			if(ul.parentNode.offsetLeft < ul.parentNode._start)
			{
				ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft + step*__step]});		
			}		
		}
	}
}
var _to = false;
function _switch()
{
	var ul = scrollObjs[0];
	if(ul)
	{
		if(ul._currstep + 1 >= _handlers.length)
		{
			_handlers[0].onclick();
		}
		else
		{
			_handlers[ul._currstep + 1].onclick();			
		}
	}
}


if (window.addEventListener)
	window.addEventListener("load", initScroll, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScroll);

