﻿function slideSwitch(pos) {

	var $next;
	var $active = $j('#slideshow a.activeSlide');
	if ($active.length == 0)
		$active = $j('#slideshow a:last');

	var $children = $active.parent().children();


	if (pos != null) {//use specfic $next
		$next = $j($children[pos]);
	} else {//figure out $next
		$next = $active.next().length ? $active.next()
	         : $j('#slideshow a:first');
	}

	if (!$next.hasClass("activeSlide")) {
		$active.addClass('last-activeSlide');

		var $activeSlideLink = $j('#slide-nav a.active');
		$activeSlideLink.removeClass('active');
		var $childrenLinks = $activeSlideLink.parent().children();

		var $nextLink = $j($childrenLinks[$children.index($next)]);
		$nextLink.addClass('active');

		$next.css({ opacity: 0.0 })
	          .addClass('activeSlide')
	          .animate({ opacity: 1.0 }, 1000, function() {
	          	$active.removeClass('activeSlide last-activeSlide');
	          });
	}
}

function initSlideNav() {
	var $slide = $j('#slideshow');
	var $slideNav = $j('#slide-nav');
	var numberOfSlides = $slide.children().length;
	var slideNavHtml = "";
	for (i = 0; i < numberOfSlides; i++) {
		if (i == 0) {
			slideNavHtml += "<a id='slide-link-" + i + "' href='#' onclick='javascript:slideSwitch(" + i + ");' class='active' >" + (i + 1) + "</a>";
		} else {
			slideNavHtml += "<a id='slide-link-" + i + "' href='#' onclick='javascript:slideSwitch(" + i + ");' >" + (i + 1) + "</a>";
		}
	}
	$slideNav.html(slideNavHtml);
}

$j(function() {
	var slideShowDelaySeconds = 5;
	try {
		initSlideNav();
		setInterval("slideSwitch()", slideShowDelaySeconds * 1000);
	}
	catch (e) { }
});
