var autoScrollDelay = 40; // ms
var maxWidth = 0;
var width = new Array();
var paused = false;
var numImages = 0;
var imageWidth = 200; // px
var viewPortWidth = 3; // pics
var set = 0;
var chainId = 0;
var debug = 0;
var start = new Date();
var appBase = null;

function startScroll(base, id, num) {
	debugNotice('Initializing Scroller...');
	
	appBase = base;
	numImages = num;
	set = 1;
	//width[set] = imageWidth * viewPortWidth;
	width[set] = 0;
	if (num < 10) {
		maxWidth = num * 200 - 400;
	} else {
		maxWidth = 10 * 200;
	}
	chainId = id;
	
	debugNotice('Number of images: ' + numImages);
	debugNotice('Current set: 1');	
	//debugNotice('Adjusting width of first set: ' + width[set]);
	debugNotice('Pausing for 2 seconds...');
	
	setTimeout('autoScroll()', 2000);
	
	if (num > 10) {
		setTimeout('loadNextSet()', 5000);
	}
	
	$('imageContainer').style.display = '';
	$('restart').style.display = 'none';
}

function pauseScroll() {
	debugNotice('Pausing scroll...');
	paused = true;
}

function resumeScroll() {
	debugNotice('Resuming scroll...');
	paused = false;
}

debugCount = 1;
function autoScroll() {
	try {
		if (!paused && numImages > 3) {
			if ((set == 0 && width[set] == (maxWidth + 600)) || (width[set] == (maxWidth - 600) && $('imageBox' + (set + 1)) != null)) {
				debugNotice('Preparing set ' + (set + 1) + ' for scroll...');		
				width[(set + 1)] = 0;
			}	
			
			for (i = 1; i <= width.length; i++) {
				if (i == 0 || width[i] == undefined || !$('imageBox' + i)) {
					continue;
				}
				
				if((width[i] <= (maxWidth + 600))) {
					if (debugCount == 50) {
						debugNotice('Set ' + i + ' moved 50 pixels (' + width[i] + '/' + maxWidth + ')');
						//debugNotice('Sets loaded: ' + width.length);
						debugCount = 1;
					} else {
						debugCount++;
					}
					width[i]++;
					new Effect.Move('imageBox' + i, { x: -1, y: 0, duration: 0});
				} else  {
					debugNotice('Removing set: ' + set);
					var currentDiv = $('imageBox' + set);
					currentDiv.parentNode.removeChild(currentDiv);
					
					if ($('imageBox' + (set + 1))) {
						debugNotice('Finished with previous set. Adjusting Next set...');
						new Effect.Move('imageBox' + (set + 1), { x: 0, y: 150, duration: 0 });
						
						debugNotice('Changing sets');
						set++;							
							
						loadNextSet();
					} else {
						debugNotice('No more images. Restarting scroller...');
						//$('imageContainer').style.display = 'none';
						//$('restart').style.display = '';
						pauseScroll();
						
						set = 0;
						loadNextSet(true);
					}
				
					delete width[i];
				}
			}
		}
		
		if (width[set]) {
			setTimeout('autoScroll()', autoScrollDelay);
		}
	} catch (exception) {
		debugNotice('An error occurred: ' + exception);	
	}
}

function loadNextSet(restart) {
	var url = '/';
	
	debugNotice('Retrieving Next Set');
	
	if (restart) {
		new Ajax.Updater('imageContainer', appBase + '/links/load_set/' + chainId + '/' + (set + 1), {
			method: 'get',
			insertion: Insertion.Bottom,
			onComplete: function () {
				$('imageBox' + (set + 1)).style.position = 'relative';

				initLightbox();
				debugNotice('First set recieved');
				
				set++;	
				width[set] = 0;
				resumeScroll();
				setTimeout('autoScroll()', 2000);
			}
		});
	} else {
		new Ajax.Updater('imageContainer', appBase + '/links/load_set/' + chainId + '/' + (set + 1), {
			method: 'get',
			insertion: Insertion.Bottom,
			onComplete: function () {
				$('imageBox' + (set + 1)).style.position = 'relative';
				new Effect.Move('imageBox' + (set + 1), { x: imageWidth * viewPortWidth, y: -150, duration: 0 });
				initLightbox();
				debugNotice('Next set recieved');
			}
		});		
	}
}

function debugNotice(string) {
	now = new Date();
	
	if (debug && console != null) {
		console.debug(Math.round(((now.getTime() - start.getTime()) / 1000) * 100) / 100 + ': ' + string);
	}
}
