window.addEvent('domready', function()
{	
	//Define variables
	
	var your = $('yourhardwork');
	var our = $('ourclearvision');
	
	your.setStyle('visibility','visible');
	our.setStyle('visibility','visible');
		
	var yourFocus = $('yourhardwork').getElement('img');
	var yourList = $('yourhardwork').getElement('div');
	var ourFocus = $('ourclearvision').getElement('img');
	var ourList = $('ourclearvision').getElement('div');

	var yourFocusTween = new Fx.Tween(yourFocus);
	var yourListTween = new Fx.Tween(yourList);
	var ourFocusTween = new Fx.Tween(ourFocus);
	var ourListTween = new Fx.Tween(ourList);
	
	var yourListHeight = yourList.getSize().y - 16;
	var ourListHeight = ourList.getSize().y - 16;
	
	var yourItems = yourList.getElements('div.item');
	var ourItems = ourList.getElements('div.item');
	
	var allItems = yourItems.concat(ourItems);
		
		
	//Create highlight functions for all list items
	
	allItems.each(function(item)
	{		
		if (item.getElement('a'))
		{
			item.colour = item.getElement('a').getStyle('color');
			
			if (item.id == "portfoliocontainer")
			{
				var folio = item.getElementById('portfolio')
				folio.setStyle('display','block');
				folio.fade('hide');
				
				item.addEvents({
					'mouseenter': function()
					{
						this.getElement('a').tween('color', '#222');
						folio.fade('in');
					},
					'mouseleave': function()
					{
						this.getElement('a').tween('color', this.colour);
						folio.fade('out');
					}
				});
			}
			else
			{
				item.addEvents({
					'mouseenter': function()
					{
						this.getElement('a').tween('color', '#222');
					},
					'mouseleave': function()
					{
						this.getElement('a').tween('color', this.colour);
					}
				});
			}
		}
	});
	
	
	//Check if "Your hard work" dropdown is selected, and apply respective functions	
		
	if (your.className != 'selected')
	{
		yourListTween.set('height',24);
		yourList.getElementById('yourcontainer').setStyle('margin-top', yourListHeight * (-1) / 2);
		yourFocusTween.set('opacity',0);
		yourListTween.set('opacity',0);
		
		your.addEvents({
			'mouseenter': function()
			{
				yourFocusTween.cancel();
				yourFocusTween.set('display','block');
				yourFocusTween.start('opacity',1);
				
				yourListTween.cancel();
				yourListTween.set('display','block');
				yourList.fade('in');
				yourList.getElementById('yourcontainer').tween('margin-top', 0);
				yourListTween.start('height',yourListHeight)
				
				if (our.className == 'selected')
				{
					ourFocusTween.cancel();
					ourFocusTween.start('opacity',0);
					
					ourList.fade('out');
				}
				
			},
			'mouseleave': function()
			{
				yourFocusTween.cancel();
				yourFocusTween.start('opacity',0).chain(function()
				{
					yourFocusTween.set('display','none');
				});
				
				yourListTween.cancel();
				yourList.fade('out');
				yourList.getElementById('yourcontainer').tween('margin-top', yourListHeight * (-1) / 2);
				yourListTween.start('height',24).chain(function()
				{
					yourListTween.set('display','none');
				});
				
				if (our.className == 'selected')
				{
					ourFocusTween.cancel();
					ourFocusTween.start('opacity',1);
					
					ourList.fade('in');
				}
			}
		});
		
	} else {
	
		yourListTween.set('height',24);
		yourFocusTween.set('display','block');
		yourListTween.set('display','block');
		yourItems.each(function(item) { item.set('opacity',0); });
		
		var yourListPos = yourList.getElement('div.selected').getPosition('yourcontainer').y + 1; /* add buffer */
		yourList.getElementById('yourcontainer').setStyle('margin-top', yourListPos * (-1));
		
		your.addEvents({
			'mouseenter': function()
			{	
				yourListTween.cancel();
				yourListTween.start('height',yourListHeight);
				yourList.getElementById('yourcontainer').tween('margin-top', 0);
				yourList.getElement('div.selected').getElement('a').tween('color', '#222');
				yourItems.each(function(item) { item.fade('in'); });
			},
			'mouseleave': function()
			{
				yourListTween.cancel();
				yourListTween.start('height',24);
				yourList.getElementById('yourcontainer').tween('margin-top', yourListPos * (-1));
				yourList.getElement('div.selected').getElement('a').tween('color', '#58b7dd');
				yourItems.each(function(item) { item.fade('out'); });
			}
		});
		
	}
	
	
	//Check if "Our clear vision" dropdown is selected, and apply respective functions
	
	if (our.className != 'selected')
	{
		ourListTween.set('height',24);
		ourList.getElementById('ourcontainer').setStyle('margin-top', ourListHeight * (-1) / 2);
		ourFocusTween.set('opacity',0);
		ourListTween.set('opacity',0);
		
		our.addEvents({
			'mouseenter': function()
			{
				ourFocusTween.cancel();
				ourFocusTween.set('display','block');
				ourFocusTween.start('opacity',1);
				
				ourListTween.cancel();
				ourListTween.set('display','block');
				ourList.fade('in');
				ourList.getElementById('ourcontainer').tween('margin-top', 0);
				ourListTween.start('height',ourListHeight)
				
				if (your.className == 'selected')
				{
					yourFocusTween.cancel();
					yourFocusTween.start('opacity',0);
					
					yourList.fade('out');
				}
				
			},
			'mouseleave': function()
			{
				ourFocusTween.cancel();
				ourFocusTween.start('opacity',0).chain(function()
				{
					ourFocusTween.set('display','none');
				});
				
				ourListTween.cancel();
				ourList.fade('out');
				ourList.getElementById('ourcontainer').tween('margin-top', ourListHeight * (-1) / 2);
				ourListTween.start('height',24).chain(function()
				{
					ourListTween.set('display','none');
				});
				
				if (your.className == 'selected')
				{
					yourFocusTween.cancel();
					yourFocusTween.start('opacity',1);
					
					yourList.fade('in');
				}
			}
		});
		
	} else {
	
		ourListTween.set('height',24);
		ourFocusTween.set('display','block');
		ourListTween.set('display','block');
		ourItems.each(function(item) { item.set('opacity',0); });
		
		var ourListPos = ourList.getElement('div.selected').getPosition('ourcontainer').y + 1; /* add buffer */
		ourList.getElementById('ourcontainer').setStyle('margin-top', ourListPos * (-1));
		
		our.addEvents({
			'mouseenter': function()
			{	
				ourListTween.cancel();
				ourListTween.start('height',ourListHeight);
				ourList.getElementById('ourcontainer').tween('margin-top', 0);
				ourList.getElement('div.selected').getElement('a').tween('color', '#999');
				ourItems.each(function(item) { item.fade('in'); });
			},
			'mouseleave': function()
			{
				ourListTween.cancel();
				ourListTween.start('height',24);
				ourList.getElementById('ourcontainer').tween('margin-top', ourListPos * (-1));
				ourList.getElement('div.selected').getElement('a').tween('color', '#58b7dd');
				ourItems.each(function(item) { item.fade('out'); });
			}
		});
	}
	
	
	//Create highlight functions for footer items
	
	if ($(document.body).className != 'home')
	{	
		var footerItems = $('footer').getElements('a');
		var footerTween = new Fx.Tween($('footer').getElementById('items'));	
		
		footerItems.each(function(item)
		{				
			if (item.className != 'toggle')
			{
				item.colour = item.getStyle('color');
				
				item.addEvents({
					'mouseenter': function()
					{
						this.tween('color', '#222');
					},
					'mouseleave': function()
					{
						this.tween('color', this.colour);
					}
				});
			}
		});
		
	
		var itemsWidth = $('footer').getElementById('items').getSize().x + 1;
		$('footer').getElementById('itemcontainer').setStyle('width', itemsWidth);
		
		if ($('portfolio').className != 'selected')
		{			
			$('footer').getElementById('itemcontainer').setStyle('display', 'none');
			$('footer').getElementById('items').setStyle('width', 1);
			$('footer').getElementById('portfolio').addEvents({
				'mouseenter': function()
				{	
					footerTween.cancel();
					$('footer').getElementById('itemcontainer').setStyle('display', 'block');
					$('footer').getElementById('portfolio').getElement('a').get('tween').cancel();
					$('footer').getElementById('portfolio').getElement('a').tween('color', '#999');
					footerTween.start('width',itemsWidth);
				},
				'mouseleave': function()
				{
					footerTween.cancel();
					footerTween.start('width',1).chain(function()
					{
						$('footer').getElementById('itemcontainer').setStyle('display', 'none');
					});
					$('footer').getElementById('portfolio').getElement('a').get('tween').cancel();
					$('footer').getElementById('portfolio').getElement('a').tween('color', '#58b7dd');
				}
			});
		} else {
			$('footer').getElementById('itemcontainer').setStyle('display', 'block');
			$('footer').getElementById('portfolio').getElement('a').setStyle('color', '#222');
		}
	}
	
	
	//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');});
						}
					}
				}
			});
		});
	}
	
	
	
	//Sliding portfolio content
	
	if ($('thumbs'))
	{	
		var slider = $('slider');
		var handle = $('handle');
		var thumbs = $('thumbslide');
		var thumbswidth = thumbs.getSize().x;
		var maxslide = thumbs.getSize().x - 880 - 20;
		
		// Create the new slider instance
		new Slider(slider, handle, {
			range: [0,maxslide],
			onChange: function(value){
				thumbs.setStyle('left', (0 - value));
			}
		});
				
				
		//Tooltips 
		
		$$('.thumbtip').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('.thumbtip',{  
			className: 'thumbtip',  
			fixed: false,  
			hideDelay: 0,  
			showDelay: 50,
			offsets: {'x': -103, 'y': 20}
		});
		
		$$('div.thumbtip').each(function(tip)
		{    
			tip.fade('hide');
		});

			
		tooltips.addEvents({  
			'show': function(tip) {  
				tip.fade('in');  
			},  
			'hide': function(tip) {  
				tip.fade('hide');  
			}  
		});
	
	}
	
	
});