var map;
var Marks=new Array();

function Mark(cC1, cC2, ciURL, ciW, ciH, cURL, cTitle, cCat) 
{
      this.C1     = cC1;
      this.C2     = cC2;
      this.iURL   = ciURL;
      this.iW     = ciW;  
      this.iH     = ciH; 
      this.URL    = cURL;
      this.Title  = cTitle;
      this.placemark=0; 
      this.cat=cCat;
}

function AddMark(cC1, cС2, ciURL, ciW, ciH, cURL, cTitle, cCat)
{
      var newMark = new Mark (cC1, cС2, ciURL, ciW, ciH, cURL, cTitle, cCat); 
      Marks.push(newMark);
}
function OpenBall(i)
{
  var cur = Marks[i];
  var point=new YMaps.GeoPoint(cur.C2, cur.C1)
  map.openBalloon(point,'<a href="'+ cur.URL +'"><nobr>'+cur.Title+'</nobr></a>');
  return false;
}
function RefreshCat(cat,refreshBox)
{
    var box = document.getElementById("ccb"+cat);
    if(refreshBox)
        box.checked=!box.checked;
    if (box.checked)
          SetMarks(cat);
    else
          RemoveMarks(cat);
    return false;
}

function RemoveMarks(cat)
{
       for(var i =0; i<Marks.length;i++)
       {
             var cur = Marks[i];
             if(cat==-1 || cat==cur.cat)
                  map.removeOverlay(cur.placemark);
       }
}
function SetMarks(cat)
{
       for(var i =0; i<Marks.length;i++)
       {
                    var cur = Marks[i];
                    if(cat!=-1 && cat!=cur.cat)
                          continue;
                    if(cat==-1 && cur.cat!=0 && cur.cat!=-1)
                    {
                          var box = document.getElementById("ccb"+cur.cat);
                          if(box && !box.checked)
                              continue;
                    }
                    var s = new YMaps.Style();
                    var placemark;
                    if(cur.iURL.indexOf("default#") == -1)
                    {
                        s.iconStyle = new YMaps.IconStyle("example#customPointIcon"+i);
                        s.iconStyle.href   = cur.iURL;
                        s.iconStyle.size   = new YMaps.Point(cur.iW, cur.iH);
                        if(cur.iURL=="http://visit.odessa.ua/wp-content/icons/sigth.PNG")
                              s.iconStyle.offset = new YMaps.Point(-11, -20);
                        else if(cur.iURL.indexOf("/iys/") == -1)
                              s.iconStyle.offset = new YMaps.Point(-cur.iW/2, -cur.iH/2);
                        else
                              s.iconStyle.offset = new YMaps.Point(-9, -25);      
                        YMaps.Styles.add("example#customPoint"+i, s);
                        var t = new YMaps.Template("<div><img alt=\"здесь\" style=\"height:$[style.iconStyle.size.y];width:$[style.iconStyle.size.x];\" src=\"$[style.iconStyle.href]\"\/></div>");
                        YMaps.Templates.add("example#customPointIcon"+i, t);
                        placemark = new YMaps.Placemark(new YMaps.GeoPoint(cur.C2,cur.C1), {style: "example#customPoint"+i});
                   }
                   else
                        placemark = new YMaps.Placemark(new YMaps.GeoPoint(cur.C2,cur.C1), {style: cur.iURL});                        
                   var text = '<a href="'+ cur.URL +'"><nobr>'+cur.Title+'</nobr></a>';
                   placemark.setBalloonContent(text);
                   cur.placemark=placemark;
                   map.addOverlay(placemark);
      }
}
function InitMap()
{ 
            var MapDiv=document.getElementById("YMapsID");
            var Div2=document.getElementById("Maplist");
            if(MapDiv.clientHeight<MapDiv.clientWidth)
            {
                   MapDiv.style.height=(MapDiv.clientWidth+"px");
                   Div2.style.height=(MapDiv.clientWidth+"px");
            }
            map = new YMaps.Map(MapDiv);
            AddMarks();
            var Max1=-10000.0;
            var Max2=-10000.0;
            var Min1= 10000.0;
            var Min2= 10000.0;
            for(var i =0; i<Marks.length;i++)
            {
                var cur = Marks[i];
                if (Max1<cur.C1)
                    Max1=cur.C1;
                if (Min1>cur.C1)
                    Min1=cur.C1;
                if (Max2<cur.C2)
                    Max2=cur.C2;
                if (Min2>cur.C2)
                    Min2=cur.C2;
            }
            var Bounds = new YMaps.GeoBounds(new YMaps.GeoPoint(Min2, Min1), new YMaps.GeoPoint(Max2, Max1));
            map.setBounds(Bounds);
            map.addControl(new YMaps.Zoom());
            map.addControl(new YMaps.TypeControl());
            SetMarks(-1);
}



