var imageFolder = '/images/slideshow/';
var galleryImages = new Array(
	/* Format: image, caption, link_url */
	new Array('image6.jpg', 'MAADA&rsquo;s  40,000 square foot office and training facility in Atlanta.', ''),
	new Array('image1.jpg', 'The Academy for Automotive Professionals offers free sales training to selected applicants', ''),
	new Array('image2.jpg', 'Our Fixed Operations training provides hands on experience for aspiring technicians.', ''),
		new Array('image4.jpg', 'The Virtual Academy for Automotive Professionals was launched in 2008.', ''),
	new Array('image5.jpg', 'Our annual International Auto Show is recognized as one of the nation&rsquo;s best.', '')
);
var targetImageId = 'gallery_target';
var targetCaptionId = 'gallery_caption';
var pauseDuration = 4000; //milliseconds
var galleryTop = 188;
var galleryHeight = 270;
var gallerySmallImageHeight = 97;
var galleryListHeight = gallerySmallImageHeight * galleryImages.length;
var galleryAutoAdvance = true;

//Internal variables
var galleryCurrentIndex = 0;
var galleryAutoAdvanceId = 0;
var galleryScrollId = 0;
var galleryScrollDirection = 0;

function startGallery() {
	//Build links
	var tmp = '';
	for (var i=0; i < galleryImages.length; i++) {
		tmp += '<a href="#" onclick="loadImage('+ i+ '); return false;">';
		tmp += '<img src="'+ imageFolder+ galleryImages[i][0]+ '" alt="" /></a>';
	}
	$('gallery_list').innerHTML = tmp;
	
	showImageNow(0);
	setInterval('galleryScroll()', 20);
	if (galleryAutoAdvance) {
		galleryAutoAdvanceId = setInterval('galleryAdvance()', 5000);
	}
}

function loadImage(index) {
	if (index < 0 || index >= galleryImages.length) return;
	
	new Effect.Opacity(targetImageId, {from:1, to:0, duration:0.5});
	new Effect.Opacity(targetCaptionId, {to:0, duration:0.5});
	setTimeout('showImageNow('+index+')', 500);
	clearInterval(galleryAutoAdvanceId);
	galleryAutoAdvanceId = -1;
}
function showImageNow(index) {
	$(targetImageId).innerHTML = '<img id="gallery_target" src="'+ imageFolder+ galleryImages[index][0]+ '" alt="" />';
	if (galleryImages[index].length > 2 && galleryImages[index][2] != '') {
		$(targetImageId).innerHTML = '<a href="'+ galleryImages[index][2]+ '">'+ $(targetImageId).innerHTML+ '</a>';
	}
	
	$(targetCaptionId).innerHTML = galleryImages[index][1];
	new Effect.Opacity(targetImageId, {from:0, to:1, duration:0.5});
	new Effect.Opacity(targetCaptionId, {from:0.0, to:0.9, duration:0.5});
	
	if (galleryAutoAdvanceId != -1) {
		galleryScrollDirection = 0;
		var newTop = 0 - gallerySmallImageHeight * (index + 1);
		if (newTop > 0) newTop = 0;
		else if (newTop < 0 - galleryListHeight + galleryHeight) {
			newTop = Number(0 - galleryListHeight) + galleryHeight;
		}
		//$('gallery_list').style.top = newTop + 'px';
		new Effect.Morph('gallery_list', {
				style: 'top:'+ newTop+ 'px',
				duration: 0.5
		});
	}
}
function galleryAdvance() {
	galleryCurrentIndex = (galleryCurrentIndex + 1) % galleryImages.length;
	
	new Effect.Opacity(targetImageId, {from:1, to:0, duration:0.5});
	new Effect.Opacity(targetCaptionId, {to:0, duration:0.5});
	setTimeout('showImageNow('+ galleryCurrentIndex+ ')', 500);
}

function galleryScroll(e) {
	if (e == null) {
		var x = $('gallery_list');
		var newTop = Number(numeric(x.style.top)) + galleryScrollDirection;
		if (newTop > 0) {
			x.style.top = '0px';
		} else if (newTop < 0 - galleryListHeight + galleryHeight) {
			x.style.top = Number(0 - galleryListHeight) + galleryHeight + 'px';
		} else {
			x.style.top = newTop + 'px';
		}
	} else {
		var break1 = galleryTop + galleryHeight * 1 / 7;
		var break2 = galleryTop + galleryHeight * 2 / 7;
		var break3 = galleryTop + galleryHeight * 5 / 7;
		var break4 = galleryTop + galleryHeight * 6 / 7;
		
		if (e.clientY < break1) {
			galleryScrollDirection = 5;
		//} else if (e.clientY < break2) {
		//	galleryScrollDirection = 2;
		} else if (e.clientY > break4) {
			galleryScrollDirection = -5;
		//} else if (e.clientY > break3) {
		//	galleryScrollDirection = -2;
		} else {
			galleryScrollDirection = 0;
		}
	}
}
function galleryStopScroll(e) {
	if (e.clientY < galleryTop || e.clientY > galleryTop + galleryHeight) {
		galleryScrollDirection = 0;
	}
}


function numeric(string) {
	string = String(string);
	if (string == null || string == '') return 0;
	var i = string.search(/[^0-9-]/);
	if (i == -1) {
		return string;
	} else if (i == 0) {
		return numeric(string.substr(1));
	} else {
		return string.substr(0,i);
	}
}