/**
 * Main map functions
 */
MAPUTILS_ADD  = function () {
    
    //global map object
    MAPUTILS_ADD.prototype.map;
    MAPUTILS_ADD.prototype.counter = 0;
    //default map settings
   //default map settings
    MAPUTILS_ADD.prototype.currentLat =  51;
    MAPUTILS_ADD.prototype.currentLng = 0;
    MAPUTILS_ADD.prototype.currentZoom = 3;
    
    
    MAPUTILS_ADD.prototype.mapSizeW = 400;
    MAPUTILS_ADD.prototype.mapSizeH = 350;
    MAPUTILS_ADD.prototype.newMarkerPoint;
    
    MAPUTILS_ADD.prototype.initMap = function() {
        
        //check if map object has been loaded or created
        try{
            document.getElementById('map').innerHTML;
        }catch(e){
            if (this.counter > 60) { 
                alert('Can\'t load map! Can\'t find map object!');
            } else { 
                setTimeout("maputil_add.initMap()", 1000);                 
            } 
            return false; 
        }
    
        //If gmaps main.js didn't load try to call init again after 60 tries we give up
        if (typeof GMap != "function") { 
            if (this.counter > 60) { 
                document.getElementById('map').innerHTML = 'Can\'t load map! Gmap server timeout! ';
            } else { 
                setTimeout("maputil_add.initMap()", 1000); 
            } 
            this.counter++; 
            return false; 
        }
        
        //if browser not compatible update map tag with error msg
        if (!GBrowserIsCompatible()) { 
            document.getElementById('map').innerHTML = 'Can\'t load map because your browser is too old!';
            return false;
        }
    
        //init gmaps object
        this.map = new GMap2(document.getElementById("map"));
        
                    
        //set center of the map
        this.map.setCenter(new GLatLng(this.currentLat, this.currentLng), this.currentZoom);  
        //this.map.setMapType(G_HYBRID_MAP);        
        
        //add controls and mouse functions
        this.map.enableDoubleClickZoom();
        this.map.enableContinuousZoom();
        //this.map.enableScrollWheelZoom();
        //this.map.addControl(new GOverviewMapControl (/*new GSize(200,180)*/));                        
        
        this.map.addControl(new GSmallMapControl());    //zoom and pan       
        this.map.addControl(new GMapTypeControl()); //Map/Sat/hybrid
         
        this.newMarkerPoint = new GMarker(this.map.getCenter(), {draggable: true});
        

        GEvent.addListener(maputil_add.newMarkerPoint, "dragend", function()  {
           var point = maputil_add.newMarkerPoint.getPoint();
             maputil_add.lookUpAddress(point);
             maputil_add.newMarkerPoint.openInfoWindow('<font face=Arial size=2>Your location has been recorded. <br />You can change it again by dragging and dropping the marker.<br /></font><button type="button" onclick="maputil_add.hideMap()">Close Map</button>');

            document.getElementById('newlat').value = point.lat();
            document.getElementById('newlng').value = point.lng();         
        });
        this.map.addOverlay(this.newMarkerPoint);
        this.newMarkerPoint.openInfoWindow("<font face=Arial size=2>Drag and drop the red marker to the exact location.<br /> You can use the zoom controls on the left to <br />  make  your position more accurate.</font>");     
        
       
    }
    
    MAPUTILS_ADD.prototype.setMapSize = function (){
        document.getElementById("map").style.width = this.mapSizeW + 'px';
        document.getElementById("map").style.height = this.mapSizeH + 'px';
    }
    
    MAPUTILS_ADD.prototype.lookUpAddress = function(latlng){  
            geocoder = new GClientGeocoder();
            geocoder.getLocations(latlng, function(addresses) {
                if(addresses.Status.code != 200) {
                    //alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
                    //latlng.toUrlValue();
                } else { 
                    var result = addresses.Placemark[0];                
                    //var street = result.address.split(',');
                    //if(document.getElementById("mapaddress").value.length<3) document.getElementById("mapaddress").value=result.address ; 
                   //document.getElementById("mapcity").value=street[1] ;                   
                }
            });            
        }

       MAPUTILS_ADD.prototype.searchAddress = function(str){  
            geocoder = new GClientGeocoder();
            geocoder.getLocations(str, function(addresses) {
                if(addresses.Status.code != 200) {
                    alert("Geocoder failed to find an address for " + str);
                    //latlng.toUrlValue();
                } else { 
                    var result = addresses.Placemark[0];                
                        point = new GLatLng(result .Point.coordinates[1], result.Point.coordinates[0]);
                        maputil_add.newMarkerPoint.setLatLng(point);
                        maputil_add.newMarkerPoint.openInfoWindow("Address found:<br/>" + result.address + '<br /> Location has been recorded');
                        maputil_add.map.setCenter(point,14);
                      document.getElementById('newlat').value = point.lat();
                      document.getElementById('newlng').value = point.lng();  
                }
            });            
        }
        
        MAPUTILS_ADD.prototype.showMap = function(){  
            document.getElementById("mapframe").style.display = "block";
            var lat = document.getElementById('newlat').value;
            var lng = document.getElementById('newlng').value;
            
            if(lat.length>0 && lng.length>0){
                maputil_add.currentLat =  lat;
                maputil_add.currentLng = lng;
                maputil_add.currentZoom = 7;
            } 
            maputil_add.initMap();
        }
        
        MAPUTILS_ADD.prototype.hideMap = function(){  
            document.getElementById("mapframe").style.display = "none";
        }
}
//create maputil_add class
var maputil_add = new MAPUTILS_ADD;

