var key_code = '';

$(function(){
	
	$("#contact_form").submit(function () {
		return validateForm();
	});
	
	$(document).keydown(function(event){
	   key_code += event.keyCode;
	   key_code == 7179 ? headButt() : '';
	});
	
	$(document).keyup(function(event){
	    key_code = '';
	});
	
	$("#sidebar ul li a").hover(
		function ()
		{
			this.parentNode.title = "";
			var tooltip = this.getElementsByTagName("strong");
			if(tooltip.length > 0)
			{  
				
				$(tooltip).animate({
				bottom: "-3px"
				}, 100);
			}
		},
		function ()
		{
			var tooltip = this.getElementsByTagName("strong");
			setTimeout(hideTooltip, 500);
			function hideTooltip()
			{
				$(tooltip).animate({
				bottom: "-24px"
				}, 100);
			}
		}
	);
	
	$("#train")[0].title = "follow the train?";
	
	$("#train").click(
		function ()
		{
				if(this.className =="")
				{
					this.className = "active";
					$(this).animate({
					left: "140px"
					}, 500);
					$("#music_link").animate({
					height: "100px"
					}, 500);
					this.title = "park the train!"
				}
				else
				{
					this.className = "";
					$(this).animate({
					left: "0px"
					}, 500);
					$("#music_link").animate({
					height: "0"
					}, 500);
					this.title = "follow the train?"
				}
		}
	);
	
	$("a").each(
		function ()
		{
			if(this.rel=="external")
			{
				this.target="blank";
			}
		}
	);
	
	var xOffset=-10;
	var yOffset=20;
	
	$("a.tooltip").hover(
		function(e)
		{
			this.t=this.title;this.title="";
			if(this.t != "") 
			{	
				$("body").append("<p id='tooltip'>"+this.t+"</p>");
				$("#tooltip").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px").fadeIn("fast");
			}
		},
		function()
		{
			this.title=this.t;
			$("#tooltip").remove();
		}
	);
	
	
	$("a.tooltip").mousemove(
		function(e)
		{
			$("#tooltip").css("top",(e.pageY-xOffset)+"px").css("left",(e.pageX+yOffset)+"px");
		}
	);

});

function validateForm()
{
	var errors;
	var email = /^.+@.+\..{2,3}$/;
	
	$("#input_name").css({'border' : '1px solid #B3B3B3'});
	$("span.1").remove();
	$("#input_email").css({'border' : '1px solid #B3B3B3'});
	$("span.2").remove();
	$("#input_comments").css({'border' : '1px solid #B3B3B3'});
	$("span.3").remove();
	
	if(document.getElementById("input_name").value == "")
	{
		$("#input_name").css({'border' : '1px solid red'});
		$("#input_name").focus();
		$("#input_name").after(' <span class="error 1">required field!</span>');
		errors = true;
	}
	if(document.getElementById("input_email").value == "")
	{
		$("#input_email").css({'border' : '1px solid red'});
		if(errors != true)
		{
			$("#input_email").focus();
		}
		$("#input_email").after(' <span class="error 2">required field!</span>');
		errors = true;
	}

	if( !email.test(document.getElementById("input_email").value ) )
	{
		$("#input_email").css({'border' : '1px solid red'});
		if(errors != true)
		{
			$("#input_email").focus();
		}
		if($("span.2:eq(0)").text() != 'required field!')
		{
			$("#input_email").after(' <span class="error 2">invalid email!</span>');
		}
		errors = true;
	}
	else
	{
		$("#input_email").css({'border' : '1px solid #B3B3B3'});
		$("span.2").remove();
	}
	if(document.getElementById("input_comments").value == "")
	{
		$("#input_comments").css({'border' : '1px solid red'});
		if(errors != true)
		{
			$("#input_comments").focus();
		}
		$("#input_comments").after(' <span class="error 3">required field!</span>');
		errors = true;
	}

	if(errors == true)
	{
		return false;
	}
}

function headButt () 
{
	$('#header').css('overflow','hidden');
	$('#header').animate({
		backgroundPosition: '530px bottom'
	}, 300, function () {
		$('#navigation').animate({
			marginRight: '-700px'
		}, 1000, function () {
			var form = document.createElement('div');
			$(form).load('/login #login_form', function () {
				$('#header').append(form);
				$('#login_form').animate({
					top: '0px'
				}, 500);
			});
		});
	});
}