$(document).ready(function() {
	// PNG fix
	$.ifixpng(url + 'images/blank.gif');
	$('img[src~=.png], .bgPng').ifixpng();
	
	// Search box
	var element = $('#search input.cooljs');
	var defaultSearchText = element.attr('value');

	element.focus(function() {
		if ($(this).attr('value') == defaultSearchText) {
			$(this).attr('value', '');
		}
	});

	element.blur(function() {
		if ($(this).attr('value').length == 0) {
			$(this).attr('value', defaultSearchText);
		}
	});
	
	// Use title to populate blank form fields
	$('form:has(input[title])').each(function() {
		var form = $(this);
		
		// Check for inputs with title
		$(this).find('input[title]').each(function() {
			var title = $(this).attr('title');
			
			// Function to copy title to non-empty inputs 
			function copyTitle(el) {
				if (el.val().length < 1) {
					el.val(title);
				}
			}
					
			// Clear on focus only if value is the same as title
			$(this).focus(function() {	
				if ($(this).val() == title) {
					$(this).val('');
				}
			});
			
			// Re-add on blur
			$(this).blur(function() {
				copyTitle($(this));
			});
			
			// If empty, populate from title
			copyTitle($(this)); 
		});
		
		// Clear title inputs on submit to allow for form validation to work
		$(this).submit(function() {
			$(this).find('input[title]').each(function() {
				if ($(this).attr('title') == $(this).val()) {
					$(this).val('');
				}
			});
		});
	});

	// External links
	$('a.external').click(function(e) {
		// Prevent default action
		e.preventDefault();
		
		// Open link in new window
		window.open($(this).attr('href'), 'blank');
	});
	
	// Rotating testimonials
	$('#testimonials').innerfade({ 
		speed: 750, 
		timeout: 12000
	});

	// Spaw image spacing compatibility for standards compliant browsers
	$('img[hspace]').each(function() {
		var hspace = $(this).attr('hspace');

		$(this).removeAttr('hspace');

		$(this).css({
			'padding-left': 0,
			'padding-right': 0,
			'margin-left': hspace,
			'margin-right': hspace
		});
	});

	$('img[vspace]').each(function() {
		var vspace = $(this).attr('vspace');

		$(this).removeAttr('vspace');

		$(this).css({
			'padding-left': 0,
			'padding-right': 0,
			'margin-left': vspace,
			'margin-right': vspace
		});
	});
});
