
var Locator = new Class({
	Implements: [Options, Events],
	options: {
		urlGetList: "/locator/list",
		imgIcon: '/images/map/gp_placemark.png',
		imgIconHigh: '/images/map/gp_placemark_hover.png',
		imgIconShd: '/images/map/gp_placemark_shadow.png',
		elmap: 'map-canvas'
		
	},
	initialize: function(form, options){
			
		// blocks
		this.elmap = $(this.options.elmap);
			
		// create the map
		this.map = new GMap2(document.getElementById(this.options.elmap));
		this.mapCenter = new GLatLng("43.737173","-79.343422");
		this.mapZoom = 11;
		this.map.setCenter(this.mapCenter, this.mapZoom);
		this.map.addControl(new GLargeMapControl3D());
		
		this.pmIcon = new GIcon(G_DEFAULT_ICON);
		//this.pmIcon.image = this.options.imgIcon;
		//this.pmIcon.shadow = this.options.imgIconShd;
		//this.pmIcon.highlight =  this.options.imgIconHigh;
		//this.pmIcon.iconSize = new GSize(32, 32);
		//this.pmIcon.shadowSize = new GSize(55, 32);
		//this.pmIcon.iconAnchor = new GPoint(32, 32);
		//this.pmIcon.imageMap = [0,0, 32,0, 32,32, 0,32];
		
		// Set up our GMarkerOptions object
		this.cpMarker = { icon:this.pmIcon };
			
		// create manager
		this.mgr = new MarkerManager(this.map);
		
		// set loaded false (will flip to true on load)
		this.cpLoaded = false;
		
		// load  the points
		var jsonRequest = new Request.JSON({
			url: this.options.urlGetList, 
			onRequest: function() { /*alert("requesting"); */ },
			onComplete: this.loadLocations.bind(this),
			onError: function() { /* alert("failure");  */ }
		}).get();
		
		
		// bind the location buttons
		var links = $$("a").filter(function(el) {
			return el.rel && el.rel.test(/^map/i);
		});

		this.links = new Array();
		$$(links).each(function(el) {
			var rel0 = el.rel.replace(/[[]|]/gi," ");
			var relsize = rel0.split(" ");
			var location_id = relsize[1];
			el.addEvent('click', this.clickLink.bindWithEvent(this,location_id));
			this.links[location_id] = el;
		}, this);
		
		
	},
	
	
	
	loadLocations: function(response, text){
	
		var locations = response.locations;
		
		if (locations) {
			this.locations = new Array();
			var placemarks = [];
			for (var i in locations) {
				
				var location = locations[i];
				var location_id = location.id;
				var point = new GLatLng(location.lat, location.lng);
				location.placemark = new GMarker(point, this.cpMarker);
				
				/*
				 * Set HTMLK
				 */				
				var overviewHtml;
				if (location.overviewHtml) {
					overviewHtml = location.overviewHtml;
				}
				else {
					overviewHtml = " ";
				}
				
				var servicesHtml;
				if (location.servicesHtml) {
					servicesHtml = location.servicesHtml;
				}
				else {
					servicesHtml = " ";
				}
				
				location.placemark.bindInfoWindowTabs([
					new GInfoWindowTab("Overview", overviewHtml), 
					new GInfoWindowTab("Services", servicesHtml)
				], {
					maxWidth: 300
				});
				
				
				/* 
				 * Set Listeners
				 */
				
				location.placemark.location_id = location_id;
//				location.placemark.pfImage = this.pmIcon.image;
//				location.placemark.pfHighlight = this.pmIcon.highlight;
//				
//				GEvent.addListener(location.placemark, "mouseout", function(){
//					this.setImage(this.pfImage);
//				});
//				GEvent.addListener(location.placemark, "mouseover", function(){
//					this.setImage(this.pfHighlight);
//				});
				GEvent.bind(this.map, "click", this, this.mapclickLink);
				GEvent.bind(location.placemark, "infowindowclose", this, this.closeLocation);
				
				this.locations[location_id] = location;
				placemarks.push(location.placemark);
			}
			
			this.mgr.addMarkers(placemarks, 8);
			this.mgr.refresh();
			this.cpLoaded = true;
		
		}
	},
	
	
	mapclickLink: function (marker, point) {		
		if (marker != null) {
			var location_id = marker.location_id;
			if (location_id && this.links[location_id] && location_id != this.locationSelected) {
				this.clickLink(null, location_id);
			}		
		}
	},
	
	
	clickLink: function (event, location_id) {
		if (event)
			event.preventDefault();		
		this.closeLocation();
		this.locationSelected = location_id;
		this.links[location_id].addClass('current');
		GEvent.trigger(this.locations[location_id].placemark, 'click');		
	},
	
	
	closeLocation: function() {
		if (this.locationSelected != null) {
			if ($defined(this.links[this.locationSelected]))
				this.links[this.locationSelected].removeClass('current');
			this.locationSelected = null;
		}			
	}
	
	
	
});