jQuery.noConflict();

(function($) {
	/**
	* maxChar jQuery plugin
	* @author Mitch Wilson
	* @version 0.3.0
	* @requires jQuery 1.3.2 (only version tested)
	* @see http://mitchwilson.net/2009/08/03/new-jquery-plugin-maxchar/
	* @param {Boolean}	debug				Specify whether to send message updates to the Firebug console. Default is false.
	* @param {String}	indicator 			Specify alternate indicator element by id. Default is indicator created dynamically.
	* @param {String}	label				Specify a default label displayed when input element is not in focus. Default is blank.
	* @param {String}	pluralMessage 		Set the plural form of the remaining count message. Default is ' remaining'.
	* @param {Number}	rate 				Set the update rate in milliseconds. Default is 200.
	* @param {String}	singularMessage 	Set the singular form of the remaining count message. Default is ' remaining'.
	* @param {String}	spaceBeforeMessage 	Set spacing in front of (to the left of) the indicator message. Default is ' '.
	* @param {Boolean}	truncate			Truncate submitted value down to limit on form submit. Default is true.
	* @description Enforces max character limit on any input or textarea HTML element and provides user feedback and many options. 
	* New features added in 0.3.0 are: 
	* 1) Feature change: Displays negative characters when past limit rather than truncating characters in form input.
	* 2) New option: truncate - If true, on form submit truncates submitted value down specified by limit. Does not change (respects) user text in form field. Default is true.
	* 3) Bug fixes: Fixed serveral issues related to removing over-the-limit characters in the form field.
	*/
	$.fn.maxChar = function(limit, options) {

		// Define default settings and override w/ options.	
		settings = jQuery.extend({
			debug: false,
			indicator: 'indicator',
			label: '',
			pluralMessage: ' remaining',
			rate: 200,
			singularMessage: ' remaining',
			spaceBeforeMessage: ' ',
			truncate: true
		}, options);

		// Get maxChar target element.
		var target = $(this); // Must get target first, since it is used in setting other local variables.

		// Get settings.
		var debug = settings.debug;
		var indicatorId = settings.indicator;
		var label = settings.label;
		var pluralMessage = settings.pluralMessage;
		var rate = settings.rate;
		var singularMessage = settings.singularMessage;
		var spaceBeforeMessage = settings.spaceBeforeMessage;
		var truncate = settings.truncate;

		// Set additional local variables.
		var currentMessage = ''; // Current message to display to the user.
		var indicator = getIndicator(indicatorId); // Element to display count, messages and label.
		var limit = limit; // Character limit.
		var remaining = limit; // Characters remaining.
		var timer = null; // Timer to run update.

		// Initialize on page ready.
		if (label) {
			indicator.html(label);
		} else {
			// Call update once on code initialization to update view if text is already in textarea,
			// eg, if user relaoads page or hits back button and form textarea retains previoulsy entered text.
			update(limit);
		}

		// When user focuses on the target element, do the following.
		$(this).focus(function() {
			if (timer == null) {
				if (label) {
					indicator.fadeOut(function() { indicator.html('') }).fadeIn(function() { start() });
				} else {
					start();
				}
			}
		});

		// When user removes focus from the target element, do the following.
		$(this).blur(function() {
			// Stop timer that updates count and the indicator message.
			stop();
			// Update view.
			if (label) {
				indicator.fadeOut(function() { indicator.html(label) }).fadeIn();
			}
		});

		function getIndicator(id) {
			// Get indicator element in the dom.
			var indicator = $('#' + id);
			// If indicator element does not already exist in the dom, create it.
			if (indicator.length == 0) {
				target.prev("label").append(spaceBeforeMessage + ' <span id="' + id + '"></span>');
				indicator = $('#' + id)
			}
			// Return reference to indicator element.
			return indicator;
		}

		// Create helper functions.
		function log(message) {
			// Display 
			if (debug) {
				try {
					if (console) {
						console.log(message);
					}
				} catch (e) {
					// Do nothing on error.
				}
			}
		}

		// Start the timer that updates indicator.
		function start() {
			timer = setInterval(function() { update(limit) }, rate);
		}

		// Stop the timer that updates the indicator.
		function stop() {
			if (timer != null) {
				clearInterval(timer);
				timer = null;
			}
		}

		// Truncate submitted value down to limit on form submit.
		if (truncate) {
			var form_id = '#' + $(this).closest("form").attr("id");
			$(form_id).submit(function() {
				target.val(target.val().slice(0, limit));
			});
		}

		// Update the indicator.
		function update(limit) {
			var remaining = limit - target.val().length;
			// Update remaining count and message.
			if (remaining == 1) {
				currentMessage = remaining + singularMessage;
			} else {
				currentMessage = remaining + pluralMessage;
			}
			// Update indicator.
			indicator.html("(" + currentMessage + ")");
			log(currentMessage);
		}
	};

	// Case insensitive :contains function from
	// http://stackoverflow.com/questions/1407434/is-there-anyway-to-speed-up-this-solution-for-a-case-insensitive-jquery-contains
	$.expr[":"].containsCaseInsensitive = function(a, i, m) {
		return (a.textContent || a.innerText || "").toLowerCase().indexOf(m[3].toLowerCase()) >= 0;
	};

	/* JQueryString v1.6.1
	By James Campbell
	Many thanks to Mike Willis for his suggestions and additions to this jQuery plugin.
	*/
	$.getAllQueryStrings = function(options) {
		defaults = { DefaultValue: "undefined", URL: window.location.href };
		options = $.extend(defaults, options);
		var qs;
		var args = new Array();
		if (typeof (options.URL.split("?")[1]) != "undefined") {
			qs = options.URL.split("?")[1].replace(/\+/g, ' ').split('&');
			$.each(qs, function(i) {
				var currentArg = this.split('=');
				if (currentArg.length == 2) {
					args[i] = { name: currentArg[0], value: currentArg[1] };
					args[currentArg[0]] = { name: currentArg[0], value: currentArg[1] };
				} else {
					args[i] = { name: currentArg[0], value: currentArg[1] };
					args[currentArg[0]] = { name: currentArg[0], value: currentArg[0] };
				}
			});
		}
		if (args.length <= 0) { };
		return args;
	}
	$.getQueryString = function(options) {
		defaults = { DefaultValue: "undefined", URL: window.location.href };
		options = $.extend(defaults, options);
		if (typeof ($.getAllQueryStrings({ URL: options.URL })[options.ID]) == "undefined") {
			return options.DefaultValue;
		} else {
			return $.getAllQueryStrings({ DefaultValue: options.DefaultValue, URL: options.URL })[options.ID].value;
		}
	};
	/*  Useful for delaying events - http://james.padolsey.com/javascript/jquery-delay-plugin/ */
	$.delay = function(time, callback) {
		// Empty function:
		$.fx.step.delay = function() { };
		// Return meaningless animation, (will be added to queue)
		return this.animate({ delay: 1 }, time, callback);
	};

	// Trigger the click event of a submit button when the enter key is used to submit some form or other
	$.fn.enterKeySubmit = function($button) {
		$(this).bind("keypress", function(e) {
			if (e.which.keyCode == 13 || e.keyCode == 13) {
				$($button).trigger("click");
				return false; //prevent default behaviour
			}
		});
	};

	// Attach a rough mileage and drive time from a defined origin to location to the object passed in
	$.fn.appendDrivingDistanceDuration = function(originLocation, destinationLat, destinationLng) {
		var $appendItem = $(this);

		var request = {
			origin: originLocation,
			destination: new google.maps.LatLng(parseFloat(destinationLat), parseFloat(destinationLng)),
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};

		var directionsService = new google.maps.DirectionsService();
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				var distanceInMiles = Math.round(response.routes[0].legs[0].distance.value * 0.000621371192);
				var durationText = response.routes[0].legs[0].duration.text;

				$($appendItem).after('<span class="driveDistanceDuration">Roughly ' + distanceInMiles + ' miles (' + durationText + ')</span>');
			}
		});

	}; // END appendDrivingDistanceDuration

	/*
	*********************************
	* RM: I have altered this from the original - run it through jslint and tidied CSS
	*********************************
	* jQuery Watermark plugin
	* Version 1.0.4 (7-DIC-2009)
	* @requires jQuery v1.2.3 or later
	*
	* Examples at: http://mario.ec/static/jq-watermark/
	* Copyright (c) 2009 Mario Estrada
	* Licensed under the MIT license:
	* http://www.opensource.org/licenses/mit-license.php
	*
	*/
	$.watermarker = function() { };
	$.extend($.watermarker, {
		defaults: {
			color: '#999',
			left: '4px'
		},
		setDefaults: function(settings) {
			$.extend($.watermarker.defaults, settings);
		},
		checkVal: function(val, label) {
			if (val == '') {
				$(label).show();
			} else {
				$(label).hide();
			}
		}
	});

	$.fn.watermark = function(text, css_options) {
		var css, elems;
		css = $.extend($.watermarker.defaults, css_options);
		elems = this.filter('input[type=text], input[type=password], textarea');

		elems.each(function() {
			var $elem, attr_name, label_text, watermark_container, watermark_label;
			var e_margin_top, pos, top, height, line_height;

			$elem = $(this);
			attr_name = $elem.attr('placeholder') !== undefined && $elem.attr('placeholder') !== '' ? 'placeholder' : 'title';
			label_text = text === undefined || text === '' ? $(this).attr(attr_name) : text;
			watermark_container = $('<span class="watermark_container"></span>');
			watermark_label = $('<span class="watermark">' + label_text + '</span>');

			// If used, remove the placeholder attribute to prevent conflicts
			if (attr_name == 'placeholder') {
				$elem.removeAttr('placeholder');
			}

			watermark_container.css({
				'float': $elem.css('float'),
				'position': 'relative'
			});

			$elem.wrap(watermark_container);
			var m_left = $elem.css('margin-left').length > 0 ? $elem.css('margin-left') : 0;
			top = $elem.css('margin-top').length > 0 ? $elem.css('margin-top') : 0;

			if (this.nodeName.toLowerCase() != 'textarea') {
				height = $elem.outerHeight();
				line_height = height + 'px';
			} else {
				pos = $elem.position();
				e_margin_top = $elem.css('margin-top') !== 'auto' ? parseInt($elem.css('margin-top'), 10) : 0;
				line_height = $elem.css('line-height');
				height = line_height === 'normal' ? parseInt($elem.css('font-size'), 10) : line_height;
			}

			$.watermarker.checkVal($elem.val(), watermark_label);

			watermark_label.css({
				color: css.color,
				left: parseInt(css.left + m_left, 10) + 'px',
				height: height + 'px',
				lineHeight: line_height,
				top: top
			}).data('jq_watermark_element', $elem);

			watermark_label.click(function() {
				$($(this).data('jq_watermark_element')).focus();
			});

			$elem.before(watermark_label)
			.focus(function() {
				$.watermarker.checkVal($(this).val(), watermark_label);
				$(watermark_label).hide();
			})
			.blur(function() {
				$.watermarker.checkVal($(this).val(), watermark_label);
				watermark_label.animate({ opacity: 1 }, 250);
			})
			.keydown(function(e) {
				$(watermark_label).hide();
			});
		});

		return this;
	};
})(jQuery);

