var inGallery = false;

window.addEvent( 'domready', function() {	
	captureLinks();
	
	if( window.innerHeight ) {
		if( window.innerHeight > $('wrapper-out').clientHeight ) {
			$('wrapper-out').setStyle('margin-top', ( window.innerHeight-$('wrapper-out').clientHeight)/1.99 );
		}
	}
	
	if( document.body.clientHeight && !window.innerHeight ) {
		if( document.body.clientHeight > $('wrapper-out').clientHeight ) {
			$('wrapper-out').setStyle('margin-top', ( document.body.clientHeight-$('wrapper-out').clientHeight)/1.99 );
		}
	}
});

function captureLinks() {

	// remove all events then copy paste cube.js code to open links here and alter the way the loader is changed.

	$$('.dyn_link_front').removeEvents('click');

	// catch the links
	$$('.dyn_link_front').addEvent('click', function(e) {
			
		// need to split the variables
		var str_to_split = $(this).getProperty('href');
		var args = str_to_split.split('?')[1];
			
		var elem = $(this);
		e = new Event(e).stop();
		
		// hide the maincontent
		myFx = new Fx.Tween( $('dyn_content'), { duration: 400 } );
		myFx2 = new Fx.Tween( $('maincontent-loader'), { duration: 400 } );
		
		myFx.start( 'opacity', 1, 0.0 );
		myFx2.start( 'opacity', 0.0, 1.0 );
		
		myFx.addEvent('complete', function() { 
			
		var req = new Request.HTML({
						method: 'get',
						url: elem.getProperty('href'),
						data: args + '&page_ajax=1',
						onRequest: function() {
						},
						update: $('dyn_content'),
						onComplete: function(responseHTML) {
										
							myFx3 = new Fx.Tween( $('dyn_content'), { duration: 400 } );
							myFx3.start( 'opacity', 0.0, 1.0 );
							myFx2.start( 'opacity', 1.0, 0.0 );
									
							captureLinks();
							if( (typeof(pageTracker) != "undefined") ) {
								pageTracker._trackPageview("/"+ elem.get('html') );
							}
						}
		}).send();
		
		});
	});
}

function loadSlideshow(args, time) { 

	// preload images for slideshow
	for(var i = 0; i < args.length; i++ ) {
		var elem = new Element('img', { 'src': args[i] } );
	}

	var lastnumber = 1;
	
	$("headerimage-swap").setStyle('background', "url(" + args[lastnumber] + ") no-repeat center bottom");
	
	var args_length = args.length;
	var args_arr = args;
	
	var timerFunction = function() {
	
		if( inGallery ) return;
	
		if( lastnumber < args_length-1 )
			lastnumber++;
		else lastnumber = 1;

	
		$("header").setStyle('background', $("headerimage-swap").getStyle('background') );
		
		var myFx = new Fx.Tween( $('headerimage-swap'), {duration: 400});
		var myFx2 = new Fx.Tween( $('headerimage-swap'), {duration: 1000});
		
		myFx.addEvent('complete', function() {
			$("headerimage-swap").setStyle('background', "url(" + args_arr[lastnumber] + ") no-repeat center bottom");
			myFx2.start( 'opacity', 0.0, 1 );
		});
		
		myFx.start( 'opacity', 1, 0.0 );
		
	}
	
	timerFunction.periodical(time*1000);
}

