var map = null;
var things_on_map = [];
var coords_on_map = [];
var mc = null;
var markerid = [];
/*
   Puts numbers on polygons.
*/


function center(bla)
{
   var ttttt = new google.maps.LatLngBounds();

   for (i in bla)
   {
      var patt = bla[i].getArray();
      for (a in patt)
      {
         ttttt.extend(patt[a]);
      }
   }
   return ttttt.getCenter();
}

function num(poli, numb)
{
   var blah = new google.maps.Marker({
      position: center(poli.getPaths().getArray()),
      map: map,
      icon: '/wp-content/plugins/text_img.php?0='+numb
   });
   things_on_map.push(blah);
}


function initialize()
{
   things_on_map = [];
   coords_on_map = [];

   var myOptions =
   {
      zoom: 8,
      center: new google.maps.LatLng(1, 1),
      mapTypeId: google.maps.MapTypeId.ROADMAP
   };
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

   //inizialize dropdown menus and map
   zoom(0,0);
   select(0);

   var gcoder = new google.maps.Geocoder();
   gcoder.geocode({'address':geoip_country_name()},function(results, status)
   {
      if (status == google.maps.GeocoderStatus.OK)
      {
         map.fitBounds(results[0].geometry.bounds);
      }
      else{}//geolocation failed
   });
}

/*
   Make changes on map.
   type =
      0 - countries view
      1 - regions view, value = country id
      2 - shops in one region, value = region id
      3 - shops in one chain, value = chain id

      more info in page-map_meta_ajax.php
*/
function zoom(type, value)
{
   jQuery.getScript('/map_meta_empty_page/?0='+type+'&1='+value+'&2='+jQuery('[name="chain"]').val());
}
/*
   Someone selected something from the menu
   type =
      0 - fill in countries and store chains
      1 - someone selected a country
      2 - someone selected a region
      3 - someone selected a store chain
      4 - someone selected a store
*/
function select(type)
{
   if (type == 0)   // fill in countries and store chains
   {

      jQuery.get('/map_meta_empty_page/?0=4&k='+LANG_CONST, function(data)
      {
         jQuery('[name="country"]').html(data);
      });

      jQuery.get('/map_meta_empty_page/?0=6&k='+LANG_CONST, function(data)
      {
         jQuery('[name="chain"]').html(data);
      });

      jQuery('#region').hide();
      jQuery('#store').hide();
      jQuery('.informationArea').hide()

   }
   else if (type == 1)//someone selected a country
   {
      jQuery('.value_country').html(jQuery('[name="country"] :selected').text());

      jQuery.get('/map_meta_empty_page/?0=6&k='+LANG_CONST+'&1='+jQuery('[name="country"]').val(), function(data)
      {
         jQuery('[name="chain"]').html(data);
      });

      if (jQuery('[name="country"]').val() == 0)
      {
         select(0);
         zoom(0,0);
      }
      else
      {
         zoom(1,jQuery('[name="country"]').val());
         jQuery.get('/map_meta_empty_page/?0=5&1='+jQuery('[name="country"]').val()+'&2='+jQuery('[name="chain"]').val()+'&k='+LANG_CONST, function(data)
         {
            jQuery('[name="region"]').html(data);
            jQuery('#region').show();
         });
      }
   }
   else if (type == 2)//someone selected a region
   {
      jQuery('.value_region').html(jQuery('[name="region"] :selected').text());

      if (jQuery('[name="region"]').val() == 0)
      {
            jQuery('#store').hide();
            jQuery('.informationArea').hide;
            zoom(1,jQuery('[name="country"]').val());
      }
      else
      {
         zoom(2,jQuery('[name="region"]').val());

         jQuery.get('/map_meta_empty_page/?0=7&1='+jQuery('[name="region"]').val()+'&2='+jQuery('[name="chain"]').val()+'&k='+LANG_CONST, function(data)
         {
            jQuery('[name="store"]').html(data);
            jQuery('#store').show();
         });
      }
   }
   else if (type == 3)//someone selected a region...
   {
      jQuery('.value_chain').html(jQuery('[name="chain"] :selected').text());
         jQuery.get('/map_meta_empty_page/?0=8&k='+LANG_CONST+'&1='+jQuery('[name="chain"]').val(),function  (data)
            {
               jQuery('.chainArea').html(data);
            });

      if (jQuery('[name="region"]').val() != null) select(2);
   }
   else if (type == 4)//someone selected a store
   {
      jQuery('.value_store').html(jQuery('[name="store"] :selected').text());

      if (jQuery('[name="store"]').val() == 0)
      {
            zoom(2,jQuery('[name="region"]').val());
            jQuery('.informationArea').hide();
      }
      else
      {
         jQuery.getScript('/map_meta_empty_page/?0=9&1='+jQuery('[name="store"]').val()+'&k='+LANG_CONST);

         jQuery.getScript('/map_meta_empty_page/?0=10&1='+jQuery('[name="store"]').val());
      }
   }
   else if (type == 5)//someone selected a country from map
   {
      if (jQuery('[name="country"]').val() == 0)
      {
         select(0);
      }
      else
      {
         jQuery('.value_country').html(jQuery('[name="country"] :selected').text());

         jQuery.get('/map_meta_empty_page/?0=5&1='+jQuery('[name="country"]').val()+'&k='+LANG_CONST, function(data)
         {
            jQuery('[name="region"]').html(data);
            jQuery('#region').show();
         });
      }
   }
   else if (type == 6)//someone selected a region from the map
   {
      if (jQuery('[name="region"]').val() == 0)
      {
            jQuery('#store').hide();
            jQuery('.informationArea').hide;
      }
      else
      {
      jQuery('.value_region').html(jQuery('[name="region"] :selected').text());

         jQuery.get('/map_meta_empty_page/?0=7&1='+jQuery('[name="region"]').val()+'&2='+jQuery('[name="chain"]').val()+'&k='+LANG_CONST, function(data)
         {
            jQuery('[name="store"]').html(data);
            jQuery('#store').show();
         });
      }
   }
   else if (type == 7)//someone selected a store from the map
   {

      if (jQuery('[name="store"]').val() == 0)
      {
            jQuery('.informationArea').hide();
      }
      else
      {
         jQuery('.value_store').html(jQuery('[name="store"] :selected').text());

         jQuery.getScript('/map_meta_empty_page/?0=9&1='+jQuery('[name="store"]').val()+'&k='+LANG_CONST);
      }
   }


}

function delallthingies()//clean map!
{
   if (mc != null) mc.clearMarkers();
   mc = null;
   if (things_on_map)
   {
      for (i in things_on_map)
      {
         things_on_map[i].setMap(null);
         things_on_map[i] = null;
      }
      things_on_map.length = 0;
   }
}

function fit(typ)//fit map to thingis on it
{
   var ttttt = new google.maps.LatLngBounds();
   
   if (things_on_map.length > 0)
   {
      for (i in things_on_map)
      {
         if (things_on_map[i] instanceof google.maps.Marker)
         {
            ttttt.extend(things_on_map[i].getPosition());
         }
         else
         {
            var pat = things_on_map[i].getPaths().getArray();
            for (e in pat)
            {
               var patt = pat[e].getArray();
               for (a in patt)
               {
                  ttttt.extend(patt[a]);
               }
            }
         }
      }
      map.fitBounds(ttttt);   
   }
}

