﻿BenefulMap.ModalLinkTo = BenefulMap.Modal.extend({
	init: function(modalTitle, modalElements, modalOptions, snippetPath, params, formElements, context) {
		this._super( modalTitle, modalElements, modalOptions, snippetPath, params, formElements);
		this.resultsPopulatedListener();
		this.mapState = {};
	},
	getMapState: function() {
		return this.mapState;
	},
	setMapState: function(newMapState) {
		this.mapState = newMapState;
	},
	addModalListeners: function() {
		this._super();
		this.populateLinkField();	
	},
	populateLinkField: function() {
		var mapState = this.getMapState();
		var json = mapState.toJSON();
		var url = jQuery.url.attr('source').split('?')[0];
		url = url + "?id=" + json;
		$("#results-go-here").html(url);
	},
	resultsPopulatedListener: function() {
		var self = this;
		$(document).bind('linkTo.resultsPopulated', function(event, mapState) {
			var params = {};
			params.id = mapState.toJSON();
			params.location = mapState.getLocation();
			self.setParams(params);
			self.setMapState(mapState);
		});
	}
});



