var cm_map;
var cm_openInfowindow;
var cm_mapMarkers = [];
var cm_mapHTMLS = [];
// Change these parameters to customize map
var param_wsId = "od6";
var param_ssKey = "o16162288751915453340.4402783830945175750";
var param_useSidebar = false;
var param_titleColumn = "title";
var param_descriptionColumn = "description";
var param_latColumn = "latitude";
var param_lngColumn = "longitude";
var param_rankColumn = "rank";
var param_iconType = "green";
var param_iconOverType = "orange";

    function createMarker(point, name, label, address, type) {
      var marker = new LabeledMarker(point, {icon: customIcons[type], labelText: label, labelOffset: new GSize(-6, -10)});
      markerGroups[type].push(marker);
      var html = "<b>" + name + "</b> " + address;
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }function cm_load() {  
  var myLatlng = new google.maps.LatLng(43.907787,-79.359741);
  var myOptions = {
    zoom: 2,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  cm_map = new google.maps.Map(document.getElementById("cm_map"), myOptions);
   var usingRank = true;
   var bounds = new google.maps.LatLngBounds();
   	var hidProductLocations =  $("#hidProductLocations").val();
	if(hidProductLocations) {
	var hidProductLocationsArray = new Array();
	hidProductLocations= hidProductLocations.split("~*");
	for (var i = 0; i < hidProductLocations.length-1; i++) {
      var lat =(!isNaN(parseFloat($("#hidLat_"+i).val())))? parseFloat($("#hidLat_"+i).val()):43.907787;
      var lng =(!isNaN(parseFloat($("#hidLng_"+i).val())))? parseFloat($("#hidLng_"+i).val()):-79.359741;
	  var locCountMap =($("#locCountMap"+i).val())? parseFloat($("#locCountMap"+i).val()):0;
      var point = new google.maps.LatLng(lat,lng);
      var html = "<div style='font-size:10px;width:200px;'> ";
      html += "<strong>" + $("#hidLocName_"+i).val()
              + "</strong>"; 
      html += ", " + $("#hidAddress_"+i).val();			  
      var label = $("#hidLocName_"+i).val();
      var rank = i+1;
	  if(locCountMap>0) {
		  rank = locCountMap+1;
	  }
	 html += "</div>";
	 var marker = cm_createMarker(cm_map,point,label,html,rank);	
      cm_mapMarkers.push(marker);
      cm_mapHTMLS.push(html);
      bounds.extend(point);
	}
	}
  cm_map.setZoom(19);
  if(rank>1){
  	cm_map.fitBounds(bounds);
  }
  cm_map.setCenter(bounds.getCenter());
}
function cm_setInfowindow(newInfowindow) {
  if (cm_openInfowindow != undefined) {
    cm_openInfowindow.close();
  }
  cm_openInfowindow = newInfowindow;
}
function cm_createMarker(map, latlng, title, html, rank) {
  var iconSize = new google.maps.Size(20, 34);
  var iconShadowSize = new google.maps.Size(37, 34);
  var iconHotSpotOffset = new google.maps.Point(9, 0); // Should this be (9, 34)?
  var iconPosition = new google.maps.Point(0, 0);
  var infoWindowAnchor = new google.maps.Point(9, 2);
  var infoShadowAnchor = new google.maps.Point(18, 25);
  var iconShadowUrl = "http://www.google.com/mapfiles/shadow50.png";
  var iconImageUrl;
  var iconImageOverUrl;
  var iconImageOutUrl;
  if(rank > 0 && rank < 100) {
    iconImageOutUrl = "http://gmaps-samples.googlecode.com/svn/trunk/" +
        "markers/" + param_iconType + "/marker" + rank + ".png";
    iconImageOverUrl = "http://gmaps-samples.googlecode.com/svn/trunk/" +
        "markers/" + param_iconOverType + "/marker" + rank + ".png";
    iconImageUrl = iconImageOutUrl;
  } else { 
    iconImageOutUrl = "http://gmaps-samples.googlecode.com/svn/trunk/" +
        "markers/" + param_iconType + "/blank.png";
    iconImageOverUrl = "http://gmaps-samples.googlecode.com/svn/trunk/" +
        "markers/" + param_iconOverType + "/blank.png";
    iconImageUrl = iconImageOutUrl;
  }
  var markerShadow =
      new google.maps.MarkerImage(iconShadowUrl, iconShadowSize,
                                  iconPosition, iconHotSpotOffset);
  var markerImage =
      new google.maps.MarkerImage(iconImageUrl, iconSize,
                                  iconPosition, iconHotSpotOffset);
  var markerImageOver =
      new google.maps.MarkerImage(iconImageOverUrl, iconSize,
                                  iconPosition, iconHotSpotOffset);
  var markerImageOut =
      new google.maps.MarkerImage(iconImageOutUrl, iconSize,
                                  iconPosition, iconHotSpotOffset);
  var markerOptions = {
    title: title,
    icon: markerImage,
    shadow: markerShadow,
    position: latlng,
    map: map
  }
  var marker = new google.maps.Marker(markerOptions);
  google.maps.event.addListener(marker, "click", function() {
	
    var infowindowOptions = {
      content: html,
	  maxWidth:200
    }
	cm_map.setZoom(12);
	cm_map.setCenter(marker.getPosition());
    var infowindow = new google.maps.InfoWindow(infowindowOptions);
    cm_setInfowindow(infowindow);
    infowindow.open(map, marker);
    marker.setIcon(markerImageOut);
  });
  google.maps.event.addListener(marker, "mouseover", function() {
    marker.setIcon(markerImageOver);
  });
  google.maps.event.addListener(marker, "mouseout", function() {
    marker.setIcon(markerImageOut);
  });
  return marker;
}
function cm_markerClicked(markerNum) {
	var offTop =$("#cm_map").offset().top-60;
	 $('html,body').animate({scrollTop:offTop},'slow');	
  var infowindowOptions = {
    content: cm_mapHTMLS[markerNum]
  }
  cm_map.setZoom(12);
  cm_map.setCenter(cm_mapMarkers[markerNum].getPosition());
  var infowindow = new google.maps.InfoWindow(infowindowOptions);
  infowindow.open(cm_map, cm_mapMarkers[markerNum]);
  cm_setInfowindow(infowindow);
}
	setTimeout('cm_load()', 500);