function GMapMain()
{
    this.map = null;
    this.trigger_action = "click";     //action that will show the bubble on the map
    this.icon = null;
    this.category_id = null;
    this.collection  = null;
    this.localSearch   = new GlocalSearch();
    this.latlngbounds  = new GLatLngBounds();
    //this.MarkerCluster = null;
    this.TopMarkersArray = [];   //stores all top markers
    this.AddMarkersArray = [];   //stores all additional markers
    this.MarkersArray    = [];   //stores all markers - used by cluster marker
    this.jsonFeedURL   = base_url + "/feeds/activity-map/";
    
    // storing these for autoCentre calculations
    this.jsonMinLat  = 0;
    this.jsonMaxLat  = 0;
    this.jsonMinLng  = 0;
    this.jsonMaxLng  = 0;
    
    this.dontReload    = false;
    this.letterMarkers = false;
    
    this.centerLat = null;
    this.centerLng = null;
    
    this.markers = [];
    this.init = function(id, markers)
    {
        if (GBrowserIsCompatible())
        {
	    if(markers == true)
	    {
		    this.letterMarkers = true;
	    }
            this.map = new GMap2(document.getElementById(id));
            this.map.addControl(new GLargeMapControl3D());
            //this.map.enableScrollWheelZoom();
            //this.map.enableDoubleClickZoom();
            this.map.enableContinuousZoom();
            this.initIcon();      
            //this.MarkerCluster = new ClusterMarker(this.map,{
			//    clusterMarkerIcon: this.icons['dot']
            //});
        }
        //this.dontReload = true;
        GEvent.addListener(this.map, "moveend", function(){
    		GMapMain.getInstance().loadPointsFromXML();
    		});
        
    }

    this.setCategory = function(category_id)
    {
    	this.category_id = category_id;
    }
    
    
    this.setCollection = function(data)
    {
	    this.collection = data;
    }
    
    this.setCenter = function(lat, lng, zoom)
    {
		intZoom = parseInt(zoom);
		flLat   = parseFloat(lat);
		flLng   = parseFloat(lng);
        this.map.setCenter(new GLatLng(flLat, flLng), intZoom);
    }

    this.loadPointsFromXML = function()
    {
		if(this.dontReload == false)
		{
			var ne_bounds = this.map.getBounds().getNorthEast();
			var maxLat = ne_bounds.lat();
			var maxLng = ne_bounds.lng();
			var sw_bounds = this.map.getBounds().getSouthWest();
			var minLat = sw_bounds.lat();
			var minLng = sw_bounds.lng();
		
			var url = this.jsonFeedURL + "maxLat/" + maxLat + "/maxLng/" + maxLng + "/minLat/" + minLat + "/minLng/" + minLng;
			//GMapMain.getInstance().addMarkersFromPHPJson(this.collection);
			GDownloadUrl(url, function(data, responseCode) {
					GMapMain.getInstance().addMarkersFromJson(data);
				});
	        } else {
			this.dontReload = false;
		}
    }

    this.addMarkersFromCollection = function()
    {
		json = this.collection;
	        if (json != '')
		{
			var self = this;
			$.each(json,function(id,value)
				{
					if(self.letterMarkers == true)
					{
						var id_count = id +1;
						var icon = self.icons[id+1];
					}
					else
					{
						var icon = self.icons['main'];
						
					}
					var newMarker = self.createStaticMarker(value,id, icon);
					self.markers[value.item_id] = newMarker;
					self.map.addOverlay(newMarker);
				});
	        }
		this.map.setCenter(this.latlngbounds.getCenter(), this.map.getBoundsZoomLevel(this.latlngbounds));
		// only reload when collection was imported;
	
	
    }
    
    this.addMarkersFromJson = function(json)
    {
    	
    	this.AddMarkersArray = new Array();
	    var icon = this.icons['dot'];
	    var self = this;
	    if (json != '')
	    {
		    var jsonData = eval("(" + json + ")");
		    if(jsonData != null)
		    {
			    for (var i = 0; i < jsonData.length; i++)
			    {
				    var newMarker = this.createStaticMarker(jsonData[i], null, icon);
				    //self.map.addOverlay(newMarker);
				    if(self.markers[jsonData[i].item_id] == undefined)
				    {
					    self.markers[jsonData[i].item_id] = newMarker;
					    this.map.addOverlay(newMarker);
					    //self.AddMarkersArray[jsonData[i].item_id] = newMarker;
				    }
			    }
		    }
	    }
	   
    }
    
    
    
    this.createStaticMarker = function(input,id, icon)
    {

    	var point = new GLatLng(input.lat, input.lng);
        this.latlngbounds.extend(point);
        var marker = new GMarker(point, {icon:icon});      
        var map    = this.map;
        var html   = this.parseHtml(input.html);
        
        GEvent.addListener(marker, this.trigger_action, function() {
                       marker.openInfoWindowHtml(html,
                    		   {
                    	   			maxWidth:320
                    		   });      
                       highlightActivityPreview(input.item_id);   
        	});
	
	GEvent.addListener(this.map, "infowindowclose",  function()
		{
			 unhighlightActivityPreview(input.item_id);   
		});
	return marker;
    }
    
    this.parseHtml = function(str)
    {
    	return (str+'').replace(/\\(.?)/g, function (s, n1) {
            switch (n1) {
                case '\\':
                    return '\\';
                case '0':
                    return '\u0000';
                case '':
                    return '';
                default:
                    return n1;
            }
        });
    }
    
    this.initIcon = function()
    {
    	this.icons = new Array();
    	for (i = 0; i<24; i++)
    	{
	        this.icons[i] = new GIcon();
	        this.icons[i].image = base_url + "/images/gmap/markers-blue/large_" + i + '.png';
	        this.icons[i].iconSize = new GSize(80, 55);
	        this.icons[i].iconAnchor = new GPoint(16, 47);
	        this.icons[i].infoWindowAnchor = new GPoint(16, 23);
    	}
    	
		this.icons['main'] = new GIcon();
		this.icons['main'].image = base_url + "/images/gmap/blue-dot.png";
		this.icons['main'].iconSize = new GSize(15, 15);
		this.icons['main'].iconAnchor = new GPoint(7, 15);
		this.icons['main'].infoWindowAnchor = new GPoint(7, 7);
		
		this.icons['dot'] = new GIcon();
		this.icons['dot'].image = base_url + "/images/gmap/small-blue-bulb.png";
		this.icons['dot'].iconSize = new GSize(36, 25);
		this.icons['dot'].iconAnchor = new GPoint(7, 21);
		this.icons['dot'].infoWindowAnchor = new GPoint(7, 10);
	
    }
}
    
GMapMain.instance = null;

GMapMain.getInstance = function()
{
    if (GMapMain.instance == null)
    {
        GMapMain.instance = new GMapMain();
    }

    return GMapMain.instance;
}

GMapMain.showMarker = function(marker_id)
{
	
	gmap = GMapMain.getInstance();
	GEvent.trigger(gmap.markers[marker_id], this.trigger_action);
}

function highlightActivityPreview(activity_id)
{
	$('#exp_list_item_' + activity_id ).css({'background-color' : '#e5f7fd'}).siblings().css('border', 'none');	
	
}
function unhighlightActivityPreview(activity_id)
{
	$('#exp_list_item_' + activity_id).css({'background-color' : ''}).siblings().css('border', 'none');	
}


