/* jQuery In Field Labels */
/*
 * In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 */
(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.0,fadeDuration:100};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);


/* jQuery Stylesheet Toggler */
/**
 * Stylesheet toggle variation on styleswitch stylesheet switcher.
 * Built on jQuery.
 * Under an CC Attribution, Share Alike License.
 * By Kelvin Luck ( http://www.kelvinluck.com/ )
 **/

(function ($) {
	// Local vars for toggle
	var availableStylesheets = [];
	var activeStylesheetIndex = 0;

	// To loop through available stylesheets
	$.stylesheetToggle = function () {
		activeStylesheetIndex++;
		activeStylesheetIndex %= availableStylesheets.length;
		$.stylesheetSwitch(availableStylesheets[activeStylesheetIndex]);
	};

	// To switch to a specific named stylesheet
	$.stylesheetSwitch = function (styleName) {
		$('link[@rel*=style][title]').each(

		function (i) {
			this.disabled = true;
			if (this.getAttribute('title') == styleName) {
				this.disabled = false;
				activeStylesheetIndex = i;
			} else {
				$('#stylehseet-toggler').text(this.getAttribute('title'))
			}
		});

		createCookie('style', styleName, 365);
	};

	// To initialise the stylesheet with it's 
	$.stylesheetInit = function () {
		$('link[rel*=style][title]').each(

		function (i) {
			availableStylesheets.push(this.getAttribute('title'));
		});
		var c = readCookie('style');
		if (c) {
			$.stylesheetSwitch(c);
		}
	};
})(jQuery);

// cookie functions http://www.quirksmode.org/js/cookies.html


function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}
// /cookie functions

/* jQuery Equal Heights */
/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com), modified by Mark Perris
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 *		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 *		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
 *		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
 * 		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
 */

$.fn.equalHeights = function() {

	var currentTallest = 0;
	$(this).each(function(i){
		if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
	});
	// for ie6, set height since min-height isn't supported
	if ($.browser.msie && $.browser.version == 6.0) { $(this).css({'height': currentTallest}); }
	$(this).css({'min-height': currentTallest}); 

	return this;
};



/* jQuery Text Overflow */
/*
 * jQuery Text Overflow v0.7
 *
 * Licensed under the new BSD License.
 * Copyright 2009-2010, Bram Stein
 * All rights reserved.
 */
(function(c){var b=document.documentElement.style,d=("textOverflow" in b||"OTextOverflow" in b),a=function(f,i){var h=0,e=[],g=function(j){var l=0,k;if(h>i){return}for(l=0;l<j.length;l+=1){if(j[l].nodeType===1){k=j[l].cloneNode(false);e[e.length-1].appendChild(k);e.push(k);g(j[l].childNodes);e.pop()}else{if(j[l].nodeType===3){if(h+j[l].length<i){e[e.length-1].appendChild(j[l].cloneNode(false))}else{k=j[l].cloneNode(false);k.textContent=c.trim(k.textContent.substring(0,i-h));e[e.length-1].appendChild(k)}h+=j[l].length}else{e.appendChild(j[l].cloneNode(false))}}}};e.push(f.cloneNode(false));g(f.childNodes);return c(e.pop().childNodes)};c.extend(c.fn,{textOverflow:function(g,e){var f=g||"&#x2026;";if(!d){return this.each(function(){var l=c(this),m=l.clone(),p=l.clone(),k=l.text(),h=l.width(),n=0,o=0,j=k.length,i=function(){if(h!==l.width()){l.replaceWith(p);l=p;p=l.clone();l.textOverflow(g,false);h=l.width()}};l.after(m.hide().css({position:"absolute",width:"auto",overflow:"visible","max-width":"inherit"}));if(m.width()>h){while(n<j){o=Math.floor(n+((j-n)/2));m.empty().append(a(p.get(0),o)).append(f);if(m.width()<h){n=o+1}else{j=o}}if(n<k.length){l.empty().append(a(p.get(0),n-1)).append(f)}}m.remove();if(e){setInterval(i,200)}})}else{return this}}})})(jQuery);

/* 
 * Wiltshire Council 2012
 * Custom Function & Initialisers
 */

$(document).ready(function(){

	/* Wire Up Auto Submit */
	$(".autosubmit").each(function (index) {
	    $(this).change(function () {
	      $(this).closest('form').submit();
	    })
	});	

	/* Hide All 'hidebyjs' Class Items */
	$(".hidebyjs").each(function(index) { $(this).addClass('screenreader');});

	/* Show All 'showbyjs' Class Items */
	$(".showbyjs").each(function(index) { $(this).removeClass('showbyjs');});
	
	/* Apply In Field Labels */
	$(".overlaylabel").inFieldLabels();
	
	/* Apply High Contrast Switcher */
	$.stylesheetInit();
	$('#stylehseet-toggler').bind('click', function (e) {
		$.stylesheetToggle();
		return false;
	});
		
	/* Apply Screen Reader Tabbed To */
	$('.screenreader a').focus(function(e) {
		$(this).closest('.screenreader').addClass('tabbedto');
	});
	$('.screenreader a').blur(function(e) {
		$(this).closest('.screenreader').removeClass('tabbedto');
	});
	
	/* Apply Equal Heights */
	$('.rowone.equal').equalHeights();
	$('.rowtwo.equal').equalHeights();
	$('.rowthree.equal').equalHeights();
	
	/* Apply Elipsis */
	$('.textoverflow').textOverflow();
	
	/* Move Overlay for IE6 */
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('#overlay').insertBefore('#container');
	}
	
});



