$(document).ready(function() {
	
	// add selected box to the first link
	$('#image_selectors a:first').addClass('selected');

	// do the image swap
	$('#image_selectors a').click(function() {
		// make the clicked link selected
		$(this).parent().find('a').removeClass('selected');
		$(this).addClass('selected');
		// get the href and change the src of the main image
		var href = $(this).attr('href');
		$('#main_image').attr('src',href);
		return false;	
	});
	
	// next image...
	$('.next-image').click(function() {
		var current = '';
		var count = '';
		$('#image_selectors a').each(function(i) {
			if ($(this).hasClass('selected')) {
				current = i;
			}
			count = i;
		});
		$(this).parent().find('a').removeClass('selected');
		if (current == count) {
			$('#image_selectors a:first').trigger('click');
		} else {
			$('#image_selectors a').each(function(i) {
				if (i == (current+1)) {
					$(this).trigger('click');
				}
			});
		}
		return false;
	});
	
	// previous image...
	$('.previous-image').click(function() {
		var current = '';
		var count = '';
		$('#image_selectors a').each(function(i) {
			if ($(this).hasClass('selected')) {
				current = i;
			}
			count = i;
		});
		$(this).parent().find('a').removeClass('selected');
		if (current == 0) {
			$('#image_selectors a:last').trigger('click');
		} else {
			$('#image_selectors a').each(function(i) {
				if (i == (current-1)) {
					$(this).trigger('click');
				}
			});
		}
		return false;
	});
	
});