// Checks for valid UK postcode. Not perfect. Includes newer London postcodes but allows PO206UL which is out of date by 10 years...
function isUkPostcode(val) {
	var regex = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
	if (regex.test(val)) { return true; }
	return false;
}

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

/*
 * jQuery.SerialScroll - Animated scrolling of series
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 06/14/2009
 * @author Ariel Flesler
 * @version 1.2.2
 * http://flesler.blogspot.com/2008/02/jqueryserialscroll.html
 */
;(function(a){var b=a.serialScroll=function(c){return a(window).serialScroll(c)};b.defaults={duration:1e3,axis:"x",event:"click",start:0,step:1,lock:!0,cycle:!0,constant:!0};a.fn.serialScroll=function(c){return this.each(function(){var t=a.extend({},b.defaults,c),s=t.event,i=t.step,r=t.lazy,e=t.target?this:document,u=a(t.target||this,e),p=u[0],m=t.items,h=t.start,g=t.interval,k=t.navigation,l;if(!r){m=d()}if(t.force){f({},h)}a(t.prev||[],e).bind(s,-i,q);a(t.next||[],e).bind(s,i,q);if(!p.ssbound){u.bind("prev.serialScroll",-i,q).bind("next.serialScroll",i,q).bind("goto.serialScroll",f)}if(g){u.bind("start.serialScroll",function(v){if(!g){o();g=!0;n()}}).bind("stop.serialScroll",function(){o();g=!1})}u.bind("notify.serialScroll",function(x,w){var v=j(w);if(v>-1){h=v}});p.ssbound=!0;if(t.jump){(r?u:d()).bind(s,function(v){f(v,j(v.target))})}if(k){k=a(k,e).bind(s,function(v){v.data=Math.round(d().length/k.length)*k.index(this);f(v,this)})}function q(v){v.data+=h;f(v,this)}function f(B,z){if(!isNaN(z)){B.data=z;z=p}var C=B.data,v,D=B.type,A=t.exclude?d().slice(0,-t.exclude):d(),y=A.length,w=A[C],x=t.duration;if(D){B.preventDefault()}if(g){o();l=setTimeout(n,t.interval)}if(!w){v=C<0?0:y-1;if(h!=v){C=v}else{if(!t.cycle){return}else{C=y-v-1}}w=A[C]}if(!w||t.lock&&u.is(":animated")||D&&t.onBefore&&t.onBefore(B,w,u,d(),C)===!1){return}if(t.stop){u.queue("fx",[]).stop()}if(t.constant){x=Math.abs(x/i*(h-C))}u.scrollTo(w,x,t).trigger("notify.serialScroll",[C])}function n(){u.trigger("next.serialScroll")}function o(){clearTimeout(l)}function d(){return a(m,p)}function j(w){if(!isNaN(w)){return w}var x=d(),v;while((v=x.index(w))==-1&&w!=p){w=w.parentNode}return v}})}})(jQuery);

jQuery(document).ready(function($) { //Start code ******************************************

	// Tabs
	$(".tabbedParagraph").tabs();
	
	// IE radio buttons only fire the change event on blur, rather than on click. IE Bugs. Grrr.
	$(function() {
		if ($.browser.msie) {
			$('input:radio').click(function() {
				this.blur();
				this.focus();
			});
		}
	});
});

(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--; ) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery);