var ms = 4000;
var intervalId = null;
/*** 
    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');
	$(".slidHeader a.allProducts").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');
			$(".slidHeader a.allProducts").attr("href", $image.attr("href"));
		}
		
		$number = $('#slideNumbers a:first');
		while ($number.length != 0) {
			if (parseInt($number.attr("id").charAt(6)) == 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++;
	}
	$("#slideNumbers").html(htmlContent);
	if (countActive == 0) $('#slideNumbers A:first').addClass("active");
}


$(function() {
	initSlideChoice();
	$(".slidHeader a.allProducts").attr("href", $("#slideshow a:first").attr("href"));
	$("#slideshow a").click(function(){return false;});
	$("#slideNumbers a").live('click', function() {
		clearInterval(intervalId);
		number = $(this).attr("id").charAt(6);
		goToSlide(parseInt(number));
		intervalId = setInterval ("slideSwitch()", ms);
		return false;
	});
	intervalId = setInterval ("slideSwitch()", ms);
});

