$(document).ready(function(){
		
  // Header cycling.
  $("#header-boxout").cycle({ fx: 'scrollLeft', speed: 1000, timeout: 8000, pause: true, pager: '#carousel_pager' });


	//The end of the image filename unique to default buttons (roll-out state)
	var defaultSuffix = '0';
	
	//The end of the image filename unique to active buttons (roll-over state)
	var activeSuffix = '1';
	
	//The image format - must be consistent throughout the file
	var imageType = '.gif';
	
	//The class used to define whether an image should have a roll-over/roll-out event attached to it
	var defaultClass = '.button';
	
	//The class used to define whether an image is active, i.e the current page
	var activeClass = '.button-active';
	
	//The class used to define whether a link should display according to the current page
	var linkClass = '.link';
	
	
	
	//These two values tell the code what to remove from the end of an image filename in order to be able to append
	//The correct suffix for the relevant state. Shouldn't need to change these
	var defaultSuffixLength = defaultSuffix.length + imageType.length;
	
	var activeSuffixLength = activeSuffix.length + imageType.length;
	
	
	//Set selected button in relation to current url
	$(linkClass).each(function() {

		if(checkActive($(this).attr("href")))
		{	
			var base = $(this).children().attr("src");
			
			$(this).children().attr("src", base.substring(0,base.length-defaultSuffixLength) + activeSuffix + imageType);
			
			$(this).children().removeClass(defaultClass.substring(1));
			$(this).children().addClass(activeClass);
			
			return false;
		}
		
	});
	
	var defaultBase = new Array();
	
	$(defaultClass).each(function(i) {
		
		//Get original src attribute to return to
		defaultBase[i] = $(this).attr("src");
		
		//Attach roll-over event
		$(this).mouseenter( function() {
			$(this).attr("src", function() {
				var base = $(this).attr("src");
				return base.substring(0,base.length-defaultSuffixLength) + activeSuffix + imageType;
			});
		});
		
		$(this).mouseleave( function() {
			$(this).attr("src", defaultBase[i]);
		});
		
	});
		

	
	//Check to see if a given url is pointing to the current location. Returns bool
	function checkActive(href)
	{
	
	
		var str = location.pathname
		//Find position of second slash. If it doesn't exist, it will return -1
		var pos = str.indexOf('/', 1);
		
		if(pos == -1)
		{
			var url = str.substring(0);
		}
		else
		{
			var url = str.substring(0,pos);
		}
		
	
		//clean href values into standard '/example....' format
		if(href.indexOf("//") != -1)
		{
			href = href.substring(href.indexOf("//") + 2);
		}
		
		href = href.substring(href.indexOf('/')-1);
	
		if(href[0] != '/')
		{
			href = '/'+href;
		}
		
		//set strings to lower case
		str = str.toLowerCase();
		url = url.toLowerCase();
	
		href = href.toLowerCase();
		
		//Check to see if the href matches the exact url ('/example' = /example), 
		//or if it contains the url (/example/somepage.htm = /example/somepage.htm
		//or derivatives thereof (/example/somepage = /example/
		if(href == url || href.match(str) || href.match(url+'/'))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

});
