/* home-specific javascript */

var map;
var bounds = new GLatLngBounds();

// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
			
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
			
			
function zoomfit() {
	newzoom = map.getBoundsZoomLevel (bounds);
	newcenter = bounds.getCenter();
	map.setCenter (newcenter,newzoom);
}

function putmarker(lat,lng,infoHTML) {
	point = new GLatLng(lat,lng);
	var marker = new GMarker(point);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(infoHTML);
    });
	
	bounds.extend(point);
}

// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(lat,lng,index,type,infoHTML) {
  point = new GLatLng(lat,lng);
  // Create a lettered icon for this point using our icon class
  var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var letteredIcon = new GIcon(baseIcon);
  if(type=="event") {	
  	letteredIcon.image = "/wp-content/themes/dcc/_lib/img/markers/blue/marker" + letter + ".png";
  } else {
    letteredIcon.image = "/wp-content/themes/dcc/_lib/img/markers/red/marker" + letter + ".png";
  }
  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(infoHTML);
  });
  
  bounds.extend(point);
  zoomfit();
			
  return marker;
}

function init_wp_geo_map_dcc(items) {
	if (GBrowserIsCompatible()) {
		if (document.getElementById("wp_geo_map")) {
			map = new GMap2 (document.getElementById("wp_geo_map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(40.00715,75.70244), 2);
			
			i=0;
			index=0;
			while(i<items.length) {
				type = items[i]["type"];
				infoHTML = "";
				if(items[i]['image']!="") {
					infoHTML += "<div class='photo'><a href='"+items[i]["link"]+"' title='"+items[i]["name"]+"'><img src=\""+items[i]["image"]+"\" alt=\""+items[i]["name"]+"\"/></a></div>";
				}
				infoHTML += "<h5><a href='"+items[i]["link"]+"' title='"+items[i]["name"]+"'>"+items[i]["name"]+"</a></h5>";
				infoHTML += "<p>"+items[i]["street"]+"<br/>";
				infoHTML += items[i]["city"]+", "+items[i]["state"]+"</p>";
				map.addOverlay(createMarker(items[i]["latitude"], items[i]["longitude"], index, type, infoHTML));
				i++;
				if(i<items.length) {
					newType = items[i]["type"];
				} else {
					newType = null;
				}
				
				// if 'type' property changes, restart lettering (A, B, C, etc)
				if(type==newType) {
					index+=1;
				} else {
					index=0;
				}
			}
			
		}
	}
}

