(function($) {
	var cookie_path_regex = /^(.*\/\/[^\/]*)?(\/(.*\/)?)[^\/]*$/;
	
	$.fn.weatherUpdate = function(weather_url) {
		return this.each(function() {
			weatherUpdate.apply(this, [weather_url]);
		});
	};
	
	function get_cookie_path(url) {
		var m = url.match(cookie_path_regex);
		return m ? m[2] : "/";
	}
	
	function weatherUpdate(weather_url) {
		var cookie_name = "weather_city_code_v2";
		var cookie_path = get_cookie_path(weather_url);
		
		var cidade = $("h2", this);
		var icones = $(".icones img, .icones-dta img", this);
		var legendas = $(".legenda, .legenda-dta", this);
		var loading = icones.attr("src");
		
		var dialog = $(".dialog", this);
		var dialogSelection = $("select", dialog);
		
		var cityCode = $("option:gt(0)", dialogSelection)
			.filter(function(i) {
				return i == 0 || this.selected;
			})
			.filter(":last")
			.attr("value");
		
		if (cityCode != $.cookie(cookie_name)) {
			cidade.text($("option:gt(0)", dialogSelection).filter(function() {
				return this.value == cityCode;
			}).text());
		}
		
		function loaded(dom) {
			var n = icones.size();
			
			$("GetWeatherForecastResult", dom).each(function() {
				if ($("> CityCode", this).text() == cityCode) {
					var i = 0;
					$("> Days > Day", this).each(function() {
						var icon = icones.eq(i), legenda = legendas.eq(i);
						var code = $("> WeatherCode", this).text();
						var img = "http://js.sapo.pt/Assets/Images/Meteo/" + code + ".png";
						icon.attr("src", img);
						icon.attr("title", $("> Description", this).text());
						legenda.text($("> Name", this).text().replace("*", "\xAA"));
						$([icon,legenda]).css("visibility", "visible");
						++i;
					});
					
					for (; i < n; ++i) {
						$([icones[i], legendas[i]]).css("visibility", "hidden");
					}
				}
			});
		}
		
		function getWeather(code) {
			cityCode = code;
			$.cookie(cookie_name, code, { expires: 2, path: cookie_path });
			$.get(weather_url, {cc: code}, loaded, "xml");
		}
		
		getWeather(cityCode);
		
		$("a:eq(1)", dialog).click(function() {
			if (dialogSelection.val() != "") {
				cidade.text($("option[selected]", dialogSelection).text());
				icones.attr("src", loading).attr("title", "");
				legendas.html("&nbsp;");
				getWeather(dialogSelection.val());
			}
			$.unblockUI({onUnblock: showObjects});
			return false;
		});
		
		$("a:eq(0)", dialog).click(function() {
			$.unblockUI({onUnblock: showObjects});
			return false;
		});
		
		$(".info a", this).click(function() {
			if (cityCode) {
				dialogSelection.val(cityCode);
				hideObjects();
				$.blockUI({
					message: dialog,
					css: {
						width: "350px",
						cursor: "default"
					}
				});
			}
			return false;
		});
		
		var hiddenObjects;
		
		function showObjects() {
			hiddenObjects.css("visibility", "visible");
		}
		
		// small hack to hide flash objects created by 3rd party javascript code
		// that does not set "wmode" = "transparent" so divs can be displayed on
		// top of flash objects
		function hideObjects() {
			var overlays = [
				// publicity iframe
				".pub iframe:visible",
				// other possible flash objects
				"object[type=application/x-shockwave-flash]:visible"
			];
			hiddenObjects = $(overlays.join(",")).css("visibility", "hidden");
		}
	};
})(jQuery);