/*
  weatherscriptlite.js
  pogoda.4informer.ru catalog page functions (Using Google Maps API)
 */


function open_addwindow(node)
{
var txt='';
  txt = "Скопируйте этот код для вставки на Ваш сайт:<br/>"+
   '<textarea rows="4" cols="50" class="addwindow">'+
   '<iframe name="infframe" frameborder="0" width="500" height="350" scrolling="no" src="http://pogoda.4informer.ru/?component=weathermap&amp;node='+node+'"></iframe>'
   +'</textarea><br/>'
   +'<a class="w-addwindow-a" href="javascript:void(0)" onclick="close_addwindow('+node+')"a>Закрыть</a>';
  var x = document.getElementById('addwindow-div');
 x.innerHTML=txt;
}

function close_addwindow(node)
{
 var txt='<a class="addwindow-a" href="javascript:void(0)" onclick="open_addwindow('+node+')"a>Добавить карту на Ваш сайт</a>';
 var x = document.getElementById('addwindow-div');
 x.innerHTML=txt;
}

function open_addinformer(node, w, h)
{
var txt='';
  txt = "Скопируйте этот код для вставки на Ваш сайт:<br/>"+
   '<textarea rows="4" cols="50" class="addwindow">'+
   '<iframe width="'+w+'" height="'+h+'" scrolling="no" src="http://pogoda.4informer.ru/weatherscript/flash/informer.php?node='+node+'&amp;size='+w+'&amp;vsize='+h+'" marginwidth="0" marginheight="0" frameborder="0"></iframe>'
   +'</textarea><br/>'
   +'<a class="story" href="javascript:void(0)" onclick="close_addinformer('+node+', '+w+', '+h+')"a>Закрыть</a>';
  var x = document.getElementById('addinformer-div-'+w+'-'+h);
 x.innerHTML=txt;
}

function close_addinformer(node, w, h)
{
 var x = document.getElementById('addinformer-div-'+w+'-'+h);
 x.innerHTML='';
}



/*Enviroment variabeles*/
var map; //Google Map
var countrynode; //Country node
var citynode; //City node
var BaseURL = "http://pogoda.4informer.ru/"
var citynames;
var citynodes;
var Mark;
var InfP;
var points;
var dd;
/*Google Map Functions*/

/*
 AddIcon
 @map - map variable
 @lat -latitude
 @lng -longitude
 @WeatherIconURL -URL to weathericon image
 @TemperatureIconURL -URL to temperature icon image
 @onclick -function(latlng){} - function to process onclick event
*/
function AddIcon(map,lat,lng,iconfname,tempfname,idx,html)
{
 var point = new GLatLng(lat,lng);
 //alert(point);
 if (iconfname != "")
 {
        iconz = new GIcon;
        iconz.image = 'http://pogoda.4informer.ru/'+iconfname;
        iconz.iconSize = new GSize(32, 32);
        iconz.shadowSize = new GSize(20, 20);
        iconz.iconAnchor = new GPoint(6, 20);
        iconz.infoWindowAnchor = new GPoint(15, 15);
        mopt = {icon:iconz};
        InfP[idx] = new GMarker(point,mopt);
        map.addOverlay(InfP[idx]);
  } else {
   InfP[idx] = false;
  }
		iconx = new GIcon;
        iconx.image = 'http://pogoda.4informer.ru/'+tempfname;
        iconx.iconSize = new GSize(41, 24);
        iconx.shadowSize = new GSize(20, 14);
        iconx.iconAnchor = new GPoint(0, 7);
        iconx.infoWindowAnchor = new GPoint(15, 15);
        mopt2 = {icon:iconx};
        Mark[idx]= new GMarker(point,mopt2);
        map.addOverlay(Mark[idx]);

        GEvent.addListener(Mark[idx],"click",function(){Mark[idx].openInfoWindowHtml(html);});

}

/*Control functions*/
function initialize(a_countrynode,a_citynode)
{
countrynode = a_countrynode;
citynode = a_citynode;

 if (GBrowserIsCompatible()) {
  var xml = GXml.parse(xmlstr);
  if (countrynode!=0)
  doLoadCountry(xml,countrynode,citynode);
 }
}

function unload()
{
 GUnload();
}

function getXMLElement(xml,name)
{
 if (xml.getElementsByTagName(name)[0].childNodes[0]==null) return null;
 return xml.getElementsByTagName(name)[0].childNodes[0].nodeValue;
}

