var iMax;
var iCurrent = 1;


// Function to run on page load and DOM ready
Event.observe(window, 'load', function() {
	// Enable previous and next buttons
	prevButton = $('btnScrollPrev');
	prevButton.onclick = function() { 
		moveScroller('left'); return false;
	}
	nextButton = $('btnScrollNext');
	nextButton.onclick = function() {
		moveScroller('right'); return false;
	}
	
	// Set total number of promos
	iMax = $$('div.workScrollerPromo').length;

	// Set initial opacity
	aPromos = $$('div.workScrollerPromo');
	aPromos.each(function(s) {
		  Effect.Fade (s, { duration: .01, from: 1.0, to: 0.25, queue: { position: 'end', scope: 'scroller' } });
	});
	
	// Scroll in
	scroller = $('workScroller');
	new Effect.Move (scroller, { duration: 1, x: 0, y: 0, mode: 'absolute', queue: { position: 'end', scope: 'scroller' } });
});

function isMouseLeaveOrEnter(e, handler) {
	if (e.type != 'mouseout' && e.type != 'mouseover') return false;
	var reltg = e.relatedTarget ? e.relatedTarget : e.type == 'mouseout' ? e.toElement : e.fromElement;
	while (reltg && reltg != handler)
		reltg = reltg.parentNode;
		return (reltg != handler);
	}

function scrollShow(promo) {
	promo.style.cursor='pointer';
	Effect.Fade (promo, { duration: .25, from: .25, to: 1.0 });
}
function scrollFade(promo) {
	Effect.Fade (promo, { duration: .25, from: 1.0, to: .25 });
}
function scrollLink(promo) {
	var cslink = promo.select('.csLink');
	window.location.href=cslink[0].readAttribute('href');
}


// Function to move the scroller box
function moveScroller(direction) {
	scroller = $('workScroller');

	switch (direction) {
		case 'left':
			if (iCurrent != 1) {
				new Effect.Move (scroller, { duration: .5, x: +274, y: 0, mode: 'relative', queue: { position: 'end', scope: 'scroller' } });
				iCurrent--;
			}
			break;
		case 'right':
			if (iCurrent != (iMax-2)) {
				new Effect.Move (scroller, { duration: .5, x: -274, y: 0, mode: 'relative', queue: { position: 'end', scope: 'scroller' } });
				iCurrent++;
			}
			break;
	}
}