var panda = {	

	init: function() {
		this.tweakLayout();
		this.imageInputs();
		this.addLoginHints();
		this.addSearchHint();
		this.addEvents();
		this.createMailtoLinks();
	},

	
	tweakLayout: function() {
		// Remove the first top border on the secondary content
		$("#secondarycontent .panel:first").css({border:"none"});
	
		/* Meke sure these elements are always the same height.
		No sure I like this, it works, but when you resize the text it overflows */
		$("#primarycontent div.panel").vjustify(); 
	},
	
	
	imageInputs: function() {
		/* Remove the normal submit buttons and replace with an image
		Yeah yeah I know, remove this if you want */
		$("#login fieldset").append('<input type="image" src="/img/login_button.png" value="Login" id="login_button" />');
		$("#login input[@type=submit]").remove();
		
		$("#search fieldset div:last").append('<input type="image" src="/img/search_button.png" value="Search" id="search_button" />');
		$("#search input[@type=submit]").remove();
		
		// IE doesn't submit the form on ENTER with (some?) image inputs
		$('#login input').keydown(function(e){
			if (e.keyCode == 13) {
				$(this).parents('form').submit();
				return false;
			}
		});
	},
	
	
	// If the input box values are empty then add hints
	addLoginHints: function(){
		if (!$("#login #login_email").val() || $("#login #login_email").val() == "Email address") {
			$("#login #login_email").val("Email address");
			$("#login_password").val("password");
		}
	},
	addSearchHint: function(){
		if (!$("#keywords").val() || $("#keywords").val() == "Keyword(s)") {
			$("#keywords").val("Keyword(s)");
		}
	},
	// If the input box values are hints then remove the hints
	removeLoginHints: function(){
		if ($("#login #login_email").val() == "Email address") {
			$("#login #login_email").val("");
			$("#login_password").val("");
		}
	},
	removeSearchHint: function(){
		if ($("#keywords").val() == "Keyword(s)") {
			$("#keywords").val("");
		}
	},

	
	addEvents: function() {
		// Clear the input box values on click
		// Login
		$("#login .input").focus(function(){
			panda.removeLoginHints();
		});
		$("#login .input").blur(function(){
			panda.addLoginHints();
		});
		// Search
		$("#keywords").focus(function(){
			panda.removeSearchHint();
		});
		$("#keywords").blur(function(){
			panda.addSearchHint();
		});
	},
	
	
	createMailtoLinks: function(){
		$(".email").each(function() {
			var email = $(this).text().replace(/ \[at\] /, "@");
			$(this).empty();
			$(this).append('<a href="mailto:'+email+'">'+email+'</a>');
		});
	}
}


$(document).ready(function(){
	panda.init();
});


// ------------------- jQuery add-ons --------------------

jQuery.fn.vjustify = function(){
	var maxHeight=0;
	this.each(function(){
		if (this.offsetHeight>maxHeight) {
			maxHeight=this.offsetHeight;
		}
	});
	this.each(function(){
		$(this).height(maxHeight + "px");
		if (this.offsetHeight>maxHeight) {
			$(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
		}
	});
};