var map;
var offices = new Object();
var oldPoly;
function roundToDec(number,dec) {
// rounds number to X decimal places, defaults to 2
    dec = (!dec ? 2 : dec);
    return Math.round(number*Math.pow(10,dec))/Math.pow(10,dec);
}
function drawCircle(center, radius) {
	//Function created by Chris Haas
	if(oldPoly) {
		map.removeOverlay(oldPoly);
		oldPoly=null;
	}
	var circleQuality = 5;			//1 is best but more points, 5 looks pretty good, too
	var M = Math.PI / 180;			//Create Radian conversion constant
	var L = map.getBounds();		//Holds copy of map bounds for use below
	var sw = L.getSouthWest();
	var ne = L.getNorthEast();

	//The map is not completely square so this calculates the lat/lon ratio
	// this works because we create a square map
	var circleSquish = (ne.lng() - sw.lng()) / (ne.lat() - sw.lat());

	var points = [];							//Init Point Array
	//Loop through all degrees from 0 to 360
	for(var i=0; i<360; i+=circleQuality){
		var P = new GLatLng(
			center.lat() + (radius * Math.sin(i * M)),
			center.lng() + (radius * Math.cos(i * M)) * circleSquish
			);
		points.push(P);
	}
	points.push(points[0]);	// close the circle
	oldPoly = new GPolyline(points)
	map.addOverlay(oldPoly);
}


Office = function (n, address, p, f, lat, lng) {
	this.name = n;
	this.address = address;
	this.lat = lat;
	this.lng = lng;
	this.phone = p;
	this.fax = f;
	this.gpoint = new GLatLng(lat, lng);
	this.spanLat = (arguments[6] != "undefined") ? arguments[6] : 0.013952;
	this.spanLong = (arguments[7] != "undefined") ? arguments[7] : 0.021629;
	this.slink = (arguments[8] != "undefined") ? arguments[8] : "undefined";
	this.marker = null;
	this.point = null;
}

Office.prototype.buildLink = function () {
	if (this.slink != "undefined" && this.slink != undefined) {
		return this.slink;
	} else {
		var lnk;
		lnk = "http://maps.google.com/maps?q="+escape(this.address)+
		"&amp;ll="+this.lat+","+this.lng+
		"&amp;spn="+this.spanLat+","+this.spanLong+"&amp;om=1";
		return lnk;
	}
}
Office.prototype.showCallout = function () {
	var content = "<span><strong>"+this.name+"</strong> <a href=\""+this.buildLink()+"\" target=\"_blank\">View Detail &raquo;</a><br />"+this.address+"<br />P#: "+this.phone+"<br />F#: "+this.fax+"<br /></span>";
	return content;
}

loadMap = function() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		var avgLat = 0;
		var avgLong = 0;

		offices.chatham = new Office("Chatham, NY", "2 Park Row, 12037", "(518) 392-8040", "(518) 392-8050", 42.362365, (-73.596897));
		offices.cambridge = new Office("Cambridge, NY", "1 W Main St, 12816", "(518) 677-3135", "(518) 677-3137", 43.027961, (-73.376892));
		offices.sand_lake = new Office("Sand Lake, NY", "1 Taborton Rd, 12018", "(518) 674-3464", "(518) 674-3250", 42.637637, (-73.540721));
		offices.valatie = new Office("East Greenbush, NY", "699 Columbia Turnpike", "(518) 477-5423", "(518) 477-5894", 42.598145,(-73.69483));
		//offices.south_egremont = new Office("South Egremont, MA", "48 main St, South Egremont, MA", "(413) 528-3340", "(413) 528-3899", 42.101153, (-73.456575), "undefined", "undefined","http://maps.google.com/maps?q=48+main+St,+South+Egremont,+MA&om=1&hl=en&sll=37.062500,-95.677068&sspn=23.875000,57.630033&cid=42101153,-73456575,13001194291300485616&li=lmd&z=14&t=m");
		offices.pittsfield = new Office("Pittsfield, MA", "137 North Street, 01201", "(413) 499-7490", "(413) 499-7493", 42.451645,(-73.253832));
		offices.stockbridge = new Office("Stockbridge, MA", "9 South Street, 01262", "(413) 298-0610", "(413) 298-0614", 42.280425,(-73.312731));
		offices.salisbury = new Office("Salisbury, CT", "19 Main Street, 06068", "(860) 435-0700", "(860) 435-9334", 41.981404,(-73.422909));
		

		for (var i in offices) {
			//avgLat += offices[i].lat;
			avgLong += offices[i].lng;
		}
		avgLat = roundToDec(((offices['salisbury'].lat)/1), 6);
		//avgLat = roundToDec((avgLat/5), 6);
		avgLong = roundToDec((avgLong/7), 6);
		var ctr = new GLatLng(avgLat, avgLong);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(ctr, 8);
		for (var i in offices) {
			offices[i].marker = new GMarker(offices[i].gpoint);
			offices[i].marker.ptr = offices[i];
			map.addOverlay(offices[i].marker);
		}
		GEvent.addListener(map, 'click', function(overlay, point) {
			if (overlay) {
				overlay.openInfoWindowHtml(overlay.ptr.showCallout());
			} else if (point) {
				//map.addOverlay(new GMarker(point));
			}
		});
		//drawCircle(map.getCenter(), 0.8);
	}
}

function showOffice(name) {
	offices[name].marker.openInfoWindowHtml(offices[name].marker.ptr.showCallout());
}

window.onload=loadMap;
window.onunload=GUnload;
