
// Mootools More
// Contains Assets Component

MooTools.More={version:"1.3.0.1",build:"6dce99bed2792dffcbbbb4ddc15a1fb9a41994b5"};var Asset={javascript:function(d,b){b=Object.append({document:document},b);
if(b.onLoad){b.onload=b.onLoad;delete b.onLoad;}var a=new Element("script",{src:d,type:"text/javascript"});var c=b.onload||function(){},e=b.document;delete b.onload;
delete b.document;return a.addEvents({load:c,readystatechange:function(){if(["loaded","complete"].contains(this.readyState)){c.call(this);}}}).set(b).inject(e.head);
},css:function(b,a){a=a||{};var c=a.onload||a.onLoad;if(c){a.events=a.events||{};a.events.load=c;delete a.onload;delete a.onLoad;}return new Element("link",Object.merge({rel:"stylesheet",media:"screen",type:"text/css",href:b},a)).inject(document.head);
},image:function(c,b){b=Object.merge({onload:function(){},onabort:function(){},onerror:function(){}},b);var d=new Image();var a=document.id(d)||new Element("img");
["load","abort","error"].each(function(e){var g="on"+e;var f=e.capitalize();if(b["on"+f]){b[g]=b["on"+f];delete b["on"+f];}var h=b[g];delete b[g];d[g]=function(){if(!d){return;
}if(!a.parentNode){a.width=d.width;a.height=d.height;}d=d.onload=d.onabort=d.onerror=null;h.delay(1,a,a);a.fireEvent(e,a,1);};});d.src=a.src=c;if(d&&d.complete){d.onload.delay(1);
}return a.set(b);},images:function(c,b){b=Object.merge({onComplete:function(){},onProgress:function(){},onError:function(){},properties:{}},b);c=Array.from(c);
var a=0;return new Elements(c.map(function(e,d){return Asset.image(e,Object.append(b.properties,{onload:function(){a++;b.onProgress.call(this,a,d,e);if(a==c.length){b.onComplete();
}},onerror:function(){a++;b.onError.call(this,a,d,e);if(a==c.length){b.onComplete();}}}));}));}};

window.addEvent('domready', function(){
		
	// vars 
	logo = $('container').getElement('.logo');
	navbar = $('container').getElement('.navbar');
	navlinks = $('container').getElements('.nav li');
	tradingas = $('container').getElements('.tradingas');
/* 	photoinfo = $('container').getElement('.photoinfo'); */
	
	images = '';
	
	// set tradingas opacity to 0
	tradingas.setStyle('opacity',0);
		
	// sort out what happens when someone clicks into a gallery
	if ($('galleries')) {
		$('galleries').getElements('li').addEvent('click', function() {
			galleryAnimation();
			getImages(this.get('id'));
		});
	}
	
	// sort out what we do for next / previous
	$('container').getElement('.next').addEvent('click', function(){
		ssNavigate('next');
	});
	
	// sort out what we do for next / previous
	$('container').getElement('.prev').addEvent('click', function(){
		ssNavigate('prev');
	});
	
	$('container').getElement('.close').addEvent('click', function() {
		// exit more galleries button
		$('container').getElement('.close').set('morph', {
			duration: 'long',
			transition: 'circ:out'
		})
		.morph({
			'left': -174
		});

		// exit next photo button
		$('container').getElement('.next').set('morph', {
			duration: 'long',
			transition: 'circ:out'
		})
		.morph({
			'left': -174
		});
		
		// exit next photo button
		$('container').getElement('.prev').set('morph', {
			duration: 'long',
			transition: 'circ:out'
		})
		.morph({
			'left': -174
		});
		
/*
		// exit photo info box
		$('container').getElement('.photo-info').set('morph', {
			duration: 'long',
			transition: 'circ:out'
		})
		.morph({
			'left': -174
		});
*/

		// bring in footer
		footerEntrance();
		
		// start animation of logo
		var entergallerylist = function(){
			// show gallery menu item
			$('content').getElement('.gallery').morph({
				'display' : ['none','block'],
				'height' : [0,$('content').getElement('.gallery').getElement('.item-content').getStyle('height')]
			});
		}.delay(2500);
	});

	// background image
	if ($('bgimage')){
	
		// stop image clicking
		$('bgimage').addEvents({
			'mousedown': function (ev){ ev.stop(); },
			'mouseup': function (ev){ ev.stop(); },
			'click': function (ev){ ev.stop(); }
		});
									
	}

	// initiate entrance of images and footer
	getImages();
	footerEntrance();	

	// add window resize event
	window.addEvent('resize', resizeImageToWindow);
	
	// force window sizing
	window.fireEvent('resize');

});

