/* Array of markers: Title, Lat, Long, City_or_Stadium, zIndex */
//var markersArray = []; Will look at this later if there is time. Maybe phase 2.
var infoWindow;

function initialize(map_id, focus, which_icons_to_add) {
  var locations = getCities('all');
  var map_center = new google.maps.LatLng(-27.48534,24.37989);
  var map_zoom = 5;
  
  if (focus=="SA") {
    map_center = new google.maps.LatLng(-29.03034,24.17989);
    map_zoom = 5;
  } else {
    for (var i = 0; i < locations.length -1; i++){
      if (focus == locations[i][0]){
        map_center = locations[i][1];
        map_zoom = 10
      }
    }
  }
  
  
  var myOptions = {
    zoom: map_zoom,
    center: map_center,
    //mapTypeId: google.maps.MapTypeId.HYBRID
    mapTypeId: google.maps.MapTypeId.ROADMAP
    
  }
  var map;
  map = new google.maps.Map(document.getElementById(map_id), myOptions);
  google.maps.event.addListener(map, 'click', function(event) {clickMap(map, event, "click");} );
  google.maps.event.addListener(map, 'rightclick', function(event) {clickMap(map, event, "rightclick");} );
  

  if (which_icons_to_add.indexOf("cities") > -1){
    /* Mark the cities */
    var image_city = new google.maps.MarkerImage('images_content/map_marker_city.png', new google.maps.Size(25,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    var shadow_city = new google.maps.MarkerImage('images_content/map_marker_city_shadow.png', new google.maps.Size(25,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    for (var i = 0; i < locations.length; i++){
      var marker = new google.maps.Marker({
          map: map,
          position: locations[i][1],
          zoom: 10,
          title: locations[i][2],
          icon: image_city,
          shadow: shadow_city,
          zIndex: 5
      });
      google.maps.event.addListener(marker, 'click', function() {clickLocation(map, this, "click");} );
      google.maps.event.addListener(marker, 'rightclick', function() {clickLocation(map, this, "rightclick");} );
    }
  }
  
  if (which_icons_to_add.indexOf("stadiums") > -1){
    /* Mark the stadiums */
    var image_stadium = new google.maps.MarkerImage('images_content/map_marker_stadium.png', new google.maps.Size(25,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    var shadow_stadium = new google.maps.MarkerImage('images_content/map_marker_stadium_shadow.png', new google.maps.Size(25,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    locations = getStadiums();
    for (var i = 0; i < locations.length; i++){
      var marker = new google.maps.Marker({
          map: map,
          position: locations[i][1],
          zoom: 10,
          title: locations[i][2],
          icon: image_stadium,
          shadow: shadow_stadium,
          zIndex: 3
      });
      google.maps.event.addListener(marker, 'click', function() {clickLocation(map, this, "click");} );
      google.maps.event.addListener(marker, 'rightclick', function() {clickLocation(map, this, "rightclick");} );
    }
  }
  
  if (which_icons_to_add.indexOf("airports") > -1){
    /* Mark the airports */
    var image_airport = new google.maps.MarkerImage('images_content/map_marker_airport.png', new google.maps.Size(30,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    var shadow_airport = new google.maps.MarkerImage('images_content/map_marker_airport_shadow.png', new google.maps.Size(30,30), new google.maps.Point(0,0), new google.maps.Point(16,35));
    locations = getAirports();
    for (var i = 0; i < locations.length; i++){
      var marker = new google.maps.Marker({
          map: map,
          position: locations[i][1],
          zoom: 10,
          title: locations[i][2],
          icon: image_airport,
          shadow: shadow_airport,
          zIndex: 3
      });
      google.maps.event.addListener(marker, 'click', function() {clickLocation(map, this, "click");} );
      google.maps.event.addListener(marker, 'rightclick', function() {clickLocation(map, this, "rightclick");} );
    }
  }
  
  if (which_icons_to_add.indexOf("bus_routes") > -1){
    /* Mark the bus routes */
    lines = getBusRoutes();
    //alert(lines[0]);
    for (var i = 0; i < lines.length; i++) {
      var line = new google.maps.Polyline({
        map: map,
        path: lines[i],
        strokeColor: "#ff0000",
        strokeOpacity: 0.6,
        zIndex: 10
      });
    }
  
  }

  if (which_icons_to_add.indexOf("rail_routes") > -1){
    /* Mark the rail routes */
    lines = getRailRoutes();
    //alert(lines[0]);
    for (var i = 0; i < lines.length; i++) {
      var line = new google.maps.Polyline({
        map: map,
        path: lines[i],
        strokeColor: "#0000ff",
        strokeOpacity: 0.6,
        zIndex: 10
      });
    }
  
  }
  
}



function getCities(what_city) {
  if (what_city == 'all'){
    cities = [
    ['Cpt', new google.maps.LatLng(-33.92372,18.42339),'Cape Town','hc_cpt.php'],
    ['Pta', new google.maps.LatLng(-25.74646,28.18817),'Pretoria','hc_pta.php'],
    ['Jhb', new google.maps.LatLng(-26.20136,28.04604),'Johannesburg','hc_jhb.php'],
    ['Dbn', new google.maps.LatLng(-29.85706,31.02470),'Durban','hc_dbn.php'],
    ['PE', new google.maps.LatLng(-33.96183,25.61153),'Nelson Mandela Bay / Port Elizabeth','hc_pe.php'],
    ['Bfn', new google.maps.LatLng(-29.11823,26.22299),'Bloemfontein / Mangaung','hc_blm.php'],
    ['Pol', new google.maps.LatLng(-23.91209,29.45366),'Polokwane','hc_pol.php'],
    ['Nel', new google.maps.LatLng(-25.47009,30.97810),'Nelspruit / Mbombela','hc_nel.php'],
    ['Rus', new google.maps.LatLng(-25.66844,27.24240),'Rustenburg','hc_rust.php']];
  } else {
    switch (what_city){
      case 'Cpt': cities = ['Cpt', new google.maps.LatLng(-33.92372,18.42339),'Cape Town','hc_cpt.php']; break;
      case 'Pta': cities = ['Pta', new google.maps.LatLng(-25.74646,28.18817),'Pretoria','hc_pta.php']; break;
      case 'Jhb': cities = ['Jhb', new google.maps.LatLng(-26.20136,28.04604),'Johannesburg','hc_jhb.php']; break;
      case 'Dbn': cities = ['Dbn', new google.maps.LatLng(-29.85706,31.02470),'Durban','hc_dbn.php']; break;
      case 'PE': cities = ['PE', new google.maps.LatLng(-33.96183,25.61153),'Nelson Mandela Bay / Port Elizabeth','hc_pe.php']; break;
      case 'Bfn': cities = ['Blm', new google.maps.LatLng(-29.11823,26.22299),'Bloemfontein / Mangaung','hc_blm.php']; break;
      case 'Pol': cities = ['Pol', new google.maps.LatLng(-23.91209,29.45366),'Polokwane','hc_pol.php']; break;
      case 'Nel': cities = ['Nel', new google.maps.LatLng(-25.47009,30.97810),'Nelspruit / Mbombela','hc_nel.php']; break;
      case 'Rus': cities = ['Rus', new google.maps.LatLng(-25.66844,27.24240),'Rustenburg','hc_rust.php']; break;    
    }
  }
  return cities;
}
function getStadiums(){
  stadiums = [
  ['CapeTown_st', new google.maps.LatLng(-33.90343,18.41118),'Cape Town Stadium','st_c_town.php'],
  ['LoftusVersfeld_st', new google.maps.LatLng(-25.753295,28.222903),'Loftus Versfeld','loftus.php'],
  ['EllisPark_st', new google.maps.LatLng(-26.197533,28.060769),'Ellis Park','ellis_park.php'],
  ['SoccerCity_st', new google.maps.LatLng(-26.234927,27.982559),'Soccer City','soccer_city.php'],
  ['MosesMabhida_st', new google.maps.LatLng(-29.829032,31.030356),'Moses Mabhida Stadium','mosesmabhida.php'],
  ['NelsonMandelaBay_st', new google.maps.LatLng(-33.937824,25.599373),'Nelson Mandela Bay Stadium','nelson_mandela.php'],
  ['FreeState_st', new google.maps.LatLng(-29.11823,26.22299),'Free State Stadium - Mangaung','free_state.php'],
  ['PeterMokaba_st', new google.maps.LatLng(-23.924807,29.468770),'Peter Mokaba Stadium','peter_mokaba.php'],
  ['Mbombela_st', new google.maps.LatLng(-25.461728,30.929695),'Mbombela Stadium','mbombela.php'],
  ['RoyalBafokeng_st', new google.maps.LatLng(-25.578752,27.160933),'Royal Bafokeng Stadium','royal_bafokeng.php']];
  return stadiums;
}

function getAirports(){
  airports = [
    ['CptIntAir', new google.maps.LatLng(-33.966575,18.594383),'Cape Town International Airport','cpt_airport.php'],
    ['BfnIntAir', new google.maps.LatLng(-29.095854,26.297516),'Bloemfontein International Airport','bfn_airport.php'],
    ['KShIntAir', new google.maps.LatLng(-29.61444,31.11639),'King Shaka International Airport','shaka_airport.php'],
    ['PEIntAir', new google.maps.LatLng(-33.984035,25.611673),'Port Elizabeth International Airport','pe_airport.php'],
    ['LansAir', new google.maps.LatLng(-25.934354,27.925773),'Lanseria Airport','lan_airport.php'],
    ['PolIntAir', new google.maps.LatLng(-23.860159,29.453248),'Polokwane International Airport','pol_airport.php'],
    ['KMpIntAir', new google.maps.LatLng(-25.385361,31.098303),'Kruger Mpumalanga International Airport','krugm_airport.php'],
    ['PilIntAir', new google.maps.LatLng(-25.335348,27.169216),'Pilanesberg International Airport','pil_airport.php'],
    ['GeoAir', new google.maps.LatLng(-34.001498,22.381927),'George Airport','geo_airport.php'],
    ['DbnIntAir', new google.maps.LatLng(-29.967997,30.943995),'Durban International Airport','dbn_airport.php'],
    ['OrtIntAir', new google.maps.LatLng(-26.134989,28.230074),'OR Tambo International Airport','ort_airport.php']
  ];
  return airports;
}

function getBusRoutes(){
  bfn = new google.maps.LatLng(-29.118568,26.22601);
  cpt = new google.maps.LatLng(-33.923834,18.426041);
  routes = [
    [cpt,getCities('Pta')[1]],
    [cpt,getCities('Jhb')[1]],
    [cpt,bfn],
    [cpt,getCities('Dbn')[1]],
    [cpt,getCities('PE')[1]],
    [getCities('PE')[1],bfn],
    [getCities('PE')[1],getCities('Pta')[1]],
    [getCities('PE')[1],getCities('Jhb')[1]],
    [getCities('PE')[1],getCities('Dbn')[1]],
    [bfn,getCities('Pta')[1]],
    [bfn,getCities('Jhb')[1]],
    [bfn,getCities('Dbn')[1]],
    [getCities('Pta')[1],getCities('Dbn')[1]],
    [getCities('Jhb')[1],getCities('Dbn')[1]]
  ];
  return routes;
}
function getRailRoutes(){
  bfn = new google.maps.LatLng(-29.118568,26.22601);
  cpt = new google.maps.LatLng(-33.923834,18.426041);

  routes = [
    [cpt,bfn],
    [cpt,getCities('Dbn')[1]],
    [getCities('PE')[1],getCities('Jhb')[1]],
    [bfn,getCities('Jhb')[1]],
    [bfn,getCities('Dbn')[1]],
    [getCities('Jhb')[1],getCities('Dbn')[1]],
    [getCities('Jhb')[1],getCities('Pol')[1]],
    [getCities('Jhb')[1],getCities('Nel')[1]]
  ];
  return routes;
}


function clickLocation(map, whatWasClicked, clickType) {
  if (clickType == 'click'){
    if (map.zoom <= 5) {
      map.setCenter(whatWasClicked.position);
      map.setZoom(10);
    } else if (map.zoom >= 11) {
      var gotoPage = "index.php";
      var locations = getCities('all');
      var j = -1;
      for (var i = 0; i < locations.length -1; i++){
        if (whatWasClicked.title == locations[i][2]){
          j = i;
        }
      }
      if (j == -1) {
        locations = getStadiums();
        for (var i = 0; i < locations.length -1; i++){
          if (whatWasClicked.title == locations[i][2]){
            j = i;
          }
        }
      }
      if (j == -1) {
        locations = getAirports();
        for (var i = 0; i < locations.length -1; i++){
          if (whatWasClicked.title == locations[i][2]){
            j = i;
          }
        }
      }
      //alert("j: " + j + " Clicked position:" + whatWasClicked.title + ". city position: " + locations[j][2] + ". Goto page: " + locations[j][3] );
      window.location = locations[j][3];
    } else {
      map.setCenter(whatWasClicked.position);
      map.setZoom(map.zoom + 1);
    }
  } else if (clickType == 'rightclick') {
    map.setCenter(whatWasClicked.position);
    map.setZoom(map.zoom - 1);
  }
}

function clickMap(map, whatWasClicked, clickType) {
  if (clickType == 'click'){
    map.setCenter(whatWasClicked.latLng);
    map.setZoom(map.zoom + 1);
  } else if (clickType == 'rightclick') {
    map.setCenter(whatWasClicked.latLng);
    map.setZoom(map.zoom - 1);
  }
}