window.addEvent('domready', function() {

// NAVIGATION BALL
	
		var navLinks = $('nav1').getChildren('a');
		var navLinksLength = navLinks.length;
		var navTimer1 = false;
		
		// find the active link
		for(i=0;i<navLinksLength;i++) {
		
			if(navLinks[i].hasClass('on')) {
				var navDefPos = $('nav1').getPosition();
				var navDefThisSize = navLinks[i].getSize();
				var navDefThisPos = navLinks[i].getPosition();
				navDefault = (navDefThisPos.x - navDefPos.x) + (navDefThisSize.x/2) - 10;
				//navDefaultconsole.log(navDefault);
				$('nav1ball').setStyle('left',navDefault);
				break;
			}
		}
		
		// function to return ball to rest
		navReturn = function () {
			new Fx.Tween($('nav1ball'),{transition: Fx.Transitions.Quad.easeOut}).start('left', navDefault + 'px');	
		}
		
		// set the events
		for(i=0;i<navLinksLength;i++) {
			
			// set the rollover
			navLinks[i].addEvent('mouseover',function() {
				// clear the return function
				if (navTimer1) {
					clearTimeout(navTimer1);
				}
				var navPos = $('nav1').getPosition();
				var navThisSize = this.getSize();
				var navThisPos = this.getPosition();
				// position of the link, minus the position of the nav container, plus half the width of the link, minus the right padding on the link
				var NavMoveTo = (navThisPos.x - navPos.x) + (navThisSize.x/2) - 10;
				new Fx.Tween($('nav1ball'),{transition: Fx.Transitions.Quad.easeOut}).start('left', NavMoveTo + 'px');	
			});
			
			// set the rolloff event
			navLinks[i].addEvent('mouseout',function() {
				navTimer1 = setTimeout('navReturn()',1000);
			});
			
		}

// XFADE
	
	if ($('xfade')!=undefined) {

		xfadeSlides = $('xfade').getChildren('img');
		xfadeSlidesLength = xfadeSlides.length;
		
		// fade in
		xfadeCycleIn = function(i) {
			// hide the spinner
			if ($('spinner').getStyle('opacity')>0) {
				new Fx.Tween($('spinner')).start('opacity',0);
			}
			xfadeSlides[i].setStyles({'visibility':'visible','opacity':0,'margin-left':'-200px'});
			// fade 
			new Fx.Morph(xfadeSlides[i],{'transition': Fx.Transitions.Cubic.easeOut,'duration': 2000}).start({
				'opacity': 1,
				'margin-left': 0
			}).chain(function() {
			
				if (xfadeSlidesLength>1) {
					// set up the fadeout
					xfadeCycleOut.delay(4000);
				}
				
				// set up the rollover caption
				/*
				var thisCaption = xfadeSlides[i].getProperty('alt');
				if (thisCaption!=undefined) {
					
					xfadeSlides[i].addEvents({
						'mouseout':function() {
							$('caption').setStyles({'bottom':'-30px','opacity':0});
							$('caption').set('text','');
							$('caption').setStyles({'bottom':'-30px','opacity':0});
						},
						'mouseover':function() {
							$('caption').set('text',thisCaption);
							new Fx.Morph($('caption'),{duration:1000}).start({'bottom':0,'opacity':1});
						}
					})
					
				}
				*/
			})
			// fade in the image title concurrently wit the image (just with a tiny delay)
			xFadeTitleIn.delay(500);
			
				
			
		}
		
		// fade in the image title
		xFadeTitleIn = function() {
		
			var i = xfadeCounter;
			
			// is there an H3 after this img?
			var possTitle = xfadeSlides[i].getNext();
			if (possTitle.nodeName=='H3') {
				possTitle.setStyles({'visibility':'visible','opacity':0,'margin-left':'-200px'});
				new Fx.Morph(possTitle,{'transition': Fx.Transitions.Cubic.easeOut,'duration': 2000}).start({
					'opacity': 1,
					'margin-left': 0
				})
			}
		}
		
		// fade out
		xfadeCycleOut = function() {
		
			var i = xfadeCounter;
			
			// kill the caption
			$('caption').setStyles({'bottom':'-30px','opacity':0})
		
			// is there an H3 after this img?
			var possTitle = xfadeSlides[i].getNext();
			// make it go away!
			if (possTitle.nodeName=='H3') {
				possTitle.setStyles({'opacity': 0,'visibility':'hidden'});
			}
			// fade the currentimage out;
			new Fx.Morph(xfadeSlides[i],{'transition': Fx.Transitions.Cubic.easeOut,'duration': 1000}).start({
				'opacity': 0
			})
			// set up what image to deal with next
			i++;
			if (i>xfadeSlidesLength-1) {
				i = 0;
			}
			xfadeCounter = i;
			xfadeCycleIn(i);
		}
		// start it off
		if (xfadeSlidesLength>0) {
		
			// create the spinner
			var myCaption = new Element('div', {
				'id': 'spinner',
				'styles' : {
					'display':'block',
					'opacity':1
				}
			});
			myCaption.inject($('xfade'),'top');
			
			// create the caption
			var myCaption = new Element('div', {
				'id': 'caption',
				'class': 'xfade_caption',
				'styles' : {
					'bottom':'-30px',
					'opacity':0
				}
			});
			myCaption.inject($('xfade'),'bottom');
			
			// start the slideshow
			xfadeCounter = 0;
			setTimeout('xfadeCycleIn(' + xfadeCounter + ')',2000);
			
		}
	
	}

})
