var currentImageId = 1;
var timeout;
var maxImages = false;
var time = 5000;

function slideSwitch(id) {
    var $active = $('.showcase img.active');
    var $nextImage = $('#showcaseImage'+id);

    if ( $active.length == 0 ) $active = $('.showcase img.last');

    var $next =  $active.next().length ? $active.next()
        : $('.showcase img.first');


    $active.addClass('last-active');
	
	if(currentImageId != id) {
	
    $nextImage.css({opacity: 0.0})
		.addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');
        });
        
       	currentImageId = id;
       	window.clearTimeout(timeout);
		timeout = setTimeout("showcaseNextOrPrev('next')", time);
		
       	showcaseText();
       	showcaseLink();    
    }
}

function showcaseText() {
	var span = document.getElementById("showcaseText");
	var alt = document.getElementById('showcaseImage'+currentImageId).alt;
	span.innerHTML = alt + '&nbsp;';
}

function showcaseLink() {
	for(var i=1;i<=maxImages;i++) {
		var anchor = document.getElementById("showcaseLink"+i);
		if(i == currentImageId) {
			anchor.className = "selected";
		} else {
			anchor.className = "";
		}
	}
}

function showcaseNextOrPrev(direction) {
	if(direction == "next") {
		if(currentImageId < maxImages) {
			slideSwitch(currentImageId + 1);
		} else {
			slideSwitch(1);
		}
	} else {
		if(currentImageId == 1) {
			slideSwitch(maxImages);
		} else {
			slideSwitch(currentImageId - 1);
		}
	}
}