var SLIDES = Array();

function next(slides) {
	resetInterval();
	current = find_highest_zindex_object(slides);
	lowest_zindex_object = find_lowest_zindex_object(slides);
	new_lowest_zindex = lowest_zindex_object.css('z-index') - 1;
	
	jQuery(current).fadeOut(3000, function() {
		jQuery(current).css('z-index', new_lowest_zindex);
		jQuery(current).show();
	});
}

function nextClicked(slides) {
	resetInterval();
	current = find_highest_zindex_object(slides);
	lowest_zindex_object = find_lowest_zindex_object(slides);
	new_lowest_zindex = lowest_zindex_object.css('z-index') - 1;
	
	jQuery(current).fadeOut(500, function() {
		jQuery(current).css('z-index', new_lowest_zindex);
		jQuery(current).show();
	});
}

function prev(slides) {
	resetInterval();
	current = find_highest_zindex_object(slides);
	// this object will be moved to the top
	lowest_zindex_object = find_lowest_zindex_object(slides);
	jQuery(lowest_zindex_object).hide();
	jQuery(lowest_zindex_object).css('z-index', parseInt(jQuery(current).css('z-index')) + 1);
	jQuery(lowest_zindex_object).fadeIn(500);
}

function find_lowest_zindex_object(collection) {
	lowest = jQuery(collection)[0];
	jQuery(collection).each(function() {
		if (parseInt(jQuery(this).css('z-index')) < parseInt(jQuery(lowest).css('z-index')))
			lowest = jQuery(this);
	});
	return jQuery(lowest);
}

function find_highest_zindex_object(collection) {
	highest = null;
	jQuery(collection).each(function() {
		if (highest == null || parseInt(jQuery(this).css('z-index')) > parseInt(jQuery(highest).css('z-index'))) {
			highest = jQuery(this);
		}
	});
	return jQuery(highest);
}

function resetInterval() {
	clearInterval(t);
	t = setInterval(function() {
		next(SLIDES);
	}, 7000);
}
