var $j = jQuery.noConflict();
$j(document).ready(function(){
  if ($j("#google_map").length){

    load(0, 0, 0);

    $j(window).unload(function () {
      GUnload();
    });

  }
});

var map = null;
var mgr;
var icons = {};
var allmarkers = [];

function load(map_center_lat, map_center_lng, map_zoom) {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("google_map"));
    if(map_center_lat > 0 && map_center_lng > 0 && map_zoom > 0){
      map.setCenter(new GLatLng(map_center_lat, map_center_lng), map_zoom);
    } else {
      map.setCenter(new GLatLng(20.0,25.520313), 2);
    }

    map.disableDragging();
    map.setMapType(G_PHYSICAL_MAP);

    mgr = new MarkerManager(map, {trackMarkers:true});
    window.setTimeout(setupFlagMarkers, 0);

  }
}

var flagLayer = [
  {
    "zoom": [0, 3],
    "places": [
      { "name": "Adrian Sobota, Irene Sobotta, John Gotze", "icon": ["DK", "flag-shadow"], "posn": [55.84900565509147, 9.31640625] },
      { "name": "Laurent Liscia, Dr. George Arnold", "icon": ["US", "flag-shadow"], "posn": [33.09829173683522, -103.359375] },
      { "name": "Flavio Souza", "icon": ["JP", "flag-shadow"], "posn": [39.05957484978343, 140.625] },
      { "name": "Sean Whetstone", "icon": ["GB", "flag-shadow"], "posn": [53.197697320992496, -1.0546875] },
      { "name": "Ariane Rüdiger", "icon": ["DE", "flag-shadow"], "posn": [51.039552244536985, 9.66796875] },
      { "name": "Rien Dijkstra, Dr.ir. Hans Moonen", "icon": ["NL", "flag-shadow"], "posn": [52.475643035249796, 5.537109375] },
      { "name": "Bianca Wirth", "icon": ["AU", "flag-shadow"], "posn": [-20.27319013311604, 133.857421875] },
      { "name": "Dominique C. Brack", "icon": ["CH", "flag-shadow"], "posn": [46.852177482304064, 8.173828125] }
    ]
  }
];

function setupFlagMarkers() {
  var mgr = new MarkerManager(map);
  for (var i in flagLayer) {
    var layer = flagLayer[i];
    var markers = [];
    for (var j in layer["places"]) {
      var place = layer["places"][j];
      var icon = getIcon(place["icon"]);
      var posn = new GLatLng(place["posn"][0], place["posn"][1]);
      markers.push(new GMarker(posn, { title: place["name"], icon: icon }));
    }
    mgr.addMarkers(markers, layer["zoom"][0], layer["zoom"][1]);
  }
  mgr.refresh();
}

function getIcon(images) {
  var icon = null;
  if (images) {
    if (icons[images[0]]) {
      icon = icons[images[0]];
    } else {
      icon = new GIcon();
      //icon.image = "images/" + images[0] + ".png";
      icon.image = "wp-content/themes/arjuna-x/images/flags/" + images[0] + ".png";
      var size = iconData[images[0]];
      icon.iconSize = new GSize(size.width, size.height);
      icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
      icon.shadow = "wp-content/themes/arjuna-x/images/flags/" + images[1] + ".png";
      size = iconData[images[1]];
      icon.shadowSize = new GSize(size.width, size.height);
      icons[images[0]] = icon;
    }
  }
  return icon;
}

var iconData = {
  "US": { width: 26, height: 18 },
  "DK": { width: 26, height: 18 },
  "JP": { width: 26, height: 18 },
  "GB": { width: 26, height: 18 },
  "DE": { width: 26, height: 18 },
  "NL": { width: 26, height: 18 },
  "AU": { width: 26, height: 18 },
  "CH": { width: 18, height: 18 },
  "flag-shadow": { width: 40, height: 30 },
};

