

function Mapigator (id) {
	this.id = id;
	/*var this.map;
	var this.address;
	var this.mgr;
	var this.canvas;*/
	this.tries = 0;
}

var markerOptions = { clickable:false };
var infoWindowOptions = { maxWidth:200 };

var icoid = new Object;

var megaZoomLevel = 13;
var zoomLevel = 13;


var cluster = { height: 52, width: 53, url: '/assets/images/m1.png' };

var brooklynCenter = new GLatLng(40.6809471,-73.9773892);
var queensCenter = new GLatLng(40.75075,-73.90049);
var bronxCenter = new GLatLng(40.84175,-73.87049);
var liCenter = new GLatLng(40.771702,-73.491669);
var siCenter = new GLatLng(40.590101,-74.146729);

if (pub == 'queens') {
	var megaCenter = queensCenter;
	megaZoomLevel = 12;
} else if (pub == 'bronx') {
	var megaCenter = bronxCenter;
	megaZoomLevel = 12;
} else var megaCenter = new GLatLng(40.6809471,-73.9773892);
//http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&view=map&q=astoria+heights+playground,+ny&sll=40.762536,-73.89636&sspn=0.047652,0.090294&gl=us&ie=UTF8&hq=Astoria+Heights+Playground&hnear=Astoria+Heights+Playground,+Queens,+NY+11103&ll=40.75675,-73.88649&spn=0.095313,0.180588&z=13

if (pub == 'family') megaZoomLevel = 11;

if (pub == 'xxxcoghfgurier') {
	if (subPub == 'queens') megaCenter = queensCenter;
	if (subPub == 'bronx') megaCenter = bronxCenter;
	megaZoomLevel = 12;
	
	//var iconSpot = new GPoint(15, 32);
	var iconSizer = new GSize(37, 34);
	
	icoid.events = new GIcon(G_DEFAULT_ICON);
	icoid.events.iconSize = iconSizer;
	//icoid.events.shadow = "/assets/images/yournabe/map_shadow.png";
	//icoid.events.shadowSize = iconSizer;
	//icoid.bites.iconAnchor = iconSpot;
	//icoid.bites.iconWindowAnchor = iconSpot;
	icoid.events.image = '/assets/images/yournabe/map_pin_generic.png';
	
	
	icoid.nightlife = new GIcon(icoid.events);
	icoid.nightlife.image = '/assets/images/yournabe/map_pin_nightlife.png';
	
	icoid.film = new GIcon(icoid.events);
	icoid.film.image = '/assets/images/yournabe/map_pin_film.png';
	
	icoid.civic = new GIcon(icoid.events);
	icoid.civic.image = '/assets/images/yournabe/map_pin_civic.png';
	
	icoid.sports = new GIcon(icoid.events);
	icoid.sports.image = '/assets/images/yournabe/map_pin_sports.png';
	
	icoid.theater = new GIcon(icoid.events);
	icoid.theater.image = '/assets/images/yournabe/map_pin_theater.png';
	
	icoid.art = new GIcon(icoid.events);
	icoid.art.image = '/assets/images/yournabe/map_pin_art.png';

	icoid.family = new GIcon(icoid.events);
	icoid.family.image = '/assets/images/yournabe/map_pin_family.png';
	
	icoid.health = new GIcon(icoid.events);
	icoid.health.image = '/assets/images/yournabe/map_pin_health.png';
	
	icoid.sales = new GIcon(icoid.events);
	icoid.sales.image = '/assets/images/yournabe/map_pin_sales.png';

	icoid.blotter = new GIcon(icoid.events);
	icoid.blotter.image = '/assets/images/yournabe/map_pin_blotter.png';
	
	icoid.bites = new GIcon(icoid.events);
	icoid.bites.image = '/assets/images/yournabe/map_pin_bites.png';

}

