var currentSlide = 0;
var initialLoad = true;
var totalSlides = 0;
var imagesToLoad;
var moreSlidesTimer;

// LOAD A REQUESTED PHOTO

function loadPhoto (id) {
	
	// DETERMINE WHETHER TO SHOW ALL ON PARTICULAR IMAGE
	var doNotCrop = ($('img_' + id).hasClass('doNotCrop')) ? true : false;
	
	// alert ("loading photo " + id);
	
	if (!initialLoad) {
		
		if (id != currentSlide) {
			
			var thisSlide = $('ph_' + currentSlide);
			var nextSlide = $('ph_' + id);
			
			nextSlide.setStyles({
				'display':'block',
				'opacity':0
			});
			
			$('viewportMap').setStyle('visibility','hidden');
			
			makeItFit($('img_' + id), doNotCrop);
			
			var ns = new Fx.Tween (nextSlide, {duration: 200});
			var cs = new Fx.Tween (thisSlide, {duration: 200, onComplete:function(){ thisSlide.setStyle('display','none'); ns.start('opacity',1); toggleCaption(id, showCaptions, doNotCrop);} } );
			cs.start('opacity', 0);
			
			if ($('audioPlayer')) { loadAudio(id); }; // LOAD AUDIO IF AUDIO PLAYER PRESENT
						
		};
		
	} else {
	
		$('ph_' + id).setStyles({
			'display':'block'
		});
		
		makeItFit($('img_' + id), doNotCrop);
		
		initialLoad = false;
	
	};
	
	// MAKE CURRENT SLIDE BUTTON ACTIVE
	
	$('sl_' + currentSlide).removeClass('active');
	$('pt_' + currentSlide).setStyle('display','none');
	$('sl_' + id).addClass('active');
	$('pt_' + id).setStyle('display','block');
	
	if (id == 0) { 
		if ($('introCopyBlock')) {
			$('introCopyBlock').setStyle('display','block');
		};
	} else {
		if ($('introCopyBlock')) {
			$('introCopyBlock').setStyle('display','none');
		};
	};
	
	if($('moreSeedSlideshows')) {
		if (id==totalSlides-1) {
			var showMore = function() {
				this.setStyle('display','block');
				$('moreSeedSlideshowsDock').setStyles({
					'margin-left':-($('moreSeedSlideshowsDock').getSize().x / 2),
					'bottom':-($('moreSeedSlideshowsDock').getSize().y)
				});
				$('moreSeedSlideshowsDock').tween ('bottom',0);
			};
			moreSlidesTimer = showMore.delay(5000, $('moreSeedSlideshows'));
		} else {
			$('moreSeedSlideshows').setStyle('display','none');
			$clear(moreSlidesTimer);
		};
	};
	
	currentSlide = id; // UPDATE CURRENT SLIDE VAR
};


// LOAD NEXT PHOTO

function loadNextPhoto () {
	if (Number(currentSlide) + 1 < totalSlides) {
		loadPhoto(Number(currentSlide) + 1);
	} else {
		loadPhoto(0);
	}
};


// LOAD PREVIOUS PHOTO

function loadPreviousPhoto () {
	if (currentSlide > 0) {
		loadPhoto(currentSlide - 1);
	} else {
		loadPhoto(totalSlides - 1);
	};
};


// SHOW AND HIDE THE PREVIEW THUMBNAILS

function showSlidePreview(id) {
	var imgSrc = $('img_' + id).getProperty('src');
	$('slideThumb').setProperty('src', imgSrc);
	$('slideThumb').setStyles({
		'width': ($('img_' + id).getProperty('width') * .1),
		'height': ($('img_' + id).getProperty('height') * .1)
	});
	var newLeft = (($('sl_' + id).getLeft()) + ($('sl_' + id).getSize().x / 2)) - (($('img_' + id).getProperty('width') * .1) / 2 + 5);
	$('slidePreview').setStyle('left',newLeft);
	$('slidePreview').setStyle('display', 'block');
	
};

function hideSlidePreview() {
	$('slidePreview').setStyle('display', 'none');
	$('slideThumb').setProperty('src', '');
};


// TOGGLE CAPTIONS

function toggleCaption(id, show, doNotCrop) {
	
	hideAllCaptions();
	
	// alert("toggle caption " + id);
	
	if ($('pc_' + id)) {
		if (show) {
			$('pc_' + id).setStyle('display', 'block');
			placeCaption(id, doNotCrop);
		} else {
			$('pc_' + id).setStyle('display', 'none');
		};
	};
};