// function to introduce footer navigation
var footerEntrance = function() {
	// get original width of navbar 
	navbarw = $('container').getElement('.navbar').getStyle('width');
	
	// set default states for footer
	navbar.setStyles({
		'height' : 0,
		'padding-bottom' : 0,
		'padding-left' : 0,
		'padding-right' : 0,
		'padding-top' : 0
	});
	
	navlinks
	.each(function(el) {
		(function(){
			el.setStyle('opacity',0);
		});
	});

	navlinks.setStyle('opacity', 0);

	// start animation of logo
	var enterlogo = function(){
		logo.set('morph', {
			duration: 'long', 
			transition: 'circ:out'
		})
		.morph({
			'margin-left' : 0
		});
	}
	.delay(600);
	
	// start animation of navbar
	var enternavbar = function(){
		navbar.set('morph', {
			duration: 1200, 
			transition: 'circ:out'
		})
		.morph({
			'height' : 54,
			'padding-left' : 26,
			'padding-right' : 26,
			'padding-top' : 16
		});
		
	}
	.delay(1800);

	// start animation of navbar
	var enternavbar = function(){
		navlinks
		.each(function(el, i) {
		
			(function(){
				el.morph({
					'opacity': [0,1]
				});
			}.delay(150 * i));
		})
	}
	.delay(2800);
	
	var tradingasenter = function() {
		tradingas.set('morph', {
			duration: 2400, 
			transition: 'circ:out'
		})
		.morph({
			'opacity' : [0,1]
		});
	}
	.delay(2800);
	
	// what do we do when we click on a link?
	navlinks.addEvent('click', function(ev){

		ev.stop();
		
		// remove selected from those which have selected
		
		// reset nav boxes.
		navlinks.removeClass('selected');
		$('content').getElements('.item').each(function(el){
			el.morph({
				'height' : [el.getSize().y,0]
			});

		});
		
		// give selected class to chosen li
		this.addClass('selected');
			
		// what info am i displaying
		var openme = $('content').getElement('.'+this.get('id'));	
				
		// display chosen information		
		openme.morph({
			'display' : ['none','block'],
			'height' : [0,openme.getElement('.item-content').getStyle('height')]
		});
				
	});	

}

// function to run slideshow of background images
var slideshow = function(){

	var el = $('imagelist').getElement('li.selected');
	var count = el.getAllPrevious('li').length;
		
/* 	$('container').getElement('.photo-info').set('html',''); */
		
	count = count + 1;	
				
	if (count == $('imagelist').getElements('li').length){
		count = 0;
	}
	
	$('imagelist').getElements('li.selected').removeClass('selected').each(function(el){
		if (el.getStyle('opacity') == 1){
			el.tween('opacity', 0);
		}
	});
	
	if (count == 0) {
		
		count++;
	
		$('imagelist').getElements('li')[0].setStyles({
			'display':'block',
			'opacity': 0
		}).addClass('selected')
		.tween('opacity', 1);

/* 		$('container').getElement('.photo-info').set('html','<h1>'+$('imagelist').getElements('li')[0].getElement('img').getProperty('data-title')+'</h1><p>'+$('imagelist').getElements('li')[0].getElement('img').getProperty('data-summary')+'</p>'); */

	}
	else {
		el.getNext('li').setStyles({
			'display':'block',
			'opacity': 0
		}).addClass('selected')
		.tween('opacity', 1);
		
/* 		$('container').getElement('.photo-info').set('html','<h1>'+el.getNext('li').getElement('img').getProperty('data-title')+'</h1><p>'+el.getNext('li').getElement('img').getProperty('data-summary')+'</p>'); */
	}
					
}


// function to initialise passed in json return
var initialiseSlideshow = function() {

	clearInterval(images);

	$('container').getElement('.loader')
	.tween('opacity',[1,0]);

    // run slideshow
	images = slideshow.periodical(7500);

	// set opacity of slideshow images to 0
	// with the exception of the 1st one which we set to 1
	$('imagelist').getElements('li').each(function(el,i) {
	
		el.getElement('img').setStyle('display','block');
	
		el.setStyles({
			'opacity':0
		});	

		if (i == 0) {
			el.addClass('selected');
			el.tween('opacity',[0,1]);
/*
			$('container').getElement('.photo-info').set('html','');
			$('container').getElement('.photo-info').set('html','<h1>'+el.getElement('img').getProperty('data-title')+'</h1><p>'+el.getElement('img').getProperty('data-summary')+'</p>');
*/
		}	
	});
}

// function to get images
var getImages = function(gallery) {

	getimages = new Request.JSON({ url: 'load.php', data: { method: 'post', gallery: gallery }})
	.addEvent("success", function(j){
		
		if(j) {

			$('container').getElement('.loader')
			.tween('opacity',[0,1]);

/*
			// define image paths
			var imagepaths = Array();
			
*/
			// need to remove all li's in the #imagelist
			$('imagelist').getElements('li').each(function(el) {
				el.dispose();
			});
		
			$each(j, function(v){
				$('imagelist').adopt(new Element('li',{html:v}));
			});
						
/*
			$('imagelist').getElements('li').each(function(el){
				imagepaths.include(el.getElement('img').getProperty('src'));
			});
			
			if (Browser.ie){
				imagepaths.each(function(el, i){
					imagepaths[i] = el + '?' + String.uniqueID();
				});
			}
*/

/*
			new Asset.images(imagepaths, {
			    onProgress: function(i) {
					if (i == 0){
						this.addClass('selected');
					}	

			},
				onComplete: function(){
					$('container').getElement('.loader')
					.tween('opacity',[1,0]);
*/
		
					// initialise slide show	    		
					(function(){ 
						initialiseSlideshow();
					 }.delay(3000));
		
					// resize image window
					(function(){ 
						resizeImageToWindow();
					 }.delay(3000));
					 
/*
			    },
			    onError: function(c,i){
			    	alert(c);
			    }
			});				
*/
						
		}
	})
	.send();

}

