var slideIndex = 0;
var totalSlides = 6;
var fadeSpeed = 2000;
var slideSpeed = 6000;

function slideSwitch2()
{
    var $items = $('#slideshow a, #slideshow div');
	var $ele = $('#slideshowmenu ul a');
	$ele.removeClass("active");
	$items.removeClass("active");

	if (slideIndex == totalSlides)
		slideIndex = 0;
/*
	$items.fadeOut(fadeSpeed, function()
	{
		$($ele[slideIndex + 1]).addClass("active");
		$($items[slideIndex]).fadeIn(fadeSpeed);
	});
*/

	$items.fadeOut(fadeSpeed);
	$($items[slideIndex]).fadeIn(fadeSpeed);
	$($ele[++slideIndex]).addClass("active");

	//$('#slidemessage').html($('#slidemessage').html() + ", " + slideIndex)
}
function slideSwitch()
{
	var $active = $('#slideshow a.active');
	var $ele = $('#slideshowmenu ul a');
	$ele.removeClass("active");
	if ($active.length == 0)
	{
		$active = $('#slideshow a:last');
	}


	// use this to pull the images in the order they appear in the markup
	var $next;
	if ($active.next().length > 0)
		$next = $active.next();
	else
	{
		$next = $('#slideshow a:first');
		slideIndex = 0;
	}
	//var $next =  $active.next().length ? $active.next() : $('#slideshow a:first'); slideIndex = 0;


	$active.addClass('last-active');

	$next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 10, function()
        {
        	$active.removeClass('active last-active');
        });
	slideIndex++;
	$($ele[slideIndex]).addClass("active");



}
var slideFTime = true
function slideStart()
{
/*	//does not fire in IE!
	$('#slideshow a:first-child img').load(function()
	{
		slideSelect2(slideIndex);
		$('#slideshow').everyTime(slideSpeed, function()
		{
			slideSwitch2();
		});
	});
*/
	slideSelect2(++slideIndex);
	$('#slideshow').everyTime(5000, function()
	{
		slideSwitch2();
	});
}
function slideSelect2(num)
{
	$('#slideshow').stopTime();
	var $ele = $('#slideshow a, #slideshow div');

	var $mele = $('#slideshowmenu ul a');
	$mele.removeClass("active");

	if (num > totalSlides)
		num = 1;

	var $current = $($ele[num - 1]);
	$ele.fadeOut(fadeSpeed);

	$current.fadeIn(fadeSpeed);
	slideIndex = num;
	$($mele[slideIndex]).addClass("active");
}
function slideSelect(num)
{


	jQuery.each(jQuery.timer.global, function(index, item)
	{
		jQuery.timer.remove(item);
	});
	var $ele = $('#slideshow a');


	var $mele = $('#slideshowmenu ul a');
	$mele.removeClass("active");

	var $current = $($ele[num - 1]);

	$ele.removeClass('active last-active');

	$current.css({ opacity: 1.0 }).addClass('active');

	//$ele.removeClass('last-active');

	slideIndex = num;
	$($mele[slideIndex]).addClass("active");
}
function slideSetup(totalSlidesVal)
{
	totalSlides = totalSlidesVal;
	//alert(totalSlides);
	$('#slideshowmenu ul a').each(function()
	{
		if ($(this).attr("rel") == "play")
		{
			$(this).click(function()
			{
				//slideSelect2(1);
				slideStart();
				return false;
			});
		}
		else
		{
			$(this).click(function()
			{
				slideSelect2($(this).attr("rel"));
				return false;
			});
		}
	});

	//set a time out before playing the slide so as to "smooth out" the loading process
	setTimeout("slideStart()", 2000);
}

