function advance(num) {
	var current = $('#slideWrapper img');
	var next = $('#imageContainer img[src*=slide'+num+']').clone();
	next.appendTo('#slideWrapper');
	
	current.add(next[0]).animate({
		left: '-=779'
		}, 750, function() {
			current.remove();
			next.css('left',0);
	});
}


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


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


function slideshow(number) {
	$('#play').hide();
	$('#pause').show();
	var visible = $('#slideWrapper img');
	if(typeof(number) == 'undefined') {
		var number = visible.attr('src').match(/slide[0-9]+/)[0];
		number = number.replace(/slide/,'');
		number *= 1;
		number++;
	}
	if(number > $('#slideCircles img').length)
		number = 1;
	
	advance(number);
	setCircle(number);

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

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

$(function() {
	
	// slideshow
	if($('#headerSlides img').length > 0) {
		$('#headerSlides img').wrapAll('<div id="slideWrapper"></div>');
		$('body').append('<div id="imageContainer">\
			<img src="images/slide1.jpg" alt="Tell your story..." />\
			<img src="images/slide2.jpg" alt="Tell your story..." />\
			<img src="images/slide3.jpg" alt="Tell your story..." />\
			<img src="images/slide4.jpg" alt="Tell your story..." />\
			<img src="images/slide5.jpg" alt="Tell your story..." />\
			<img src="images/slide6.jpg" alt="Tell your story..." />\
			<img src="images/slide7.jpg" alt="Tell your story..." />\
		</div>');
		
		$('#slideCircles img').each(function() {
			$(this).mouseover(function() {
				var num = this.getAttribute('alt');
				clearTimeout(timerId);
				slideshow(num);
			});
			this.style.cursor = 'pointer';
		});
		timerId = setTimeout('slideshow()',7000);
	}
	
	
	$('#playPause').click(function() {
		$(this).children('div').toggle();
		if(timerId)
			pause();
		else
			timerId = setTimeout('slideshow()',1000);
	});
});