swapTo = 1;

function swapImages(manualSwap) {
	// Swapper images
	// Sanitize swapTo, manualSwap overrides "automatic" swap
	if (manualSwap != undefined) swapTo = manualSwap;
	if (swapTo < 0 || swapTo > (images.length - 1)) swapTo = 0;

	// Create and add new image to DOM
	var next = $('<img>').attr('src', 'images/' + images[swapTo]);
	$('#imageSwap').append(next);

	// Fade in new image and remove old one after that
	$('#imageSwap img:last').fadeIn(function() {
		$('#imageSwap img:last').addClass('active');
		$('#imageSwap img:first').remove();
	});

	// Fade "tabs" to full / half opacity
	$('ul#buttonSwap li.active').fadeTo(600, 1).removeClass('active');
	$('ul#buttonSwap li#slide_' + swapTo).fadeTo(600, 1).addClass('active');

	if(links[swapTo] == '') {
		$('#click_here').css('display','none');
	} else {
		$('#click_here').css('display','block');
		$('#click_here a').attr('href',links[swapTo]);
	}
	// Increase automatic swap
	swapTo++;
}

$(document).ready(function() {
	// Run our swapImages() function every 5secs
	var imageSwapper = setInterval('swapImages()', 7000);

	// Add manual swap functionality for each tab
	$('ul#buttonSwap li').bind('click', function() {
		clearInterval(imageSwapper);
		swapTo = $(this).attr('id').substr(6);
		swapImages();
	});
});
