var currPhoto = 0;
var nextPhoto = 1;
if(typeof currPhotoSet != "undefined") var prevPhoto = currPhotoSet.length - 1;

var audioSetting = 1;

var firstLoad = true;

var slideshowState = false;

var prevPhotoLoad = false;
var nextPhotoLoad = false;


$(document).ready(function(){
	
	//preload first and last photos
	preloadNextPhoto(1,currPhotoPrefix);
	preloadPrevPhoto(currPhotoSet.length-1,currPhotoPrefix);
	
	//check browser width vs height
	if($("body").width() > $("body").height()) {
		$(".slide img").css("height","88%").css("width","auto");
	}
	else {
		$(".slide img").css("max-width",($("body").width() - 80) + "px").css("width","auto").css("height","auto");
	}
		
	//initialize Shutter sound volume
	document.getElementById("shutter").volume = 0.3;
	
	//initialize Slideshow counter
	$("section.main .slideshow .counter .photos-total").text(currPhotoSet.length-1);
	
	//click on Previous button
	$(".btn.prev, .slideNav.prev").live("click",function(){
		stopSlideshow();
		loadPhoto(prevPhoto,"prev");
		return false;
	});
	
	//click on Next button
	$(".btn.next, .slideNav.next").live("click",function(){
		stopSlideshow();
		loadPhoto(nextPhoto,"next");
		return false;
	});
	
	//click on Audio button
	$(".btn.audio").live("click",function(){
		if(audioSetting == 0) {
			document.getElementById("shutter").volume = 0.3;
			$(this).find("img").attr("src","images/ui/btn-audio-1.png");
			audioSetting = 1;
		}
		else if(audioSetting == 1) {
			document.getElementById("shutter").volume = 0.6;	
			$(this).find("img").attr("src","images/ui/btn-audio-2.png");	
			audioSetting = 2;	
		}
		else if(audioSetting == 2) {
			document.getElementById("shutter").volume = 1;	
			$(this).find("img").attr("src","images/ui/btn-audio-3.png");	
			audioSetting = 3;	
		}
		else if(audioSetting == 3) {
			document.getElementById("shutter").volume = 0;	
			$(this).find("img").attr("src","images/ui/btn-audio-0.png");	
			audioSetting = 0;	
		}
		return false;
	});
	
	//click on Play button 
	$("section.main .slideshow .btn.play").live("click",function(){
		$(this).removeClass("play").addClass("pause").find("img").attr("src","images/ui/btn-pause.png");
		slideshowState = true;
		runSlideshow(nextPhoto);
		return false;
	});
	
	//click on Pause button 
	$("section.main .slideshow .btn.pause").live("click",function(){
		stopSlideshow();
		return false;
	});


}); //document.ready


function preloadPrevPhoto(photoIndex,photoPrefix) {
	var newPhoto = new Image();
	$(newPhoto).load(function(){
		prevPhotoLoad = true;
	});
	newPhoto.src = "images/" + currPhotoDirectory + "/photo_" + photoPrefix + "-" + photoIndex + ".jpg";
} //preloadPrevPhoto


function preloadNextPhoto(photoIndex,photoPrefix) {
	var newPhoto = new Image();
	$(newPhoto).load(function(){
		nextPhotoLoad = true;
	});
	newPhoto.src = "images/" + currPhotoDirectory + "/photo_" + photoPrefix + "-" + photoIndex + ".jpg";
} //preloadNextPhoto


function loadPhoto(photoIndex,direction) {	
	if(direction == "prev" && prevPhotoLoad != true) {
		//alert('1')		
		$("body").animate({opacity:1},500,function(){
			loadPhoto(photoIndex,direction);
		});
		//return;
	}
	else if(direction == "next" && nextPhotoLoad != true) {
		//alert('2')
		$("body").animate({opacity:1},500,function(){
			loadPhoto(photoIndex,direction);
		});
		//return;
	}
	else {
		nextPhotoLoad = false;
		prevPhotoLoad = false;
		
		if(currPhotoSet[photoIndex] != undefined) {
			swatchColor = currPhotoSet[photoIndex];
		}
		else {
			swatchColor = "#818181";
		}
			
		if(firstLoad == false) {
			$(".logo .frame").addClass("rotate",150,function(){
				$(this).removeClass("rotate",250);
			});
		}
		if(browserIEOld == false) {
			document.getElementById("shutter").play();
		}
		
		$("section.main .mask").fadeIn(200,function(){
			$(".portfolio figure").addClass("active");
			$(".portfolio figure img").attr("src","images/" + currPhotoDirectory + "/photo_" + currPhotoPrefix + "-" + photoIndex + ".jpg");
			
			$(".logo .bg-1, nav li.curr .bg-1, .btn .bg-1, .callout .bg-1, .category.bg-1").animate({backgroundColor:swatchColor},500);
			$(".cl-1").not("nav li li a").animate({color:swatchColor},750);
			//var portfolioPage = photoThemes[photoIndex].replace(" ", "");
			if(typeof photoThemes != "undefined") $(".slideshow .category").attr("href",photoThemes[photoIndex].replace(" ", "") + ".html").find("span").text(photoThemes[photoIndex]);
			
			$("section.main .mask").fadeOut(200,function(){
				if(direction == "prev") {
					currPhoto = prevPhoto;
				}
				else {
					currPhoto = nextPhoto;
				}
				if(currPhoto < currPhotoSet.length - 1) {
					nextPhoto = currPhoto + 1;
				}
				else {
					nextPhoto = 1;
				}
				if(currPhoto > 1) {
					prevPhoto = currPhoto - 1;
				}
				else {
					prevPhoto = currPhotoSet.length - 1;
				}
				
				$(".portfolio .slideshow .counter .curr-photo").text(currPhoto);
				
				if(currPhoto == 3) {
					$("section.main .callout").show("scale",1000);
				}
				
				preloadNextPhoto(nextPhoto,currPhotoPrefix);
				preloadPrevPhoto(prevPhoto,currPhotoPrefix);
				
				firstLoad = false;
			});
		});
	}
} //loadPhoto


function runSlideshow(photoIndex) {
	loadPhoto(photoIndex,"next");
	$(".slideshow").animate({opacity:1},5000,function(){
		if(slideshowState == true) {
			runSlideshow(nextPhoto);
		}
	});
} //runSlideshow


function stopSlideshow() {	
	$("section.main .slideshow .btn.pause").removeClass("pause").addClass("play").find("img").attr("src","images/ui/btn-play.png");
	slideshowState = false;
} //stopSlideshow
