//<![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()) {
	var lat = document.getElementById("spotLat").value;
	var lon = document.getElementById("spotLon").value;
    map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(lat, lon), 17);
    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);
}

setTimeout(initOverLabels, 50);

}

function showAddPhoto() {
	var addPhoto = document.getElementById("addPhoto");
	addPhoto.className = "visible";
	
	return false;
	
}

function showAllPhoto() {
	var allPhotos = document.getElementById("allPhotos");
	allPhotos.className = "visible";
	
	return false;
	
}

function showAddComment() {
	var addComment = document.getElementById("addComment");
	addComment.className = "visible";
	document.getElementById('comment').focus();	
	
	return false;
	
}

  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';

}


// Form fix for line breaks

function escapeVal(textarea){
textarea.value=escape(textarea.value)
for(i=0; i<textarea.value.length; i++){
 	if(textarea.value.indexOf("%0D%0A") > -1){
	textarea.value=textarea.value.replace("%0D%0A","<br />")
	}
	else if(textarea.value.indexOf("%0A") > -1){
	textarea.value=textarea.value.replace("%0A","<br />")
	}
	else if(textarea.value.indexOf("%0D") > -1){
	textarea.value=textarea.value.replace("%0D","<br />")
	}
}

textarea.value=unescape(textarea.value)

validateFields();

}

