$(window).load(function(){
	$("#loading").remove();
	$("#feature1").fadeIn('slow');
});

$(document).ready(function(){
	$("#features").append('<img id="loading" src="/images/ajax-loader.gif" />');
	
	$(".details li a").click(function(){
		var $this = $(this);
		$this.addClass("current");
		$(".details nav li a").not($this).removeClass("current");
		var featureId = $this.attr("id");
		featureId = featureId.replace("nav","");
		$(".feature").not("#feature"+featureId).fadeOut(1000);
		$("#feature"+featureId).fadeIn(1000);
		return false;
	});
	
	$('form input, form textarea').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).prev('label').text());	
		}
		
		$(this).focus(function() {
			if($(this).val() == $(this).prev('label').text()) {
				$(this).val('').addClass('focused');	
			}
		});
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).prev('label').text()).removeClass('focused');	
			}
		});
	});
	
	$("#contact").submit(function(){
		var fields = $(this).serialize();
		$("#form-message").hide().empty();
		$('#contact button[type=submit]').attr("disabled","disabled");
		$.ajax({
			type: "POST",
			url: "mailer.php",
			data: fields,
			success: function(msg){
				$("#contact").ajaxComplete(function(event, request, settings){
					if(msg == 'OK') {
						result = '<p>Your message has been sent. Thank you!</p>';
						$(this).empty().append(result).hide().fadeIn(1500);
					} else if (msg.substring("0,4")=="Error") {
						result = msg;
						$(this).empty().append(result).hide().fadeIn(1500);
					} else {
						result = msg;
						$('#contact button[type=submit]').removeAttr("disabled");
						$("#form-message").html(result).fadeIn(1500);
					}
					
				});
			}
		});
		return false;
	});
	
	$(".primary li a").mouseover(function(){
		$(this).fadeTo('fast',0,function(){
			$(this).css("background-position","0px -73px").fadeTo('fast',1);
		});
	}).mouseout(function(){
		$(this).fadeTo('fast',0,function(){
			$(this).css("background-position","0px 0px").fadeTo('fast',1);
		});
	});

});
