var animation;

$(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 the height of the feature panel
	function chgFeature(a){
		if($('div.feature').length > 0) {
			var fh = $('div.mainpanel').height() + a;
			$('div.feature').height(fh);
		}
	}
	// Changes the height when the page loads
	chgFeature(25);

	// Sets the height of the gallery on the homepage
	if($('div.home-gallery').length > 0) {
		var fh = $('div.mainpanel').height() + 35;
		$('div.home-gallery').height(fh);
	}

	if ($('#slider').length > 0) {

		// Animation for homepage panel...

		var hpw = $('#viewport').width();// alert(hpw);
		var hpa = $('#slider img').length;
		$('#slider img:first').clone().appendTo($('#slider'));

		$('#slider').width(parseInt(hpw*(hpa+1))+'px');

		var sl = $('#slider').position().left;
		$('#slider').css('left', 0);

		animation = setInterval("animate_slide", 5000);
		
		// End of animation for homepage panel

		function animate_slide(){
			
			$('#slider').stop().animate({
				"left": (sl - 520)
			}, 500, function(){

				sl = $('#slider').position().left;

				$('#slider').css('left', parseInt(Math.floor(sl/520) * 520) + 'px');

				sl = parseInt(Math.floor(sl/520) * 520);

				if(sl <= ((hpw*(hpa))*-1)){
					$('#slider').css('left', 0);
					sl = 0;
				}

			});

		}
		
		function stop_slide(){
			clearInterval(animation);
		}
		
		$('#playpause').show().bind('click', function(){
			if(!$(this).hasClass('paused')){
				// if playing...
				$(this).addClass('paused');
				stop_slide();
				$(this).attr('title','Click this button to play the animation');
			} else {
				// if paused...
				animate_slide();
				animation = setInterval(animate_slide, 5000);
				$(this).removeClass('paused');
				$(this).attr('title','Click this button to pause the animation');
			}
			return !1;
		});

	}


	// Animates the component view area
	$('#componentselect li a').click(function(){
		$('#componentselect li a').removeClass('active');
		$(this).addClass('active');
		return chgComponent($(this).parent().attr('rel'));
	});

	//$('#componentcontainer .nextcomp:last').remove();

	$('#componentcontainer .nextcomp a').live('click', function(){

		var w = (parseInt($('#componentcontainer').offset().top - $('#componentoverview').offset().top) - 285) / -285;

		if (w == 4) { w = 0; }

		return chgComponent(w + 1);

	});

	// Sets the height of the feature panel
	function chgFeature(a){
		if($('div.feature').length > 0) {

			if($('div.mainpanel').height() < ($('div.home-video').height() - 20 )) {
				$('div.mainpanel').height($('div.home-video').height() - 20);
			}

			var fh = $('div.mainpanel').height() + a;
			$('div.feature').height(fh);
		}
	}
	// Changes the height when the page loads
	chgFeature(10);
	

	function chgComponent(w){
		var m = (-285 * w);
		$('#componentcontainer').animate({
			"top": m+285
		}, 500);
		return false;

	}

	// Animates the help view area
	$('#helpselect li').hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});
	$('#helpselect li a').click(function(){
		$('#helpselect li').removeClass('active');
		$(this).parent().addClass('active');
		var w = $(this).parent().attr('rel');
		var m = (-940 * w);
		$('#helpcontainer').stop().animate({
			"left": m+940
		}, 500);
		return false;
	});

	var hpanelh = 0;

	$('#helpcontainer>div').each(function(){

		if($(this).height() > hpanelh){ hpanelh = $(this).height(); }

	});

	$('#helpcontainer,#helpoverview').height(hpanelh);

	// Removes borders from the dropdown menu
	if($('#menu li ul').length > 0){
		$('#menu li ul').each(function(){
			$(this).find('li:first').css({'border-top':'none'});
			$(this).find('li:last').css({'border-bottom':'none'});		
		});
	}


	// Increase / Decrease text size
	function setFontSize(size) {
	    $('#copy').css("font-size", size);
	}
	$('#u-font-increase').click(function(){
		var fsize = parseFloat($('#copy').css("font-size"));
		if ((fsize + 2) > 20 ) {return false} else {
			$('#copy').css("font-size", fsize + 2 + "px");
			// Adjusts the height of the panel if needed
			chgFeature(8);
			return false;
		}
	});
	$('#u-font-decrease').click(function(){
	    var fsize = parseFloat($('#copy').css("font-size"));
		if ((fsize - 2) < 8 ) {return false} else {
			$('#copy').css("font-size", fsize - 2 + "px");
			// Adjusts the height of the panel if needed
			chgFeature(8);
			return false;
		}
	});

	// Print the page
	$('#u-print').click(function(){window.print();});

	// Adds a shadow image to the each hero shot
	if($('img.hero').length > 0) {
		$('img.hero').each(function(){
			if($(this).hasClass('alignright')){$(this).wrap('<span class="hasshadow alignright"></span>');}
			if($(this).hasClass('alignleft')){$(this).wrap('<span class="hasshadow alignright"></span>');}
		});
	}

	$('ul#menu>li').hover(
		function(){
			$(this).addClass('over').find('>a').addClass('hover');
		},
		function(){
			$(this).removeClass('over').find('>a').removeClass('hover');
		}
	);
	
	//Site Select display and hide
	$('#header #site-select a#site-select-button').click(function(){
		if ($(this).attr("class") == "active") {
			$(this).removeClass('active');
		} else {
			$(this).addClass('active');
		}
		$('#header #site-select span#site-list').slideToggle('fast');
		return false;
	});

	// Disable right click on images
	$("img").bind("contextmenu", function(e) {
	    return false;
	});

	$('a[href*=".pdf"]').bind('click', function(e){
		e.preventDefault();
		_gaq.push(['_trackPageview', $(this).attr('href')]);
		window.open($(this).attr('href'));
	});

});
