var WeatherBlock=WeatherBlock?WeatherBlock:(function()
{
	var sLang;
	var sHost;
	var jsonCache=null;
	var sResource="";
	var oHint;
	var fInitOptions=function (sId,json)
	{
		$("#weather-menu").html(json["menu"]);
		var i;
		var fFillOptions=function(a,sClass)
		{
			switch (sLang)
			{
				case "rus":
					var sOption = "<option value=\"0\">Выберите город</option>";
					break;
				case "kaz":
					var sOption = "<option value=\"0\">Қаланы таңдау</option>";
					break;
			}
			var bResult=false;
			for(i=0;i<a.length;++i)
			{
				sOption+="<option value=\""+a[i][0]+"\""+(a[i][3]?" selected=\"selected\"":"")+">"+a[i][2]+"</option>";
				bResult=bResult || a[i][3];
			}
			$("form#"+sId+"Form select."+sClass).each(function()
			{
				$(this).html(sOption);
				if(!bResult)
					this.setAttribute("disabled","disabled");
			});
			return bResult;
		};
		var bSelected1=fFillOptions(json.a,"wCity1");
		var bSelected2=fFillOptions(json.b,"wCity2");
		if(!(bSelected1 || bSelected2))
			bSelected1=true;
		if (bSelected1)
		{
			$("input#"+sId+"country1").each(function()
			{
				this.setAttribute("checked","checked");
			});
			fSelectHide(sId,2);
			fSelectShow(sId,1);
		}
		else if (bSelected2)
		{
			$("input#"+sId+"country2").each(function()
			{
				this.setAttribute("checked","checked");
			});
			fSelectHide(sId,1);
			fSelectShow(sId,2);
		}
		$("input#"+sId+"country1").add("label[for=input#"+sId+"country1]").bind("click",function()
		{
			fSelectHide(sId,2);
			fSelectShow(sId,1);
		});
		$("input#"+sId+"country2").add("label[for=input#"+sId+"country2]").bind("click",function()
		{
			fSelectHide(sId,1);
			fSelectShow(sId,2);
		});
		$("input#"+sId+"OK").bind("click",function()
		{
			var oForm=$("form#"+sId+"Form").get(0);
			var iSelect=fGetRadioValue(oForm["weather"]);
			var iCity=oForm["wCity"+iSelect].value;
			if(iCity==0)
			{
				alert("Выберите город");
				return false;
			}
			$.getJSON("http://weather.nur.kz/set_city.php?uuid="+new Date().getTime()+"&jsoncallback=?",{
				place: sId,
				city: iCity
			},function (json)
			{
				if(!sHost)
					sHost="";
				setTimeout(function()
				{
					$.getJSON(sHost+"informer/weather/kz3/"+sLang+"/"+sResource+iCity+".js?jsoncallback=?",function(json, sStatus)
					{
						if(json)
						{
							$("#afisha-menu").remove();
							$("#weather-menu").after(json["menu"]).remove();
							$("#weather").html(json["content"]);
							oHint=$("#weather");
						}
						oHint.removeData("isEdited")
					});
				},1);
			});
			return false;
		});
	}
	var fGetRadioValue=function (el)
	{
		if(!el) return "";
		var radioLength=el.length;
		if(radioLength == undefined)
			if(el.checked)
				return el.value;
		else return "";
		for (var i = 0; i < radioLength; ++i)
			if(el[i].checked)
				return el[i].value;
		return "";
	}
	var fSelectShow=function(sId, iId)
	{
		$("input#"+sId+"country"+iId).attr("checked","checked");
		$("form#"+sId+"Form select.wCity"+iId).removeAttr("disabled");
		$("form#"+sId+"Form select.wCity"+iId).show();
	}
	var fSelectHide=function(sId, iId)
	{
		$("input#"+sId+"country"+iId).removeAttr("checked");
		$("form#"+sId+"Form select.wCity"+iId).attr("disabled","disabled");
		$("form#"+sId+"Form select.wCity"+iId).hide();
	}
	return {
		"init": function (sLang1,sHost1,sResource1)
		{
			sLang=sLang1;
			sHost=sHost1;
			sResource=sResource1?(sResource1+"/"):"";
			oHint=$("#weather");
			$("a#weatherPlace3.edit").each(function ()
			{
				var sId=this.getAttribute("id")+"Settings";
				$("div.weather-settings").hide();
				$(this).click(function()
				{
					if(jsonCache)
					{
						fInitOptions(sId,jsonCache);
						$("div.weather-settings").toggle();
						$("div.weather div.name").toggle();
						$("div.weather-box div.name").toggle();
					}
					else
						$.getJSON("http://weather.nur.kz/cl.php?jsoncallback=?",{
							place: sId,
							city: $("form#"+sId+"Form input[name=city]").val()
						},function (json)
						{
							jsonCache=json;
							fInitOptions(sId,jsonCache);
							$("div.weather-settings").toggle();
							$("div.weather div.name").toggle();
							$("div.weather-box div.name").toggle();
						});
					oHint.data("isEdited",true);
					return false;
				});
			});
			$("#weather-menu").mouseenter(function (e)
			{
				var bIsHinted=$(this).data("isHinted")?true:false;
				$(this).data("isHinted",true);
				var sClass=""
				var iTID=oHint.data("iTID");
				if(iTID)
					clearTimeout(iTID);
				oHint.removeData("iTID");
				if(!bIsHinted)
				{
					var offset1=$("#wrapper").offset();
					var offset2=$(this).offset();
					oHint.css({
						"left": Math.min(offset2.left-offset1.left,1000-270),
						"top": offset2.top-offset1.top-4
					}).show();
				}
			}).mouseleave(function ()
			{
				var oA=$(this);
				if(oA.data("isHinted"))
				{
					oHint.mouseenter(function()
					{
						var iTID=$(this).data("iTID");
						if(iTID)
							clearTimeout(iTID);
						$(this).removeData("iTID");
					});
					oHint.mouseleave(function()
					{
						if(!oHint.data("isEdited"))
						{
							$(this).hide();
							oHint.unbind("mouseenter");
							oHint.unbind("mouseleave");
						}
					});
					oHint.data("iTID",setTimeout(function ()
					{
						oHint.hide();
						oHint.unbind("mouseenter");
						oHint.unbind("mouseleave");
						oHint.removeData("iTID");
					},800));
					oA.data("isHinted",false);
				}
			});
			$("div.weather-settings a.undo").click(function()
			{
				oHint.removeData("isEdited");
				$($(this).parents("div.weather-settings").get(0)).toggle();
				$(this).parents("div.weather").find("div.name").toggle();
				$(this).parents("div.weather-box").find("div.name").toggle();
				return false;
			});
		}
	};
})();



