﻿//$(document).ready(function () {
//	$("body").translate("de");
//});
function translatePage(languageCode) {
	var indicator = $("#languages .translatingIndicator");

	switch (languageCode)
	{
		case "sv": indicator.text("\326vers\344tter..."); break;
		case "en": indicator.text("Translating..."); break;
		case "de": indicator.text("\334bersetzen..."); break;
		case "fr": indicator.text("Traduire..."); break;
	}
	if (languageCode == "fr")
		$(".tr_buffers").hide();
//	else if (languageCode == "sv")
//		$(".fr2000_cert, .about_mixOfPersonnel").hide();

	$("body").translate(languageCode, {
		from: "sv",
		toggle: true,
		not: "#languages, .notr",
		start: function () {
			$("#languages .translatingIndicator").show();


		},
		complete: function () {
			//			console.log("Complete");
			$("#languages .translatingIndicator").hide();
			if (languageCode == "fr")
			{
				$(".tr_buffers").text("amortisseurs");
				$(".tr_buffers").show();
			}
//			else if (languageCode == "sv")
//			{
//				$(".fr2000_cert").html("Vi jobbar med att bli certifierade mot<br />FR 2000 under 2011");
//				$(".about_mixOfPersonnel").html("Vår mix av personal gör att vi för kunskapen vidare från generation till generation.");
//				$(".fr2000_cert, .about_mixOfPersonnel").show();
//			}
		},
		error: function (response) {
			console.log("Error " + response.code + ": " + response.message);
			$("#languages .translatingIndicator").hide();
		},
		onTimeout: function () {
			console.log("Timeout");
			$("#languages .translatingIndicator").hide();
		}
	});
}

$(document).ready(function () {
	$("#languages ul li").click(function () {
		var t = $(this);
		//		setCookie("language", t.data().lang, 365);

		//		translatePage(t.data().lang);
		ws_default.setLanguage({ language: t.data().id }, function () {
//			setTimeout(function () {
				window.location.reload();
//			});
		})
	});


	$("#footer #submitContact").click(function (e) {
		var t = $(this);
		e.preventDefault();

		var name = $("#nameInput").val().trim();
		var email = $("#emailInput").val().trim();
		var phone = $("#phoneInput").val().trim();

		if (name == "" || email == "" || phone == "")
		{
			alert($("#missingFieldsText").text());
			return;
		}
		else
		{
			if (t.data().busy) return;
			t.data("busy", true).fadeTo(200, 0.5).css({ cursor: "default" });
			$(".contactAjaxProgress").fadeIn(200);
			$.get("handler.ashx?do=contact&name=" + encodeURIComponent(name) + "&email=" + encodeURIComponent(email) + "&phone=" + encodeURIComponent(phone), function (data) {
				setTimeout(function () {
					t.data("busy", false).fadeTo(200, 1).css({ cursor: "" });
					$(".contactAjaxProgress").fadeOut(200, function () {
						alert($("#contactSuccessfull").text());
						$("#nameInput").val("");
						$("#emailInput").val("");
						$("#phoneInput").val("");
						$("#footer .one-fourth.last ul li p").show();
					});
				}, 500);
				if (data == "1")
					console.log("nice");
				else
					console.log(data);
			});
		}
	});

	$("#footer .one-fourth.last ul li p").live("click", function (e) {
		$(this).hide();
		$(this).next().focus();
	}).live("mousedown", function (e) {
		$(this).hide();
		$(this).next().focus();
	});

	$("#footer .one-fourth.last ul li input[type=text]").val("").live("focus keydown", function (e) {
		$(this).prev("p").hide();
	}).live("blur", function (e) {
		if ($(this).val() == "")
			$(this).prev("p").show();
		else
			$(this).prev("p").hide();
	});
});


// Will be executed when the Language API is loaded
//$.translate.ready(function () {
////	cookieLanguage = getCookie("language");

////	if (cookieLanguage != null && cookieLanguage != "" && cookieLanguage != "sv")
////	{
////		translatePage(cookieLanguage);
////	}
//});




function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
	document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++)
	{
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
		x = x.replace(/^\s+|\s+$/g, "");
		if (x == c_name)
		{
			return unescape(y);
		}
	}
}
