if($.browser.mozilla && navigator.userAgent.split("Firefox/")[1] == "3.6.3" && navigator.userAgent.toLowerCase().indexOf("ubuntu") == -1){
	var is_firefox_363 = true;
}else{
	var is_firefox_363 = false;
}
document.onmousemove = function(e) {
	var posx = 0;
	var posy = 0;
	if (!e)
		var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
	}
	window._POSX = posx;
	window._POSY = posy;
}
function mail_to_close(){
	$("#contactContainer").remove();
	$("#contactOverlay").remove();
}
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

if(is_firefox_363){
	var minus = 140;
}else{
	var minus = 30;
}
if($.browser.msie && $.browser.version == "6.0"){
	var plus = 50;
}else{
	var plus = 0;
}

function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function mail_to(email){
	$('<div id="contactContainer"></div>').appendTo("#contentLeft");
	$('<div id="contactOverlay"></div>').appendTo("#contentLeft");
	window.config = {
		width:400,
		height:406
	}
	window._EMAIL = email.split("|||").join("@");
	
	window.arrPageSizes = ___getPageSize();
	window.arrPageScroll = ___getPageScroll();

	
	$("#contactOverlay").css({
		"background":"#000",
		"opacity":0.7,
		"z-index":999999,
		"width":arrPageSizes[0],
		"height":arrPageSizes[1] + plus,
		"position":"absolute",
		"top":0,
		"left":0
	});
	
	$("#contactOverlay").click(function(){
		mail_to_close();
	});
	
	$("#contactContainer").css({
		"position":"absolute",
		"top":window._POSY+"px",
		"left":window._POSX+"px",
		"width":"0px",
		"height":"0px",
		"background":"#fff",
		"-moz-box-shadow":"0px 0px 20px #000",
		"box-shadow":"0px 0px 10px #000",
		"-webkit-box-shadow":"0px 0px 20px #000",
		"padding":"10px",
		"z-index":10000000
	});
	$("#contactContainer").html('<div id="contactLoading"><img src="images/ajax-loader.gif" /'+'></div>');
	$("#contactLoading").css({
		"display":"block",
		"text-align":"center",
		"margin-top":(config.height / 2 - 16) + "px"
	});
	$(window).resize(function() {
		window.arrPageSizes = ___getPageSize();
		window.arrPageScroll = ___getPageScroll();
		$("#contactContainer").animate({
			//top:  (((arrPageSizes[1] / 2) - (config.height / 2)) + arrPageScroll[1]) - minus,
			top: 150,
			left: (( arrPageSizes[0] / 2) - (config.width / 2))
		});
		
		$("#contactOverlay").css({
			"width":arrPageSizes[0],
			"height":arrPageSizes[1] + plus
		});
	});
	/*$(window).scroll(function(a,b,c){
		window.arrPageSizes = ___getPageSize();
		window.arrPageScroll = ___getPageScroll();
		
		$("#contactContainer").animate({
			//top:  (((arrPageSizes[1] / 2) - (config.height / 2)) + arrPageScroll[1]) - minus,
			top: 150,
			left: (( arrPageSizes[0] / 2) - (config.width / 2))
		});
	});*/	
	$("#contactContainer").animate({
		//top:  (((arrPageSizes[1] / 2) - (config.height / 2)) + arrPageScroll[1]) - minus,
		top: 150,
		left: (( arrPageSizes[0] / 2) - (config.width / 2)),
		"height":config.height,
		"width":config.width
	},300,function(){
		$.ajax({
			type: "POST",
			url: "contact_form",
			success: function(msg){
				window._CONTACT_FORM = msg.split("{email}").join(window._EMAIL);
				$("#contactContainer").html(window._CONTACT_FORM);
				
				window.arrPageSizes = ___getPageSize();
				$("#contactOverlay").css({
					"width":arrPageSizes[0],
					"height":arrPageSizes[1] + plus
				});
				
				$("#sendbtn div")
					.mouseenter(function(){
						$(this).parent().addClass("button-hover");
					})
					.mouseout(function(){
						$(this).parent().removeClass("button-hover");
						$(this).parent().removeClass("button-active");
					})
					.mousedown(function(){
						$(this).parent().addClass("button-active");
					})
					.mouseup(function(){
						$(this).parent().removeClass("button-active");
					});
			}
		});
	});
	
	
	return false;
}
function send_email(){
	var email = {
		from: $("#emailadress").val(),
		to: window._EMAIL,
		text: $("#feedback").val()
	}
	window._TEMP_EMAIL_DATA = email;
	var error = false;
	$("#emailadress").removeClass("error-field");
	$("#feedback").removeClass("error-field");
	
	if(!(/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/.test(email.from))){
		$("#emailadress").addClass("error-field");
		error = true;
	}
	if(email.text.length < 3){
		$("#feedback").addClass("error-field");
		error = true;
	}
	
	if(!error){
		$("#contactContainer").html('<div id="contactLoading"><img src="images/ajax-loader.gif" /'+'></div>');
		$("#contactLoading").css({
			"display":"block",
			"text-align":"center",
			"margin-top":(config.height / 2 - 16) + "px"
		});
		$.ajax({
			type: "POST",
			url: "send_mail",
			data:({from:email.from,to:email.to,text:email.text}),
			success: function(msg){
				var json = eval(msg)[0];
				if(json.success){
					$("#contactContainer").html('<div id="contactMessage">Feedback abgesendet!</div>');
					$("#contactMessage").css({
						"display":"block",
						"text-align":"center",
						"font-weight":"bold",
						"margin-top":(config.height / 2 - 16) + "px"
					});
					window.setTimeout(function(){
						
					$("#contactContainer").animate({
							"opacity":0
						},500,function(){
							mail_to_close()
						});
					},2000);
				}else{
					$("#contactContainer").html(window._CONTACT_FORM);
					$("#emailadress").val(window._TEMP_EMAIL_DATA.from);
					$("#feedback").val(window._TEMP_EMAIL_DATA.text);
					for(var i = 0; i < json.error.length; i++){
						if(json.error[i] == "from"){
							$("#emailadress").addClass("error-field");
						}
						if(json.error[i] == "text"){
							$("#feedback").addClass("error-field");
						}
					}
				}
			}
		});
	}
}

