﻿var Norrbotten = {
	
	loadWeatherData: function(target) 
	{
		
		$.ajax({
			type: "GET",
			url: "/vader/observationer.xml",
			success: Norrbotten.parseWeatherData
		});
		
	},
	
	parseWeatherData: function(xml, status)
	{
	
		ul = $("<ul />");
		
		// Get the observations
		$(xml).find("PRODUCTELEMENT[TYPE='Observation']").each(function(i, item) {
			
			obs = new Object();
			
			obs.city = $(item).attr("ELEMENTID");
			obs.temp = $($(item).find("PARAMETER[CONTENT-DESCRIPTION='Temperatur']").get(0)).text();
			obs.wind = $($(item).find("PARAMETER[CONTENT-DESCRIPTION='Vindstyrka']").get(0)).text();
			obs.icon = $($(item).find("PARAMETER[CONTENT-DESCRIPTION='Vädersymbol'][UNIT='String']").get(0)).text();

			ul.append(Norrbotten.renderWeatherData(obs));
			
		});
		
		$("#weatherData").append(ul);
		
	},
	
	renderWeatherData: function(obj)
	{
		
		
		var baseUrl = "/wp-content/themes/norrbotten-se/images/icons/";
		
		if(obj.icon == "Snöfall.")
			obj.icon = "Snofall.";
		
		return $("<li />")
				.addClass("weatherItem")
				.html("<img src='" + baseUrl + obj.icon + "png' alt='" + obj.icon + "' /> " + obj.city + ", " + obj.temp + "&deg;");
	
	}
	
}

function isEmail(str) 
{
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(str);
} 

function subscribe2Newsletter(event)
{		
	//console.log($("#newsletterSignupEmail").val());
	
	$("#newsletterSignupForm").fadeOut("slow", function()
	{			
		setTimeout(function()
		{                   																					
					
			// valid
			if(isEmail( $("#newsletterSignupEmail").val() ))
			{
				$("#subscription_validationmsg").empty();
				$("#subscription_formsentmsg").empty();
				$("<h4/>").text("Tack! Din epostadress är nu registrerad.").appendTo("#subscription_formsentmsg");
				
				// register
				$("#subscription_validationmsg").load("http://norrbotten.se/wp-content/themes/norrbotten-se/register-email.php?email=" + $("#newsletterSignupEmail").val() ); 
			}
			// not valid
			else
			{
				$("#subscription_formsentmsg").empty(); 
				$("#subscription_validationmsg").empty();
				
				$("<h4/>").text("Felaktig epostadress.").appendTo("#subscription_validationmsg");
				// activate form
				
				$("#newsletterSignupEmail").removeAttr("disabled");
			}
				                      
		        
			$("#subscription_formsentmsg").fadeIn("slow", function()
			{
				setTimeout(function()
				{                   
					$("#subscription_formsentmsg").fadeOut("slow");
				}, 2000);
				
				setTimeout(function()
				{                   
					$("#newsletterSignupForm").fadeIn("slow");
				}, 2000);						
			});
			
			$("#subscription_validationmsg").fadeIn("slow", function()
			{
				setTimeout(function()
				{                   
					$("#subscription_validationmsg").fadeOut("slow");
				}, 2000);
				
				$("#newsletterSignupEmail").removeAttr("disabled");
			});  
				   										 
			
			return false;									
		
		
		}, 1000);
	});
					
}


$(document).ready(function() 
{

	if ($(".current-cat").length == 0)
	{
		var id = $("body").attr("id");
	
		if (id)
		{
			ids = id.substring(3).split("-");

			for (var i=0; i<ids.length; i++)
			{
				$("#nav li.cat-item-" + ids[i]).addClass("current-cat");
			}
		}
		else
			$("#nav li:first").addClass("current-cat");
			
		
		// Register emailadress for newsletter 
		$("#newsletterSignUpBtn").bind("click", subscribe2Newsletter);
		
	}

	Norrbotten.loadWeatherData();			
});