//@author updated ssidhu May 2009
//@author Yunwei Dong (yunwei.dong@gmail.com) 2011

// Store the arctic and antarctic maps
var maps = new Array(); 
var boxes = new Array();
//Store the layers where the user selection is drawn
var polygonLayers = new Array(); 
								
//Store the layers where the footprints are drawn
var resultsLayers = new Array();

//Store the layers where the markers for the user selected points are drawn
var selectionMarkerLayers = new Array(); 

var wmsLayers = new Array(); // Store the WMS layers

var mouses = new Array(); // Store the OpenLayers.Control.MousePosition objects
						
var hovers = new Array(); // Store the OpenLayers.Control.SelectFeature objects
							
//Store the longitudes and latitudes of the user selected points (four points)
var lonlats = new Array(new Array(), new Array());

var slides = new Array();
var extentForDisplay = new OpenLayers.Bounds(-5000000,-5000000,4000000,4000000);
var extentTilecache = new OpenLayers.Bounds(-5000000, -5000000, 5000000, 5000000);
var extentFrameForDisplay = new OpenLayers.Bounds(-5000000, -5000000, 6000000, 5500000);

// wholeMapLonlats[0] stores the longitudes and latitudes of the 4 boundary
// points of the arctic map
// wholeMapLonlats[1] stores the longitudes and latitudes of the 4 boundary
// points of the antarctic map
// this represents the boundary of whitesnows dataset in the database
var wholeMapLonlats = new Array(new Array(), new Array());
// four points for the arctic map
wholeMapLonlats[0][0] = new OpenLayers.LonLat(125, 40);
wholeMapLonlats[0][1] = new OpenLayers.LonLat(35, 35);
wholeMapLonlats[0][2] = new OpenLayers.LonLat(-55, 35);
wholeMapLonlats[0][3] = new OpenLayers.LonLat(-145, 35);
// four points for the antarctic map
wholeMapLonlats[1][0] = new OpenLayers.LonLat(-45, -40);
wholeMapLonlats[1][1] = new OpenLayers.LonLat(45, -40);
wholeMapLonlats[1][2] = new OpenLayers.LonLat(135, -40);
wholeMapLonlats[1][3] = new OpenLayers.LonLat(-135, -40);

// Temporary storage variables for the user selection
var points = new Array(), points_ring, area_of_interest_polygon, area_of_interest_feature;

// Projections
var projGlobal = new OpenLayers.Projection('EPSG:4326'); // long lat projection system, used to display coordniates on the map
var projArctic = new OpenLayers.Projection('EPSG:3573');  // arctic projection system
var projAntarctic = new OpenLayers.Projection('EPSG:3031');

// The size of the markers representing the user selected points on the map
var icon_size = new OpenLayers.Size(17, 17); 

// Store the IDs of the footprints being displayed on the map
var resultsIdArrays = new Array(new Array(), new Array());

// Store polygons representing the footprints be displayed on the map
var resultsVectorArrays = new Array(new Array(), new Array());

// Store markers displayed at the center of each footprint on the map
var resultsMarkerArrays = new Array(new Array(), new Array());

// Styles for features on the map
var vector_style = OpenLayers.Util.extend( {},
		OpenLayers.Feature.Vector.style['select']);
vector_style.strokeColor = "#7F0000";
vector_style.fillColor = "red";

var select_style = OpenLayers.Util.extend( {},
		OpenLayers.Feature.Vector.style['default']);

var default_style = OpenLayers.Util.extend( {},
		OpenLayers.Feature.Vector.style['select']);

var resultsMarkerDefaultStyle = {
	externalGraphic : "./img/blue.png",
	pointRadius : 18,
	cursor : "pointer"
};
var resultsMarkerSelectStyle = {
	externalGraphic : "./img/orange.png",
	pointRadius : 18,
	cursor : "pointer"
};

// Store the ID and content of the brief popup for each footprint
var briefPopupIdArrays = new Array(new Array(), new Array());
var briefPopupContentArrays = new Array(new Array(), new Array());

var search_flag = true;
var dbrequest_isloaded = false;

// Load the map
// region can be either arctic or antarctic
function load(region) {
	
	// store basic information about arctic map and antarctic map
	// check OpenLayer Map object for further info
	// the information in here has to be consistent with that setted up in titlecache module in Apache (//.../webapps/titlecache)
	var options = new Array();
	// Options for the arctic map
	options[0] = {
		units : "m",
		maxResolution : "auto",
		projection : "EPSG:3573",
		resolutions : [ 42968.75, 21484.375, 10742.1875, 5371.09375,
				2685.546875, 1342.7734375, 671.38671875, 335.693359375,
				167.8466796875, 83.92333984375, 41.961669921875,
				20.9808349609375, 10.49041748046875 ],
		maxExtent : extentForDisplay,
		restrictedExtent : extentFrameForDisplay
	};
	// Options for the antarctic map
	options[1] = {
		units : "m",
		maxResolution : "auto",
		projection : "EPSG:3031",
		resolutions : [ 42968.75, 21484.375, 10742.1875, 5371.09375,
				2685.546875, 1342.7734375, 671.38671875, 335.693359375,
				167.8466796875, 83.92333984375, 41.961669921875,
				20.9808349609375, 10.49041748046875 ],
		maxExtent : extentForDisplay, 
		restrictedExtent : extentFrameForDisplay
	};

	var n = 0; // should use enum type, in order to get rid of magic number
	if (region == "arctic") {
		n = 0;
	} else if (region == "antarctic") {
		n = 1;
	}

	//create openlayer map objects
	maps[n] = new OpenLayers.Map(region, options[n]);
	
	//server should be resolute.uwaterloo.ca for production and www.polardata.ca for testing
	wmsLayers[n] = new OpenLayers.Layer.WMS(region,
			"http://gamet.uwaterloo.ca/tilecache/tilecache.py?", {
				layers : "nsidc_" + region,
				format : 'image/png'
	},{'attribution': 'Map courtesy of the <a href="http://nsidc.org/data/atlas/atlas_info.html" target="_blank">NSIDC </a>'});
	
	polygonLayers[n] = new OpenLayers.Layer.Vector("Polygon Layer", {
		style : vector_style
	});
	resultsLayers[n] = new OpenLayers.Layer.Vector("Result Layer");
	selectionMarkerLayers[n] = new OpenLayers.Layer.Markers(
			"Selection Marker Layer");
	maps[n].addLayers( [ wmsLayers[n], polygonLayers[n], resultsLayers[n],
			selectionMarkerLayers[n] ]);
	maps[n].setCenter(extentTilecache, 1);
	//maps[n].zoomToMaxExtent();
	maps[n].zoomToExtent(extentFrameForDisplay);
	mouses[n] = new OpenLayers.Control.MousePosition( {
		displayProjection : projGlobal
	});
	maps[n].addControl(mouses[n]);

	hovers[n] = new OpenLayers.Control.SelectFeature(resultsLayers[n], {
		hover : true,
		onSelect : onFeatureSelect,
		onUnselect : onFeatureUnselect
	});
	maps[n].addControl(hovers[n]);
	hovers[n].activate();
	boxes[n] = new OpenLayers.Layer.Boxes("boxes");
    maps[n].addLayer(boxes[n]);
	
	maps[n].events.register("click", maps[n], function(e) {
		if (lonlats[n].length < 4) {
			lonlats[n].push(maps[n].getLonLatFromViewPortPx(e.xy));
			setOutputPoints(n);
			drawUniversalSelection(n);
		}
	});
	
}
