function getElementsByClassName(clss, prnt, tg) {
    var elements = new Array();
    tg = tg || '*';
    prnt = prnt || document;
    var list = prnt.getElementsByTagName(tg);
    for (var i = 0; i < list.length; ++i) {
        if (list[i].className == clss) {
            elements.push(list[i]);
        }
    }
	return elements;
}

var ScrollReg = new scrollers();
var scrollStat = new Array ('inactive','active','hover');
var scrollRight = new Array();
var scrollLeft  = new Array();
for(i=0;i<scrollStat.length;i++)
{
	scrollRight[i] = new Image();
	scrollLeft[i]  = new Image();

	scrollRight[i].src = './images/scroller_right_'+ scrollStat[i] +'.gif'
	scrollLeft[i].src  = './images/scroller_left_'+ scrollStat[i] +'.gif'
}

function scrollers ()
{
	var sarr = new Array()
	this.add = add
	this.get = get

	function add(scr) {
		sarr.push(scr)
	}

	function get(id) {
		for(i=0;i<sarr.length;i++) {
			if(sarr[i].id == id) {
				return sarr[i]
			}
		}
	}
}

function nso () {
	var me = this
	this.id = ''
	this.dir = 0
	this.pos = 0
	this.offset = 0
	this.max = 15
	this.view = 10
	this.cpos = 0
	this.moving = false
	this.interval = ''
	/*This is to define how many pixels to move the news article each time, left or right*/
	//this.movearray = new Array ( 1, 2, 3, 5, 8, 12, 17, 23, 31,35,19,35,31, 23, 17, 12, 8, 5, 3, 2, 1 );
	//this.movearray = new Array ( 6, 5, 8, 12, 17, 23, 38, 23, 17, 12, 8, 5, 6 );
	this.movearray = new Array ( 5, 10, 15, 25, 40, 60, 85, 115, 180, 115, 85, 60, 40, 25, 15, 10, 5 );
	this.getStatus = getStatus
	this.updateIcons = updateIcons

	function getStatus(side)
	{
		if(side==0)
		{
			if(me.cpos==0)
				return 0
			return 1
		}

		if(side==1)
		{
			if( (me.cpos+me.view) < me.max)
				return 1
			return 0
		}

	}

	function updateIcons(hover,side)
	{
		if(hover)
		{
			if(side&1 && getStatus(0))
			document.getElementById('scrollleft_'+me.id).src = scrollLeft[2].src
			if(side&2 && getStatus(1))
			document.getElementById('scrollright_'+me.id).src = scrollRight[2].src
		} else {
			if(side&1)
			document.getElementById('scrollleft_'+me.id).src = scrollLeft[getStatus(0)].src
			if(side&2)
			document.getElementById('scrollright_'+me.id).src = scrollRight[getStatus(1)].src
		}
	}

}

function initScrollers()
{

	var scrollers = getElementsByClassName('scroll_container', document.getElementById('scroll-wrap'), 'div')
	for(i=0;i<scrollers.length;i++)
	{

		var scr = scrollers[i]
		var scid = scr.id

		var sc = document.getElementById( 'scrollcontent_'  + scid )

		var theScroller = new nso();
		theScroller.id = scid
		if(sc.style.left != '')
		{
			theScroller.offset = parseInt(sc.style.left);
		}

		// count children
		theScroller.max = getElementsByClassName('scrollPromoBox',document.getElementById('scrollshell_'+scid),'div').length
		theScroller.updateIcons(false,3)
		ScrollReg.add(theScroller)
	}
}


function scrollOnce(direction,scid)
{
	var theScroller = ScrollReg.get(scid)
	if(direction == "left")
		theScroller.dir = -1
	else
		theScroller.dir = 1
	
	if(!theScroller.moving && ((theScroller.dir>0 && (theScroller.cpos+theScroller.view)<theScroller.max )  ||  (theScroller.dir<0 && theScroller.cpos>0) ) )
	{
		theScroller.moving = true
		theScroller.cpos += theScroller.dir
		if(theScroller.dir > 0)
		{
			theScroller.updateIcons(false,1)
		} else {
			theScroller.updateIcons(false,2)
		}
		if(theScroller.getStatus(0) == 0)
			theScroller.updateIcons(false,1)
		if(theScroller.getStatus(1) == 0)
			theScroller.updateIcons(false,2)
		theScroller.interval = setInterval(function () { unitScroller(theScroller) },15)
	}
}

function unitScroller(theScroller)
{

	var sc = document.getElementById( 'scrollcontent_'  + theScroller.id )

	if(theScroller.pos == theScroller.movearray.length)
	{
		theScroller.pos = 0
		clearInterval(theScroller.interval)
		theScroller.moving = false
		return
	}

	theScroller.offset += ((-1 * theScroller.dir) * theScroller.movearray[theScroller.pos])
	sc.style.left = theScroller.offset + 'px'
	theScroller.pos++

}

window.onload = function() {
if(typeof(initScrollers) != "undefined") {
	    initScrollers();
	}
}
