// JavaScript Document
function slideShow() {

var currImg = 0;
var totalImgs = myShow.length;

function init() {

	// generate top link buttons
	$('#pLibNums').append('<div class="pLibLinkHldrCurrent"></div>');
	
	for (var i = 0; i < totalImgs; i++) {
	
		$('#pLibNums').append('<div class="pLibLinkHldr"><a href="#" class="pLibLink">'+(i+1)+'</a></div>');
	
	}
	
	$('a.pLibLink').each(function(i){
		$(this).click(function(){
			pause();
			goto(i);
			currImg = i;
		});
	});
	
	$('.pLibLinkHldrCurrent').css({
		left: $('a.pLibLink:first').position().left-1,
		top: $('a.pLibLink:first').position().top-1
	});
	
	// add play button
	$('img.next').before('<img src="images/btn_play_onBlue.gif" alt="button to play slideshow" width="107" height="32" class="play" /><img src="images/btn_pause_onBlue.gif" alt="button to pause slideshow" width="107" height="32" class="pause" />');
	
	$('img.play').click(function(){
		next();
		play();
	});
	
	$('img.pause').click(function(){
		pause();
	});

	$('img.next').click(function(){
		pause();
		next();
	});
	
	$('img.previous').click(function(){
		pause();
		previous();
	});
	
	var initImg = new Image();
	initImg.onload = function() {
		
		$('#pLibImg1').attr('src', initImg.src).show();
		play();
		
	};
	
	initImg.src = myShow[currImg];
	
}

function next() {
	currImg = (currImg == totalImgs-1) ? 0 : (currImg+1);
	goto(currImg);
}

function previous() {
	currImg = (currImg == 0) ? totalImgs-1 : currImg-1;
	goto(currImg);
}

function goto(imgNum)	{

	var newImg = new Image();
	var fadeSpeed = 1000;
	var newImgSrc = myShow[imgNum];
	
	newImg.onload = function() {
		
		$('.pLibLinkHldrCurrent').animate({
			left: $('a.pLibLink:eq('+imgNum+')').position().left-1,
			top: $('a.pLibLink:eq('+imgNum+')').position().top-1
		}, fadeSpeed);
	
		if ($('#pLibImg1').css('display') == 'none') {
			$('#pLibImg1').attr('src',newImg.src).fadeIn(fadeSpeed);
			$('#pLibImg2').fadeOut(fadeSpeed);
		} else {
			$('#pLibImg2').attr('src',newImg.src).fadeIn(fadeSpeed);
			$('#pLibImg1').fadeOut(fadeSpeed);
		}
		
	}

	newImg.src = newImgSrc;

}

function play() {
	$('img.play').hide();
	$('img.pause').show();
	myTimer = setInterval(function(){
		next();
	},4000);
}

function pause() {
	$('img.pause').hide();
	$('img.play').show();
	clearInterval(myTimer);
}

init();

}

$(document).ready(function(){
   // Your code here
	slideShow();
});