function advance(num) {
	var current = jQuery('#slideWrapper a');
	var next = jQuery('#imageContainer a:nth-child('+num+')').clone();
	next.appendTo('#slideWrapper');
	
	current.add(next[0]).animate({
		left: '-=779'
		}, 750, function() {
			current.remove();
			next.css('left',0);
	});
}

function slideshow(number) {
	jQuery('#play').hide();
	jQuery('#pause').show();
	var visible = jQuery('#slideWrapper a');
	
	if(typeof(number) == 'undefined') {
		if(typeof(slideShowCurImg) == 'undefined') {
			slideShowCurImg=2;			
		} else {
			slideShowCurImg++;
			if(slideShowCurImg > jQuery('#slideCircles img').length)
				slideShowCurImg = 1;				
		}
		number=slideShowCurImg;
	} else {
		slideShowCurImg=number;
	}
	
	advance(number);
	setCircle(number);

	timerId = setTimeout('slideshow()',7000);
}

function pause() {
	clearTimeout(timerId);
	timerId = null;
}

function setCircle(num) {
	jQuery('#slideCircles img').each(function() {
		var alt = this.getAttribute('alt');
		if(alt == num)
			this.setAttribute('src',site_theme_path+'/images/circleBlue.png');
		else
			this.setAttribute('src',site_theme_path+'/images/circleEmpty.png');
	});
}

// slideshow timer id (in case need to cancel
var timerId;
var slideShowCurImg;

jQuery(function() {
	
	// slideshow
	if(jQuery('#headerSlides a').length > 0) {
		jQuery('#headerSlides a').wrapAll('<div id="slideWrapper"></div>');
		jQuery('body').append(jQuery('#grabImageContainer').html());
		jQuery('#grabImageContainer').remove();
		
		jQuery('#slideCircles img').each(function() {
			jQuery(this).mouseover(function() {
				var num = this.getAttribute('alt');
				clearTimeout(timerId);
				slideshow(num);
			});
			this.style.cursor = 'pointer';
		});
		timerId = setTimeout('slideshow()',1000);
	}	
	
	jQuery('#playPause').click(function() {
		jQuery(this).children('div').toggle();
		if(timerId)
			pause();
		else
			timerId = setTimeout('slideshow()',1000);
	});
});
