$(function() {
	
	var lang = "en";
	
	$("#newsletterform").submit(function(e) {
		
		var emailRegex = /[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/;
		
		var email = $("#emailfield").val();
		var language = $("#selectedlanguage").val();
		
		if (email.search(emailRegex) != -1) {
			
			if (language != "language") {
				
				var requestUrl = "newsletter.php"
				var requestData = "email=" + email + "&lng=" + language;
				
				$.post(requestUrl, requestData, function(requestReply){
					if (requestReply == "ok") {
					
						if (lang == "en") {
							alert('Thanks! You will receive a confirmation e-mail shortly. Have a nice day and see you in Klaipėda! :)');
						} else {
							alert('Ačiū! Netrukus gausite patvirtinimo laišką. Geros dienos ir pasimatysime Klaipėdoje! :)');
						}
						
					} else {
					
						if (lang == "en") {
							alert("We are terribly sorry, but something went wrong. Please try again later.");
						} else {
							alert("Mes labai atsiprašome, tačiau įvyko kažkokia klaida. Pabandykite vėliau.");
						}
						
					}
				});
				
			} else {
				
				if (lang == "en") {
					alert("Please select your newsletter's language.");
				} else {
					alert("Prašome pasirinkti naujienlaiškio kalbą.");
				}
				
			}
			
		} else {
			
			if (lang == "en") {
				alert("Please check that you entered your e-email address correctly.");
			} else {
				alert("Prašome pasitikrinti, kad el.pašto adresas, kurį įvedėte, yra teisingas.");
			}
		}
		
		e.preventDefault();
		
	});
	
	
	$(".lng_lt").click(function(){
		$(".introtext").html('Deje renginį teko atidėti 2010 metams. Tačiau tai reiškia, kad turėsime pakankamai laiko išblizginti visas renginio smulkmenas. Pasimatysime 2010 Klaipėdoje! Puslapis startuos 2009 rugsėjo 1-ąją. O tuo tarpu kviečiame visus užsiprenumeruoti mūsų naujienlaiškį! Tokiu būdu būsite pirmieji sužinoję bet kokius gandus apie Summer Street Klaipėda.');
		$(".volunteertext").html('Turi minčių arba pasiūlymų? <a href=\"mailto:hello@summerstreet.info\">Pasidalink jais su mumis!</a> Tarp kitko, mes ieškome savanorių, dj\'ėjų, vedėjų, fotografų, vaizdo operatorių, remėjų, maisto ir nakvynės tiekėjų, ir, žinoma, žmonių, kurie dievina riedlenčių sportą ir vakarėlius!');
		if ($("#emailfield").val() == "Your e-mail"){ $("#emailfield").val("Jūsų el.paštas") }
		$("#selectedlanguage option[value='language']").html("Pasirinkite kalbą...");
		$("#selectedlanguage option[value='english']").html("Anglų").removeAttr("selected");
		$("#selectedlanguage option[value='lithuanian']").html("Lietuvių").attr("selected", "selected");
		$("#newslettersubmit").val("Registruotis!");
		$(".copy").html("&copy; 2008 - 2009 Summer Street Klaipėda. Susipažinkime <a href=\"mailto:hello@summerstreet.info\">hello@summerstreet.info!</a>");
		lang = "lt";
	});
	
	$(".lng_en").click(function(){
		$(".introtext").html("The event won't be held this year because we want to make it really shine. We will see you in 2010 in Klaipeda! The website will be launched 1st September, 2009. Meanwhile, you can sign-up for our newsletter and be the first one to hear all the latest rumours about Summer Street Klaipeda!");
		$(".volunteertext").html("Have any ideas? <a href=\"mailto:hello@summerstreet.info\">Share them with us!</a> By the way, we're on the look-out for volunteers, djs, hosts, photographers, filmers, sponsors, food and accommodation suppliers and, off course, people, who like skateboarding and parties!");
		if ($("#emailfield").val() == "Jūsų el.paštas"){ $("#emailfield").val("Your e-mail") }
		$("#selectedlanguage option[value='language']").html("Language...");
		$("#selectedlanguage option[value='english']").html("English").attr("selected", "selected");
		$("#selectedlanguage option[value='lithuanian']").html("Lithuanian").removeAttr("selected");
		$("#newslettersubmit").val("Sign me up!");
		$(".copy").html("&copy; 2008 - 2009 Summer Street Klaipėda. Say <a href=\"mailto:hello@summerstreet.info\">hello@summerstreet.info!</a>");
		lang = "en";
	});
	
	
	$("#emailfield").focus(function(){
		
		if (lang == "en") {
			if ( $(this).val() == "Your e-mail" ) { $(this).val(''); }
		} else {
			if ( $(this).val() == "Jūsų el.paštas" ) { $(this).val(''); }
		}
		
	});
	
	$("#emailfield").blur(function(){
		
		if (lang == "en") {
			if ( $(this).val() == "" ) { $(this).val('Your e-mail'); }
		} else {
			if ( $(this).val() == "" ) { $(this).val('Jūsų el.paštas'); }
		}
		
	});
	
});
