
var LRC = {};

LRC.Map = function(map_id) {
	this.map_id = map_id || "map";
	
	this.geojson_format = new OpenLayers.Format.GeoJSON();
	this._project_900913 = new OpenLayers.Projection("EPSG:900913");
	this._project_4326 = new OpenLayers.Projection("EPSG:4326");
	
	
	this.map = new OpenLayers.Map(this.map_id, {
		projection: this._project_900913,
		displayProjection: this._project_4326,
		maxResolution: 39135.758475,
		units: "m",
		maxExtent: new OpenLayers.Bounds(-180, -85, 180, 85).transform(
			this._project_4326, this._project_900913),
		restrictedExtent: new OpenLayers.Bounds(-180, -85, 180, 85).transform(
			this._project_4326, this._project_900913)
	});
	
	this.base_layer = new OpenLayers.Layer.Google(
		"Google Hybrid", {
			type: G_HYBRID_MAP,
			sphericalMercator: true,
			numZoomLevels: 15,
			transitionEffect: "resize"
		});
	
	var SHADOW_Z_INDEX = 10;
    var MARKER_Z_INDEX = 11;
    
	var report_styles = new OpenLayers.StyleMap({
        // Set the external graphic and background graphic images.
        externalGraphic: "/assets/lrc/img/marker-gold.png",
        backgroundGraphic: "/assets/lrc/img/marker_shadow.png",
        
        // Makes sure the background graphic is placed correctly relative
        // to the external graphic.
        backgroundXOffset: 0,
        backgroundYOffset: -7,
        
        // Set the z-indexes of both graphics to make sure the background
        // graphics stay in the background (shadows on top of markers looks
        // odd; let's not do that).
        graphicZIndex: MARKER_Z_INDEX,
        backgroundGraphicZIndex: SHADOW_Z_INDEX,
        
        pointRadius: 10
    });
	
	this.reports_layer = new OpenLayers.Layer.Vector("Reports", {
		styleMap: report_styles,
		rendererOptions: { yOrdering: true }
	});
	
	var that = this;
	this.select_control = new OpenLayers.Control.SelectFeature(
		[this.reports_layer], {
            clickout: true, toggle: false,
            multiple: false, hover: false,
            //toggleKey: "ctrlKey", // ctrl key removes from selection
            //multipleKey: "shiftKey", // shift key adds to selection
			onSelect: function(feature) { that.onFeatureSelect(feature); },
			onUnselect: function(feature) { that.onFeatureUnselect(feature); }
        });
	this.map.addControl(this.select_control);
	this.select_control.activate();
	
	
	this.map.addLayers([this.base_layer, this.reports_layer]);
	
	this.add_features();
	
	this.map.zoomToMaxExtent();
};

LRC.Map.prototype = {
	add_features: function() {
		var that = this;
		jQuery.getJSON("/lrc/location/map", function(data, status) {
			var features = that.geojson_format.read(data);
			that.reports_layer.addFeatures(features);
			
			that.map.zoomToExtent(that.reports_layer.getDataExtent());
			
			that.select_control.deactivate();
			that.select_control.activate();
		});
	},
	
	onPopupClose: function(feature) {
		this.select_control.unselect(feature);
	},
    onFeatureSelect: function(feature) {
        var that = this;
        popup = new OpenLayers.Popup.Anchored(
			"chicken", 
			feature.geometry.getBounds().getCenterLonLat(),
            null,
			make_map_popup(feature),
            //"<div style='font-size:.8em'>Feature: " + feature.id +"<br />Area: " + feature.geometry.getArea()+"</div>",
            null, true, function() { return that.onPopupClose(feature); });
		popup.closeOnMove = true;
        feature.popup = popup;
        this.map.addPopup(popup);
    },
    onFeatureUnselect: function(feature) {
        this.map.removePopup(feature.popup);
		// Cannot 'destroy()' the popup -> it causes an error that breaks map panning and zooming
        //feature.popup.destroy();
        feature.popup = null;
    },
	
	goto_page: function(page) {
		if (page < 0)
			page = 0;
		else if (page > this.page_count)
			page = this.page_count - 1;
		
		// Load data for next page
		var data = jQuery.extend({}, this.base_data, { page: page });
		var that = this;
		jQuery.getJSON(this.url, data, function(){ 
			that._page_callback.apply(that, arguments); 
		});
	},
	
	_page_callback: function(data, status) {
		if (status != "success") {
			//console.debug(
			//	"Page-request failed: status='" + status + "', data='" + data + "'");
			return null;
		} else {
			this.reports_layer.removeFeatures(this.reports_layer.features);
			var features = this.geojson_format.read(data);
			this.reports_layer.addFeatures(features);
		}
	},
	
	goto_next_page: function() {
		return goto_page(this.current_page + 1);
	},
	
	goto_prev_page: function() {
		return goto_page(this.current_page - 1);
	}
};



