var formactivated = 0;
var formsent = 0;
var textfocus = false;
var boxes = new Array();
$(document).ready(function(){
	$("#ref1").hover(
    function () { $("#refwrap1").show(); }, 
    function () { $("#refwrap1").hide(); }
	);
	
	// wird für die "Referenzen"-Seite gebraucht.
	$("#ref2").hover(
    function () { $("#refwrap2").show(); }, 
    function () { $("#refwrap2").hide(); }
	);
	
	$("#refwrap1").hover(
    function () { $("#refwrap1").show(); }, 
    function () { $("#refwrap1").hide(); }
	)
	
	$("#refwrap2").hover(
    function () { $("#refwrap2").show(); }, 
    function () { $("#refwrap2").hide(); }
	)
	
	$("#kontakt").click(function() {
		$("#kontakt").hide();
		$("#email").attr("value","");
		$("#emailtext").show();
		$("#message").attr("value","");
		$("#messagetext").show();
		$("#telefon").attr("value","");
		$("#telefontext").show();
		$('#kontaktoffen').animate({
		top: "0px"
		},{
			duration: 700,
			easing:"easeOutBounce",
			complete: function() { $("#allwrapper").show(); }
		});
	});
	
	$(".onclickclose").mousedown(function() {
		reactivateForm();
	});
	
	$("#email").focus(function() {
		$("#email").css({backgroundColor:"#FFE700"});
		$("#emailtext").hide();
		checkfields("email");	
		$('#emailmsg').animate({
				top: "-51px",
				duration: 300
			}
		);
	});
	
	$("#emailtext").click(function() {
		$("#emailtext").hide();
		$("#email").focus();
		checkfields("email");
	});
	$("#telefon").focus(function() {
		$("#telefontext").hide();
		$("#telefon").css({backgroundColor:"#FFE700"});
		checkfields("telefon");
	});
	$("#telefontext").click(function() {
		$("#telefontext").hide();
		$("#telefon").focus();
		checkfields("telefon");
	});
	$("#message").focus(function() {
		textfocus = true;
		$("#messagetext").hide();
		checkfields("message");
		$("#message").css({backgroundColor:"#FFE700"});
		$('#msgmsg').animate({
			top: "-51px",
			duration: 300
		});	
	});
	$("#message").blur(function() {
		textfocus = false;
	});
	$("#messagetext").click(function() {
		$("#messagetext").hide();
		$("#message").focus();
		checkfields("message");
	});
	
	$("#abschicken_inactive").click(
    function () {
			checkform();
		}
	);
	
  $(document).keyup(function(event){
		if(event.keyCode == 13) {
			if($('#kontaktoffen').css("top") == "0px" && textfocus == false) { checkform(); }
		}
  });
	
	var thisbox = Math.round(Math.random() * (boxes.length-1));
	$("#bigtextbox_"+boxes[thisbox]).show();
	$("#smalltextbox_"+boxes[thisbox]).hide();
	
	for(i=0;i<boxes.length;i++) {
		if(i != thisbox) {
			$("#bigtextbox_"+boxes[i]).hide();
			$("#smalltextbox_"+boxes[i]).show();	
		}
	}
	setSizes();
	checkload();
});


function checkload() {
var param = window.location.search;
var _gp = param.replace("?id=","");
if(_gp.length == 0 || _gp == 1 || _gp == 7) { $(window).load(function() { $("#preloader").fadeOut("fast"); }); }
else { $("#preloader").hide();}
}

window.onresize = setSizes;

function addtosbox(num) {
	boxes.push(num);
}

function checkform() {
	var email = $("#email").attr("value");
	var message = $("#message").val();
			
	// Email-Syntax-Check
	var a   = false;
	var res = false;
	if(typeof(RegExp) == 'function') {
		var b = new RegExp('abc');
		if(b.test('abc') == true){a = true;}
	}
	if(a == true) {
		reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-Z0-9\\-\\.]+)(\\.)([a-zA-Z]{2,4})$');
		res = (reg.test(email));
	}
	else { res = (email.search('@') >= 1 && email.lastIndexOf('.') > email.search('@') && email.lastIndexOf('.') >= email.length-5) }
			
	if(email.length == 0 || res == false) {
		$('#emailmsg').animate({
			top: "1px"
		},{
			duration: 400,
			easing:"easeOutBounce",
			complete: function() { checkfields("email");$("#email").css({backgroundColor:"#FFE700"}); }
		});
	}
	else if(message.length == 0) {
		checkfields("message");
		$('#msgmsg').animate({
			top: "60px"
		},{
			duration: 400,
			easing:"easeOutBounce",
			complete: function() {
				checkfields("message");$("#message").css({backgroundColor:"#FFE700"});
			}
		});
	}
	else {
		formactivated = 1;
		$("#email").css({backgroundColor:"#FFFFFF"});
		$("#telefon").css({backgroundColor:"#FFFFFF"});
		$("#message").css({backgroundColor:"#FFFFFF"});
		$("#abschicken_inactive").attr("src",$("#abschicken_sent").attr("src"));
		//$(this).attr("src","fileadmin/templates/images/abschicken_sent.png");
		plane_animate();
	}
}

