(function($) {

var imagebox = $("#imagebox");
var slides = imagebox.find(".slides");
var currentImageIndex = 0;

//create images from the UL list

imagebox.find("ul a").each(function() {
	
	var link = $(this);
	
	$("<img />").attr({
					src : link.attr("href"),
					alt : link.html(),
					onclick : link.attr("onclick")
					})
				.hide()
				.appendTo(slides)
				.click(function(){
					window.open(link.attr("target"), '_self')
				});
});

//put all images in one node set and hide the list

var images = slides.find("img");
imagebox.find("ul").hide();

//save a reference to the first image

var activeImage = images.eq(currentImageIndex);

//show the next or previous image

function showImage(newIndex) {
	currentImageIndex = newIndex >= images.length ? 0 : newIndex;
	currentImageIndex = currentImageIndex < 0 ? images.length - 1 : currentImageIndex;
	
	activeImage.fadeOut(2000);
	activeImage = images.eq(currentImageIndex).fadeIn(2000);
}

//start timer to cycle images

var interval = setInterval(function() {
	showImage(currentImageIndex + 1);
}, 7000);


})(jQuery);