// resize image to window
var resizeImageToWindow = function (){
						
	// loop over images
	$('imagelist').getElements('li img').each(function(i, count){
																												
		// get x and y size
		var imgsize = i.getSize();
		// get window width and height
		var windowsize = window.getSize();
						
		// how much to multiply width/height
		var ratio = 1;
												
		// landscape
		if (windowsize.x > windowsize.y){
		
			ratio = windowsize.x / imgsize.x;	
			
			// if y is going to give us whitespace
			if ((ratio * imgsize.y) < windowsize.y){
				ratio = windowsize.y / imgsize.y;
			}	
		
		} else { // portrait
		
			ratio = windowsize.y / imgsize.y;
			
			// if x is going to give us whitespace
			if ((ratio * imgsize.x) < windowsize.x){
				ratio = windowsize.x / imgsize.x;
			}
		
		}
		
		// we have a minimum width/height
		if ((ratio * imgsize.x) < 800){
			ratio = 800 / imgsize.x;
		} else if ((ratio * imgsize.y) < 600){
			ratio = 600 / imgsize.y;
		}

		// set height
		i.setStyles({
			width: ratio * imgsize.x,
			height: ratio * imgsize.y
		});
									
	});
	
};

// gallery animation
var galleryAnimation = function() {
	
	// exit logo
	logo.set('morph', {
		duration: 'long', 
		transition: 'circ:out'
	})
	.morph({
		'margin-left' : [logo.getStyle('margin-left'),-62]
	});
	
	// remove navlinks
	navlinks
	.each(function(el, i) {
		(function(){
			el.morph({
				'opacity': [1,0]
			});
		}.delay(50 * i));
	});
	
	// start animation of navbar
	var exitnavbar = function(){
		navbar.set('morph', {
			duration: 500, 
			transition: 'circ:out'
		})
		.morph({
			'height' : 0,
			'padding-top' : 0
		});
		
	}
	.delay(300);
	
	// remove trading as
	tradingas.set('morph', {
		duration: 500, 
		transition: 'circ:out'
	})
	.morph({
		'opacity' : [1,0]
	});
	
	// close any open navigation
	$('content').getElements('.item').each(function(el){
		el.morph({
			'height' : 0
		});

	});
	
	// bring in more galleries button
	$('container').getElements('.close').set('morph', {
		duration: 'long',
		transition: 'circ:out'
	})
	.morph({
		'left': 0
	});

	// bring in more galleries button
	$('container').getElements('.next').set('morph', {
		duration: 'long',
		transition: 'circ:out'
	})
	.morph({
		'left': 134
	});
	
	// bring in more galleries button
	$('container').getElements('.prev').set('morph', {
		duration: 1075,
		transition: 'circ:out'
	})
	.morph({
		'left': 93
	});
	
	// bring in photo info button
	$('container').getElements('.photo-info').set('morph', {
		duration: 'long',
		transition: 'circ:out'
	})
	.morph({
		'left': 0
	});
	
}

// gallery animation
var ssNavigate = function(direction) {
	
	clearInterval(images);
	
	var d = direction;
	var el = $('imagelist').getElement('li.selected');
	var count = el.getAllPrevious('li').length;
			
	switch(d) {
	
		case 'next' :
						
			count = count + 1;	

			if (count == $('imagelist').getElements('li').length){
				count = 0;
			}
			
			$('imagelist').getElements('li.selected').removeClass('selected').each(function(el){
				if (el.getStyle('opacity') == 1){
					el.tween('opacity', 0);
				}
			});
			
			if (count == 0) {
				count++;
			
				$('imagelist').getElements('li')[0].setStyles({
					'display':'block',
					'opacity': 0
				}).addClass('selected')
				.tween('opacity', 1);	
			}
			else {
				el.getNext('li').setStyles({
					'display':'block',
					'opacity': 0
				}).addClass('selected')
				.tween('opacity', 1);
			}
		
		break;
		
		case 'prev' : 
			
			if (count == 0){
				count = $('imagelist').getElements('li').length;
			}
			
			$('imagelist').getElements('li.selected').removeClass('selected').each(function(el){
				if (el.getStyle('opacity') == 1){
					el.tween('opacity', 0);
				}
			});
						
			if (count == $('imagelist').getElements('li').length) {
				count--;
			
				$('imagelist').getElements('li')[count].setStyles({
					'display':'block',
					'opacity': 0
				}).addClass('selected')
				.tween('opacity', 1);	
			}
			else {
				el.getPrevious('li').setStyles({
					'display':'block',
					'opacity': 0
				}).addClass('selected')
				.tween('opacity', 1);
			}
		break;

	}
	
}

