$(document).ready(function() {

	// Local Scroll
	function filterPath(string) {
		return string
			.replace(/^\//,'')  
			.replace(/(index|default).[a-zA-Z]{3,4}$/,'')  
			.replace(/\/$/,'');
		}
		$('a[href*=#]').each(function() {
			if ( filterPath(location.pathname) == filterPath(this.pathname)
			&& location.hostname == this.hostname
			&& this.hash.replace(/#/,'') ) {
			var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
			var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($target) {
				var targetOffset = $target.offset().top;
				$(this).click(function() {
					$('html, body').animate({scrollTop: targetOffset}, 400);
					return false;
				});
			}
		}
	});
	
	// Open links in a new window if rel is set to external
	$('a[rel="external"]').click( function() { window.open( $(this).attr('href') ); return false; });
	
	// Initialises Make Nice Inputs
	$('.isMNice').makeNiceInput();
	
	// Sets class to last menu item
	$('#menu li:last').addClass('last');
	
	// Resize featurettes
	if($('.featurette').length > 0) {
		var tallest = 0;
		$('.featurette p').each(function(){
			if($(this).height() > tallest){tallest = $(this).height();}
		});
		$('.featurette p').height(tallest);
		$('.featurette').each(function(){
			$(this).createShadow();
		});
	}
	
	// Add corner shadows
	if($('.addshadow, #services-feature').length > 0) {
		$('.addshadow, #services-feature').each(function(){
			$(this).createShadow();
		});
	}
	
	if($('img.hero').length > 0) {
		$('img.hero').each(function(){
			var h = $(this).height();
			$(this).wrap('<span class="hero-wrapper" />');
			$(this).parent().height(h).createShadow();
			$(this).removeClass('hero');
		});
	}
	
	$("*:contains('&')", document.body).contents().each(
		function() {
			if( this.nodeType == 3 ) {
				$(this)
				.replaceWith( this
					.nodeValue
					.replace( /&/g, '<span class="ampersand">&</span>')
				);
			}
		}
	);
	
	if($('#banner-scroller').length > 0) {$('#banner-scroller').show();}
	theRotator();
	equal_heights();

});

function equal_heights(){

	if($('#content').height() < $('#sidearea').height()){
		
		$('#content').height($('#sidearea').height());
		
	} else {

		$('#sidearea').height($('#content').height());

	}

}

function theRotator() {
	//Set the opacity of all images to 0
	$('div#banner-scroller ul li').css({opacity: 0.0});

	//Get the first image and display it (gets set to full opacity)
	$('div#banner-scroller ul li:first').css({opacity: 1.0});

	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);

}

function rotate() {	
	//Get the first image
	var current = ($('div#banner-scroller ul li.show')?  $('div#banner-scroller ul li.show') : $('div#banner-scroller ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#banner-scroller ul li:first') :current.next()) : $('div#banner-scroller ul li:first'));	

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};

// Corner shadows
jQuery.fn.createShadow = function(){
	$(this).append('<span class="shadow"></span>');
}
