$(function() {
	var container = $('.sfimagesListRotator'),
		images = container.find('.sfimageListItem'),
		count = images.size(),
		current = -1;
	images.each(function(index) {
		var indicator = $('<div class="sfimageIndicator"></div>');
		if (index == 0) {
			indicator.addClass('current');
		}
		container.after(indicator);
	});

	var indicators = container.parent().find('.sfimageIndicator');
	indicators.click(function() {
		select(indicators.index(this));
		clearTimeout(timeout);
	});

	var select = function(index, callback) {
		var old = current;
		current = index;
		if (old != current) {
			images.each(function(index, image) {
				if (index == old) {
					$(image).fadeOut().css('position', 'absolute').css('top', '10px');
				} else if (index == current) {
					$(image).fadeIn(callback).css('position', 'static');
				}
			});
			indicators.removeClass('current');
			indicators.eq(current).addClass('current');
		}
	};

	var timeout = null;
	(function() {
		var callback = arguments.callee;
		select((current + 1) % count, function() {
			timeout = setTimeout(callback, 5000);
		});
	})();
});

$(function() {
	$('.scroll').scrollbar({ arrows: false });

	$('#navigation li').hover(
		function() {
			var $this = $(this),
				fadeOut = $this.data('fadeOut');
			if (fadeOut) {
				clearTimeout(fadeOut);
			} else {
				$this.data('fadeIn',
					setTimeout(function() {
						$this.removeData('fadeIn');
						$this.children('ul').stop(true, true).fadeIn();
					}, 100)
				);
			}
		},
		function() {
			var $this = $(this),
				fadeIn = $this.data('fadeIn');
			if (fadeIn) {
				clearTimeout(fadeIn);
			} else {
				$this.data('fadeOut',
					setTimeout(function() {
						$this.removeData('fadeOut');
						$this.children('ul').stop(true, true).fadeOut();
					}, 500)
				);
			}
		}
	);
});
