//Hash Change Funktion
$(function(){
  $(window).hashchange( function(){		
    var hash = location.hash.replace( /^#!/, '' );
	if (hash in {'angebot':'','ueber-uns':'','linus':'','matthias':'','netzwerk':'','referenzen':'','kontakt':'','impressum':'','datenschutz':''})
	{
		showPage(hash);
	} else {
		$("body").attr("id","home-active");
		showPage("home");
	}
  })
});	

$(document).ready(function() {	
  //BG preloaden und einfaden
  var mi = 1;
  var ma = 4;
  var bg = Math.floor(Math.random() * (ma - mi + 1)) + mi;		
  
  $("<img />")
	.load(function(){
		$('#bg').append($(this));
		$('#bgimg').fadeIn(1000);
	})
	.attr({
		  "src": "bg"+bg+".jpg",
		  "width":1020,
		  "height":1020,
		  "style": "display:none",
		  "id": "bgimg"
		  })
	.addClass("bg");
  //BG "schützen"						   
  $(".bg").live('contextmenu',function(){return false;});
  $(".bg").live('drag',function(){return false;});
  $(".bg").live('mousedown',function(){return false;});
  
  //Contact Form functions
  $('#contactForm').ajaxForm({ beforeSubmit: validateForm, clearForm: true, success: function() { 
			   $("#contactFormDiv").slideUp({duration:800,easing:"easeInCubic",complete: function() { $("#thankYouDiv").fadeIn("fast"); } });
		}
  }); 

  //Elemente einfaden
  $("#menu").fadeIn(800);
  $("#footer").animate({left: 160}, 1000);
  //Initial content display
  if (location.hash===""||location.hash==="#!home")
  {
	  $("#home").delay(1000).slideDown(1200, "easeOutBounce").addClass('active');
  } else {
	  if (location.hash=="#!linus"||location.hash=="#!matthias")
	  {
		  location.hash="#!ueber-uns";
	  }
	  $("#home").show().addClass("active");
  }
  $(window).hashchange();
});

function showPage(page)
{
	//Kontaktform resetten
	if (page==="kontakt")
	{
		$("#contactSubmit").removeAttr("disabled");
	    $("#contactSubmit").attr("value", "Absenden");
		$("#contactFormDiv").show();
		$("#thankYouDiv").hide();
	}
	
	if (!$("#"+page).is('.active'))
	{		
		$(".active").slideUp({duration:800,easing:"easeInCubic",complete: function() {
			$(".content").css({width:'660',left:'160px'})
			$("body").attr("id",page+"-active");
			$("#"+page).slideDown(1200, "easeOutBounce", function() {
				$(".primary").fadeIn("fast");
			}).addClass('active');
		}}).removeClass('active');
	} else {
		if ($("#"+page).is('.resized'))
		{
			$(".secondary").fadeOut(100).removeClass('active');
			$(".content").animate({width:'660',left:'160px'}).removeClass("resized");
			$("#about-content").fadeIn(800);
		}
	}
	try
	{
		var pageTracker=_gat._getTracker("UA-324854-4");
		pageTracker._trackEvent("showPage", page);
	} catch(e)
	{
		void(0);
	}
}

function matthias()
{
	$("#about-content").fadeOut("fast");
	$("#ueber-uns").animate({width:'430px',left:'40px'}, function() { $("#about-content:visible").fadeOut("fast"); }).addClass("resized");;
	$("#matthias").fadeIn(800).addClass('active');
}

function linus()
{
	$("#about-content").fadeOut("fast");
	$("#ueber-uns").animate({width:'430px',left:'510px'}, function() { $("#about-content:visible").fadeOut("fast"); }).addClass("resized");;
	$("#linus").fadeIn(800).addClass('active');
}

function validateForm(formData, jqForm, options)
{
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('Bitte alle Felder ausfüllen!'); 
            return false; 
        } 
    }
	$("#contactSubmit").attr("disabled", "true");
	$("#contactSubmit").attr("value", "sendet...");
}

function UnCryptMailto(s)
{var n=0;var r="";for(var i=0;i<s.length;i++)
{n=s.charCodeAt(i);if(n>=8364)
{n=128;}
r+=String.fromCharCode(n-1);}
return r;}
function linkTo_UnCryptMailto(s)
{location.href=UnCryptMailto(s);}
