// - - - - - set globals - - - - - 

var Global = {
	'imgsLoaded' : 0,
	'photoContainerWidth' : 0,
	'setTotalImgs' : 0,
	'progressBarIndicatorWidth' : 0,
	'progressStepLength' : 0,
	'slidePhotoContainer' : '',
	'imageMargin' : 10,
	'largestImgHeight' : 0
}

// - - - - - add events to window - - - - - 

window.addEvent('load', function(){
	centerImages();
	showImages();
});

if (!Browser.Engine.trident) {
	window.addEvent('domready', function() {
		prepareProjectView();
		prepareMenuAndPopWindows();
	});
}

// - - - - - functions - - - - - 

function prepareProjectView() {
	
	if ($('photosetContainer') !== null) {
	
		// create slide FX
		Global.slidePhotoContainer = new Fx.Slide($('photosetContainer'),{
			'transition' : 'quad:in:out',
			'duration' : 'short',
			'onComplete' : function() {
				$('photoContainer').fade('in');
			}
		});
		
		// hide elements first
		Global.slidePhotoContainer.hide();
		$('photoContainer').fade('hide');

		// preload images
		preloadImages();
		
	}

}

function prepareMenuAndPopWindows() {

	// hide elements first

	$('winImprint').fade('hide');
	$('setMenu').fade('hide');

	// add event listeners & actions

	$$('.lnkImprint').addEvent('click',function(e) { e.stop(); $('winImprint').fade('toggle'); });
	$('setMenu').addEvent('mouseleave',function() { $('setMenu').fade('out'); });
	$('menuLabel').getElement('.btn').addEvent('mouseenter',function() { $('setMenu').fade('in'); });

}

function preloadImages() {

	// create progress bar
	progressBar('create');

	// get elements, set some vars
	var photoSetContainer = $('photosetContainer');
	var photoContainer = $('photoContainer');
	var imgs = photoContainer.getElements('img');
	Global.setTotalImgs = imgs.length;

	$each(imgs,function(item,index){

		item.addEvent('load',function(){

			// grow progress bar
			Global.imgsLoaded++;
			progressBar('grow');
		
		});

	});

}

function centerImages() {
	
	var imgs = $('photoContainer').getElements('img');
	
	// determine the largest image height
	
	$each(imgs,function(item,index){
		
		var imgHeight = item.getSize().y;

		if (imgHeight > Global.largestImgHeight) {
			Global.largestImgHeight = imgHeight;
		}
		
	});
	
	// center image if image is in landscape orientation
	
	$each(imgs,function(item,index){
		
		var imgHeight = item.getSize().y;
		var imgWidth = item.getSize().x;

		if (imgWidth > imgHeight) {
			var imgMarginTop = (Global.largestImgHeight / 2) - (imgHeight / 2);
			item.setStyle('marginTop',imgMarginTop + 'px');
		};
	
	});

}



function showImages() {
	
	if ($('photosetContainer') !== null) {
	
		var photoSetContainer = $('photosetContainer');
		var photoContainer = $('photoContainer');
		var imgs = photoContainer.getElements('img');
		
		// photoset container visibility
		photoSetContainer.setStyle('visibility','visible');
	
		// last-child fix for IE
		imgs.getLast().setStyle('marginRight','0');

		// set photoContainer width
	
		$each(imgs,function(item,index){
			Global.photoContainerWidth += (item.getSize().x + Global.imageMargin);
		});
	
		photoContainer.setStyle('width',(Global.photoContainerWidth) - Global.imageMargin + 'px');
	
		// dispose the progress bar
		$('progressBar').dispose();

		// slide in container
		Global.slidePhotoContainer.slideIn();
		
		// adjust height for IE
		if (Browser.Engine.trident) {
			photoSetContainer.setStyle('height',photoSetContainer.getSize().y + 27 + 'px');
		}
	
	}

}

function progressBar(action) {

	if (action == 'create') {
	
		// create the progress bar
		var progressBar = new Element('div',{ 'id' : 'progressBar' });
		var progressBarIndicator = new Element('div').inject(progressBar);
		progressBar.inject(document.body);
	
	}

	if (action == 'grow') {
	
		// make the progress bar grow
		var progressBar = $('progressBar');
		Global.progressStepLength = parseInt(progressBar.getStyle('width')) / Global.setTotalImgs;
		Global.progressBarIndicatorWidth += Global.progressStepLength;
		progressBar.getElement('div').setStyle('width',Global.progressBarIndicatorWidth+'px');
	
	}

}
