$(document).ready(function(){

	$("a.email").each(function(){ //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		e = e.replace("mailto:","");
		$(this).text(e);
	});

	$("a.external").click(function(){ //Open link in new window
		window.open(this.href);
		return false;
	});

	$(".rollover").hover( //Image rollovers
		function(){
			if($(this).attr("src").indexOf("-over")==-1) {
				var newSrc = $(this).attr("src").replace(".gif","-over.gif");
				newSrc = newSrc.replace(".jpg","-over.jpg");
				newSrc = newSrc.replace(".png","-over.png");
				$(this).attr("src",newSrc);
			}
		},
		function(){
			if($(this).attr("src").indexOf("-over")!=-1) {
				var oldSrc = $(this).attr("src").replace("-over.gif",".gif");
				oldSrc = oldSrc.replace("-over.jpg",".jpg");
				oldSrc = oldSrc.replace("-over.png",".png");
				$(this).attr("src",oldSrc);
			}
		}
	);
	
	$("input.input-text").each ( //Define default text for each input element
		function() {
			this.rel=this.value;
		}
	);

	$("input.input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$("input.input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
	
	//Navigation menus
	$("#nav ul li:first a").hover (
		function() {
			$("#nav ol").hide();
			$("#nav ol:first").show();
		},
		function() {
			
		}
	);
	
	$("#nav ul li:eq(1) a").hover (
		function() {
			$("#nav ol").hide();
			$("#nav ol:eq(1)").show();
		},
		function() {
			
		}
	);
	
	$("#nav ul li:eq(2) a").hover (
		function() {
			$("#nav ol").hide();
			$("#nav ol:eq(2)").show();
		},
		function() {
			
		}
	);
	
	$("#nav ul li:eq(3) a").hover (
		function() {
			$("#nav ol").hide();
			$("#nav ol:eq(3)").show();
		},
		function() {
			
		}
	);
	
	$("#nav ul li:eq(4) a").hover (
		function() {
			$("#nav ol").hide();
			$("#nav ol:eq(4)").show();
		},
		function() {
			
		}
	);
	
	$("#nav").hover (
		function() {

		},
		function() {
			$("#nav ol").show();
			$("#nav ol.hidden").hide();
		}
	);
	
	//Categories/comments lists
	$("#lists ul#comment-list").hide();
	
	$("#lists ol li:eq(0) a").click(
		function() {
			$("#lists ol").removeClass();
			$("#lists ol li:eq(0)").removeClass();
			$("#lists ol li:eq(1)").addClass("inactive");
			$("#lists ul#category-list").show();
			$("#lists ul#comment-list").hide();
			return false;
		}
	);
	
	$("#lists ol li:eq(1) a").click(
		function() {
			$("#lists ol").addClass("comments");
			$("#lists ol li:eq(1)").removeClass();
			$("#lists ol li:eq(0)").addClass("inactive");
			$("#lists ul#comment-list").show();
			$("#lists ul#category-list").hide();
			return false;
		}
	);

	//Comment form validation
	$("#comment-form").submit(
		function() {
			var error=false
			var response="There was an error filling out the form:\n";
			
			$name=$("#comment-author").val();
			$email=$("#comment-email").val();
			$url=$("#comment-url").val();
			$comment=$("#comment-comment").val();
			
			if (!isString($name)) {
				error=true;
				response+="Please enter your name\n";
			}
			
			if (!isEmail($email)) {
				error=true;
				response+="Please enter your email address\n";
			}
			
			if (isString($url) && !isURL($url)) {
				error=true;
				response+="Please enter a valid website address (must start with http://)\n";
			}
			
			if (!isString($comment)) {
				error=true;
				response+="Please enter a comment\n";
			}
			
			if (error) {
				alert(response);
				return false;
			}
		}
	);
});

//Data type validation
function isString(str) {
	if (str.length!="") {
		return true;
	} else {
		return false;
	}
}

function isInteger(str) {
	return (str.toString().search(/^-?[0-9]+$/) == 0);
}

function isEmail(str) { //Email address
	var emailRegExp="^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex=new RegExp(emailRegExp);
	return regex.test(str);
}

function isURL(str) { //URL
	var urlRegExp=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	var regex=new RegExp(urlRegExp);
	return regex.test(str);
}