var g_bAnimationRunning = false;
var g_iCurrentIndex = 0;
var g_iNumImages = g_BannerURLs.length;

function highlightLinks()
{
	$('#scrollbox_footer a').removeClass();
	$('#scrollbox_footer a#scrolltext'+g_iCurrentIndex).addClass('selected');
}

function highlightMap()
{
	$('#rmap a').removeClass();
	$('#rmap a#mappoint'+g_iCurrentIndex).addClass('selected');
}

function initializeAnimation(bRight, iNewIndex)
{
	g_iCurrentIndex = iNewIndex;
	highlightLinks();
	highlightMap();


	$('div#scroller').css('left', '-958px');

	var itemid = (bRight ? 'scrollitem3' : 'scrollitem1');
	var scrollitem = document.getElementById(itemid);
	
	//var newsrc = "/banner/0"+(iNewIndex+1)+".jpg";
	var newsrc = g_BannerURLs[iNewIndex];

	//preloader test
	//newsrc = "http://www.letsgodigital.org/images/artikelen/35/d90-test-photo.jpg";

	$('#loader').fadeIn('slow');

	scrollitem.onload = function() {
		$('#loader').hide();
		scrollitem.onload = null;
		startAnimation(bRight, iNewIndex);
	} 
	scrollitem.src = newsrc;
}

function doAnimation(bRight, iNewIndex)
{
	if(g_bAnimationRunning) {
		return;
	}

	g_bAnimationRunning = true;

	initializeAnimation(bRight, iNewIndex);
}

function startAnimation(bRight, iNewIndex)
{
	var amount = (bRight ? "-958" : "958");

	$('#scroller').animate({
		left: '+='+amount
	}, 1500, function() {
		finishAnimation(bRight, iNewIndex);
	});
}

function finishAnimation(bRight, iNewIndex)
{
	if(bRight) {
		$('img#scrollitem2').attr("src", $('img#scrollitem3').attr("src"));
		$('img#scrollitem1').attr("src", $('img#scrollitem3').attr("src"));
	} else {
		$('img#scrollitem2').attr("src", $('img#scrollitem1').attr("src"));
		$('img#scrollitem3').attr("src", $('img#scrollitem1').attr("src"));
	}

	g_bAnimationRunning = false;
}

function determineNextIndex(bRight)
{
	var nextidx = g_iCurrentIndex;

	if(bRight) {
		nextidx++;
	} else {
		nextidx--;
	}

	if(nextidx < 0) {
		nextidx = g_iNumImages-1;
	} else if(nextidx >= g_iNumImages) {
		nextidx = 0;
	}

	return nextidx;
}

$(document).ready(function() {
	$('#scrollbox_buttonleft').click(function() {
		doAnimation(false, determineNextIndex(false));
	});

	$('#scrollbox_buttonright').click(function() {
		doAnimation(true, determineNextIndex(true));
	});

	$('#scrollbox_footer a').click(function() {
		var i = parseInt(this.rel);
		doAnimation(true, i);
	});
	$('#scrollbox_detailclickarea').click(function() {
		var targeturl = g_BannerTargetURLs[g_iCurrentIndex];
		document.location = targeturl;
	});

	$('#rmap a').click(function() {
		var i = parseInt(this.rel);
		doAnimation(true, i);
	});

	highlightLinks();
	highlightMap();

});