Mapigator.prototype.megaMapInit = function() {

	this.canvas = document.getElementById("map_canvas_"+this.id);
	if (! this.canvas) return false;
	var addy = document.getElementById("map_address_"+this.id);
	var coo = document.getElementById("map_coordinates_"+this.id);
	
	if ((this.canvas != null) && GBrowserIsCompatible()) {
	
		this.map = new GMap2(this.canvas);
		this.map.addControl(new GSmallMapControl());
		this.map.setCenter(megaCenter, megaZoomLevel);
		
		if (coo != null) {
			if (coo.value != '') {
				var point = new GLatLng.fromUrlValue(coo.value);
				//alert(point);
				this.map.setCenter(point, zoomLevel);
				var marker = new GMarker(point, markerOptions);
				this.map.addOverlay(marker);
				return true;
			}
		}
		if (addy != null) {
			this.address = addy.value;
			return this.get_coords();
		}
	}
	return false;
}

Mapigator.prototype.get_coords = function() {
	if (this.address == 'MEGA') {
		
		var marks = this.markMaker();
		
		var maxz = 12;
		var grid = 40;
		if (marks.length > 50) {
			maxz = 13;
		}
		if (marks.length > 100) {
			maxz = 14;
			var grid = 50;
		}
		if (marks.length > 300) {
			maxz = 15;
			var grid = 60;
		}
		
		this.map.setCenter(megaCenter, megaZoomLevel);
	
		
		var markerCluster = new MarkerClusterer(this.map, marks, { gridSize: grid, maxZoom: maxz, styles: [cluster] });
		
		//var marker = new GMarker(marks[0], markerOptions);
		//this.map.addOverlay(marker);
		
		//mgr = new MarkerManager(map);
		//mgr.addMarkers(markMaker(), 1);
		//mgr.refresh();
		return true;
		
	} else {
		
		var geocoder = new GClientGeocoder();	
		
		//bind this to a generic object
		var obj = this;
		//pass pointy using a an anonymous closure function to preserve obj/this  
		//as owner of pointy. yeesh.
		geocoder.getLatLng(this.address, function(point){obj.pointy(point)});
		
		
		return true;
	}
}

Mapigator.prototype.markMaker = function() {
	this.markers = new Array();
	var formInputs = document.getElementsByTagName("input");
	this.x = 0;
	for (var i = 0; i < formInputs.length; i++) {
		var f = formInputs.item(i);
		if (f.className == "megamap_coordinates_"+this.id) {
			this.marky(f.value.split("{@@}"));
		} 
	}
	return this.markers;
}

Mapigator.prototype.marky = function(info) {
	point = new GLatLng.fromUrlValue(info[0]);
	iconName = info[2];
	myHtml = info[1];
	//alert(info[1]);
	//var thisIcon = icoid[iconName];
	//if (pub == 'courier') var marker = new GMarker(point, thisIcon);
	//else 
	var marker = new GMarker(point);
	
	if (marker != null) {
		this.markers[this.x] = marker;
		var obj = this;
		//GEvent.addListener(this.markers[this.x], "click", function(){obj.popper()});
		
		GEvent.addListener(this.markers[this.x], "click", function() {
			var myHtml = info[1];
			obj.map.openInfoWindowHtml(new GLatLng.fromUrlValue(info[0]), myHtml, infoWindowOptions);
		});
		
		this.x++;
	}
	
}

Mapigator.prototype.pointy = function(point) {
	if (! point) {
		this.canvas.style.display = 'none';
		/*if (tries > 0) canvas.style.display = 'none';
		else {
			var addy = document.getElementById("map_address_alt");
			tries++;
			if (addy) {
				var address = addy.value;
				//alert(address);
				var geocoder = new GClientGeocoder();
				geocoder.getLatLng(address, pointy);
			}
		}*/
	} else {
	
		//alert(0);
		this.map.setCenter(point, zoomLevel);
		//alert(1);
		var marker = new GMarker(point, markerOptions);
		//alert(2);
		this.map.addOverlay(marker);
	}
}

/*
function findMarker(info, id) {
	var geocoder = new GClientGeocoder();
	
	geocoder.getLatLng(info[0], 
		function(point) {
			if (! point) return false;
			map.addOverlay(createMarker(point, info, id));
		});
	return;
}

function createMarker(point, info, id) {
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
		var myHtml = '<a href="'+info[3]+'">'+info[1]+'<\/a><br><b>'+info[2]+'<\/b><br>'+info[0];
		map.openInfoWindowHtml(point, myHtml, infoWindowOptions);
	});
	
	return marker;
}
*/