function hideAllCaptions() {
	$$('.slideShowCaption').each(function(div){
		div.setStyle('display', 'none');
	});
};

// PLACE THE CAPTION IN RELATION TO THE IMAGE

function placeCaption(id, doNotCrop) {
	
	// alert("placing caption " + id);

	if (pinCaptions || doNotCrop) {
		var ref;
		if (showAll || doNotCrop) {
			ref = $('img_' + id);
		} else {
			ref = $('ph_' + id);
		};
		if ($('pc_' + id)) {
			if ($('pc_' + id).hasClass('captionBottom')) {
				$('pc_' + id).setStyles ({
					'bottom': (((window.getSize().y + 90) - ref.getSize().y) / 2) - 5,
					'left':ref.getLeft() + 10
				});
			} else {
				$('pc_' + id).setStyles ({
					'top':ref.getTop() + 10,
					'left':ref.getLeft() + 10
				});
			};
		};
	};
};

// SHOW THE PROGRESS OF LOADED IMAGES

function showLoadProgress() {
	if(imagesToLoad==0) {
		$('loadProgress').setStyle('display','none');
		toggleCaption(currentSlide, showCaptions);
	};
	$('loadProgress').set('html', 'Loading ' + imagesToLoad + ' photos...');
};

// MAKE THE IMAGE FIT TO THE VIEWPORT SIZE

function makeItFit(img, doNotCrop) {

	var wd = window.getSize();
				
	var osw = hasBorder ? 60 : 0;
	var osh = hasBorder ? 120 : 0;
	
	var pw = wd.x - osw;
	
	var ph = wd.y - osh;
	
	
	var pd = $('ph_' + img.id.split('_')[1]);
	
	pd.setStyles ({
		'width':pw,
		'height':ph
	});
	
	$('slideOverlayButtons').setStyles ({
		'width':pw,
		'height':ph
	});
	
	var vm = $('viewportMap');
	vm.setStyles ({
		'width': img.getProperty('width') * .05,
		'height': img.getProperty('height') * .05
	});
	
	if(!showAll && !doNotCrop) {
		vm.setStyle('visibility','visible');
	} else {
		vm.setStyle('visibility','hidden');
	};
	
	var wpc = parseInt((pw / img.getProperty('width')) * 100);
	var hpc = parseInt((ph / img.getProperty('height')) * 100);
	
	if (!showAll && !doNotCrop) {
		
		var va = $('viewArea');
		
		if (wpc > hpc) {
		
			var nh = (img.getProperty('height') * (wpc / 100));
			img.setStyles({
				'width': pw,
				'height': nh,
				'left':0,
				'top': -((nh - ph) / 2)
			});
			va.setStyles ({
				'width': img.getProperty('width') * .05,
				'height': (img.getProperty('height') * .05) * (ph / nh),
				'left': 0,
				'top': ((img.getProperty('height') * .05) - ((img.getProperty('height') * .05) * (ph / nh))) / 2
			});
			
		} else {
			var nw = (img.getProperty('width') * (hpc / 100));
			img.setStyles({
				'width': nw,
				'height': ph,
				'top':0,
				'left': -((nw - pw) / 2)
			});
			va.setStyles ({
				'width': (img.getProperty('width') * .05) * (pw / nw),
				'height': (img.getProperty('height') * .05),
				'left': ((img.getProperty('width') * .05) - (img.getProperty('width') * .05) * (pw / nw)) / 2,
				'top': 0
			});
		};
		
	} else {
		if (wpc < hpc) {
			var nh = (img.getProperty('height') * (wpc / 100));
			img.setStyles({
				'width': pw,
				'height': nh,
				'left':0,
				'top': -((nh - ph) / 2)
			});
		} else {
			var nw = (img.getProperty('width') * (hpc / 100));
			img.setStyles({
				'width': nw,
				'height': ph,
				'top':0,
				'left': -((nw - pw) / 2)
			});
		};
	};
	
};

// COMMUNICATE WITH FLASH TO LOAD AUDIO XML

function loadSlide(id) {
	$('startButton').setStyle('display','none');
	loadPhoto(id);
};

function startSlideShow() {
	// alert('starting slideshow');
	thisMovie('seedAudioPlayer').startAudio();
};

function removeStartSlideshow() {
	if($('startButton')) {
		$('startButton').setStyle('display','none');
	};
	/*
	if ($('introCopyBlock')) {
		$('introCopyBlock').setStyle('display','none');
	};
	*/
};

function loadAudio(id) {
	thisMovie('seedAudioPlayer').playAudio(id);
};

function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 };
}