function reactivateForm() {
	if(formactivated == 0) {
		$("#emailmsg").css({top:"-51px"});
		$("#email").css({border:"none"});
		$("#msgmsg").css({top:"-51px"});
		$("#message").css({border:"none"});
		$('#kontaktoffen').animate({
			top: "-260px"
		},{
			duration: 500,
			complete: function() {
				$("#kontakt").show();
				$("#allwrapper").hide();
				$("#email").attr("value","");
				$("#email").css({backgroundColor:"#FFFFFF"});
				$("#telefon").css({backgroundColor:"#FFFFFF"});
				$("#message").css({backgroundColor:"#FFFFFF"});
				$("#emailtext").show();
				$("#telefon").attr("value","");
				$("#telefontext").show();
				$("#message").attr("value","");
				$("#messagetext").show();
				$("#kontaktsent").hide();
				$("#abschicken_inactive").show();
					counter = 0;
					ps = 0;
			}
		});
	}
	$(window).focus();
}

function setSizes() {
	var winheight = $(window).height();
	var winwidth = $(window).width();
	
	if(winwidth <= 742 ) { $("#cloudbg").css("left","433px"); }
	else { $("#cloudbg").css("left","50%"); }
	
	/*
	var cpos = $("#cloudbg").position();
	if(cpos.left <= 432) {
		$("#cloudbg").css("left","433px");
	}
	*/
	
	$("#contentwrapper").css("height",winheight+"px");
	$("#maincontent").css("height",(winheight-(parseInt($("#maincontent").css("top"))))+"px");
	if(winheight < 768) {
		$("#referenzen1").css("height",(winheight-(parseInt($("#refwrap1").css("top"))+170))+"px");
		$("#referenzen2").css("height",(winheight-(parseInt($("#refwrap1").css("top"))+170))+"px");
	}
	$("#cloud").css({height:($(window).height())+3+"px"});
	$("#cloud").css({width:($(window).width())+3+"px"});
}

function checkfields(field) {
	var fields = new Array("email","telefon","message");
	for(i=0;i<fields.length;i++) {
		if(fields[i] != field) {
			$("#"+fields[i]).css({backgroundColor:"#FFFFFF"});
			if($("#"+fields[i]).attr("value").length == 0) {
				$("#"+fields[i]+"text").show();
			}
		}
	}
}

var xint = 0;
var ps = 1;
var counter = 0;
function plane_animate() {
	$("#plane_images").show();
	var message = $("#message").attr("value");
	message = message.replace(/\n/g,"%%%%%");
  xint = setInterval("plane_animate2()",50);
	var asave = '{"forms":[';
	asave =	asave+"{\"email\":\""+$("#email").attr("value")+"\",\"telefon\":\""+$("#telefon").attr("value")+"\",\"nachricht\":\""+message+"\"},";
	asave = asave + "]}";
	asave = asave.replace(/\}\,\]\}/,"}]}");
	$.post("index.php?id=6&eID=user_mailsend&send=1", {xsave:asave}, function(data) { ; });
}

var hks = 0;
function plane_animate2() {
	$("#fl"+ps).hide();
	ps++;
	counter++;
	$("#fl"+ps).show();
	if(ps == 30) {
		$("#fl"+ps).hide();
		ps = 1;
		$("#fl"+ps).show();
		$("#plane_images").css({top:(parseInt($("#plane_images").css("top"))-8)+"px"})
	}
	if(counter == 45) {
		clearInterval(xint);
		xint = 0;
		$("#plane_images").fadeOut(200, function() {
			$("#kontaktoffen").css({top:"-260px"});
			$("#kontaktsent").show();
			$("#abschicken_inactive").show();
			$("#abschicken_inactive").attr("src","fileadmin/templates/images/abschicken_inactive.png");
			$("#abschicken_sent").hide();
			$("#allwrapper").hide();
			hks = setTimeout('hideksent()',4000);
			formactivated = 0;
			counter = 0;
			ps = 0;
			for(i=2;i<32;i++) { $("#fl"+i).hide(); }
		});
	}
}

function hideksent() {
	clearTimeout(hks);
	hks = 0;
	reactivateForm();
}

jQuery(document).ready(function() {
   $().mousemove(function(e){
      window.mouseXPos = e.pageX;
      window.mouseYPos = e.pageY;
   }); 
});

