window.addEvent('domready', function(){
	
	var siteRoot = "http://www.gareth-fuller.com/eventyr";
	  
  	background= new Image(); 
  	background.src= siteRoot + "/images/background.jpg";
  	scroll_bottom= new Image(); 
  	scroll_bottom.src= siteRoot + "/images/scroll-bottom.png";
  	scroll_repeat= new Image(); 
  	scroll_repeat.src= siteRoot + "/images/scroll-repeat.png";
  	scroll_top= new Image(); 
  	scroll_top.src= siteRoot + "/images/scroll-top.png";
  	
  	
  	if ($('main-content'))
	{
			
		//work out the doc height
		var main = $('main-content');
		var height = main.getStyle('height');
		var heightNum = height.replace('px','');
		var heightInt = parseInt(heightNum);
		
		//apply new hiehg to bottom scroll image
		var newHeightNumScroll = (heightInt - 43)
		var newHeightPxScroll = newHeightNumScroll + 'px';
		var scrollBottom = $('scroll-botom-container').getElement('img');
		scrollBottom.setStyle('margin-top', newHeightPxScroll);
		
		//apply new height to footer
		var newHeightNumFooter = (heightInt + 127)
		var newHeightPxFooter = newHeightNumFooter + 'px';
		$('footer').setStyle('top', newHeightPxFooter);
		
		//deal with corner image positions
		if($('news-container'))
		{
			//NEWS BOX CALCULATIONS
			var newsBox = $('news-container');
			var newsHeight = newsBox.getStyle('height');
			var newsHeightNum = newsHeight.replace('px', '');
			var newsHeightInt = parseInt(newsHeightNum);
			var newNewsHeight = (newsHeightInt - 130);
			var newNewsHeightPx = newNewsHeight + 'px';
			
			$('bottom-left-news').setStyle('top', newNewsHeightPx);
			$('bottom-right-news').setStyle('top', newNewsHeightPx);
		}
		
		if($('about-container'))
		{
			//ABOUT BOX CALCULATIONS
			var aboutBox = $('about-container');
			var aboutHeight = aboutBox.getStyle('height');
			var aboutHeightNum = aboutHeight.replace('px', '');
			var aboutHeightInt = parseInt(aboutHeightNum);
			var newAboutHeight = (aboutHeightInt - 130);
			var newAboutHeightPx = newAboutHeight + 'px';
			
			$('bottom-left-about').setStyle('top', newAboutHeightPx);
			$('bottom-right-about').setStyle('top', newAboutHeightPx);
		
		}
		
		if($('generic-container'))
		{
			//GENERIC BOX CALCULATIONS
			var genericBox = $('generic-container');
			var genericHeight = genericBox.getStyle('height');
			var genericHeightNum = genericHeight.replace('px', '');
			var genericHeightInt = parseInt(genericHeightNum);
			var newGenericHeight = (genericHeightInt - 130);
			var newGenericHeightPx = newGenericHeight + 'px';
			
			$('bottom-left-generic').setStyle('top', newGenericHeightPx);
			$('bottom-right-generic').setStyle('top', newGenericHeightPx);
		
		}
		
		if($('sub-nav'))
		{
			var subBox = $('sub-nav');
			var subHeight = subBox.getStyle('height');
			var subHeightNum = subHeight .replace('px', '');
			var subHeightInt = parseInt(subHeightNum);
			var newSubHeight = (subHeightInt - 116);
			var newSubHeightPx = newSubHeight + 'px';
			$('sub-nav-repeat').setStyle('height', newSubHeightPx);	
		}
		
		
	}
  	
  	//deal with the photo gallery
	if ( $('photo-container'))
	{
					
		var tweening = false;
		
		//assign first classes on images
		var amountArray = $('photo-nav-container').getElements('img');
		var firstLeft = amountArray[0];
		firstLeft.addClass('left');
		firstLeft.addClass('active');
		var firstRight = amountArray[4];
		firstRight.addClass('right');
		
		for (var i =0; i<amountArray.length; i ++)
		{
			amountArray[i].id = (i + 1);
			amountArray.setStyle('cursor', 'pointer'); 
		}
		
		
		
		//get the current active img
		var currentActive = $('photo-nav-container').getElement('.active');
		
		//set the dwafult image to be loaded
		var defaultImage = $('photo-container').getElement('img');
		defaultImage.src = currentActive.src;
		
		defaultImage.addEvent('load', function(event){
			//re-position the header and footer based on new height
			reSortHeader();
		});
		
		
		
		//deal with the click of the thumbnail images 
		$('photo-nav-container').getElements('img').addEvent('click', function(event){
			event.stop();
			
			var defaultImage = $('photo-container').getElement('img');
			defaultImage.src = this.src;
			
			//switch active state
			var previousActive = $('photo-nav-container').getElement('.active');
			previousActive.removeClass('active');
			this.addClass('active');
			
			//re-position the header and footer based on new height
			reSortHeader();
			
		});
		
		//set the style of the left and right arrow cursor to pointer
		$('arrow-left').getElement('span').setStyle('cursor', 'pointer');
		$('arrow-right').getElement('span').setStyle('cursor', 'pointer');
		
		//deal with left click
		$('arrow-left').getElement('span').addEvent('click', function (event){

			
			var slideTargetLeft = $('photo-nav-container');
			var myFx = new Fx.Tween(slideTargetLeft, {onComplete: function(){
                					tweening = false;} } );

			var currentActive = $('photo-nav-container').getElement('.left');
			var container = $('photo-nav-container');
			
			if (currentActive.id != 1)
			{
				if(tweening == false)
				{
					tweening = true;
					var containerLeft = container.getStyle('left');
					var containerLeftNum = containerLeft.replace('px', '');
					var containerLeftInt = parseInt(containerLeftNum);
	
					var moveAmount = 400;
					
					var currentActiveId = parseInt(currentActive.id);
					var nextActiveId = (currentActiveId - 1);
					var nextActive = $('photo-nav-container').getElementById(nextActiveId);
					nextActive.addClass('left');
					currentActive.removeClass('left');
					
					myFx.start('left', containerLeftInt, (containerLeftInt + moveAmount));
				}
			}
		});
		
		//deal with right click
		$('arrow-right').getElement('span').addEvent('click', function (event){
			

			var slideTargetLeft = $('photo-nav-container');
			var myFx = new Fx.Tween(slideTargetLeft, {onComplete: function(){
                					tweening = false;} } );

			var currentActive = $('photo-nav-container').getElement('.left');
			var container = $('photo-nav-container');
			
			if (currentActive.id * 5 < amountArray.length)
			{
				if(tweening == false)
				{
					tweening = true;
					var containerLeft = currentActive.getStyle('left');
					var containerLeftNum = containerLeft.replace('px', '');
					var containerLeftInt = parseInt(containerLeftNum);
					
					var moveAmount = (currentActive.id * -400);
	
					var currentActiveId = parseInt(currentActive.id);
					var nextActiveId = (currentActiveId + 1);
					var nextActive = $('photo-nav-container').getElementById(nextActiveId);
					nextActive.addClass('left');
					currentActive.removeClass('left');
	
	
					myFx.start('left', containerLeftInt, moveAmount);
				}
				
			}
		});
		
		
	}
	
	
	if ($('main-slide-wrapper'))
	{
		var amountArrayHome = $('slide-large').getElements('img');
		
		for (var i =0; i<amountArrayHome.length; i ++)
		{
			if (i == 0)
			{
				amountArrayHome[i].addClass('active');	
			}
			amountArrayHome[i].id = (i + 1);
		}
		
		setInterval ( "moveSlideHome()", 4000 );
	}
	
	if( $('bg').getStyle('position') == 'absolute' )
	{
		movebg_IE6();
	}
	
	
	
});