window.addEvent('domready', function() {
	
	var bodyElement = $(document.body);
	
	if (!showAll) {
		bodyElement.addClass('fullWindow');
	};
	
	if (hasBorder) {
		$$('.slideShowPhoto').each(function(div) {
			div.setStyles ({
				'top':30,
				'left':30
			});
		});
		$('slideOverlayButtons').setStyles ({
			'top':30,
			'left':30
		});
	};
	
	$('viewportMap').setStyle('visibility','hidden');
	
	if (showAll) {
		$('viewportMap').setStyle('display','none');
	};
	
	if (showCaptions) {
		if ($('slideCaptionsCheckbox')) {
			$('slideCaptionsCheckbox').setProperty('checked','checked');
		};
	} else {
		if ($('slideCaptionsCheckbox')) {
			$('slideCaptionsCheckbox').setProperty('checked','');
		};
	};
	
	var p = new Preloader();
				
	$$('.slideShowPhoto img').each(function(img){
		p.addEventOnLoad(img.src, function() {
			img.getParent().setStyle('background', 'none');
			img.setStyle('display', 'block');
			img.setStyle('opacity', 0);				
			img.tween('opacity',1);
			imagesToLoad--;
			showLoadProgress();
		});
		p.addToQueue(img.src);
		totalSlides++;
		var sln = Number(img.id.split("_")[1]);
		$('slideButtons').innerHTML += '<li><a id="sl_' + sln + '" href="#">' + Number(sln+1) + '</a></li>';
	});
	
	$$('#slideButtons a').each(function(link){
		link.addEvent ('mouseover', function(event) {
			showSlidePreview(this.id.split("_")[1]);
		});
		link.addEvent ('mouseout', function(event) {
			hideSlidePreview();
		});
		link.addEvent ('click', function(event) {
			e = new Event(event).stop();
			var num = this.id.split("_")[1];
			if (num != currentSlide) {
				loadPhoto(num);
			};
			removeStartSlideshow()
			this.blur();
		});
	});
	
	$('slideShowPrev').addEvent('click', function(evt) {
		e = new Event(evt).stop();
		loadPreviousPhoto();
		removeStartSlideshow();
		this.blur();
	});
	
	$('slideOverlayPrev').addEvent('click', function(evt) {
		e = new Event(evt).stop();
		loadPreviousPhoto();
		removeStartSlideshow();
		this.blur();
	});
	
	
	$('slideShowNext').addEvent('click', function(evt) {
		e = new Event(evt).stop();
		loadNextPhoto();
		removeStartSlideshow();
		this.blur();
	});
	
	$('slideOverlayNext').addEvent('click', function(evt) {
		e = new Event(evt).stop();
		loadNextPhoto();
		removeStartSlideshow();
		this.blur();
	});
	
	if ($('slideCaptionsCheckbox')) {
		$('slideCaptionsCheckbox').addEvent('click', function(evt) {
			if (this.getProperty('checked')) {
				showCaptions = true;
			} else {
				showCaptions = false;
			};
			toggleCaption(currentSlide, showCaptions);
		});
	};
	
	if ($('slideOptionsCheckbox')) {
		$('slideOptionsCheckbox').addEvent('click', function(evt) {
			if (this.getProperty('checked')) {
				showAll = true;
			} else {
				showAll = false;
			};
			var doNotCrop = ($('img_' + currentSlide).hasClass('doNotCrop')) ? true : false;
			makeItFit($('img_' + currentSlide), doNotCrop);
		});
	};
	
	if($('startButton')) {
		$('startButton').addEvent('click', function(evt) {
			e = new Event(evt).stop();
			startSlideShow();
			this.setStyle('display','none');
			if ($('introCopyBlock')) {$('introCopyBlock').setStyle('display','none');};
		});
	};
	
	if($('hideMoreSlideshows')) {
		$('hideMoreSlideshows').addEvent('click', function(evt) {
			e = new Event(evt).stop();
			$('moreSeedSlideshows').setStyle('display','none');
		});
	};
	
	// start load progress counter
	imagesToLoad = totalSlides;
	
	showLoadProgress();
	
	var h = document.location.hash;
			
	if (h=='') {
		h = currentSlide;
	} else {
		h = h.split('#')[1];
	}
	
	loadPhoto(h);

});

window.addEvent('resize', function(){

	var doNotCrop = ($('img_' + currentSlide).hasClass('doNotCrop')) ? true : false;

	makeItFit($('img_' + currentSlide), doNotCrop);
	
	placeCaption(currentSlide, doNotCrop);

});