// propellerhead functions
var PHD = {
	submitContactForm:function() {

		// ev.preventDefault();
		var from = $('input[name=from]').val();
		var email = $('input[name=email]').val();
		var website = $('textarea[name=website]').val();
		var message = $('textarea[name=message]').val();
		var wizpe = $('input[name=wizpe]').val();
		var allok = true;
		$('input').removeClass('error_msg');
		$('textarea').removeClass('error_msg');
		if(allok && from == "") { PHD.hintMandatory("from", "Please enter your name."); allok = false; }
		if(allok && email == "") { PHD.hintMandatory("email", "Please enter your e-mail address. Adrian will not share it with anybody, he promises!"); allok = false;}
		if(allok && message == "") { PHD.hintMandatory("message", "Please enter what you want to tell Adrian."); allok = false;}
		if(allok && wizpe == "") { PHD.hintMandatory("wizpe", "Please answer the little spam-protection answer, so Adrian knows you are a real person."); allok = false;}
		if(allok == true) {
			$.post("/_system/php/docontact.php", { name: from, email: email, website: website, message: message, wizpe: wizpe }, 
				function(data) {
					if(data.status != "ok") {
						$('#contactform').fadeIn();
						var errormsg = "";
						switch(data.status) 
						{
							case 'wizpe':
								errormsg = "The spam protection answer was not correct.";
								break;
							case 'error':
							// @TODO: adapt the mail address
								errormsg = "Something went wrong when trying to send the mail, and we don't know what it was. Please write a regular email to 'adria"+"n.tailor[at]4si" +"ng.c"+"om'.";
								break;
							break;
						}
						PHD.showErrorMsg(errormsg);
					}
					else
					{
						var errormsg = "<strong>Thank you.</strong><br/>We have received your email, and will contact you if neccessary.";
						PHD.showErrorMsg(errormsg);
						document.forms['contactform'].reset();
					}
				}, 'json' 
			);
		}
		return false;			
	},
	
	hintMandatory:function(item, msg) {
		$("#"+item).addClass('error_msg');
		PHD.showErrorMsg(msg);
	},
	
	showErrorMsg:function(msg) {
		PHD.pagePassive($('.fg-content'));
		PHD.pageActive($('#bg-content-mailmsg'));
		$('#err-content').html(msg);
	},
	
	// ----- BEGIN spam protection -----
	UnCryptMailto:function(s) {
		var n=0;
		var r="";
		for(var i=0;i<s.length;i++) { 
			n=s.charCodeAt(i);
			// 45 is * which allows special treatment of the letters x to z
			if (n == 45)
			{
				n=s.charCodeAt(i+1);
				r += String.fromCharCode(n);
				++i;
			}
			else
			{
				if (n>=8364) {n = 128;}
				r += String.fromCharCode(n-3);
			}
		}
		return r;
	},
	linkTo_UnCryptMailto:function(s)	{
		location.href=PHD.UnCryptMailto(s);
	},
	// ----- END spam protection -----

	pagePassive:function(that) {
		$(that).css("z-index", "0");
		$(that).find(".body").children("p,input,label,textarea, ul").css("opacity","0.3");
		$(that).find(".boxtext").css("opacity","0.3");
		$('#deactivator_'+$(that).attr('id')).show();
	},

	pageActive:function(that) {
		$(that).css("z-index", "3");
		$(that).find(".body").children("p,input,label,textarea, ul").css("opacity","1");
		$(that).find(".boxtext").css("opacity","1");
		$('#deactivator_'+$(that).attr('id')).hide();
	},
	
	setDeactivatorFor:function(that) {
		var d_target_id = $(that).attr('id');
		var d_id = "deactivator_"+d_target_id;
		$('#'+d_id).height($(that).height());
		// $(".deactivator").css("opacity","0.6");
		$(".deactivator").css("opacity","0.2");
		$(that).css("opacity","0.6");
		if($(that).find('.sidebox').length==0) {
			// 
		} else {
			$('#content-begin').css('height','26px');
		}
		// if there aren't any sideboxes AT ALL, make the deactivator less wide
		if($('.sidebox').length==0) {
			$('#'+d_id).addClass("onlycontent");
			$("#fg-deactivator").addClass("onlycontent");
		}
		
	}
		
}