function moveSlideHome(){
	var slider = $('slide-large');
	var activeImage = $('slide-large').getElement('.active');
	var activeLeftPx = activeImage.getStyle('left');
	var activeLeftNum = activeLeftPx.replace('px','');
	var activeLeftNumInt = parseInt(activeLeftNum);
	var moveAmount = (activeImage.id * -617);
	
	var myFx = new Fx.Tween(slider);
	activeImage.removeClass('active');
	if (activeImage.id != 5)
	{
		var nextActive = (parseInt(activeImage.id) + 1);
		var nextActiveImage = slider.getElementById(nextActive);
		nextActiveImage.addClass('active');
	}
	else
	{
		moveAmount = 0;
		var nextActiveImage = slider.getElementById(1);
		nextActiveImage.addClass('active');
	}

	myFx.start('left', activeLeftNumInt, moveAmount);
	
}

function movebg_IE6()
{
	var bg = $('bg');
	window.addEvent('resize', function() { bg.setStyle('height', window.getSize().y); });
	window.addEvent('scroll', function() {
		var bg_height = bg.getSize().y;
		var scrolled_to = window.getScroll().y;
		var doc_height = window.getScrollSize().y;
		
		var bg_pos = 0;
		if(scrolled_to + bg_height > doc_height)
		{
			bg_pos = doc_height - bg_height;
		}
		else
		{
			bg_pos = scrolled_to;
		}
		bg.setStyle('top', bg_pos);
	});
}

function reSortHeader()
{
	var defaultImage = $('photo-container').getElement('img');
	//change the height of the photo container to be that of the image
	var photoContainer = $('photo-container');
	
	var size = defaultImage.getSize();
	var height = size.y;
	var heightInt = parseInt(height);
	var newHeightPx = heightInt + 'px'
	
	photoContainer.setStyle('height', newHeightPx);
	
	//work out the doc height
	var main = $('main-content');
	var height = main.getStyle('height');
	var heightNum = height.replace('px','');
	var heightInt = parseInt(heightNum);
	
	//apply new hiehg to bottom scroll image
	var newHeightNumScroll = (heightInt - 43)
	var newHeightPxScroll = newHeightNumScroll + 'px';
	var scrollBottom = $('scroll-botom-container').getElement('img');
	scrollBottom.setStyle('margin-top', newHeightPxScroll);
	
	//apply new height to footer
	var newHeightNumFooter = (heightInt + 127)
	var newHeightPxFooter = newHeightNumFooter + 'px';
	$('footer').setStyle('top', newHeightPxFooter);	
	
	
	if($('generic-container'))
	{
		//GENERIC BOX CALCULATIONS
		var genericBox = $('generic-container');
		var genericHeight = genericBox.getStyle('height');
		var genericHeightNum = genericHeight.replace('px', '');
		var genericHeightInt = parseInt(genericHeightNum);
		var newGenericHeight = (genericHeightInt - 130);
		var newGenericHeightPx = newGenericHeight + 'px';
		
		$('bottom-left-generic').setStyle('top', newGenericHeightPx);
		$('bottom-right-generic').setStyle('top', newGenericHeightPx);
	
	}
	

}



