﻿
function BurjaInit(cfg)
{
	var div1 = document.createElement("div");
	
//A  div1.innerHTML="<H2 class='moduletitle RMapControl'>"+GetDictValue("burTitle", cfg.lng)+"</H2>" + GetDictValue("burRegionalka", cfg.lng)+ "<br/><br/>";
  div1.innerHTML="<H2 class='moduletitle RMapControl'>"+GetDictValue("burTitle", cfg.lng)+"</H2><br/>";

  document.getElementById(cfg.burCnt).parentNode.insertBefore(div1,document.getElementById(cfg.burCnt));
	var div2 = document.createElement("div");
  div2.innerHTML="<br/><a href='"+appSettings.burjaListPage+"'>"+GetDictValue("burListLink", cfg.lng)+"</a>";
  document.getElementById(cfg.burCnt).parentNode.appendChild(div2);
	document.getElementById(cfg.burCnt).innerHTML="<div style='text-align:center'><img src='"+appSettings.iconsRepository+appSettings.loadingImg+"'/></div>";
	window.burCfg=cfg;
	window.burCfg.n=0;
	window.burCfg.loop=setInterval(BurjaLoad, 61000*3);
	BurjaLoad();
}

function BurjaLoad()
{
	window.burCfg.maxBurja=null;
	var url=appSettings.proxyUrl + "?method=GET&rproxytype=json&remoteUrl=" + escape(appSettings.burjaZnakiUrl);
	ServerRequest(url,BurjaLoadMerilniki);
}

function BurjaLoadMerilniki(result)
{
	var obj=GetEval(result);
	if(obj==null && result!=null && window.RMap!=null)
		obj=GetEval(getDStr(result));
	if(obj!=null && obj.feed!=null && obj.feed.entry!=null && obj.feed.entry.length>0)
		window.burCfg.maxBurja=obj.feed.entry[0].dispveter;
	var url=appSettings.proxyUrl + "?method=GET&rproxytype=json&remoteUrl=" + escape(appSettings.burjaUrl);
	ServerRequest(url,BurjaLoaded);
}

function BurjaLoaded(result)
{
	var windLimit=80;
	var obj=GetEval(result);
	if(obj==null && result!=null && window.RMap!=null)
		obj=GetEval(getDStr(result));
	if(obj!=null && obj.feed!=null && obj.feed.entry!=null)
	{
		var v=0;
		var locs="<br/>";
		for(var f=0;f<obj.feed.entry.length;f++)
		{
			var loc=obj.feed.entry[f];
			if(loc["veter"]>v)
				v=loc["veter"];
			if(loc["veter"]>=windLimit)
			{
				var locStr=loc["title"] + ": " + loc["veter"] + " km/h";
				if(burCfg.doLinks==true)
					locs+="<br/><a href='' onclick='BurjaDoLink(&quot;"+loc["id"]+"&quot;,"+loc["LATITUDE"]+","+loc["LONGITUDE"]+",&quot;"+locStr+"&quot;);return false;'>"+loc["title"] + "</a>: " + loc["veter"] + " km/h";
				else
					locs+="<br/>"+locStr;
			}
		}
		var dt=new Date();
		var max="";
		if(window.burCfg.maxBurja)
		{ 
			v=Math.max(v,burCfg.maxBurja);
			max=(locs.length>5 ? "<br/><br/>" : "<br/>")+GetDictValue("burMaxSpeed", window.burCfg.lng)+" "+burCfg.maxBurja+"&nbsp;km/h";
		}
		
//A		var str = dt.format(GetDictValue("dateFormat", window.burCfg.lng)) + "<br/><br/>" + (v>=windLimit ? GetDictValue("burHigh", window.burCfg.lng)+locs+max : GetDictValue("burNormal", window.burCfg.lng));
		var str = dt.format(GetDictValue("dateFormat", window.burCfg.lng)) + "<br/><br/>" + GetDictValue("burRegionalka", window.burCfg.lng) +":<br/>"+ (v>=windLimit ? GetDictValue("burHigh", window.burCfg.lng)+locs+max : GetDictValue("burNormal", window.burCfg.lng));

		document.getElementById(burCfg.burCnt).innerHTML=str;
	}
	else if(window.burCfg.n<1)
	{
		var dt=new Date();
		var str=dt.format(GetDictValue("dateFormat", window.burCfg.lng)) + "<br/><br/>"+ GetDictValue("unavaible",window.burCfg.lng);
		document.getElementById(burCfg.burCnt).innerHTML=str;
	}
	window.burCfg.n++;
}

function BurjaDoLink(id,lat,lon,str)
{
	if(id!=null && window.map!=null)
	{
		var ok=false;
		if(window.map.layers["burja"]!=null && !window.map.layers["burja"].isHidden())
		{
			for(var f in window.map.layers["burja"].drawings)
			{
				var drw=window.map.layers["burja"].drawings[f];
				if(drw.getFeature().properties.id==id)
				{
					drw.openInfoWindow();
					ok=true;
					break;
				}
			}
		}
		if(ok==false)
			map.openInfoWindow(str, lon, lat, RMap.CoordinateSystem.getCoordinateSystem('EPSG:4326'));
	}
}



