﻿// A rosettaMapTypeControl is a GControl that displays custom map type
// control buttons
// @author: tnewton

function rosettaMapTypeControl() {
}
rosettaMapTypeControl.prototype = new GControl();

// Creates one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
rosettaMapTypeControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  // Styles set in style sheet
  container.id = "rosettaMapTypeControls";
  
  // "MAP" Control - switches to normal map
  var mapNormalDiv = document.createElement("div");
  mapNormalDiv.id = "mapNormalDiv";
  mapNormalDiv.className = "button";
  mapNormalDiv.appendChild(document.createTextNode(Locator.Config.mtControl.labelMap));
  Element.addClassName(mapNormalDiv, 'active');
  GEvent.addDomListener(mapNormalDiv, "click", function() {
    rmtcReset();
    Element.addClassName(this, 'active');
    map.setMapType(G_NORMAL_MAP);
  });

  // "SATELLITE" Control - switches to satellite map
  var mapSatelliteDiv = document.createElement("div");
  mapSatelliteDiv.id = "mapSatelliteDiv";
  mapSatelliteDiv.className = "button";
  mapSatelliteDiv.appendChild(document.createTextNode(Locator.Config.mtControl.labelSatellite));
  GEvent.addDomListener(mapSatelliteDiv, "click", function() {
    rmtcReset();
    Element.addClassName(this, 'active');
    map.setMapType(G_SATELLITE_MAP);
  });
  
  // "Hybrid" Control - switches to hybrid map
  var mapHybridDiv = document.createElement("div");
  mapHybridDiv.id = "mapHybridDiv";
  mapHybridDiv.className = "button";
  mapHybridDiv.appendChild(document.createTextNode(Locator.Config.mtControl.labelHybrid));
  GEvent.addDomListener(mapHybridDiv, "click", function() {
    rmtcReset();
    Element.addClassName(this, 'active');
    map.setMapType(G_HYBRID_MAP);
  });
  
  // "Terrain" Control - switches to terrain map
  var mapTerrainDiv = document.createElement("div");
  mapTerrainDiv.id = "mapTerrainDiv";
  mapTerrainDiv.className = "button";
  mapTerrainDiv.appendChild(document.createTextNode(Locator.Config.mtControl.labelTerrain));
  GEvent.addDomListener(mapTerrainDiv, "click", function() {
    rmtcReset();
    Element.addClassName(this, 'active');
    map.setMapType(G_PHYSICAL_MAP);
  });
  
  var mtcBackgroundDiv = document.createElement("div");
  mtcBackgroundDiv.id = "mtcBackground";
  
  var btnContainer = document.createElement("div");
  btnContainer.id = "mtcButtons";
  btnContainer.appendChild(mapTerrainDiv);
  btnContainer.appendChild(mapHybridDiv);
  btnContainer.appendChild(mapSatelliteDiv);
  btnContainer.appendChild(mapNormalDiv);
  
  var resContainer = document.createElement("div");
  resContainer.id = "mtcResultsArea";
  
  var resBackground = document.createElement("div");
  resBackground.id = "mtcResultsBackground";
  
  var resText = document.createElement("div");
  resText.id = "mtcResultsText";
  
  resContainer.appendChild(resBackground);
  resContainer.appendChild(resText);
  
  container.appendChild(mtcBackgroundDiv);
  container.appendChild(btnContainer);
  container.appendChild(resContainer);

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top right corner of the
// map with 12 pixels of top padding.
rosettaMapTypeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 12));
}

// Function removes the active button class from all buttons
function rmtcReset() {
    var tmpContainer = document.getElementById("rosettaMapTypeControls");
    var tmpChildren = tmpContainer.getElementsByTagName("div");
    for(a=0;a<tmpChildren.length;a++){
        Element.removeClassName(tmpChildren[a], 'active');
    }
}