//*** Document Ready ***
$(document).ready(function() {

	//Append the InputFocus() and InputBlur() functions to any elements with the class "clearfocus"
	$(".clearfocus").focus(function() { InputFocus(this); });
	$(".clearfocus").blur(function() { InputBlur(this); });
	$(".resetfield").each(function() { $(this).val( this.defaultValue ); });

	//preload CSS images
	$.preloadCssImages();
	
	
	//date picker
	if ($('input.datepicker').length > 0) {
		$("input.datepicker").datepicker({ showOn: 'button', buttonImageOnly: true, buttonText: 'Choose date', buttonImage: 'images/datepicker.png', dateFormat: 'dd-M-yy' });
	}
	
	
    //jquery validate defaultInvalid method
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		if (element.value == element.defaultValue) {return false;}
		return true;
	}, "This field is required.");
	
	//video box popups
	jQuery(document).ready(function() {
		$(".video-popup").click(function() {
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 640,
				'height'		: 385,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				'wmode'				: 'transparent',
				'allowfullscreen'	: 'true'
				}
			});
			return false;
		});
	});
	
});


//*** Window Load ***
$(window).load(function() {
});


//*** Window Resize ***
$(window).resize(function() {
});


//*** Custom Functions ***



//*** Basic Functions ***

function InputFocus(element) {
	if (element.value == element.defaultValue) {
		element.value = "";
		$(element).addClass('filled');
	}
}
function InputBlur(element) {
	if (element.value == "") {
		$(element).removeClass('filled');
		element.value = element.defaultValue;
	}
}

//Function to make an element, e.g. content area, fill the visible space so a footer appears at the bottom. Wrapper can be a container or body element.
function MakeElementFillVisibleSpace(element_to_adjust, wrapper_element) {
	if (wrapper_element == undefined) { wrapper_element = 'body'; }
	$(element_to_adjust).height('auto');
	if ( $(wrapper_element).height() < $(window).height() ) {
		height_difference = $(window).height() - $(wrapper_element).height();
		new_height = $(element_to_adjust).height() + height_difference;
		$(element_to_adjust).height(new_height);
	}
}

//Make single element the same height as another single element - good for having two columns the same height
function MatchHeight(elementToAdjust, elementToCompare, adjust_offset, compare_offset) {
	if (isNaN(adjust_offset)) {
		adjust_offset=$(elementToAdjust).outerHeight(true) - $(elementToAdjust).height();
	};
	if (isNaN(compare_offset)) {
		compare_offset=$(elementToCompare).outerHeight(true) - $(elementToCompare).height();
	};
	if ( $(elementToAdjust).height()+adjust_offset < $(elementToCompare).height()+compare_offset ) {
		$(elementToAdjust).height( $(elementToCompare).height()+compare_offset-adjust_offset );	//adjust the height of the adjust element to the compare element
	}
}

//get a querystring by name
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}
