$(document).ready(function(){
	get_header();
	init_tabs();
	init_toggle();
	
	init_form_submit();
	
	init_skills_hover();
	
});

function init_form_submit(){
	var ajax_message = "";
	$("#contact_form").submit(function(){
		if( $(this).find('#email').val() != get_val('#contact_form #email') && $(this).find('#message').val() != get_val('#contact_form #message') && echeck( $("#contact_form #email").val() ) == true){
			$.ajax({
				type: 		"POST",
				url: 		"lib/action.php",
				data: 		"email=" + $('form #email').val() + 
							"&message=" + $('form #message').val(),
				success: 	function(html){
								ajax_message = html;
							}
			});
			$("#contact_form .btn")
				.ajaxStart(function(){
					$(this).html("<img src=\"images/loader.gif\" />");
				})
				.ajaxStop(function(){
					$(this).parent().fadeOut("fast");
					$("#thanks")
						.fadeIn("fast")
						.html(ajax_message);
				});
		} else {
			alert("You have to enter a valid email address and a message.");
		}
		return false;
	});
	
	$("#contact_form .btn").click(function(){
		return false;
	});
	$("#contact_form .btn")
		.mousedown(function(){
			$(this).css("background", "transparent url(images/button_press.png) top left no-repeat");
		})
		.mouseup(function(){
			$(this).css("background", "transparent url(images/button_norm.png) top left no-repeat");
			$(this).parent().submit();
			return false;
		});
}

function init_toggle(){

	$('a.toggle.shrink').click(function(){
		do_shrink();
	});
	$('a.toggle.grow').click(function(){
		do_grow();
	});
}

function do_shrink(){
	$('a.toggle.shrink').hide();
	$('a.toggle.grow').show();
	
	$("#header").animate({
		"height":		"150px"
	});
	$(".tabs").fadeOut();
	
	set_header("shrunk");
}

function do_grow(){
	$('a.toggle.grow').hide();
	$('a.toggle.shrink').show();
		
	$("#header").animate({
		"height":		"475px"
	});
	$(".tabs").fadeIn();
	
	set_header("grown");
}

function init_tabs(){
	$(".options li").click(function(){
		$(".options li.active").removeClass("active");
		$(this).addClass("active");
	});
	$(".options li img.twitter").click(function(){
		$(".middle #blog").fadeTo("fast", 0, function(){
			$(this).hide();
			$(".middle #twitter")
			.show()
			.fadeTo("slow", 1);
		});
	});
	$(".options li img.blog").click(function(){
		$(".middle #twitter").fadeTo("fast", 0, function(){
			$(this).hide();
			$(".middle #blog")
			.show()
			.fadeTo("slow", 1);
		});
	});
}

function get_header(){
	$.ajax({
		type: 		"POST",
		url: 		"lib/ajax.php",
		data: 		"mode=get",
		success:	function(msg){
						if(msg == 'shrunk'){
							do_shrink();
						}
					},
		async:		true
	});
}

function set_header(position){
	$.ajax({
		type: 		"POST",
		url: 		"lib/ajax.php",
		data: 		"mode=set&header="+position,
		async:		true
	});
}


function init_skills_hover(){
	$("#skills ul li span").hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this)
			.removeClass('hover');
	});
	$("#skills ul li span").click(function(){
		$("#skills ul li .active").removeClass('active');
		$(this)
			.addClass('active')
			.parent().find('div').addClass('active');
	});
	
	$("#skills ul li:first span").trigger("click");
}


function echeck(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}