var scrollposition = 0;
var _steps = [4*232,1*717];
var liPerDivs = [4,1];
var _fix = [0,0];
var scrollIds = ['step-scroll','img-gallery'];
var scrollIsRepeat = [false,false];
var scrollButtonIds = {'step-scroll':['btn-prev','btn-next']};

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 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_offsetWidth = ul.offsetWidth;		
				ul.parentNode._ul_offsetWidth = ul_offsetWidth;
				
				ul.parentNode.mooeffect = new Fx.Styles( ul.parentNode.id, {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;
			}
			
		}
	}

	if(scrollObjs[1])
	{
		var _node = document.getElementById('gallery-buttons');
		if(_node)
		{
			var _nodes = _node.getElementsByTagName('a');
			for(var i = 0; i < _nodes.length; i++)
			{
				var _img = _nodes[i].getElementsByTagName('img');
				if(_img[0])
				{
					_nodes[i]._img = _img[0];
				}
				_nodes[i].href = "javascript:;";
				_nodes[i]._index = i;
				_nodes[i].onclick = function()
				{
					var _nodes = _node.parentNode.getElementsByTagName('a');
					for(var i = 0; i < _nodes.length; i++)
					{
						if(_nodes[i].className.indexOf('active') != -1)
						{
							_nodes[i].className = _nodes[i].className.replace("active","");
							_nodes[i]._img.src = _nodes[i]._img.src.replace('-active.gif','.gif');
						}
					}
					
					this.className += " active";
					this._img.src = this._img.src.replace('.gif','-active.gif');
					
					var _step = this._index - scrollObjs[1]._currstep;
					if(_step > 0)
					{
						_next(1,_step);
					}
					else if(_step < 0)
					{
						_prev(1,(-1)*_step);
					}
				}
			}
		}
	}

}

function _next(_index,step)
{
	var ul = scrollObjs[_index];
	if(ul)
	{
		scrollObjs[_index]._currstep = scrollObjs[_index]._currstep + step;
		if(ul._isrepeat)
		{
			ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft - step*_steps[_index]]});
		}
		else
		{
			if(ul.parentNode.offsetLeft > ul.parentNode._end)
			{
				ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft - step*_steps[_index]]});
			}
		}
	}
}

function _prev(_index,step)
{
	var ul = scrollObjs[_index];
	if(ul)
	{
		scrollObjs[_index]._currstep = scrollObjs[_index]._currstep - step;
		if(ul._isrepeat)
		{
			ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft + step*_steps[_index]]});
		}
		else
		{
			if(ul.parentNode.offsetLeft < ul.parentNode._start)
			{
				ul.parentNode.mooeffect.custom({'left':[ul.parentNode.offsetLeft,ul.parentNode.offsetLeft + step*_steps[_index]]});		
			}		
		}
	}
}

if (window.addEventListener)
	window.addEventListener("load", initScroll, false);
else if (window.attachEvent)
	window.attachEvent("onload", initScroll);
	
function initTips() {
		var Tips1 = new Tips("img.tips");
}

if (window.addEventListener) {
	window.addEventListener("load", initTips, false);
}
else if (window.attachEvent) {
	window.attachEvent("onload", initTips);
}