function BurjaIzpisInit(cfg)
{
	var div = document.createElement("div");
	
//A	div.innerHTML = "<H2 class='moduletitle RMapControl'>" + GetDictValue("burListTitle", cfg.lng) + "</H2>" + GetDictValue("burRegionalkaLong", cfg.lng) + "<br/><br/>";
  div.innerHTML="<H2 class='moduletitle RMapControl'>"+GetDictValue("burListTitle", cfg.lng)+"</H2><br/>";
  
  document.getElementById(cfg.burCnt).parentNode.insertBefore(div,document.getElementById(cfg.burCnt));
 	document.getElementById(cfg.burCnt).innerHTML="<div style='text-align:center'><img src='"+appSettings.iconsRepository+appSettings.loadingImg+"'/></div>";

 	if(appSettings.burjaSistemPage != null)
 	{
 		var div2 = document.createElement("div");
 		div2.innerHTML = "<a href='" + appSettings.burjaSistemPage + "'>" + GetDictValue("burSistem", cfg.lng) + "</a>";
 		document.getElementById(cfg.burCnt).parentNode.appendChild(div2);
 	}
 	
	window.burListCfg=cfg;
	window.burListCfg.n=0;
	window.burListCfg.loop=setInterval(BurjaIzpisLoad,61000*3);
	BurjaIzpisLoad();
}

function BurjaIzpisLoad()
{
	window.burListCfg.maxBurja=null;
	var url=appSettings.proxyUrl + "?method=GET&rproxytype=json&remoteUrl=" + escape(appSettings.burjaZnakiUrl);
	ServerRequest(url,BurjaIzpisLoadMerilniki);
}

function BurjaIzpisLoadMerilniki(result)
{
	var obj=GetEval(result);
	if(obj==null && result!=null && window.RMap!=null)
		obj=GetEval(getDStr(result));
	if(obj!=null && obj.feed!=null && obj.feed.entry!=null && obj.feed.entry.length>0)
		window.burListCfg.maxBurja=obj.feed.entry[0].dispveter;
	var url=appSettings.proxyUrl + "?method=GET&rproxytype=json&remoteUrl=" + escape(appSettings.burjaUrl);
	ServerRequest(url,BurjaIzpisLoaded);
}

function BurjaIzpisLoaded(result)
{
	var obj=GetEval(result);
	if (obj == null && result != null && window.RMap != null)
		obj=GetEval(getDStr(result));
	if(obj!=null && obj.feed!=null && obj.feed.entry!=null && obj.feed.entry.length!=null)
	{
		var cc=appSettings.tblBrdColor;
		var hh=appSettings.tblHdrColor;
		var dt=new Date();
		
		var s=[];
		s.push(new Date().format(GetDictValue("dateFormat", burListCfg.lng))+"<br/><br/>");
		
//A		s.push(burListCfg.maxBurja ? GetDictValue("burMaxSpeed", burListCfg.lng)+" "+burListCfg.maxBurja+"&nbsp;km/h<br/><br/>" : "");
		s.push(GetDictValue("burRegionalkaLong", burListCfg.lng)); 
		s.push(burListCfg.maxBurja ? ":<br/>"+GetDictValue("burMaxSpeed", burListCfg.lng)+" "+burListCfg.maxBurja+"&nbsp;km/h<br/><br/>" : "");
		
		s.push("<table border='0' cellpadding='3' cellspacing='0' style='width:100%;'>");
		s.push("<tr style='background:"+hh+"'>");
		s.push("<td style='border-top:"+cc+" 1px solid; border-bottom:"+cc+" 1px solid; border-left:"+cc+" 1px solid; border-right:"+cc+" 0px solid;'> &nbsp; </td>");
		s.push("<td style='border-top:"+cc+" 1px solid; border-bottom:"+cc+" 1px solid; text-align:left'><b>"+GetDictValue("burLoc", burListCfg.lng)+"</b></td>");
		s.push("<td style='border-top:"+cc+" 1px solid; border-bottom:"+cc+" 1px solid; text-align:left; border-right:"+cc+" 1px solid; '><b>"+GetDictValue("burSpeed", burListCfg.lng)+"</b></td>");
		s.push("</tr>");
		var sort=[];
		for(var f=0; f<obj.feed.entry.length; f++)
		{
			for(var f2=0;f2<sort.length;f2++)
			{
				if(obj.feed.entry[f]["veter"]>=sort[f2]["veter"])
					break;
			}
			if(f2<sort.length)
				sort.splice(f2,0,obj.feed.entry[f]);
			else
				sort[sort.length]=obj.feed.entry[f]
		}
		for(var f=0; f<sort.length; f++)
		{
			var loc=sort[f];
			var v=loc['veter'];
			var icon="";
			if(v<5)
				icon=appSettings.iconsRepository + "burja/wind1.gif";
			else if(v<20)
				icon=appSettings.iconsRepository + "burja/wind2.gif";
			else if(v<50)
				icon=appSettings.iconsRepository + "burja/wind3.gif";
			else if(v<80)
				icon=appSettings.iconsRepository + "burja/wind4.gif";
			else
				icon=appSettings.iconsRepository + "burja/wind5.gif";
			s.push("<tr class='PrometPanelItem'>");
			s.push("<td style='text-align:left; vertical-align:top; border-left:"+cc+" 1px solid; border-bottom:"+cc+" 1px solid; width:50px'><img src='"+icon+"'/></td>");
			s.push("<td style='text-align:left; vertical-align:top; border-bottom:"+cc+" 1px solid; '>"+loc["title"]+"</td>");
			s.push("<td style='text-align:left; vertical-align:top; border-bottom:"+cc+" 1px solid; border-right:"+cc+" 1px solid;'>"+loc["veter"]+" km/h</td>");
			s.push("</tr>");
		}
		s.push("</table>");
		s.push("<br/>");
		document.getElementById(burListCfg.burCnt).innerHTML=s.join("");
	}
	else if(window.burListCfg.n<1)
	{
		var dt=new Date();
		var str=GetDictValue("unavaible",burListCfg.lng);
		document.getElementById(burListCfg.burCnt).innerHTML=str;
	}
	burListCfg.n++;
}