Map = {
    wgs84: new OpenLayers.Projection("EPSG:4326"), // default projection
    map: null, // will hold OpenLayers.Map object
    addBaseLayers: function(){}, // overridden by logic for specific baselayer
    addSpecifics: function(){}, // overridden by logic for specific page
    areFeatures: false,
    defaultLonglat: new OpenLayers.LonLat(0, 0),
    defaultZoom: 0,
    zoomChangeNeeded: false,

    create: function () {
        StatusLog.init('status');
        var options = { controls: [
            new OpenLayers.Control.Navigation(),
            new OpenLayers.Control.KeyboardDefaults(),
            new OpenLayers.Control.PanZoomBar(),
            new OpenLayers.Control.Scale(),
			new OpenLayers.Control.LayerSwitcher(),
            new OpenLayers.Control.Attribution(),
            new OpenLayers.Control.TouchNavigation({dragPanOptions: {interval: 0, enableKinetic: true}})
        ]};
        this.map = new OpenLayers.Map('map', options);
        this.addBaseLayers();
        this.map.addControl(new OpenLayers.Control.MousePosition( {id: "ll_mouse", formatOutput: this.formatMousePosition} ));

        this.addSpecifics();
        if ((this.areFeatures == false) && (!location.search)) {
            this.map.setCenter(this.defaultLonglat, this.defaultZoom);
        }
        if (location.search) {
            var args = OpenLayers.Util.getParameters();
            if (args.mark == 'y') {
                this.Markers.addMarker(args.lon, args.lat, false);
            }
        }
        if (this.areFeatures == false) {
            if (this.zoomChangeNeeded)
                this.map.events.register('zoomend', this.map, this.zoomChanged);
            StatusLog.endMsg();
        }
    },

     //this.addControl(new OpenLayers.Control.LayerSwitcher());
	
	formatMousePosition: function(lonLat) {
        return Utils.latlongtoNews(lonLat.lat, lonLat.lon);
    },

    Markers: {
        //icon: new OpenLayers.Icon(Utils.imageLocation+'bluearrow.png', new OpenLayers.Size(16, 16), new OpenLayers.Pixel(-8, -16)),
		icon: new OpenLayers.Icon(Utils.imageLocation+'marker_green.png', new OpenLayers.Size(35, 45), new OpenLayers.Pixel(-19, -37)),
        layer: null, // holds layer object
        map: null, // holds reference to map object

        addLayer: function() {
            this.map = Map.map;
            this.layer = new OpenLayers.Layer.Markers( "Markers" );
            this.map.addLayer(this.layer);
        },

        addMarker: function (long, lat, isMapProj) {
            var point = new OpenLayers.LonLat(long, lat);
            if (isMapProj != true)
                if (this.map.projection != "EPSG:4326")
                    point.transform(this.map.displayProjection, this.map.baseLayer.projection);
            var bounds = this.map.getMaxExtent();
            if (bounds.containsLonLat(point)) {
                var zoom = this.map.getZoom();
                if (zoom < 3)
                    zoom = zoom+3;
                this.map.setCenter(point, zoom);
                var mark = new OpenLayers.Marker(point, this.icon.clone());
                this.layer.addMarker(mark);
                var imgTitle = 'Lat '+lat+', long '+long;
                if (isMapProj == true)
                    imgTitle = 'x '+long+', y '+lat;
                mark.icon.imageDiv.firstChild.setAttribute('title', imgTitle);
            }
            else alert('Coordinates were not found on this map!');
        },

        clearMarkers: function () { // 'this' is button control
            this.map.getLayersByName('Markers')[0].clearMarkers();
        }
    },

     Panel: {
        panel: null, // will hold OpenLayers.Control.Panel object

        create: function () {
            this.panel = new OpenLayers.Control.Panel();
            //this.panel.addControls([ // all panels have help button
           //     new OpenLayers.Control.Button({
           //        displayClass: "helpButton", trigger: WindowOpen.help, title: 'Help'
           //     })
           // ]);
            Map.map.addControl(this.panel);
        },

        addSearch: function () {
            this.panel.addControls([
                new OpenLayers.Control.Button({
                    displayClass: "searchButton", trigger: WindowOpen.search, title: 'Press to Search for Latitude/Longitude Coordinates or Placenames'
                }),
              //  new OpenLayers.Control.Button({
             //       displayClass: "clearButton", trigger: Map.Markers.clearMarkers, title: 'Clear all Markers'
               // })
            ]);
        }
    },

   addClick: function (trigger) {
        
       //Add click with new marker--------------------
	//   try{
    //        var myClick= new ClickControl();
    //        this.map.addControl(myClick);
    //        myClick.activate();
	//	     	}
	//	      catch(err){
	//	   	  }	
		//End of new marker---------------------------- 
			  
        var click = new OpenLayers.Control.Click({'trigger': trigger});
       this.map.addControl(click);
        click.activate();
		
    },
	
   
   addPerma: function () {
        this.map.addControl(new OpenLayers.Control.Permalink(null, null, {id: 'thisPerma', title: "Link to display this map again"}));
    },

    getOtherMap: function () {
        var thisPerma = this.map.getControl('thisPerma');
        var base = thisPerma.base.split('?')[0];
        var otherBase;
        var zoom = thisPerma.createParams().zoom;
        var country = ServerVars["country"];
        if (base.match('google')) {
            otherBase = base.replace("google", country);
            zoom -= 7;
        }
        else {
            otherBase = base.replace("/"+country+"/", "/google/");
            zoom += 7;
        }
        otherBase += '?zoom='+zoom+'&lat='+thisPerma.createParams().lat+'&lon='+thisPerma.createParams().lon;
        location.href = otherBase;
    },

    positionDisplay: function(e) {
        var map = e.object;
        lonlat = map.getLonLatFromViewPortPx(e.xy);
        if (map.projection != "EPSG:4326")
            lonlat.transform(map.baseLayer.projection, map.displayProjection);
        lat = Math.round(lonlat.lat * 1000000) / 1000000;
        lon = Math.round(lonlat.lon * 1000000) / 1000000;
        var html = 'You clicked at latitude: ' + lat + ', longitude: ' + lon;
        html += '<p>Get '+Utils.jsCallify("Feed.Json.getMetar(lon, lat)", "METAR weather report")+' from nearest station</p>';
        html += '<div id="weatherResults"></div>';
        html += '<p>Show nearby '+Utils.jsCallify("Feed.Json.getWikipedia(lon, lat)", "Wikipedia entries")+'</p>';
        html += '<div id="wikiResults"></div>';
        WindowOpen.title = 'Selected Item: Details';
        
		triggermarker(lonlat);
		
		//This open windowhen you click
		//WindowOpen.html(html);
    },

    VEDisplay: function(e) {
        var map = e.object;
        var lonlat = map.getLonLatFromViewPortPx(e.xy);
        if (map.projection != "EPSG:4326")
            lonlat.transform(map.baseLayer.projection, map.displayProjection);
        WindowOpen.title = 'Virtual Earth Birdseye Imagery';
        WindowOpen.statusBar = '';
        WindowOpen.url('../files/birdseye.html?lat='+lonlat.lat+'&long='+lonlat.lon);
    }
}

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
    defaultHandlerOptions: {
        'single': true, 'double': false, 'pixelTolerance': 0, 'stopSingle': false, 'stopDouble': false
    },
    initialize: function(options) {
        this.handlerOptions = OpenLayers.Util.extend( {}, this.defaultHandlerOptions );
        OpenLayers.Control.prototype.initialize.apply( this, arguments );
        this.handler = new OpenLayers.Handler.Click( this, { 'click': options.trigger }, this.handlerOptions );
    }
});




