function adjustFooter()
{
	if (window.innerHeight) var docheight = window.innerHeight;
	else if (document.all) var docheight = document.body.clientHeight;
	var contentheight = $('content').getScrollSize().y;

	if (docheight < contentheight + 30)
	{
/* 		$('footer').setStyle('bottom', contentheight - docheight - $(document.body).getScroll().y + 30); */
		$('footer').setStyle('top', docheight + $(document.body).getScroll().y - $('footer').getSize().y);
		$('footer').setStyle('bottom', 'auto');
	}
	else
	{
		$('footer').setStyle('top', 'auto');
		$('footer').setStyle('bottom', 0);
	}
}

if ($('content')) $('content').addEvent('resize',function(){adjustfooter();});

window.addEvent('domready', function()
{	
	//Floating footer
	if (!$('content').hasClass('home')) adjustFooter();
	
	//Define variables
	var left = $('menu').getElementById('left');
	var right = $('menu').getElementById('right');
	
	left.setStyle('visibility','visible');
	right.setStyle('visibility','visible');
	
	var leftFocus = left.getElement('div.focus');
	var leftList = left.getElement('div.list');
	var leftContainer = left.getElement('div.container');
	var rightFocus = right.getElement('div.focus');
	var rightList = right.getElement('div.list');
	var rightContainer = right.getElement('div.container');

	var leftFocusTween = new Fx.Tween(leftFocus);
	var leftListTween = new Fx.Tween(leftList);
	var rightFocusTween = new Fx.Tween(rightFocus);
	var rightListTween = new Fx.Tween(rightList);
	
	var leftListHeight = leftList.getSize().y - 16;
	var rightListHeight = rightList.getSize().y - 16;
	
	var leftItems = leftList.getElements('div.item');
	var rightItems = rightList.getElements('div.item');
	
	var allItems = leftItems.concat(rightItems);
		
		
	//Create highlight functions for all list items
	allItems.each(function(item)
	{		
		if (item.getElement('a'))
		{
			item.colour = item.getElement('a').getStyle('color');
			
			item.addEvents({
				'mouseenter': function()
				{
					this.getElement('a').tween('color', '#222');
				},
				'mouseleave': function()
				{
					this.getElement('a').tween('color', this.colour);
				}
			});
		}
	});
	
	//Check if left menu is selected, and apply respective functions	
	if (left.className != 'selected')
	{
		leftListTween.set('height',24);
		leftList.getElement('div.container').setStyle('margin-top', leftListHeight * (-1) / 2);
		leftFocusTween.set('opacity',0);
		leftListTween.set('opacity',0);
		
		left.addEvents({
			'mouseenter': function()
			{
				leftFocusTween.cancel();
				leftFocusTween.set('display','block');
				leftFocusTween.start('opacity',1);
				
				leftListTween.cancel();
				leftListTween.set('display','block');
				leftList.fade('in');
				leftList.getElement('div.container').tween('margin-top', 0);
				leftListTween.start('height',leftListHeight)
				
				if (right.hasClass('selected'))
				{
					rightFocusTween.cancel();
					rightFocusTween.start('opacity',0);
					rightList.fade('out');
				}
				
			},
			'mouseleave': function()
			{
				leftFocusTween.cancel();
				leftFocusTween.start('opacity',0).chain(function()
				{
					leftFocusTween.set('display','none');
				});
				
				leftListTween.cancel();
				leftList.fade('out');
				leftList.getElement('div.container').tween('margin-top', leftListHeight * (-1) / 2);
				leftListTween.start('height',24);
				
				if (right.hasClass('selected'))
				{
					rightFocusTween.cancel();
					rightFocusTween.start('opacity',1);
					rightList.fade('in');
				}
			}
		});
		
	} else {
		
		leftListTween.set('height',24);
		leftFocusTween.set('display','block');
		leftListTween.set('display','block');
		leftListTween.set('visibility','visible');
		leftItems.each(function(item) { item.set('opacity',0); });
		
		var leftListPos = leftList.getElement('div.selected').getPosition(leftContainer).y;
		leftList.getElement('div.container').setStyle('margin-top', leftListPos * (-1));
		
		left.addEvents({
			'mouseenter': function()
			{	
				leftListTween.cancel();
				leftListTween.start('height',leftListHeight);
				leftList.getElement('div.container').tween('margin-top', 0);
				leftList.getElement('div.selected').getElement('a').tween('color', '#222');
				leftItems.each(function(item) { item.fade('in'); });
			},
			'mouseleave': function()
			{
				leftListTween.cancel();
				leftListTween.start('height',24);
				leftList.getElement('div.container').tween('margin-top', leftListPos * (-1));
				leftList.getElement('div.selected').getElement('a').tween('color', '#58b7dd');
				leftItems.each(function(item) { item.fade('out'); });
			}
		});
		
	}
	
	//Check if right menu is selected, and apply respective functions
	if (right.className != 'selected')
	{
		rightListTween.set('height',24);
		rightList.getElement('div.container').setStyle('margin-top', rightListHeight * (-1) / 2);
		rightFocusTween.set('opacity',0);
		rightListTween.set('opacity',0);
		
		right.addEvents({
			'mouseenter': function()
			{
				rightFocusTween.cancel();
				rightFocusTween.set('display','block');
				rightFocusTween.start('opacity',1);
				
				rightListTween.cancel();
				rightListTween.set('display','block');
				rightList.fade('in');
				rightList.getElement('div.container').tween('margin-top', 0);
				rightListTween.start('height',rightListHeight);
				
				if (left.className == 'selected')
				{
					leftFocusTween.cancel();
					leftFocusTween.start('opacity',0);
					leftList.fade('out');
				}
				
			},
			'mouseleave': function()
			{
				rightFocusTween.cancel();
				rightFocusTween.start('opacity',0).chain(function()
				{
					rightFocusTween.set('display','none');
				});
				
				rightListTween.cancel();
				rightList.fade('out');
				rightList.getElement('div.container').tween('margin-top', rightListHeight * (-1) / 2);
				rightListTween.start('height',24);
				
				if (left.className == 'selected')
				{
					leftFocusTween.cancel();
					leftFocusTween.start('opacity',1);
					leftList.fade('in');
				}
			}
		});
		
	} else {
	
		rightListTween.set('height',24);
		rightFocusTween.set('display','block');
		rightListTween.set('display','block');
		rightListTween.set('visibility','visible');
		rightItems.each(function(item) { item.set('opacity',0); });
		
		var rightListPos = rightList.getElement('div.selected').getPosition(rightContainer).y;
		rightList.getElement('div.container').setStyle('margin-top', rightListPos * (-1));
		
		right.addEvents({
			'mouseenter': function()
			{	
				rightListTween.cancel();
				rightListTween.start('height',rightListHeight);
				rightList.getElement('div.container').tween('margin-top', 0);
				rightList.getElement('div.selected').getElement('a').tween('color', '#222');
				rightItems.each(function(item) { item.fade('in'); });
			},
			'mouseleave': function()
			{
				rightListTween.cancel();
				rightListTween.start('height',24);
				rightList.getElement('div.container').tween('margin-top', rightListPos * (-1));
				rightList.getElement('div.selected').getElement('a').tween('color', '#58b7dd');
				rightItems.each(function(item) { item.fade('out'); });
			}
		});
	}
	
	
	//Create highlight functions for footer items
	$('footer').getElements('a.item').each(function(item)
	{		
		item.set('tween', {duration: 300});
		item.addEvents({
			'mouseenter': function()
			{
				if (!this.hasClass('selected')) this.tween('opacity', 0.4);
			},
			'mouseleave': function()
			{
				this.tween('opacity', 1);
			}
		});
	});
	
	//If footer buttons, add functionality to news feed
	if ($('footer').getElement('div.button'))
	{
		$('footer').getElements('div.button').each(function(button)
		{
			button.setStyle('opacity',0.4);
			
			button.addEvents({
				'mouseenter': function()
				{
					this.tween('opacity', 1);
				},
				'mouseleave': function()
				{
					this.tween('opacity', 0.4);
				}
			});
		});
		
		$('footer').getElements('a.newstitle').each(function(title)
		{
			title.addEvents({
				'mouseenter': function()
				{
					this.tween('color', '#cdeaf5');
				},
				'mouseleave': function()
				{
					this.tween('color', '#fff');
				}
			});
		});
		
		var newsItems = $('footer').getElements('div.newsitem');
		var currentItem = 0;
		newsItems[0].setStyles({'opacity':0,'display':'block'});
		newsItems[0].fade('in');

		function getItem(which)
		{
			newsItems[which].setStyles({'opacity':0,'display':'block'});
			newsItems[which].fade('in');
		}
		
		$('footer').getElement('div.leftarrow').addEvent('click',function()
		{
			newsItems[currentItem].fade('out').get('tween').chain(function()
			{
				newsItems[currentItem].setStyle('display','none');
				currentItem--;
				if (currentItem < 0) currentItem = newsItems.length-1;
				getItem(currentItem);
			})
		});
		
		$('footer').getElement('div.rightarrow').addEvent('click',function()
		{
			newsItems[currentItem].fade('out').get('tween').chain(function()
			{
				newsItems[currentItem].setStyle('display','none');
				currentItem++;
				if (currentItem > newsItems.length-1) currentItem = 0;
				getItem(currentItem);
			})
		});
	}
	
	//Sliding text content
	if ($('contentslide'))
	{
		//Define variables
		var currentContent = 0;
		var lastContent = 0;
		var slideLinks = $('contentslide').getElementById('submenu').getElements('li');
		var slide = $('contentslide').getElementById('slide');
		var slideContainer = slide.getElement('div');
		var slideContents = slideContainer.getElements('div.block');
		
		//Prepare images
		if ($('image'))
		{
			var images = $('image').getElements('div');
			for (i=1; i<images.length; i++)
			{
				images[i].fade('hide');
				images[i].getElement('img').className = '';
			}
		}
				
		//Prepare first item
		slideLinks[currentContent].getElement('a').setStyle('color', '#222');
		slide.setStyle('height', slideContents[currentContent].getSize().y-1);
		slideContainer.setStyle('top', (-1) * slideContents[currentContent].getPosition(slideContainer).y);
		
		var linkcount = 0;
		slideLinks.each(function(item)
		{	
			item.id = linkcount;
			linkcount++;
					
			item.addEvents({
				'mouseenter': function()
				{
					this.getElement('a').tween('color', '#222');
				},
				'mouseleave': function()
				{
					if (currentContent != this.id) this.getElement('a').tween('color', '#58b7dd');
				},
				'click': function(e)
				{	
					e.stop();
					if (currentContent != this.id)
					{
						lastContent = currentContent;
						currentContent = this.id;
						for (i=0; i<slideLinks.length; i++) {
							if (i != this.id) slideLinks[i].getElement('a').tween('color', '#58b7dd');
						}
						if (slideContents[currentContent].getSize().y != slideContents[lastContent].getSize().y)
							slide.tween('height', slideContents[currentContent].getSize().y-1);
							
						var newPos = Number((-1) * slideContents[currentContent].getPosition(slideContainer).y)
						slideContainer.tween('top', newPos);
						
						if ($('image'))
						{
							images[lastContent].fade('out').get('tween').chain(function(){ images[currentContent].fade('in');});
						}
					}
				}
			});
		});
	}
	
	//Project grid & tooltip functionality
	if ($('projectgrid'))
	{
		//Set up tooltips
		$$('.projectthumb a').each(function(element,index)
		{    
			var tipcontent = element.get('rel').split('::'); 
			element.store('tip:title', tipcontent[0]);  
			element.store('tip:text', tipcontent[1]);
		});
		
		var tooltips = new Tips('.projectthumb a',{  
			className: 'thumbtip',  
			fixed: false,  
			hideDelay: 0,  
			showDelay: 50,
			offsets: {'x': -103, 'y': 20}
		});
		
		tooltips.addEvents(
		{  
			'show': function(tip)
			{
				tip.fade('show');  
			},  
			'hide': function(tip)
			{  
				tip.fade('hide');
			}  
		});
		
		//Set up filtering
		var classShowing = 'all';
		
		//Function for grid filtering
		function filterGrid(className)
		{
			//Reset buttons
			$('projectfilters').getElements('div.filter').each(function(filter)
			{
				if (filter.hasClass('selected'))
				{	
					filter.tween('color', '#58b7dd');
					filter.removeClass('selected');
				}
				if (filter.hasClass(className)) filter.addClass('selected');
			});
			
			//Filter projects
			$$('.projectthumb').each(function(thumb)
			{
				if (className == 'all') thumb.tween('opacity',1);
				else
				{
					if (thumb.hasClass(className)) thumb.tween('opacity', 1);
					else thumb.tween('opacity', 0.25);
				}
			});
		}
		
		//Filter function triggers
		$('projectfilters').getElements('div.filter').each(function(filter)
		{
			if (filter.hasClass('selected')) filter.setStyle('color', '#222');
			
			filter.addEvents({
				'mouseenter': function()
				{
					this.tween('color', '#222');
				},
				'mouseleave': function()
				{
					if (!this.hasClass('selected')) this.tween('color', '#58b7dd');
				},
				'click': function()
				{
					if (!this.hasClass('selected'))
					{
						if (this.hasClass('all')) filterGrid('all');
						else if (this.hasClass('broadcast')) filterGrid('broadcast');
						else if (this.hasClass('branding')) filterGrid('branding');
						else if (this.hasClass('web')) filterGrid('web');
					}
				}
			});
		});
	}
	
	//Individual Project Page
	if ($('projectimages'))
	{
		var images = $('projectscroll').getElements('div.piece');
		var slider = new Fx.Tween($('projectscroll'), {property: 'left', duration: 1000, transition: 'quad:in:out'});
		var leftArrow = $('projectcontrols').getElement('.leftarrow');
		var rightArrow = $('projectcontrols').getElement('.rightarrow')
		var currentImage = 0;
		
		if (images.length == 1)
		{
			$('projectscroll').addClass('noscroll');
			rightArrow.addClass('disabled');
		}
		
		//Slide images and adjust buttons
		function slideImages()
		{
			//Slide and update subtitle
			slider.cancel();
			slider.start(960*currentImage*(-1));
			if (images[currentImage].hasClass('image')) $('projectsubtitle').getElement('h2').innerHTML = images[currentImage].getElement('img').getAttribute('alt');
			else if (images[currentImage].hasClass('video')) $('projectsubtitle').getElement('h2').innerHTML = images[currentImage].getElement('div.videowindow').getAttribute('alt');
			
			//Reset arrow buttons
			if (currentImage == 0) leftArrow.addClass('disabled'); else leftArrow.removeClass('disabled');
			if (currentImage == images.length-1) rightArrow.addClass('disabled'); else rightArrow.removeClass('disabled');
			
			//Pause all video playback
			$('projectscroll').getElements('div.videowindow embed').each(function(player) { if (player.getConfig()['state'] == 'PLAYING') player.sendEvent('PLAY'); });
		}
		function slideLeft() { if (currentImage > 0) slideImages(currentImage--); };
		function slideRight() { if (currentImage < images.length-1) slideImages(currentImage++); };
		
		//Left/Right Arrows
		leftArrow.addEvent('click',function()
		{
			slideLeft();
		});
		rightArrow.addEvent('click',function()
		{
			slideRight();
		});
		
		//Allow clicking on the image to progress and return to first image
		images.each(function(image)
		{
			image.addEvent('click',function(event)
			{	
				//Determine X position of mouse at click relative to the project image area
				var posx = event.client.x - $('projectimages').getCoordinates().left;
				
				//Determine which way to slide based on that value
				if (posx < 480) slideLeft();
				else slideRight();
			});
			
			//Prevent slide on video click (empty function)
			if (image.getElement('div.videowindow')) image.getElement('div.videowindow').addEvent('click',function(){ return false; });
		});
		
		
		//Set up Project Details functionality
		var detailsShowing = false;
		var details = new Fx.Tween($('projectdetails'), {property: 'top', duration: 800, transition: 'quad:in:out'});
		var projectimages = new Fx.Tween($('projectscroll'), {property: 'top', duration: 800, transition: 'quad:in:out'});
		
		//Set up details & container
		$('projectdetails').getElement('.bg').setStyle('opacity',0.9);
		$('projectdetails').getElement('.container').setStyle('margin-top', (480-$('projectdetails').getElement('.container').getSize().y)/2 + 4);
		
		function toggleDetails()
		{
			if (!detailsShowing)
			{
				//Pause all video playback
				$('projectscroll').getElements('div.videowindow embed').each(function(player) { if (player.getConfig()['state'] == 'PLAYING') player.sendEvent('PLAY'); });
				
				detailsShowing = true;
				details.cancel();
				details.start(0);
				//projectimages.cancel();
				//projectimages.start(-480);
				$('projectcontrols').getElement('.detailsbutton').addClass('selected');
			} else {
				detailsShowing = false;
				details.cancel();
				details.start(480);
				//projectimages.cancel();
				//projectimages.start(0);
				$('projectcontrols').getElement('.detailsbutton').removeClass('selected');
				$('projectcontrols').getElement('.detailsbutton').tween('color', '#58b7dd');
			}
		}
		
		$('projectcontrols').getElement('.detailsbutton').addEvents({
			'mouseenter': function()
			{
				this.tween('color', '#222');
			},
			'mouseleave': function()
			{
				if (!detailsShowing) this.tween('color', '#58b7dd');
			},
			'click': function() { toggleDetails(); }
		});
		
		$('projectdetails').addEvent('click', function() { toggleDetails(); });
		
		if ($('projectcontrols').getElement('.linkbutton'))
		{
			$('projectcontrols').getElement('.linkbutton').addEvents({
				'mouseenter': function()
				{
					this.getElement('a').tween('color', '#222');
				},
				'mouseleave': function()
				{
					this.getElement('a').tween('color', '#58b7dd');
				}
			});
		}
	}	
	
	if ($('caseselections'))
	{
		//Case Study case selection menu
		//Filter function triggers
		$('caseselections').getElements('div.case').each(function(filter)
		{
			filter.addEvents({
				'mouseenter': function()
				{
					this.getElement('a').tween('color', '#222');
				},
				'mouseleave': function()
				{
					if (!this.hasClass('selected')) this.getElement('a').tween('color', '#58b7dd');
				}
			});
		});
	}
	
	if ($('newsside'))
	{
		//News filter menu
		$('newsside').getElements('.filtermenu li a').each(function(filter)
		{
			filter.colour = filter.getStyle('color');
			filter.addEvents({
				'mouseenter': function()
				{
					this.tween('color', '#58b7dd');
				},
				'mouseleave': function()
				{
					this.tween('color', this.colour);
				}
			});
		});
		
		//Set up tooltips
		$('archives').getElements('a').each(function(element,index)
		{    
			element.store('tip:title', element.get('rel')); 
			element.store('tip:text',''); 
		});
		
		var tooltips = new smallTips('.months a',{  
			className: 'thumbtip',  
			fixed: false,  
			hideDelay: 0,  
			showDelay: 50,
			offsets: {'x': -60, 'y': 20}
		});
		
		tooltips.addEvents(
		{  
			'show': function(tip)
			{
				tip.fade('show');
			},  
			'hide': function(tip)
			{  
				tip.fade('hide');
			}  
		});
	}
	
	if ($('pagination'))
	{
		$('pagination').getElements('a').each(function(link)
		{
			link.addEvents({
				'mouseenter': function()
				{	
					this.morph({
						'background-color': '#ccc',
						'color': '#222'
					});
				},
				'mouseleave': function()
				{
					this.morph({
						'background-color': '#fff',
						'color': '#58b7dd'
					});
				}
			});
		});
	}
	
});

window.addEvent('resize', function()
{	
	if ($('content').hasClass('home'))
	{
		if (window.innerHeight) { // Firefox
			var docheight = window.innerHeight;
			var docwidth = window.innerWidth;
		} else if (document.all) { // IE
			var docheight = document.body.clientHeight;
			var docwidth = document.body.clientWidth;
		};
		
		if (docwidth <= 960) $('content').setStyles({'left': 0, 'margin-left': 0});
		else $('content').setStyles({'left': '50%', 'margin-left': -480});
	}
	else
	{
		adjustFooter();
	}
});

window.addEvent('scroll', function()
{
	if (!$('content').hasClass('home')) adjustFooter();
});
