/** 
 * Launch openlayers JavaScript File. Used to add markers and modify controls to the map.
 * @author elam - Eric Lam - 24/05/2007 - 1:11:43 PM 
 * @version $Id: launchmap.js,v 1.3 2007-07-02 07:04:24 elam Exp $
 */

/** @type OpenLayers.Map*/
var map = null;

/** @type OpenLayers.Popup*/
var popup = null;

/** @type OpenLayers.Popup*/
var popupShadow = null;

/** @type Double*/
var markerMinX = 180;
		
/** @type Double*/
var markerMinY = 90;

/** @type Double*/
var markerMaxX = -180;

/** @type Double*/
var markerMaxY = -90;

/**
 * must be called first.
 */
function createInitialMap(initialZoom) {
	
	var maxRes = 360/512;
	var minRes = 360/67108864;
	
	//creates the initial map object
	map = new OpenLayers.Map( 
            			'map',
            			{
						 maxResolution: maxRes,
						 minResolution: minRes,
            			 controls: [
						 	new OpenLayers.Control.MouseDefaults(),
            				//new OpenLayers.Control.Scale(),
							new OpenLayers.Control.LogoControl(),
            				new OpenLayers.Control.PanZoom.PanZoomControl()
            			 ],
            			 buffer: 1
            			} 
	);  

	// cretes the terrapages/geoserver street layer
	var wmscdev = [
					"http://wmsc1.terrapages.net/getmap?",
					"http://wmsc1.terrapages.net/getmap?",
					"http://wmsc1.terrapages.net/getmap?",
					"http://wmsc1.terrapages.net/getmap?"
			];
			
	var terrapagesStreetLayer = new OpenLayers.Layer.WMS( 
		"TerraPages&nbsp;Street",
		wmscdev, 
		{	layers: 'UnprojectedStreet',
			format: 'image/jpeg' },
		{	buffer: 1,
			isBaseLayer: true}
	);
	
	// adds the terrapages street layer to the map
	map.addLayer(terrapagesStreetLayer);

	// Adds the overview map controls
	var options = {
		layers: [terrapagesStreetLayer.clone()],
		mapOptions: {maxResolution: maxRes},
		minRatio: 10,
		maxRatio: 10
    };
    var overview = new OpenLayers.Control.OverviewMap2(options);
	map.addControl(overview);
	
	// sets an initial point of view for the map
	var point = new OpenLayers.LonLat(134, -26);
	map.setCenter(point, initialZoom);        	
	
}


/**
 * Adds a icon to map
 * 
 * @param {Object} lon
 * @param {Object} lat
 * @param {Object} image
 * @param {Object} desc
 */
function addMarker(cssName,lon,lat,image,descriptionHTML) {

	addPointToBounds(lon,lat);
	var lonlat = new OpenLayers.LonLat(lon,lat);

	// sets up the icon/image to be used as the marker.
	var size = new OpenLayers.Size(33,26);
	var offset = new OpenLayers.Pixel(-3,-23);
	var icon = new OpenLayers.Icon(image, size, offset);
	
	// displays the marker
	var newpoint = new OpenLayers.Marker(lonlat, icon );
	var markers = new OpenLayers.Layer.Markers( "Markers" );
	map.addLayer(markers);
	markers.addMarker(newpoint);

	// creates the mouse control for the popups
	marker = new OpenLayers.Marker(lonlat,icon.clone());
	marker.events.register('mouseover', marker, function () {addPopup(cssName,lonlat,image,descriptionHTML)} );
	marker.events.register('mouseout', marker, function () {hidePopup()} );
	markers.addMarker(marker);
}

/**
 * Shows the popup
 * 
 * @param {Object} desc
 */
function addPopup(cssName,lonlat,image,descriptionHTML) {
	
	if (cssName == '') {
		cssName = 'toolTip';
	}
	
	// the shadow of the popup
	popupShadow = new OpenLayers.Popup.AutoHeight(cssName,lonlat,null,null,80,false);
    popupShadow.setContentHTML(descriptionHTML);
    popupShadow.setBackgroundColor("#000000");
    popupShadow.setOpacity(0.4);
	popupShadow.offset(7,15);
	
	// the popup with content
	popup = new OpenLayers.Popup.AutoHeight(cssName,lonlat,null,null,80,false);
    popup.setContentHTML(descriptionHTML);
    popup.setBackgroundColor("#dee7f7");
	popup.setBorder("1px solid black");
	popup.offset(0,8);

	// need to add the shadow first so it is drawn beneath
	map.addPopup(popupShadow);
    map.addPopup(popup);
}    

/**
 * hides the popup
 */
function hidePopup() {
	popupShadow.hide();
	popup.hide();
}

/**
 * Adds the pan zoom control where the "global" icon will
 * zoom to the extent of all the markers added to the map
 */
function addResetCentreControl() {

	//create the mouse controls, override scroll zooming (disabled)
	var mouseControl = new OpenLayers.Control.MouseDefaults();
	mouseControl.registerWheelEvents = function() {};

	//create the pan zoom control, override world zoom to bounds of markers
	var panZoomCon = new OpenLayers.Control.PanZoom.PanZoomControl()
	panZoomCon.buttonDown = function(evt) {
		if (!OpenLayers.Event.isLeftClick(evt)) return;
			switch (this.action) {
				case "panup":
					this.map.pan(0, -50);
					break;
				case "pandown":
					this.map.pan(0, 50);
					break;
				case "panleft":
					this.map.pan(-50, 0);
					break;
				case "panright":
					this.map.pan(50, 0);
					break;
				case "zoomin":
					this.map.zoomIn();
					break;
				case "zoomout":
					this.map.zoomOut();
					break;
				case "zoomworld":
					this.map.zoomToExtent(new OpenLayers.Bounds(markerMinX,markerMinY,markerMaxX,markerMaxY));
					break;
			}
		OpenLayers.Event.stop(evt);
	};
	
	map.addControl(panZoomCon);
}

/**
 * Zoom the map to the closest zoom level centered on long/lat
 * 
 * @param {Object} lon
 * @param {Object} lat
 */
function zoomToPoint(lon,lat) {
	zoomTo(new OpenLayers.Bounds(lon,lat,lon,lat));
}

/**
 * zooms the map to the given bounds.
 * 
 * @param {Object} bounds
 */ 
function zoomTo(bounds) {
	map.zoomToExtent(bounds);
}

/**
 * zoom to a point such that all added markers will be displayed.
 */
function zoomToMarkerBounds() {
	map.zoomToExtent(new OpenLayers.Bounds(markerMinX,markerMinY,markerMaxX,markerMaxY));
}

/**
 * Update the global marker bounds with the new point that is added.
 * 
 * @param {Object} lon
 * @param {Object} lat
 */
function addPointToBounds(lon,lat) {
	markerMinX = Math.min(lon,markerMinX);
	markerMinY = Math.min(lat,markerMinY);
	markerMaxX = Math.max(lon,markerMaxX);
	markerMaxY = Math.max(lat,markerMaxY);
}
