var tour_start_point;
var tour_start_position;
var map;
var infowindow;

function tour_initialize() {
  var my_position = null;

  var marker_main = new google.maps.MarkerImage('/images/layout/marker-default.png',
      new google.maps.Size(24, 38),
      new google.maps.Point(0, 0),
      new google.maps.Point(12, 38));

  var marker_youarehere = new google.maps.MarkerImage('/images/layout/marker-youarehere.png',
      new google.maps.Size(50, 50),
      new google.maps.Point(0,0),
      new google.maps.Point(25, 25));

  if (user_address) {
    var geocoder = new google.maps.Geocoder();
    if (geocoder) {
      // NOTE: this is asynchronous so likely returns after the map is created
      geocoder.geocode( { 'address': user_address }, function(results, status) {
	if (status == google.maps.GeocoderStatus.OK) {
	  my_position = results[0].geometry.location;
	  if ( map ) {
	    /*
	    map.setCenter(results[0].geometry.location);
	    */
	    var marker = new google.maps.Marker({
	      map: map,
	      icon: marker_youarehere,
	      position: results[0].geometry.location,
	      zIndex: 0,
	      title: "You are here"
	    });
	  }
	}
      });
    }
    else {
      alert("Geocode was not successful for the following reason: " + status);
    }
  }

  if ( my_position == null ) {
    var latlng = new google.maps.LatLng(42.5, -98.35); // 49.5lat = center US
    my_position = latlng;
  }

  tour_start_point = my_position;
  tour_start_zoom = 4;

  var myOptions = {
    zoom: tour_start_zoom,
    center: tour_start_point,
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    scrollwheel: false
  };

  if (tour_dates.length == 1) {
    myOptions.zoom = 8;
    myOptions.mapTypeId = google.maps.MapTypeId.ROADMAP;
    myOptions.center = new google.maps.LatLng(tour_dates[0].lat, tour_dates[0].long);
  }

  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  if ( typeof(tour_dates) != "undefined" ) {
    infowindow = new google.maps.InfoWindow({
      content: "<LOADING>"
    });

    // make sure closing via the [x] also goes back to full map view
    google.maps.event.addListener(infowindow, 'closeclick', function() {
      fullmap();
    });

    for (i = 0; i < tour_dates.length; i++) {
      var pos = new google.maps.LatLng(tour_dates[i].lat, tour_dates[i].long);
      if (tour_dates.length == 1) {
    	  var infoContent = "";
      }
      else
      {
      var infoContent = "<div style='text-transform:capitalize; padding-right: 15px; font-size: 14px; color: #000000;'>"
          + tour_dates[i].show_date + " @ " + tour_dates[i].name.toLowerCase()
          + "</div>";

          if ( tour_dates[i].address1 ) { infoContent += tour_dates[i].address1 + "<br/>"; }
          if ( tour_dates[i].address2 ) { infoContent += tour_dates[i].address2 + "<br/>"; }

          if ( tour_dates[i].city || tour_dates[i].state || tour_dates[i].country ) {
    	var csc;
    	if ( tour_dates[i].city ) { csc = tour_dates[i].city; }
    	if ( tour_dates[i].state ) {
    	  if ( csc ) csc += ", ";
    	  csc += tour_dates[i].state;
    	}
    	if ( tour_dates[i].country ) {
    	  if ( csc ) csc += " ";
    	  csc += tour_dates[i].country;
    	}
    	infoContent += csc + "<br/>";
          }

          infoContent += "<br/><a style='text-decoration:none;padding:2px;background-color:#ff0000;color:#ffffff' href='/tour/date/id/"
    	+ tour_dates[i].tour_date_id
    	+ "'>Event Detail Page &raquo;</a><br/>";

          infoContent += "<br><a target='_blank' style='color:#ff0000;text-decoration:none' href='http://maps.google.com/maps?daddr="
    	+ tour_dates[i].lat + "," + tour_dates[i].long
    	+ "+(" + tour_dates[i].name.replace(/ /g, "+") + ")"
    	+ "'>Get directions &raquo;</a>";

          infoContent += " &nbsp;&nbsp;&nbsp;&nbsp; <a style='color:#ff0000;text-decoration:none' onclick='fullmap();'>Back to full map &raquo;</a>";
      }  
      var marker = new google.maps.Marker({
	map: map,
	icon: marker_main,
	position: pos,
	zIndex: 1,
	title: tour_dates[i].show_date + ", " + tour_dates[i].city + " @ " + tour_dates[i].name,
	infoContent: infoContent
      });

      google.maps.event.addListener(marker, 'click', function() {
	map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
	map.setZoom(8);
	map.setCenter(this.position); 
	infowindow.setContent(this.infoContent);
	infowindow.open(map, this);
      });
    }
  }
}

function fullmap() {
  if ( map ) {
    map.setMapTypeId(google.maps.MapTypeId.TERRAIN);
    map.setZoom(tour_start_zoom);
    map.setCenter(tour_start_point);
    infowindow.close();
  }
}


