$(function(){
	$.preLoadImages = function(images) {
		var args_len = images.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = images[i];
		}
	};
	
	function startRotation()
	{
		window.indexPicsTimer = setInterval(function(){
		
			if (!window.indexPicsTimer) return;

			var targetElem = $('#index-pics img');
			if (typeof(indexPics) == 'undefined' || typeof(indexPics.length) == 'undefined' || !indexPics.length) return;

			if (targetElem.attr('src') == indexPics[indexPicsCurrent])
				indexPicsCurrent++;

			if (typeof(indexPics[indexPicsCurrent]) == 'undefined' || indexPics[indexPicsCurrent] == 'last')
				indexPicsCurrent = 0;

			targetElem.fadeOut('slow', function(){
				targetElem.attr('src', indexPics[indexPicsCurrent]);
				
				targetElem.fadeIn('slow', function(){
					indexPicsCurrent++;
				});
			});

		}, 10000);
	}
	function stopRotation()
	{
		if (window.indexPicsTimer)
		{
			clearInterval(window.indexPicsTimer);
		}
			
		window.indexPicsTimer = null;
	}
	
	$('.services li').mouseleave(function(a){
		startRotation();
	});
	
	$('.services li').mouseenter(function(e, a){
		stopRotation();
		var tag = $(this).find('a').attr('tag');
		var targetElem = $('#index-pics img');
		targetElem.fadeOut('slow', function(){
			targetElem.attr('src', indexPicsTagged[tag]);
			
			targetElem.fadeIn('slow', function(){
				indexPicsCurrent++;
			});
		});
	});
	
	if (window['indexPics'])
	{
		$.preLoadImages(indexPics);

		startRotation();
	}
});

