$(document).ready(function() {
	//For OO JS Goodness
	var that = this;
	// Configuration area
		
		// Search config items
		this.googlecse = 'form#cse-search-box'
		this.searchElement = 'input#q';
		this.searchValue = 'Search the site';
		this.searchDefaultTextError = 'Please enter a valid query in the text box';
		
		// Menu system elements
		this.menuElement = 'div.menu';
		this.subMenuElement = 'div.submenu';
		
		// Tag and class for popup window
		this.apop = 'a.external';
		
		// Text resize config
		this.resizer = 'div.resizer';
		this.decreaseClass = 'decrease';
		this.defaultClass = 'default';
		this.increaseClass = 'increase';
		this.defaultSize = $('body').css('font-size');
		this.currentSize = $('body').css('font-size');
		this.incrementSize = '2px';
		this.sizeFromDefault = '6px';
		this.cookieExpiry = 5;
		
	// End configuration area
	
	// Cookie get and set functions
	this.setCookie = function(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" : "" );
	};
	this.getCookie = function(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 "";
	};
	this.deleteCookie = function(name, path, domain) {
		if (that.getCookie(name)) {
			document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +	( ( domain ) ? ";domain=" + domain : "" ) +	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	}

	if ($(that.searchElement).length > 0) {
		// Search remove and reapply text
		$(that.searchElement).val(that.searchValue);
		$(that.searchElement).focus(function () {
			if ($(this).val() == that.searchValue) {
				$(this).val('');
			}
		});
		$(that.searchElement).blur(function () {
			if ($(this).val() == '') {
				$(this).val(that.searchValue);
			}
		});
		// Fail if search default text present
		$(that.googlecse).submit(function () {
			if ($(that.searchElement).val() == that.searchValue || $(that.searchElement).val() == '') {
				alert(that.searchDefaultTextError);
				return false;
			}
		});
	}
	// Menu system
	if ($(that.menuElement).length > 0) {
		$(that.menuElement + ' ul.main').children('li').each(function (i) {
			$(this).data('item', $(this).children('a').text());
			$(that.subMenuElement + ' ul').eq(i).data('item', $(this).children('a').text());
		});
		$(that.menuElement + ' ul.main').children('li').each(function () {
			var $parent = $(this);
			$(this).children('a').mouseover(function () {
				if (!$parent.hasClass('active')) {
					var itemActive = $parent.siblings('.active');
					itemActive.toggleClass('active');
					$(that.subMenuElement).children('ul').each(function () {
						if ($(this).data('item') == itemActive.data('item')) {
							$(this).hide();
						}
						else if ($(this).data('item') == $parent.data('item')) {
							$(this).show();
						}
					});
					$parent.toggleClass('active');
				}
			});
		});
	}
	// External link open in new window
	$(that.apop).click(function(){
		window.open(this.href);
		return false;
	});
	// New form validation with jQuery validate
	$('form.validation').validate();
	
	// Homepage quick links
	if ($('form#quicklinks').length > 0) {
		$('form#quicklinks select').change(function () {
			var optionval = $('form#quicklinks select option:selected').val();
			if (optionval != 'null') {
				window.location = optionval;
				return false;
			}
		});
	}
	// Text resizer
	if ($(that.resizer).length > 0) {
		if (that.getCookie('resizer') != '') {
			if (that.getCookie('resizer') == that.defaultSize) {
				that.deleteCookie('resizer', '/');
			} else {
				that.currentSize = that.getCookie('resizer');
				var lh = parseInt(that.currentSize) + 4 + 'px';
				$('body, div#content p, div#content li').css({
					'font-size' : that.currentSize,
					'line-height' : lh
				});
			}
		}
		$(that.resizer).children('a').click(function () {
			//cache
			var $this = $(this);
			if ($this.hasClass(that.defaultClass)) {
				var lh = parseInt(that.defaultSize) + 4 + 'px';
				$('body, div#content p, div#content li').css({
					'font-size' : that.defaultSize,
					'line-height' : lh
				});
				that.currentSize = that.defaultSize;
				if (that.getCookie('resizer')) {
					that.deleteCookie('resizer', '/');
				}
			} else if ($this.hasClass(that.decreaseClass)) {
				var min = parseInt(that.defaultSize) - parseInt(that.sizeFromDefault) + 'px';
				if (parseInt(that.currentSize) > parseInt(min)) {
					var size = parseInt(that.currentSize) - parseInt(that.incrementSize);
					var lh = parseInt(size) + 4 + 'px';
					$('body, div#content p, div#content li').css({
						'font-size' : size,
						'line-height' : lh
					});
					that.currentSize = size + 'px';
					that.setCookie('resizer',size + 'px',that.cookieExpiry, '/');
				}
			} else if ($this.hasClass(that.increaseClass)) {
				var max = parseInt(that.defaultSize) + parseInt(that.sizeFromDefault) + 'px';
				if (parseInt(that.currentSize) < parseInt(max)) {
					var size = parseInt(that.currentSize) + parseInt(that.incrementSize);
					var lh = parseInt(size) + 4 + 'px';
					$('body, div#content p, div#content li').css({
						'font-size' : size,
						'line-height' : lh
					});
					that.currentSize = size + 'px';
					that.setCookie('resizer',size + 'px',that.cookieExpiry, '/');
				}
			}
		});
	}
});
