// propellerhead.de functions
var PHD = {
	
	// load a content window from the url hash information
	loadOnLoad:function() {
		if(location.hash) {
			// loads the content described in the the url hash
			var url = location.hash.replace(/^\#/, "");
			PHD.loadFromUrlWithAnim(url);
		}
	},
	
	// load a content window by url (looks for the corresponding click item first)
	loadFromUrlWithAnim:function(url) {
		// guess the url's item
		var item = $("a[href='"+url+"']"); // the a elem
		if(item.length > 0) {
			// It was a url that belongs to a menu item. Open the item's menu.
			var ph2 = item.parent().get(0); // the current h2
			var pdiv = $(ph2).parent().get(0); // the h1's div
			var ph1 = $(pdiv).prev().get(0); // the h1
			$(ph1).click();
			PHD.loadFromItemWithAnim(item.parent()); /* call with the the h2 */
		} else {
			// It was an arbitrary url, e.g. the error page
			PHD.loadContent(url, 235, true);
		}
	},
	
	// load a content window with the clicked item given
	loadFromItemWithAnim:function(item) {

		var target_url="";
		if(item) {
			var refYPos=0;
			var h2pos=item.position();
			if(h2pos) {
				refYPos = h2pos.top - 18;
			}
			// $("#content").animate( {top: h2pos.top - 18}, PHD.ANIM_SPEED , "swing");
			
			var linkItem=item;
			if(!(linkItem.attr('href'))) { linkItem = item.find("a"); }
			target_url = linkItem.attr('href');
		}
		PHD.loadContent(target_url, refYPos);
		
		return false;
	},
  
	// display the information from a given url in the content window
	loadContent:function(url, refYPos, doHideArrow) {
		if(doHideArrow==null) doHideArrow = false;
		if($("#content").css('display') == "none")
			$("#content").fadeIn(PHD.ANIM_SPEED);

		if(!doHideArrow) {
			if($("#content .content-arrow").css('display') == "none")
				$("#content .content-arrow").show("slide", { direction: "left" }, PHD.ANIM_SPEED);
		}
		
		$("#content-inner").animate( {'opacity':0}, PHD.ANIM_SPEED, function () { // fade out

			$("#spinner").fadeIn(PHD.ANIM_SPEED, function () {
		
				$("#content-load").load(url, function () {
				
					$("#spinner").fadeOut(PHD.ANIM_SPEED, function () { 
				
						var maxBottom = $(window).height() - 60;
						var newHeight = $("#content-load").height();

						var curTop = refYPos;
						var totalBottom = curTop + newHeight;
						var newBottom = Math.min(totalBottom, maxBottom);
						var newTop = newBottom - newHeight;
						
						$("#content").animate( { "top": newTop } );
						$("#content .content-arrow").animate( { "top": (curTop-newTop + 8) } );

						$("#content-inner").animate( { "height": newHeight }, PHD.ANIM_SPEED, function () { 
							$("#content-inner").html($("#content-load").html());
							$("#content-load").html("");
							$("#content-inner").animate( {'opacity':1}, PHD.ANIM_SPEED, function () { 
								// IE 7/8 fix for not losing its cleartype
								if(jQuery.browser.msie)
									$(this).get(0).style.removeAttribute('filter');
							} ); // fade in
						});
						pageTracker._trackPageview(url); // Google Analytics ajax tracking
					});
				});
			});
		});
		
		location.hash=url;
	},
	
	showArrow:function() {
		if($("#content .content-arrow").css('display') != "none")
		$("#content .content-arrow").hide("slide", { direction: "left" }, PHD.ANIM_SPEED);
	},
	
	submitContactForm:function() {
		// ev.preventDefault();
		var from = $('input[name=from]').val();
		var email = $('input[name=email]').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", "Bitte geben Sie Ihren Namen an."); allok = false; }
		if(allok && email == "") { PHD.hintMandatory("email", "Bitte geben Sie Ihre E-Mail-Adresse an. Wir geben Sie keinem weiter, versprochen!"); allok = false;}
		if(allok && message == "") { PHD.hintMandatory("message", "Sagen Sie uns doch, was Sie uns sagen möchten."); allok = false;}
		if(allok && wizpe == "") { PHD.hintMandatory("wizpe", "Bitte beantworten Sie die Spamschutz-Frage, damit wir sicher sein können, dass Sie kein ferngesteuerter russischer Killerroboter sind."); allok = false;}
		if(allok == true) {
		
			$('#contactform').fadeOut();
			$('#errormsg').slideUp(function () {
				$.post("/_system/php/docontact.php", { name: from, email: email, message: message, wizpe: wizpe }, 
					function(data) {
						if(data.status != "ok") {
							$('#contactform').fadeIn();
							var errormsg = "";
							switch(data.status) 
							{
								case 'wizpe':
									errormsg = "Die eingegebene Antwort auf den Spamschutz ist nicht richtig.";
									break;
								case 'error':
									errormsg = "Die Versendung der Mail hat nicht funktioniert, und wir wissen auch nicht, warum. Bitte schreiben Sie doch einfach eine herkömmliche E-Mail an 'propi"+"nfo[at]pr" +"opellerhead.d"+"e'.";
									break;
								break;
							}
							PHD.showErrorMsg(errormsg);
						}
						else
						{
							$('#contactform-wrapper').addClass("contact-ok").html("<strong>Besten Dank.</strong><br/>Wir haben Ihre Mail erhalten und melden uns ggf. bei Ihnen zurück.").slideDown();;
						}
					}, 'json' 
				);
			});
		}
		return false;			
	},
	
	hintMandatory:function(item, msg) {
		$("#"+item).addClass('error_msg');
		PHD.showErrorMsg(msg);
	},
	
	showErrorMsg:function(msg) {
		$('#errormsg').slideUp(function () {
			$(this).text(msg).slideDown();
		});
	},
	
	// ----- 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);
	}
	
}
// static class variables
PHD.ANIM_SPEED="fast";



