$(document).ready(function () {
	
	var loadCounter = 0;
	var images = $('.depth_mini > a > img');
	var activeImage = null;
	var link = $('.depth_mini > a');
	var mode = 'next';
	
	var hasHover = false;
	var hasFocus = false;
		
	var animation = null;
	
	var animate = function () {
		if (mode == 'next') {
			elem = activeImage.next();
			if (typeof elem.attr('src') == 'undefined') {
				mode = 'prev';
				activeImage = activeImage.fadeOut().prev();
			} else {
				activeImage = elem.fadeIn();
			}
		} else {
			elem = activeImage.prev();
			if (typeof elem.attr('src') == 'undefined') {
				mode = 'next';
				elem = activeImage.next();
				activeImage = elem.fadeIn();
			} else {
				activeImage = activeImage.fadeOut().prev();
			}
		}
	}
	
	var startAnimation = function () {
		if (!hasHover && !hasFocus) {
			animate();
			animation = setInterval(animate, 1300);
		}
	}
	
	var stopAnimation = function () {
		if (!hasHover && !hasFocus) {
			clearInterval(animation);
		}
	}
	
	images.attr('style', 'display:none')
	.load( function () {
		loadCounter++;
		if (loadCounter == images.size()) {
			link.hover(function (event) {
				startAnimation(event);
				hasHover = true;
			}, function (event) {
				hasHover = false;
				stopAnimation(event);
			}).bind('focus', function (event) {
				startAnimation(event);
				hasFocus = true;
			}).bind('blur', function (event) {
				hasFocus = false;
				stopAnimation(event);
			});
			activeImage = images.eq(0).fadeIn();
		}
	}).each(function () {
		var src = $(this).attr('src');
		$(this).attr('src', '');
		$(this).attr('src', src);
	}); 
	
});






