window.addEvent('domready', function(){

//Set up variable to stop user clicking muiltiple times and breaking script
	var isReady=('ready');
	$('images').getElements('img').each(function(anchor, index) {

		//Set up landing page defaults
		if(anchor.getProperty('id') == ('img-0')){
			$$('#images img').setStyle('opacity',0);
			$('img-0').setStyle('opacity',1);
			$$('#panels div').setStyles({'left': '6px', 'opacity': 0});
			$('panel-0').setStyles({'left': '6px', 'opacity': 1});
			$('botlink-intro').setProperty('class','db');
		}
	});

	//Functions to swap sub bottom nav link displays
	var hideLink1 = function(){
		$('botlink-intro').setProperty('class','db');
		$('botlink').setProperty('class','dn');
	};

	var hideLink = function(){
		$('botlink-intro').setProperty('class','dn');
		$('botlink').setProperty('class','db');
	};
	
	
	$('getfacts').addEvent('click', function(event){
		event.stop();
	
		if(isReady==('ready')){
			isReady=('busy');
			$('panel-0').setStyle('opacity', 0);
	
			var easeFirst = function(){
				var panelIn = new Fx.Morph($('panel-1'), {duration: 500, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
				panelIn.start({'left': '6px', 'opacity': 1});
				isReady=('ready');
			}.delay(2500);
		
			var imgOut = new Fx.Morph($('img-0'), {duration: 2000, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
			imgOut.start({'opacity': 0});
			var imgIn = new Fx.Morph($('img-1'), {duration: 2000, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
			imgIn.start({'opacity': 1});
		
			$('link-1').addClass('selected');
		
			$$('#pn_prev a').setProperties({
				'class': 'selected',
				'id': 'pn-0'
			});
			$$('#pn_next a').setProperties({
				'class': 'selected',
				'id': 'pn-2'
			});
			hideLink1();
			hideLink();
		};
	});
	

	//Bottom nav action image and panel fades
	var tweenImg = function() {
		$('nav').getElements('a').each(function(anchor, index) {

			anchor.addEvents({
				'click': function(event) {
					event.stop();
					
					//Conditional to check if effect is still active...
					if(isReady==('ready')){
						var linkEffects = function(){
							//...And set variable from ready to busy							
							isReady=('busy');

							// Work out which Prev/Next button to show
							pn = (index);	
							pnp = pn-1;
							pnn = pn+1;
							if(pnp < 0){
								$$('#pn_prev a').setProperties({
									'class': 'disabled',
									id: 'pn-'+pnp
								}),
								hideLink1();
							}  else{
								$$('#pn_prev a').setProperties({
									'class': 'selected',
									id: 'pn-'+pnp
								}),
								hideLink();
							}		
							if(pnn >7){
								$$('#pn_next a').setProperties({
									'class': 'disabled',
									'id': 'pn-'+pnn
								})	
							} else {
								$$('#pn_next a').setProperties({
									'class': 'selected',						
									'id': 'pn-'+pnn
								});	
							}

							//Begin image and panel tween / morph							
							$$('#panels div').each(function(panel){
								if(panel.getStyle('opacity') == '1') {
									var panelOut = new Fx.Morph(panel, {duration: 0, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
						 			panelOut.start({'left': '6px', 'opacity': 0});
								};
							});
					
							var delayPanelIn = function(){
								var theNewPanel = $('panel-'+index).id
								var panelIn = new Fx.Morph(theNewPanel, {duration: 500, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
					 			panelIn.start({'left': '6px', 'opacity': 1});
								var restVariable = function(){
									//reset variable so user can continue to click
									isReady=('ready');
								}.delay(500);
							}.delay(2500);
				
							$$('#nav a').removeClass('selected');
							$('link-'+index).addClass('selected');

							$$('#images img').each(function(image) {
								if(image.getProperty('id').substring(image.getProperty('id').indexOf('-')) == anchor.getProperty('id').substring(anchor.getProperty('id').indexOf('-'))) {
									 image.get('tween',{link:'ignore', property:'opacity', duration:2000}).start(1);
								} else if(image.getStyle('opacity') > 0) {
									image.get('tween',{link:'ignore', property:'opacity', duration:2000}).start(0);
								}
							});
						}.delay(0);
					};
					//End of conditional to test effect activity
				}	
			});
		});
	};

	var navClickFunction = function(event) {
		event.stop();
						
//						console.log('START: isReady: '+isReady);
				//Conditional to check if effect is still active...
				if(isReady==('ready')){
					var arrowEffects = function(){

						//...And set variable from ready to busy	
						isReady=('busy');
						//Extract  id info from Prev/Next buttons				
						theId = event.target.getProperty('id');

						theIdSplit = theId.split('pn-');
						theIdSplit = theIdSplit[1];
						theLink = ('link-'+theIdSplit);
						
//link-8						
//						console.log(theLink);
						$$('#nav a').removeClass('selected');
		
						//Switch main nav styles depending on where the slide show is				
						if (theLink != 'link--1'){
							$(theLink).addClass('selected');
							now_val = parseInt(theIdSplit);
						} else {
							$('link-0').addClass('selected');
							now_val = 0;
						}
						
			
	
						prev_val = now_val-1;
						next_val = now_val+1;
						
						
						if(!$defined(document.id('link-'+prev_val))) {
//							console.log('removed previous click event listeners');
							document.id('pn_prev').getElement('a').removeEvent('click',navClickFunction);
						} else {
//						console.log('added previous click event listener');
							document.id('pn_prev').getElement('a').addEvent('click',navClickFunction);
						}
						
						if(!$defined(document.id('link-'+next_val))) {
//							console.log('removed next click event listener');
							document.id('pn_next').getElement('a').removeEvent('click',navClickFunction);
						} else {
//						console.log('added next click event listener');
							document.id('pn_next').getElement('a').addEvent('click',navClickFunction);
						}
						
						
						// Work out which Prev/Next button to show				
						if (prev_val < 0){
							$$('#pn_prev a').setProperties({
								'id': 'pn--1',
								'class': 'disabled'
							});
							$$('#pn_next a').setProperties({
								'id': 'pn-' + next_val,
								'class': 'selected'
								});
								hideLink1();
						} else {
							$$('#pn_prev a').setProperties({
								'id': 'pn-' + prev_val,
								'class': 'selected'
							});
							$$('#pn_next a').setProperties({
								'id': 'pn-' + next_val,
								'class': 'selected'
							});
								hideLink();
						};
													
						if (next_val > 7){
							$$('#pn_next a').setProperties({
								'id': 'pn-7',
								'class': 'disabled'
							});

						};
													
						//Begin image and panel tween / morph	
						if (theId != 'pn--1'){
							$$('#images img').each(function(image) {
								if(image.getStyle('opacity') > 0) {
									image.get('tween',{link:'ignore', property:'opacity', duration:2000}).start(0);
								};
								imageId = image.getProperty('id').split('img-');
								imageId = imageId[1];
								if(theIdSplit == imageId){
									image.get('tween',{link:'ignore', property:'opacity', duration:2000}).start(1);
								};
							});
							$$('#panels div').each(function(panel){
								if(panel.getStyle('opacity') == '1') {
									var panelOut = new Fx.Morph(panel, {duration: 0, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
						 			panelOut.start({'left': '6px', 'opacity': 0});
								};
							});
							var delayPanelIn = function(){
								var theNewPanel = $('panel-'+theIdSplit).id
								var panelIn = new Fx.Morph(theNewPanel, {duration: 500, link: 'ignore', transition: Fx.Transitions.Sine.easeOut});
					 			panelIn.start({'left': '6px', 'opacity': 1});
								var restVariable = function(){
									//reset variable so user can continue to click
									isReady=('ready');
								}.delay(500);
								
							}.delay(2500);	
						} else {
						isReady='ready';
						console.log('isReady: '+isReady);
						}
					}.delay(0);
					
				};
				//End oif conditional to test effect activity
			//}
	}
	var prevNext = function() {
		//Prev / Next arrows action for panel & image fades	
		//$('prev-next').getElements('a').each(function(anchor, index){
			document.id('pn_prev').getElement('a').addEvents({
				'click':navClickFunction
			});	
			document.id('pn_next').getElement('a').addEvents({
				'click':navClickFunction
			});	
	//});
};		

//Run  tween/morph functions
	tweenImg();
	prevNext();
});