$(function () {
	
	var here, path;
	
	// Fancybox links.
	$("a.fancybox").click(function() {
		// This is so that hitting return after the box is open doesn't re-open the box.
		this.blur();
	}).fancybox({
		width: 380,
		height: 253,
		padding: 0,
		overlayColor: '#000',
		overlayOpacity: 0.4
	});
	
	// Clear search on focus.
	$('input.search').each(function () {
		var dis, def, fill, empty;
		dis = $(this);
		def = 'Search';
		fill = function () {
			if (dis.val() === '') {
				dis.val(def);
			}
		};
		empty = function () {
			if (dis.val() === def) {
				dis.val('');
			}
		};
		dis.focus(empty).blur(fill);
		fill();
	});
	
	path = function (url) {
		return url.split('envirocare.com/')[1];
	};
	
	// mark ancestor links in navigation
	here = path(window.location.href);
	$('li').each(function () {
		var li = this,
			link;
		$('a', li).each(function () {
			link = path(this.href);
			if (!(here || link) || (link && here.indexOf(link) === 0)) {
				$(li).addClass('nav-current-ancestor');
			}
		});
	});
	
	// hack for safari rendering bug:
	// change the style on any element (even outside the nav) and the nav corrects itself
	$('#site-top h2').css({
		color: '#000000'
	}).css({
		color: ''
	});
	
});
