﻿// JSLint
// var $, setTimeout, clearTimeout;

/*
** Animates the Exhibition Stand images to fade from
** one to the next.
*/
function pkSwapImage() {
	var	index = 0,
			fadeTime = 2000,
			swapDelay = 4000,
			images = [
				$("img#stand01"),
				$("img#stand02"),
				$("img#stand03"),
				$("img#stand04")
			];


	// Fades out the current image and fades in the next image
	function swapImage() {
		images[index].fadeOut(fadeTime);
		index++;
		if (index >= images.length) {
			index = 0;
		}
		images[index].fadeIn(fadeTime, function() {
			setTimeout(function() {
				swapImage();
			}, swapDelay);
		});
	}

	// Call the inital swapImage() to start the ball rolling
	setTimeout(function() {
		swapImage();
	}, swapDelay);
}


/*
** Creates a vertical marquee for the forthcoming events
*/
function pkEventScroller() {
	var	$ul = $("ul#events"),
			$items, $out, $in,
			scrollTimer = null,
			scrollSpeed = 3000,
			scrollDelay = 4000,
			scrollOffset = 35;

	function clearTimer() {
		if (scrollTimer) {
			clearTimeout(scrollTimer);
		}
		scrollTimer = null;
	}
		
	function scroll() {
		clearTimer();
		$items = $ul.children();
		$out = $items.eq(0);
		$in =  $items.eq(1);
		
		$out.css("top", "0");
		$in.css("top", scrollOffset + "px");
		
		$out.animate({"top": -scrollOffset + "px"}, scrollSpeed);
		$in.animate({"top": "0"}, scrollSpeed, function() {
			$out.removeClass("first").remove().appendTo($ul);
			scrollTimer = setTimeout(function() {
				scroll();
			}, scrollDelay);
		});
	}
	
	function start (delay) {
		clearTimer();
		scrollTimer = setTimeout(function() {
			scroll();
		}, delay || scrollDelay);
	}
	
	function stop() {
		clearTimer();
	}
	
	start();
}


/*
** Page entry point
*/
$(function() {
	pkSwapImage();
	// pkEventScroller();
});