function loadGallery() {

	// construct array
	var images = Array();
	
	// flag for next/previous
	var isFirst = false;
	var isLast = false;

	// get array of images
	$$('a.imgset').each( function(item, index) {
		// for each image, add to array
		images[index] = item.get('href');
	});
	
	// get the active image
	if( $('galleryimg') ) var activeImgURL = $('galleryimg').get('src');
	else var activeImgURL = '';
	
	// now fix the buttons
	$('arrowbutton-previous').addEvent('click', function(e) {
		
	// if the active image is the first image, disable previous button
	if( activeImgURL == images[0] ) isFirst = true; else isFirst = false;
	if( activeImgURL == images[ images.length-1 ] ) isLast = true; else isLast = false;
	
			if( !isFirst ) {
				// fade the image out
				myFx = new Fx.Tween( $('galleryimg'), { duration: 400 } );
				myFx2 = new Fx.Tween( $('galleryimg'), { duration: 400 } );
			
				myFx.start( 'opacity', 1.0, 0.0 ); // fade out
			
				// on complete, change the image
				myFx.addEvent('complete', function() {
					// load the image
					var img = new Element('img', {src: images[ images.indexOf(activeImgURL)-1 ], 'class': 'tmpImage' });
					
					img.addEvent( 'load', function() {	
						// change the image
						$('galleryimg').set( 'src', images[ images.indexOf(activeImgURL)-1 ] );
						$('galleryimglink').set( 'href', images[ images.indexOf(activeImgURL)-1 ] );
						// fade back in
						myFx2.start( 'opacity', 0.0, 1.0 ); // fade in
					});
				});
				
				myFx2.addEvent('complete', function() {
					// check current active image
					activeImgURL = $('galleryimg').get('src');
				
					// if the active image is the first image, disable previous button
					if( activeImgURL == images[0] ) isFirst = true; else isFirst = false;
					if( activeImgURL == images[ images.length-1 ] ) isLast = true; else isLast = false;
				});
			}
			
			// stop the link
			this.blur();
			e = new Event(e).stop();
	});
	
	$('arrowbutton-next').addEvent('click', function(e) {
		
			if( !isLast ) {
				// fade the image out
				myFx = new Fx.Tween( $('galleryimg'), { duration: 400 } );
				myFx2 = new Fx.Tween( $('galleryimg'), { duration: 400 } );
			
				myFx.start( 'opacity', 1.0, 0.0 ); // fade out
			
				// on complete, change the image
				myFx.addEvent('complete', function() {
					// load the image
					var img = new Element('img', {src: images[ images.indexOf(activeImgURL)+1 ], 'class': 'tmpImage' });
					
					img.addEvent( 'load', function() {	
						// change the image
						$('galleryimg').set( 'src', images[ images.indexOf(activeImgURL)+1 ] );
						$('galleryimglink').set( 'href', images[ images.indexOf(activeImgURL)+1 ] );
						// fade back in
						myFx2.start( 'opacity', 0.0, 1.0 ); // fade in
					});
				});
				
				myFx2.addEvent('complete', function() {
					// check current active image
					activeImgURL = $('galleryimg').get('src');
				
					// if the active image is the first image, disable previous button
					if( activeImgURL == images[0] ) isFirst = true; else isFirst = false;
					if( activeImgURL == images[ images.length-1 ] ) isLast = true; else isLast = false;
				});
			}
			
			// stop the link
			this.blur();
			e = new Event(e).stop();
	});
	
	$$('a.imgset').addEvent('click', function(e) {
		// fade the image out
		myFx = new Fx.Tween( $('galleryimg'), { duration: 400 } );
		myFx2 = new Fx.Tween( $('galleryimg'), { duration: 400 } );
			
		myFx.start( 'opacity', 1.0, 0.0 ); // fade out
		
		var imga = this;
		
		// on complete, change the image
		myFx.addEvent('complete', function() {
			// load the image
			var img = new Element('img', {src: imga.get('href'), 'class': 'tmpImage' });
			
			img.addEvent( 'load', function() {	
				// change the image
				$('galleryimg').set( 'src', imga.get('href') );
				$('galleryimglink').set( 'href', imga.get('href') );
				// fade back in
				myFx2.start( 'opacity', 0.0, 1.0 ); // fade in
			});
		});
				
		myFx2.addEvent('complete', function() {
			// check current active image
			activeImgURL = $('galleryimg').get('src');
				
			// if the active image is the first image, disable previous button
			if( activeImgURL == images[0] ) isFirst = true; else isFirst = false;
			if( activeImgURL == images[ images.length-1 ] ) isLast = true; else isLast = false;
		});
		
		// stop the link
		this.blur();
		e = new Event(e).stop();
	});
	
	// attach drag scroller
	var steps = 980 - $('dragContainer').getSize().x;
	
	//var myScroller = new Fx.Scroll( 'dragContainer' );
	
	var slider = new Slider( $('scroller'), $('dragScroller'), {	
		'steps': steps,
		'mode': 'horizontal',
		'onChange': function(step){
			// Scrolls the content element in x or y direction.
			var x = step;			
			$('dragContainer').setStyle('margin-left', x );
		}
	}).set(0);
	
	/*if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}*/
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});

}