function doLoadCountry(xml,a_countrynode,a_citynode)
{
countrynode = a_countrynode;
citynode = a_citynode;
var countryname;
  document.getElementById('main').innerHTML =
    '<div style="text-align: center"><div id="map_canvas" style="width: 500px; height : 350px"></div><div id="addwindow-div"><div id="addinformer-div"></div></div><div id="forecast"></div></div>';
  map = new GMap2(document.getElementById('map_canvas'));
  map.addControl(new GLargeMapControl());

  var bounds = new GLatLngBounds();
   points = xml.documentElement.getElementsByTagName('points')[0].getElementsByTagName('point');

  Mark=new Array(); InfP=new Array(); citynodes=new Array(); citynames=new Array();

  for (var i=0; i<points.length;i++)
  {
   bounds.extend(new GLatLng(getXMLElement(points[i],'lat'),
   getXMLElement(points[i],'lng')));

   var zoom = map.getBoundsZoomLevel(bounds);
   if (zoom > 7) zoom = 7;
   map.setCenter(bounds.getCenter(),zoom);
   citynodes[i] = getXMLElement(points[i],'id');

 AddIcon(map,
   getXMLElement(points[i],'lat'),
   getXMLElement(points[i],'lng'),
   getXMLElement(points[i],'iconfname'),
   getXMLElement(points[i],'tempfname'),
   i,
   InformerContent(points[i],i,false)
   );

  }


   //document.getElementById('addinformer-div').innerHTML = '';

   if (citynode != 0)
   {
    OpenInformer(getidxbynode(citynode));

   }
   close_addwindow(countrynode);

}

function getnodebyidx(idx)
{
 return points[idx].getElementsByTagName('id')[0].childNodes[0].nodeValue;
}

function getidxbynode(node)
{
 if (node==0) return -1;
 for (var i = 0; i < points.length; i++)
 {
  if (getnodebyidx(i)==node)
   return i;
  }
}


function OpenInformer(idx)
{
 GEvent.trigger(Mark[idx],"click");
}


function InformerContent(xml,idx,forexport)
{
 winf = '<table class="informer"><tr id="titletr"><td id="titletd" colspan="2"><b>'+getXMLElement(xml,'showname')+'</b></td></tr><tr id="weathertr"><td id="weathertd">';
 winf+= "<b>Температура:</b> "+getXMLElement(xml,'temperature')+"°C";
 if (getXMLElement(xml,'chance')!=-1) winf+= "<br/><b>Вероятность осадков: </b>"+getXMLElement(xml,'chance')+"%";
 if (getXMLElement(xml,'wind')!=-1) winf+= "<br/><b>Сила ветра: </b>"+getXMLElement(xml,'wind');
 if ((getXMLElement(xml,'wind')!=0)&&(getXMLElement(xml,'winddir')!="")&&(getXMLElement(xml,'winddir')!="CALM")) winf+= "<br/><b>Направление ветра: </b>"+getXMLElement(xml,'winddir');
 if (getXMLElement(xml,'humidity')!=-1) winf+= "<br/><b>Влажность воздуха: </b>"+getXMLElement(xml,'humidity')+" %";
 winf+='</tr></table>';
 if (forexport){
 winf+= '<div class="i-showforecast-a"><a href="http://pogoda.4informer.ru/?component=catalog&amp;node='+citynodes[idx]+'" target="_blank">Показать прогноз</a></div>';
 } else {
winf+= '<div class="i-showforecast-a"><a href="http://pogoda.4informer.ru/?component=catalog&amp;node='+citynodes[idx]+'" >Показать прогноз</a></div>';
 }
 return winf;
}

   function initializeexport(node)
   {
  countrynode = node;
  citynode=0;
  var countryname;
  var xml = GXml.parse(xmlstr);
  map = new GMap2(document.getElementById("map_canvas"));
  map.addControl(new GLargeMapControl());

  var bounds = new GLatLngBounds();
   points = xml.documentElement.getElementsByTagName("points")[0].getElementsByTagName("point");

  Mark=new Array(); InfP=new Array(); citynodes=new Array(); citynames=new Array();

  for (var i=0; i<points.length;i++)
  {
   bounds.extend(new GLatLng(getXMLElement(points[i],"lat"),
   getXMLElement(points[i],"lng")));

   var zoom = map.getBoundsZoomLevel(bounds);
   if (zoom > 7) zoom = 7;
   map.setCenter(bounds.getCenter(),zoom);
   citynodes[i] = getXMLElement(points[i],"id");

 AddIcon(map,
   getXMLElement(points[i],"lat"),
   getXMLElement(points[i],"lng"),
   getXMLElement(points[i],"iconfname"),
   getXMLElement(points[i],"tempfname"),
   i,
   InformerContent(points[i],i,false)
   );
  }


   }

function initializeexport2(a_countrynode, a_citynode) {
  countrynode = a_countrynode;
  citynode = a_citynode;

  var countryname;
  var xml = GXml.parse(xmlstr);
  map = new GMap2(document.getElementById("map_canvas"));
  map.addControl(new GLargeMapControl());

  var bounds = new GLatLngBounds();
   points = xml.documentElement.getElementsByTagName("points")[0].getElementsByTagName("point");

  Mark=new Array(); InfP=new Array(); citynodes=new Array(); citynames=new Array();

  for (var i=0; i<points.length;i++) {
   bounds.extend(new GLatLng(getXMLElement(points[i],"lat"),
   getXMLElement(points[i],"lng")));

   var zoom = map.getBoundsZoomLevel(bounds);
   if (zoom > 7) zoom = 7;
   map.setCenter(bounds.getCenter(),zoom);
   citynodes[i] = getXMLElement(points[i],"id");

 AddIcon(map,
   getXMLElement(points[i],"lat"),
   getXMLElement(points[i],"lng"),
   getXMLElement(points[i],"iconfname"),
   getXMLElement(points[i],"tempfname"),
   i,
   InformerContent(points[i],i,true)
   );
  }
}

