if (typeof SvD == 'undefined') {
	var SvD = {};
}

if (typeof jQuery != 'undefined') {

	/*** Väder-funktioner ***/
	SvD.weather = function ($) {

		var container;

		return  {

			baseUrl : "",

			init : function () {
				container = $("#weather");
				if (container.length === 0) { return; }
				this.hideWeather();
				this.weather();
			},

			hideWeather : function() {
				container.hide();
			},

			weather : function () {
				var weatherPath = this.baseUrl;
				var showAdminlink = false;
				var COOKIE_NAME = "weatherStation";
				var weatherStation = $.cookie(COOKIE_NAME);
				var adminLink="<li class=\"weatherAdmin\"><a class=\"readmore\" href=\"\">Välj annan ort</a></li>";
				var errorMsg = "<p class=\"headline\">Ett fel uppstod</p>";

				if (weatherStation != null) {
					$.ajax({
						type: "GET",
						url: weatherPath,
						data: ({weatherStation : unescape(weatherStation), service:"ajax", component:"weather"}),
						success: function(html){
							if (html.length > 0 && html.indexOf("<head>") < 0) {
								container.html(html);
								showAdminlink = true;
							}
						},
						complete: function() {
							if(showAdminlink == true) {
								container.find("ul li:first").before(adminLink);
							}
							container.show();
						}
					});
				} else {
					container.find("ul li:first").before(adminLink);
					container.show();
				}

				container.find(".weatherAdmin").live("click", (function() {
					$.ajax({
						type: "GET",
						url: weatherPath,
						data: ({weatherPresentation:"admin", service:"ajax", component:"weather"}),
						success: function(html){
							if(html.length > 0 && html.indexOf("<head>") < 0) {
								container.html(html);
							}
						},
						error: function(html){
							container.html(errorMsg);
						},
						complete: function() {
							container.show();
						}
					});
					return false;
				}));

				$("#weatherRegionSubmit").live("click", (function() {
					var weatherRegion = $("#weatherRegion").val();
					$.ajax({
						type: "GET",
						url: weatherPath,
						data: ({weatherRegion:weatherRegion, weatherPresentation:"admin", service:"ajax", component:"weather"}),
						success: function(html){
							if(html.length > 0 && html.indexOf("<head>") < 0) {
								container.html(html);
							}
						},
						error: function(html){
							container.html(errorMsg);
						},
						complete: function() {
							container.show();
						}
					});
					return false;
				}));

				$("#weatherStationSubmit").live("click" ,(function() {
		            var options = { path: '/', expires: 1000 };
		            var weatherStation = $("#weatherStation").val();
		            $.cookie(COOKIE_NAME, escape(weatherStation), options);
		            $.ajax({
						type: "GET",
						url: weatherPath,
						data: ({weatherStation:weatherStation, service:"ajax", component:"weather"}),
						success: function(html){
			            	if(html.length > 0 && html.indexOf("<head>") < 0) {
								container.html(html);
								showAdminlink = true;
							}
						},
						error: function(html) {
							container.html(errorMsg);
						},
						complete: function() {
							if(showAdminlink == true) {
								container.find("ul li:first").before(adminLink);
							}
							container.show();
						}
					});
		 			return false;
				}));
			}
		};
	}(jQuery);
}