$(document).ready(function () {
	// Opens links in new window on a anchor with an attribute of rel="openwin"
	$('a[rel=openwin]').click(function(){
		window.open(this.href);
		return false;
	});
	
	// Bind a click event to anchor with rel="submit" and submits form its contained within
	$("a[rel=submit]").click( function(){
		$(this).parents("form").submit();
	});
	
	// Validate the sidebar request a callback form
	//$("form").validate({
	//	rules: {
	//		password: "required",
	//		passwordConfirm: {
	//			equalTo: "#password"
	//		}
	//	}
   //});

	// Sidebar menu system
	if ($('ul#menu').length > 0) {
		var asub = $('ul#menu li a').not('a[href]');
		asub.click(function () {
			var a = $(this);
			var sub = a.siblings('ul');
			var parent = a.parent();
			if (sub.is(':visible')) {
				sub.slideUp(500);
				parent.removeClass('open');
			} else {
				$('ul#menu li.open').removeClass('open');
				$('ul#menu li ul:visible').slideUp(500);
				sub.slideDown(500);
				parent.addClass('open');
			}
		});
	}
	
	// Text resizer system
	if ($('div.resizer').length > 0) {
		var resizer = $('div.resizer');
		// Elements selection
		var elements = $('h1,h3,h4,h5,h6,li:not(:has(a)),li a,p');
		// Set defaults of all page elements
		var elementsDefaults = {};
		elements.each(function(i) {
			$(this).data('default', {
				'fontSize': $(this).css('font-size'),
				'lineHeight': $(this).css('line-height')
			});
		});
		// Resize if there is a cookie set
		if (getCookie('pcResizer')) {
			resizeText(null, null, elements);
		}
		
		// Set click event on resizer elements
		resizer.children('a').each(function(e) {
			$(this).click(function(e) {
				resizeText(e,this,elements);
			});
		});
	}
	
	// Input default value store and remove on focus
	if ($('input:text').length > 0) {
		var defaultValueClassName = 'defaultVal';
		var $textInputs = $('input:text');
		$textInputs.each(function(i) {
			var $this = $(this);
			// Set default values when document loads into data
			if ($this.val()) {
				$this.data('defaultValue', $this.val());
				$this.addClass(defaultValueClassName);
			}
			// Set events
			$this.focus(function(e) {
				if ($this.val() == $this.data().defaultValue) {
					$this.val('');
					$this.removeClass(defaultValueClassName);
				}
			});
			$this.blur(function(e) {
				if ($this.val() == '') {
					$this.val($this.data().defaultValue);
					$this.addClass(defaultValueClassName)
				}
			})
		});
	}
	
	// Register form gift aid eligible popup
	if ($('a.eligibleGiftAid').length > 0) {
			var $aidlink = $('a.eligibleGiftAid');
			var $aiddiv = $('div.eligibleGiftAidPopup');
				$aidlink.mouseover(function(e) {
					// show
					$aiddiv.show();
				});
				$aidlink.mouseout(function(e) {
					// hide
					$aiddiv.hide();
				});
	}
	
	function resizeText(event,elem,elements) {
		var options = {
			// Set step of increase and decrease in pixels
			'resizeStep' : 2,
			// Limit of increase/decrease amount from default
			'limit' : 4,
			// Name of cookie
			'cookieName' : 'pcResizer',
			// Expires in days
			'cookieExpire' : 30
		};

		if ($(elem).hasClass('default')) {
			elements.each(function(i) {
				$(this).css({
					'fontSize' : $(this).data('default').fontSize,
					'lineHeight' : $(this).data('default').lineHeight
				});
			});
			deleteCookie(options.cookieName, '/');
		} else if ($(elem).hasClass('increase')) {
			if (parseInt(elements.css('fontSize')) < (parseInt(elements.data('default').fontSize) + options.limit)) {
				elements.each(function(i) {
					var increaseFontSize = parseFloat($(this).css('fontSize'), 10) + options.resizeStep;
					var increaseLineHeight = parseFloat($(this).css('lineHeight'), 10) + options.resizeStep;
					$(this).css({
						'fontSize' : increaseFontSize + 'px',
						'lineHeight' : increaseLineHeight + 'px'
					});
				});
				if (getCookie(options.cookieName)) {
					setCookie(options.cookieName,parseInt(getCookie(options.cookieName))+options.resizeStep,options.cookieExpire,'/');
				} else {
					setCookie(options.cookieName,options.resizeStep,options.cookieExpire,'/');
				}
			}
		} else if ($(elem).hasClass('decrease')) {
			if (parseInt(elements.css('fontSize')) > (parseInt(elements.data('default').fontSize) - options.limit)) {
				elements.each(function(i){
					var decreaseFontSize = parseFloat($(this).css('fontSize'), 10) - options.resizeStep;
					var decreaseLineHeight = parseFloat($(this).css('lineHeight'), 10) - options.resizeStep;
					$(this).css({
						'fontSize': decreaseFontSize + 'px',
						'lineHeight': decreaseLineHeight + 'px'
					});
				});
				if (getCookie(options.cookieName)) {
					setCookie(options.cookieName, parseInt(getCookie(options.cookieName)) - options.resizeStep, options.cookieExpire, '/');
				} else {
					setCookie(options.cookieName,'-'+options.resizeStep,options.cookieExpire,'/');
				}
			}
		} else {
			elements.each(function(i) {
				$(this).css({
					'fontSize' : parseInt($(this).data('default').fontSize) + parseInt(getCookie(options.cookieName)) + 'px',
					'lineHeight' : parseInt($(this).data('default').lineHeight) + parseInt(getCookie(options.cookieName)) + 'px'
				});
			});
		}
	}
	
	function setCookie(name, value, expires, path, domain, secure) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		// if the expires variable is set, make the correct	expires time, the current script below will set	it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
	}
	
	function getCookie(name) {
		if (document.cookie.length > 0) {
			c_start = document.cookie.indexOf(name + "=");
			if (c_start != -1) {
				c_start=c_start + name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}
	
	function deleteCookie(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +	( ( domain ) ? ";domain=" + domain : "" ) +	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}
	
});
