var LocationResult = Class.create({

	initialize: function(li){
		this.li = li;
		this.id = this.li.id.split('_')[1];
		this.form = this.li.down('form');
		this.li.observe('click', this.select_location.bind(this));
		this.form.observe('submit', this.get_directions.bind(this));
		this.ul = this.li.up('ul');
	},
	
	select_location: function(){
		// Short circuit when already selected
		if (this.li.hasClassName('active')) { return false; }
		// Hide currently active result
		$$('#map_results li.active div.extended').invoke('hide');
		$$('#map_results li.active').invoke('removeClassName', 'active');
		this.li.addClassName('active');
		this.ul.scrollTop = this.li.offsetTop - this.ul.offsetTop - 5;
		this.li.down('.extended').appear();
		// Select gMarker on Map
		try {
			if ($('map_canvas').mgr.active_marker != this.id) {
				var marker = $('map_canvas').mgr.markers[this.id];
				var point = marker.getLatLng();
				point.id = this.id;
				GEvent.trigger(marker, 'click', point);
			}
		} catch(err) {}
		// Track the click
		flash_track({events: 'event11', products: ';' + this.li.id});
		return false;
	},
	
	get_directions: function(){
		// Track the click
		flash_track({events: 'event12', products: ';' + this.li.id});
		return true;
	}

});

Event.observe(window, 'load', function() { 
	$$('#map_results li').each(function(li){
		new LocationResult(li);
	});
});