//<![CDATA[

var map = null;
var geocoder = null;


// Google Map: Make the markers
  var cicon = new GIcon();

  cicon.image             = "/themes/default/spots/images/icon_greyWheel.png";
  cicon.shadow            = "/themes/default/spots/images/icon_shadow.png";
  cicon.iconSize          = new GSize(27, 23);
  cicon.shadowSize        = new GSize(35, 20);
  cicon.iconAnchor        = new GPoint(14, 6);
  cicon.infoWindowAnchor  = new GPoint(8, 1);

  var houseicon = new GIcon();
  houseicon.image             = "/themes/default/spots/images/icon_house.png";
  houseicon.shadow            = "/themes/default/spots/images/icon_houseShadow.png";
  houseicon.iconSize          = new GSize(27, 20);
  houseicon.shadowSize        = new GSize(35, 20);
  houseicon.iconAnchor        = new GPoint(6, 20);
  houseicon.infoWindowAnchor  = new GPoint(8, 1);

  var tanWheel = new GIcon();
  tanWheel.image             = "/themes/default/spots/images/icon_tanWheel.png";
  tanWheel.shadow            = "/themes/default/spots/images/icon_shadow.png";
  tanWheel.iconSize          = new GSize(27, 23);
  tanWheel.shadowSize        = new GSize(35, 20);
  tanWheel.iconAnchor        = new GPoint(14, 6);
  tanWheel.infoWindowAnchor  = new GPoint(8, 1);

  var redorangeWheel = new GIcon();
  redorangeWheel.image             = "/themes/default/spots/images/icon_redorangeWheel.png";
  redorangeWheel.shadow            = "/themes/default/spots/images/icon_shadow.png";
  redorangeWheel.iconSize          = new GSize(27, 23);
  redorangeWheel.shadowSize        = new GSize(35, 20);
  redorangeWheel.iconAnchor        = new GPoint(14, 6);
  redorangeWheel.infoWindowAnchor  = new GPoint(8, 1);

  var yellowWheel = new GIcon();
  yellowWheel.image             = "/themes/default/spots/images/icon_yellowWheel.png";
  yellowWheel.shadow            = "/themes/default/spots/images/icon_shadow.png";
  yellowWheel.iconSize          = new GSize(27, 23);
  yellowWheel.shadowSize        = new GSize(35, 20);
  yellowWheel.iconAnchor        = new GPoint(14, 6);
  yellowWheel.infoWindowAnchor  = new GPoint(8, 1);

  var orangeWheel = new GIcon();
  orangeWheel.image             = "/themes/default/spots/images/icon_orangeWheel.png";
  orangeWheel.shadow            = "/themes/default/spots/images/icon_shadow.png";
  orangeWheel.iconSize          = new GSize(27, 23);
  orangeWheel.shadowSize        = new GSize(35, 20);
  orangeWheel.iconAnchor        = new GPoint(14, 6);
  orangeWheel.infoWindowAnchor  = new GPoint(8, 1);

  var redWheel = new GIcon();
  redWheel.image             = "/themes/default/spots/images/icon_redWheel.png";
  redWheel.shadow            = "/themes/default/spots/images/icon_shadow.png";
  redWheel.iconSize          = new GSize(27, 23);
  redWheel.shadowSize        = new GSize(35, 20);
  redWheel.iconAnchor        = new GPoint(14, 6);
  redWheel.infoWindowAnchor  = new GPoint(8, 1);

  var icons = [];
  icons[0] = cicon;
  icons[1] = yellowWheel;
  icons[2] = tanWheel;
  icons[3] = orangeWheel;
  icons[4] = redorangeWheel;
  icons[5] = redWheel;


// stuff I need to load on page load

function load() {
	
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(40.58058466412761, -100.01953125), 4);
    geocoder = new GClientGeocoder();
  }


  setTimeout(initOverLabels, 50);

}

// google map: show the address and zoom in when searched

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 10);
	    map.addControl(new GLargeMapControl());
      	map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_TYPE);
	     // Read the data from example.xml
	      var request = GXmlHttp.create();
	      request.open("GET", "/Spots.xml", true);
	      request.onreadystatechange = function() {
	        if (request.readyState == 4) {
	          var xmlDoc = request.responseXML;
	          // obtain the array of markers and loop through it
	          var markers = xmlDoc.documentElement.getElementsByTagName("marker");

	          for (var i = 0; i < markers.length; i++) {
	            // obtain the attribues of each marker
	            var lat = parseFloat(markers[i].getAttribute("lat"));
	            var lng = parseFloat(markers[i].getAttribute("lng"));
	            var point = new GLatLng(lat,lng);
	            var html = markers[i].getAttribute("html");
	            var label = markers[i].getAttribute("label");
	            var icontype = parseInt(markers[i].getAttribute("rating"));
	            // create the marker
	            var marker = createMarker(point,label,html,icontype);
	            map.addOverlay(marker);
	          }
	        }
	      }
	      request.send(null);
        }
      }
    );
  }
 }

// Google Map: show the form and allow map to be clickable

function addSpot() {
	var addSpotFormWrapper = document.getElementById("addSpotFormWrapper");
	addSpotFormWrapper.className = "visible";
	var addSpotStep1 = document.getElementById("addSpotStep1");
	addSpotStep1.className = "";
//	var messages = document.getElementById("messages");
//	messages.className = "hidden";

	GEvent.addListener(map, "click", function(overlay, cpoint){
      map.clearOverlays();
        if (cpoint) {
        map.panTo(cpoint);
        lat = cpoint.lat();
        lon = cpoint.lng();
        document.getElementById('spotLat').value = lat;
        document.getElementById('spotLon').value = lon;
        // marker
        var cmarker = new PdMarker(cpoint, cicon);
        //cmarker.setHoverImage("img/icon_pfeil_grau.png");
        map.addOverlay(cmarker, cicon);
        }
    });

	return false;
}

// Google Map: add marker to map

function viewOnMap(latlon,name){
	var point = new GLatLng(latlon);
	var marker = createMarker(point,name)
  	map.addOverlay(marker);
}

//-->

  function createMarker(point,name,html,icontype) {
    var marker = new GMarker(point,icons[icontype]);
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    return marker;
  }


// Preload Images

var imgNames = ['/themes/default/spots/images/icon_house', '/themes/default/spots/images/icon_houseShadow', '/themes/default/spots/images/icon_redorangeWheel', '/themes/default/spots/images/icon_blackWheel', '/themes/default/spots/images/icon_tanWheel', '/themes/default/spots/images/icon_orangeWheel', '/themes/default/spots/images/icon_redWheel', '/themes/default/spots/images/icon_shadow', '/themes/default/spots/images/icon_yellowWheel', '/themes/default/spots/images/icon_greyWheel'];
var imgObjects = [];

for (var i = 0; i < imgNames.length; i++)
{
  imgObjects[i] = new Image();
  imgObjects[i].src = imgNames[i] + '.png';

}



