errorMessage = "Please supply a valid Username and Password.";

jQuery(document).ready( function() {
	jQuery("a")
		.filter(function(){
			// filter out mailto: and internal links
			return (jQuery(this).attr('href').indexOf("http://") == -1 || jQuery(this).attr('href').indexOf("http://"+window.location.hostname+"/") == -1) && jQuery(this).attr('href').indexOf("mailto:") == -1
		})
		.click(function(e){
			window.open(jQuery(this).attr('href'));
			e.preventDefault();
		});
	
	jQuery("#loginform input[type=text], #loginform input[type=password]")
		.focus(function(){
			var defaultValue = jQuery(this).siblings("label").text();
			if (jQuery(this).val() == ''){
				return;
			}
			
			if (jQuery(this).val() == defaultValue){
				clearValue(jQuery(this));
			}
			
			if (jQuery(this).attr("id") == "user_pass" && jQuery(this).val() == '' && jQuery(this).attr("type") == "text"){
				newJO = swapInputType(jQuery(this), "password");
				newJO.focus();
				jQuery(this).remove();
			}
		})
		.blur(function(){
			var defaultValue = jQuery(this).siblings("label").text();
			if (jQuery(this).val() == ''){
				addDefaultValue(jQuery(this));
			}
			if (jQuery(this).attr("id") == "user_pass" && jQuery(this).val() == defaultValue && jQuery(this).attr("type") == "password"){
				swapInputType(jQuery(this), "text");
				jQuery(this).remove();
			}
		})
		.each(function(i){
			jQuery(this).prev().hide();
			addDefaultValue(jQuery(this));
			if (jQuery(this).attr("id") == "user_pass"){
				swapInputType(jQuery(this), "text");
				jQuery(this).remove();
			}
			loginFormInitComplete = true;
		});
		
	jQuery("#loginform").submit(function(){
		var errors = new Array();
		jQuery("#loginform input[type=text], #loginform input[type=password]")
			.each(function(i){
				var defaultValue = jQuery(this).siblings("label").text();
				if (jQuery(this).val() == defaultValue || jQuery(this).val() == '')
				{
					errors.push(jQuery(this).attr("id"));
				}
			})	
		if (errors.length > 0)
		{
			alert(errorMessage);
			return false;
		}
		
	});
});

loginFormInitComplete = false;

function swapInputType(jQueryObject, newType)
{
	try{
		newJO = jQueryObject.clone(true).attr("type",newType).insertAfter(jQueryObject);
		//newJO = jQueryObject.clone(true).removeAttr("type").attr("type", "text").insertAfter(jQueryObject);
	}catch(e){
		// IE HAVING PROBLEMS!!!
		inputId = jQueryObject.attr("id");
		tmp = jQueryObject.wrap("<span></span>").parent().html().replace(/type=\"?password\"?/, "type=\"text\"");		
		//jQueryObject.replaceWith(tmp);
		tmpSpan = jQuery("#"+inputId);
		//alert(tmpSpan.html());
		//newJO = tmpSpan.replaceWith(tmpSpan.html());
		//alert(tmpSpan);
		newJO = jQueryObject.replaceWith(tmp);
		alert(tmpSpan.attr("type"));
		alert(jQuery("#loginform").html());
		alert("derp");
	}
	return newJO;
}

function clearValue(jQueryObject)
{	jQueryObject.val('');
	return;	
}

function addDefaultValue(jQueryObject)
{
	var labelText = jQueryObject.siblings("label").text();
	jQueryObject.val(labelText);
	return;
}