Beezwax.Behaviour.define({

	/**
	 * Displays a spashpage
	 **/
    '#splash' : { initialize : function(element){
    
		element.morph('opacity: 0;', { 
			delay: 1,
			duration: 2,
			after: function(){
				element.remove();
			}
		});
		
	}},
	
	/**
	 * Disable links that should not be clicked
	 **/
	'.inactive' : { 
		click : function(e) { e.stop(); }
	}

})

/**
 * Loads extra backgroundimages
 * @param	none
 * @return	void
 **/
var images = $A([
    'images/background-slideshow-5.jpg',
	 'images/background-slideshow-3.jpg',
    'images/background-slideshow-4.jpg',
	'images/background-slideshow-2.jpg'
]);
	
Event.observe(window, 'load', function(){	
	images.each(function(src) { (new Image()).src = src; });
	
	var front = $('background'), back = $('background-2'),
		swapAhead = function() { var t = front; front = back; front = t; },
		swapBack = function() { var t = back; back = front; front = t; },
		oldDirection = null, direction, animating = false;
		
	$('gallery-next', 'gallery-prev').each(function(button) {
		var direction = button.id == 'gallery-next';
		var animating = false;
		
		button.observe('click', function(e) {
			e.stop();
			if (animating) return;
			animating = true;
			
			if (direction) {
				if (oldDirection !== null && oldDirection != direction) {
					var t = back; back = front; front = t;
					front.setStyle('width:1920px;z-index:1;');
					back.setStyle('width:1920px;z-index:-1;')
				} else {
					back.setStyle({backgroundImage : 'url(' + images.first() + ')'});
					images.push(images.shift());
				}
				
				front.morph('width: 0px;', { 
					duration : .9,
					after : function() {
						var t = front; front = back; back = t;
						back.setStyle('width: 1920px;z-index:-1;');
						front.setStyle('z-index:1;');
						animating = false;
						oldDirection = direction;
					}
				});
				
			} else {
				if (oldDirection != direction) {
					var t = back; back = front; front = t;
					front.setStyle('width: 0;z-index: 1;')
					back.setStyle('width: 1920px;z-index: -1;')
					if (oldDirection === null) {
						images.unshift(images.pop());
						front.setStyle({backgroundImage : 'url(' + images.last() + ')'});
						images.unshift(images.pop());
					}
				} else {
					front.setStyle({backgroundImage : 'url(' + images.last() + ')'});
					images.unshift(images.pop());
				}
				
				front.morph('width: 1920px;', { 
					duration : .9, 
					after : function() {
						var t = back; back = front; front = t;
						front.setStyle('width:0;z-index:1;');
						back.setStyle('z-index:-1;')
						animating = false;
						oldDirection = direction;
					}
				});
				
			}
		});
	})
	
	
});


