var Routes = {
	list: new Hash(),
	add: function(id, instance) {
		this.list[id] = instance;
	},
	getInstance: function(id) {
		return this.list[id];
	}
}
var RouteManager = Class.create();
RouteManager.prototype = {
	itemid: 0,
	markers: [],
	mapDivId: '',
	mapDiv: null,
	handleLinks: true,
	initialize: function(itemid, handleLinks) {
		this.itemid = itemid;
		this.markers = [];
		this.mapDivId = "gmap_"+itemid;
		this.mapDiv = document.getElementById(this.mapDivId);
		this.handleLinks = handleLinks;
		Routes.add(itemid, this);
	},
	addPoint: function(x, y) {
		var icon = new GIcon();
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var point = new GLatLng(x, y);
		marker = new GMarker(point, icon);
		this.mgr.addMarker(marker, 1);
		return marker;
	},
	setHome: function(lat, lng, zoom) {
		this.lat = lat;
		this.lng = lng;
		this.startlat = lat;
		this.startlng = lng;
		this.zoomlev = zoom;
		this.maptype = G_NORMAL_MAP;
		this.maptypeu = 'm';
		if (this.handleLinks) {
			link = document.location.href.split('#');
			if (link && link[1]) {
				keys = link[1].split('&');
				for (i=0;i<keys.length;i++) {
					kv = keys[i].split('=');
					if (kv[0] == 'pid')
						this.pid = kv[1];
					if (kv[0] == 'lat')
						this.lat = kv[1];
					if (kv[0] == 'lng')
						this.lng = kv[1];
					if (kv[0] == 'maptype') {
						this.maptypeu = kv[1];
						if (kv[1] == 'm')
							this.maptype = G_NORMAL_MAP;
						if (kv[1] == 'h')
							this.maptype = G_HYBRID_TYPE;
						if (kv[1] == 'k')
							this.maptype = G_SATELLITE_MAP;
					}
					if (kv[0] == 'zoomlev')
						this.zoomlev = parseInt(kv[1]);
					
				}
			}
		}
	},
	loadGMaps: function(startlat, startlong) {
		if (!GBrowserIsCompatible()) {
			return;
		}
		this.map = new GMap2(this.mapDiv);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		
		this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoomlev);
		this.map.setMapType(this.maptype)
		var mgrOptions = { borderPadding: 50, maxZoom: 18, trackMarkers: true };
		this.mgr = new GMarkerManager(this.map, mgrOptions);
		
		handler = this;

		var markerx = this.addPoint(startlat, startlong);
		GEvent.addListener(markerx, "click", function() {
		    markerx.openInfoWindowHtml("<b>Start</b>");
		});
		GEvent.addListener(this.map, "zoomend", function(oldLevel,  newLevel) {
			handler.zoomlev = newLevel;
			handler.updateLink();
		});
		GEvent.addListener(this.map, "maptypechanged", function() {
			type = handler.map.getCurrentMapType();
			handler.maptypeu = type.getUrlArg();
			handler.updateLink();
		});
	},
	updateLink: function() {
		if (this.handleLinks) {
			document.location = handler.buildLink();
		}
	},
	buildLink: function() {
		var str = '#lat=' + this.lat + '&lng=' + this.lng + '&zoomlev=' + this.zoomlev + '&maptype=' + this.maptypeu;
		if (this.pid) 
			str += '&pid=' + this.pid;
		return str;
	},
	addRoute: function(line, zoom) {
		var encodedPolyline = new GPolyline.fromEncoded(
			{
			    color: "#FF0000",
			    weight: 2,
			    points: line,
			    levels: zoom,
			    zoomFactor: 32,
			    numLevels: 4
			}
		);
		this.map.addOverlay(encodedPolyline);
	},
	addPhoto: function(id, lat, lon, pid, preview) {
		this.markers[id] = this.addPoint(lat, lon);
		this.markers[id].previewText = preview;
		if (this.pid && this.pid == pid) {
			this.highlightpid = id;
			if (!browser.isIE)
		    	mgr.markers[id].openInfoWindowHtml(preview);
		}
		mgr = this;
		GEvent.addListener(this.markers[id], "click", function() {
	    		mgr.markers[id].openInfoWindowHtml(preview);
				point = mgr.markers[id].getPoint();
				mgr.lat = point.lat();
				mgr.lng = point.lng();
				mgr.pid = pid;
				mgr.updateLink();
			}
		);
	},
	saveView: function() {
		var center = this.map.getCenter();
		var zoom = this.map.getZoom();
		var xmlHttp= new Ajax.Request(nnph_ajax_root + 'ajax/item/'+this.itemid + '/save-view/',
			{	method: "get",
				parameters: {lat: center.lat(), lng: center.lng(), zoom: zoom},
				onComplete:function(request){
					//alert(request.responseText);
				}
			}
		);
	},
	removeView: function() {
		var xmlHttp= new Ajax.Request(nnph_ajax_root + 'ajax/item/'+this.itemid + '/remove-view/',
			{	method: "get",
				onComplete:function(request){
					//alert(request.responseText);
				}
			}
		);
	}
}
