var marker_count = 0;
function GMapExp()
{    
    this.map = null;
    this.icon = null;
    this.found = true;
    this.localSearch = new GlocalSearch();   
    this.geocoder    = new GClientGeocoder(); 
    
    
    
    this.init = function(id)
    {
        if (GBrowserIsCompatible())
        {            
            this.map = new GMap2(document.getElementById(id));
            this.map.addControl(new GSmallZoomControl3D());
            //this.map.addControl(new GOverviewMapControl());
            this.map.disableScrollWheelZoom();
            this.map.disableDoubleClickZoom();
            this.map.enableContinuousZoom();
            this.initIcon();
        }
    }

    this.setCenter = function(lat, lng, zoom)
    {
        this.map.setCenter(new GLatLng(lat, lng), zoom);
    }

    this.initIcon = function()
    {
    	this.icon       = new GIcon();
		this.icon.image = base_url + "/images/gmap/large-blue-bulb.png";
		this.icon.iconSize = new GSize(58, 40);
		this.icon.iconAnchor = new GPoint(12, 34);
		this.icon.infoWindowAnchor = new GPoint(12, 17);
    }

    this.addMarkerToMap = function(lat, lng, draggable)
    {
        var point = new GLatLng(lat, lng);
        var marker = new GMarker(point,{icon:this.icon, draggable: draggable});
        if (draggable == true)
        {
	        GEvent.addListener(marker, "drag", function(){
	                document.getElementById("lat").value = marker.getPoint().lat();
	                document.getElementById("lng").value = marker.getPoint().lng();                
	        });
	
	
	        GEvent.addListener(marker, "dragend", function(){
	                GMapExp.getInstance().map.setCenter(marker.getPoint());
	        });
        }
        marker_count = 1;
       // this.map.addOverlay(marker);
        this.map.addOverlay(marker);
        //this.mm.addMarker(marker,0,17); 
        //mm.refresh(); 
        this.setCenter(lat, lng, 12);
        
    }
        
    
    this.locateOnMap = function()
    {
    	// locates the location on the map
    	// checking the country first
    	var postcode = $('#postcode').val();
    	var country  = $('#country_id :selected').text();
    	this.map.clearOverlays();
    	//this.mm.refresh();
    	marker_count = 0;
    	this.localSearch.setSearchCompleteCallback(null,
                function() {
           			localSearch = GMapExp.getInstance().localSearch;
           			if (localSearch.results[0])
           			{
           				if(marker_count == 0)
           				{
	           				var resultLat = localSearch.results[0].lat;
	           				var resultLng = localSearch.results[0].lng;
	                                                   
	           				$("#lat").val(resultLat);
	           				$("#lng").val(resultLng);
	           				
	           				GMapExp.getInstance().map.clearOverlays(); 
	           				GMapExp.getInstance().addMarkerToMap(resultLat, resultLng, true); 
           				}
           			}
           			
    			});
    	if(postcode != "")
    	{
    		this.localSearch.setCenterPoint(country);
    		this.localSearch.execute(postcode + ", " + country);
        }
		
	    var address = $('#address1').val() + ", " + $('#town').val() + ", " + $('#county').val() + ", " + ", " + country;
	    this.localSearch.execute(address);
      	//this.localSearch = GMapExp.getInstance().localSearch;
        //this.localSearch.execute(country);
                
           
    }
    
    this.usePointFromPostcode = function(postcode) 
    {
        this.map.clearOverlays();
        
        if(postcode == "")
        {
            alert("Please insert Postcode first");
        }
        else
        {
            this.localSearch.setSearchCompleteCallback(null,
                function() {
                    localSearch = GMapExp.getInstance().localSearch;

                    if (localSearch.results[0])
                    {
                        var resultLat = localSearch.results[0].lat;
                        var resultLng = localSearch.results[0].lng;
                                                
                        document.getElementById("lat").value = resultLat;
                        document.getElementById("lng").value = resultLng;

                        GMapExp.getInstance().addMarkerToMap(resultLat, resultLng, true);                     
                    }
                    else
                    {
                        alert("Postcode not found!");
                    }
                });

            this.localSearch.execute(postcode);
        }
    }
}

GMapExp.instance = null;

GMapExp.getInstance = function()
{
    if (GMapExp.instance == null)
    {
        GMapExp.instance = new GMapExp();
    }

    return GMapExp.instance;
}

