(function($){

// Utility functions
$.fn.setClearDefault = function(text) {
	return this.each(function(){
		$(this)
		.focus(function(){
			if($(this).val() == text) {
				$(this).val("").css("opacity", 1);
			}
		})
		.blur(function(){
			if($(this).val().length == 0) {
				$(this).val(text).css("opacity", 0.7);
			}
		});
		if($(this).val().length == 0) {
			$(this)
			.css("opacity", 0.7)
			.val(text);
		}
	});
}


// Things to do on page load
$(function(){

	// Navigation bar drop down menus
	$("#navbar li a").removeAttr("title");
	$("#navbar ul > li")
	.each(function(){
		var text = $(this).find("a");
		var pos = text.position();
		$(this).find("ul")
		.css({
			"position": "absolute",
			"z-index": 100,
			"left": pos.left,
			"top": pos.top + text.height(),
			"min-width": text.width()
		});
	})
	.hover(function(){
		$(this).find("ul").show();
	}, function() {
		$(this).find("ul").fadeOut();
	});

	// Show/hide meta on hover for logged in users
	if(window.wordpress_user_logged_in) {
		$(".post")
		.hover(function(){
			$(this).find(".meta").slideDown();
		}, function() {
			$(this).find(".meta").slideUp();
		});
	}

	// Clear search field when clicked on
	$("#search input[type='text']")
	.setClearDefault("Search...");

	// Ensure that the page content is at least as tall as the sidebar
	var fixHeight = function(){
		$("#content").css("min-height", $("#sidebar").height());
	}
	fixHeight();
	$(window).resize(fixHeight);

	// iframe height fixes
	$("iframe").load(function(){
		try {
		$(this).css("height", this.contentWindow.document.documentElement.scrollHeight);
		} catch(e) {}
	});

	// hide the search box for the shop frame
	if($("iframe.fullpage").size() > 0) {
		$("#content").css("margin-right", 0);
		$("#search").hide();
	}

	// jump out of frames
	if(window != top) {
		top.location.href = location.href;
	}

});

})(jQuery);

