var wedding =	['/img/photos/wedding/16.jpg',
				 '/img/photos/wedding/06.jpg',
				 '/img/photos/wedding/07.jpg',
				 '/img/photos/wedding/08.jpg',
				 '/img/photos/wedding/09.jpg',
				 '/img/photos/wedding/10.jpg',
				 '/img/photos/wedding/11.jpg',
				 '/img/photos/wedding/12.jpg',
				 '/img/photos/wedding/13.jpg',
				 '/img/photos/wedding/14.jpg',
				 '/img/photos/wedding/15.jpg',
				 '/img/photos/wedding/01.jpg',
				 '/img/photos/wedding/17.jpg',
				 '/img/photos/wedding/18.jpg',
				 '/img/photos/wedding/19.jpg',
				 '/img/photos/wedding/20.jpg',
				 '/img/photos/wedding/21.jpg',
				 '/img/photos/wedding/22.jpg',
				 '/img/photos/wedding/23.jpg',
				 '/img/photos/wedding/24.jpg',
				 '/img/photos/wedding/25.jpg',
				 '/img/photos/wedding/26.jpg',
				 '/img/photos/wedding/27.jpg',
				 '/img/photos/wedding/28.jpg',
				 '/img/photos/wedding/29.jpg',
				 '/img/photos/wedding/30.jpg',
				 '/img/photos/wedding/31.jpg',
				 '/img/photos/wedding/32.jpg',
				 '/img/photos/wedding/33.jpg',
				 '/img/photos/wedding/34.jpg',
				 '/img/photos/wedding/35.jpg',
				 '/img/photos/wedding/36.jpg',
				 '/img/photos/wedding/37.jpg',
				 '/img/photos/wedding/38.jpg',
				 '/img/photos/wedding/39.jpg',
				 '/img/photos/wedding/40.jpg',
				 '/img/photos/wedding/41.jpg',
				 '/img/photos/wedding/42.jpg',
				 '/img/photos/wedding/43.jpg'];
				
var charity =	['/img/photos/charity/01.jpg',
				 '/img/photos/charity/02.jpg'];

var corporate =	['/img/photos/corporate/01.jpg',
				 '/img/photos/corporate/02.jpg',
				 '/img/photos/corporate/03.jpg',
				 '/img/photos/corporate/04.jpg',
				 '/img/photos/corporate/05.jpg',
				 '/img/photos/corporate/06.jpg',
				 '/img/photos/corporate/07.jpg',
				 '/img/photos/corporate/08.jpg',
				 '/img/photos/corporate/09.jpg',
				 '/img/photos/corporate/10.jpg',
				 '/img/photos/corporate/11.jpg',
				 '/img/photos/corporate/12.jpg',
				 '/img/photos/corporate/13.jpg',
				 '/img/photos/corporate/14.jpg',
				 '/img/photos/corporate/15.jpg'];

var holiday =	['/img/photos/holiday/01.jpg',
				 '/img/photos/holiday/02.jpg',
				 '/img/photos/holiday/03.jpg',
				 '/img/photos/holiday/04.jpg',
				 '/img/photos/holiday/05.jpg',
				 '/img/photos/holiday/06.jpg',
				 '/img/photos/holiday/07.jpg',
				 '/img/photos/holiday/08.jpg',
				 '/img/photos/holiday/09.jpg',
				 '/img/photos/holiday/10.jpg',
				 '/img/photos/holiday/11.jpg'];
				
var imgArrays = [corporate, wedding, holiday, charity];
// var totalImages = 0;
var curImage = 0;
var curArray = 0;
var slideshowTimeout = null;
var timeout = 4000;
var fadeout = 2000;

jQuery(document).ready(function($){	
	$('div#nav li').eq(curArray).addClass('active');//activate first tab
	$('#slideshow div#new_image').fadeOut(0);
	
	for (var index in imgArrays){//preload first 3 images in each array.
		var length = (imgArrays[index].length > 3)?3:imgArrays[index].length;
		for (var count = 0; count < length; count++){
			preloadImage(imgArrays[index][count]);
		}
	}
	
	setTimeout('nextImage()', timeout);
	
	$('div#nav a').click(function(){
		// clearTimeout(slideshowTimeout);
		curArray = $(this).parent().prevAll().length;
		curImage = 0;
		jQuery('div#nav li').removeClass('active').eq(curArray).addClass('active');//update tab
		jQuery('#slideshow div#new_image').find('img').attr('src', imgArrays[curArray][curImage]);//set new image
		return false;
	});
	$('a.play_pause').fadeTo(2000, 0.0).click(function(){
		// debugger;
		if (slideshowTimeout !== null){
			clearTimeout(slideshowTimeout);
			slideshowTimeout = null;
		}else{
			nextImage();
		}
		return false;
	}).hover(function(){
		$(this).stop().fadeTo(500, 1.0);
	}, function(){
		$(this).stop().fadeTo(500, 0.0);
	});
});

function nextImage(){
	// if (jQuery('#slideshow div#old_image').queue().length || jQuery('#slideshow div#old_image').queue().length){
	// 	clearTimeout(slideshowTimeout);
	// 	slideshowTimeout = setTimeout("nextImage()", timeout);
	// 	return;
	// }
	
	var oldImage = imgArrays[curArray][curImage];

	if (curImage + 1 >= imgArrays[curArray].length){//switch to next array.
		curImage = 0;
		if (curArray + 1 >= imgArrays.length){//restart.
			curArray = 0;
		}else{
			curArray++;
		}
		jQuery('div#nav li').removeClass('active').eq(curArray).addClass('active');//update tab
	}else{
		curImage++;
	}

	for (var count = 0; ((count + curImage) < imgArrays[curArray].length) && count < 3; count++){//preload the next 3 images
		preloadImage(imgArrays[curArray][curImage + count]);
	}
	
	jQuery('#slideshow div#old_image').find('img').attr('src', oldImage);//set old to new
	jQuery('#slideshow div#new_image').find('img').attr('src', imgArrays[curArray][curImage]);//set new
	
	jQuery('#slideshow div#old_image').stop().show().fadeOut(fadeout);
	jQuery('#slideshow div#new_image').stop().hide().fadeIn(fadeout);
	
	clearTimeout(slideshowTimeout);
	slideshowTimeout = setTimeout("nextImage()", timeout);
}

function preloadImage(imgsrc){
	jQuery("<img/>").attr("src", imgsrc);
}
