/***
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
 ***/

function slideSwitch() {
	var $active = $('#slideshow a.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 =  $active.next().length ? $active.next()	: $('#slideshow a:first');

	// uncomment the 3 lines below to pull the images in random order
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );
	$active.addClass('last-active');

	$next.children('img').css({opacity: 0.0})
							.animate({opacity: 1.0}, 1000, function() {
								$active.removeClass('active last-active');
							});
	$next.addClass('active');
	$("a.view-all-products").attr("href", $next.attr("href"));
	initSlideChoice();
}


function goToSlide(number) {
	if (number != undefined) {

		var $image = $('#slideshow a:first');
		var $active = $('#slideshow a.active');

		if ( $active.length == 0 ) $active = $('#slideshow a:last');

		var i=1;
		while (i < number) {
			i++;
			$image = $image.next().length ? $image.next() : $('#slideshow a:first');
		}

		if ($image.hasClass("active") != true) {
			$active.addClass('last-active');

			$image.children('img').css({opacity: 0.0})
									.animate({opacity: 1.0}, 1000, function() {
										$active.removeClass('active last-active');
									});
			$image.addClass('active');
			$("a.view-all-products").attr("href", $image.attr("href"));
		}

		$number = $('#slideshow-numbers a:first');
		while ($number.length != 0) {
			if (getSlideIndex($number.attr("id")) == number)
				$number.addClass("active");
			else if ($number.hasClass("active"))
				$number.removeClass("active");
			$number = $number.next();
		}
	}
}


function initSlideChoice() {
	var $image = $('#slideshow a');
	var i = 1;
	var countActive = 0;
	var htmlContent = "";
	while ($image.length > 0) {
		if (($image.hasClass("active") == true) && ($image.hasClass("last-active") == false)) {
			htmlContent += "<a class='active' href='#' id='number" + i + "'>" + i + "</a>";
			countActive++;
		}
		else {
			htmlContent += "<a href='#' id='number" + i + "'>" + i + "</a>";
		}
		$image = $image.next();
		i++;
	}
	$("#slideshow-numbers").html(htmlContent);
	if (countActive == 0) $('#slideshow-numbers a:first').addClass("active");
}

function getSlideIndex(linkId) {
	regLinkId = new RegExp('^number([0-9]+)$', '');
	var resultLinkId = regLinkId.exec(linkId);
	var number = 0;
	if(resultLinkId) {
		number = parseInt(resultLinkId[1]);
	}
	return number;
}

var ms = 4000;
var intervalId = null;

$(function() {
	if ($("#slideshow a").length > 1) {
		initSlideChoice();
		$("a.view-all-products").attr("href", $("#slideshow a:first").attr("href"));
		$("#slideshow a").click(function(){return false;});
		$("#slideshow-numbers a").live('click', function() {
			clearInterval(intervalId);
			goToSlide(getSlideIndex($(this).attr("id")));
			intervalId = setInterval ("slideSwitch()", ms);
			return false;
		});
		intervalId = setInterval ("slideSwitch()", ms);
	}
});

