/**
 * Main map functions
 */
MAPUTILS  = function () {
    
    //global map object
    MAPUTILS.prototype.map;
    MAPUTILS.prototype.counter = 0;
    
    //default map settings
    MAPUTILS.prototype.currentLat =  27.463597;
    MAPUTILS.prototype.currentLng = -82.608220;
    MAPUTILS.prototype.currentZoom = 13;
    
    MAPUTILS.prototype.mapSizeW = 500;
    MAPUTILS.prototype.mapSizeH = 500;
    MAPUTILS.prototype.newMarkerPoint;
    
    MAPUTILS.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.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.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 or key is invalid!';
            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_SATELLITE_TYPE);        
        
        //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 GLargeMapControl());    //zoom and pan       
        this.map.addControl(new GMapTypeControl()); //Map/Sat/hybrid
         
        this.newMarkerPoint = new GMarker(this.map.getCenter(), {draggable: true});
        

        GEvent.addListener(maputil.newMarkerPoint, "dragend", function()  {
           var point = maputil.newMarkerPoint.getPoint();
             maputil.lookUpAddress(point);
             //maputil.newMarkerPoint.openInfoWindow(document.createTextNode("This location has been recorded."));

            document.getElementById('lat').value = point.lat();
            document.getElementById('lng').value = point.lng();      
            compute(1);
        });
        this.map.addOverlay(this.newMarkerPoint);
        //this.newMarkerPoint.openInfoWindow("Drag and drop the red marker to the exact location.");     
        
       
    }
    
    MAPUTILS.prototype.setMapSize = function (){
        document.getElementById("map").style.width = this.mapSizeW + 'px';
        document.getElementById("map").style.height = this.mapSizeH + 'px';
    }
    
    MAPUTILS.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(',');
                    document.getElementById("mapaddress").value=result.address ; 
                   //document.getElementById("mapcity").value=street[1] ;                   
                }
            });            
        }

       MAPUTILS.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.newMarkerPoint.setLatLng(point);
                        //maputil.newMarkerPoint.openInfoWindow("Address found:<br/>" + result.address + '<br /> Location has been recorded');
                        maputil.map.setCenter(point); 

                      document.getElementById('lat').value = point.lat();
                      document.getElementById('lng').value = point.lng();  
                      compute(1);
                }
            });            
            return false;
        }
}
//create maputil class
var maputil = new MAPUTILS;

