function loadmap(focuslon, focuslat, map, zl, markers, addy) {

  if (GBrowserIsCompatible()) {
    var bounds = new GLatLngBounds();
    map.setCenter(new GLatLng(focuslat, focuslon), zl);
    map.addControl(new GSmallMapControl())
    
    GEvent.addListener(map, "moveend", function() {
        var center = map.getCenter();
        //alert(center.toString());
    });

    //Grab the current center of the map.
    bounds.extend(new GLatLng(focuslat,focuslon));
    
    for(id in markers.Sale) {
        initializePoint(markers.Sale[id].id, markers.Sale[id].lat,markers.Sale[id].lon, map);
        var point = new GLatLng(markers.Sale[id].lat,markers.Sale[id].lon);
        bounds.extend(point);
    }

    if (map.getBoundsZoomLevel(bounds) == 17) {
        map.setZoom(parseInt(zl));
    } else {
        map.setZoom(map.getBoundsZoomLevel(bounds) -1);
    }

    if (parseInt(addy) == 0) {
        //We regular results
        map.setCenter(new GLatLng(focuslat, focuslon));   
    } else { 
        //We have focused results
        var point = new GLatLng(focuslat, focuslon);
        map.addOverlay(createArrow(point));
        map.setCenter(GLatLng(focuslat, focuslon)); 
        if (map.getBoundsZoomLevel(bounds) == 17) {
            //If there is no zoom setup, probably no records found.
           map.setZoom(parseInt(zl));
        }
    }
  }
  
}

function initializePoint(pos, lat, lon, map) {

    var point = new GLatLng(lat, lon);
    var icon = new GIcon();
    icon.image = "http://www.capeyardsales.com/img/mk/marker" + pos + ".png";
    icon.shadow = "http://www.capeyardsales.com/img/mm_20_shadow.png";
    icon.iconSize = new GSize(20, 34);
    icon.shadowSize = new GSize(37, 34);
    icon.iconAnchor = new GPoint(9, 34);
    icon.infoWindowAnchor = new GPoint(9, 2);
    icon.infoShadowAnchor = new GPoint(18, 25);
    map.addOverlay(new GMarker(point, icon));

}

function createArrow(point) {

    var marker = new GMarker(point);
    var baseIcon = new GIcon();
	var icon = new GIcon(baseIcon);
	
    baseIcon.image = 'http://www.capeyardsales.com/img/arrow.png';
    baseIcon.shadow = 'http://www.capeyardsales.com/img/arrowshadow.png';
    baseIcon.iconSize = new GSize(40, 34);
    baseIcon.shadowSize = new GSize(40, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.infoShadowAnchor = new GPoint(18, 25);
   
	var marker = new GMarker(point, baseIcon);
	
	return marker;

}

function initDirections(lon,lat, dlon, dlat, mapcontrol,dircontrol ) {

    var map = new GMap2(mapcontrol);
    var directionsPanel;
    var directions;

    map.addControl(new GSmallMapControl())
    //map.setCenter(new GLatLng(lat,lon), 15);
    directionsPanel = dircontrol;
    directions = new GDirections(map, directionsPanel);
    directions.load(lat + "," + lon + " to " + dlat + "," + dlon);
    
}

// Location Finder //

function findLocation(address) {
    // Create new geocoding object
    var geocoder = new GClientGeocoder();
    if (address.indexOf(",") == -1)
    {
    address = address + ' FL';
    }
    // Retrieve location information
    geocoder.getLocations(address, getresponse);
}

function getresponse(response) {
    // Retrieve the object
    try
    {
        place = response.Placemark[0];
        // Retrieve the latitude and longitude
        var redirParams = 'lat=' + place.Point.coordinates[1] + '&long=' + place.Point.coordinates[0] + '&addy=' + URLencode($('#inpAddress').val());
        //Check to see if this URL already has params
        window.document.location = 'http://www.capeyardsales.com/default.aspx?' + redirParams;
    }
    catch(ex)
    {
        $("#ctl00_cplBD_hidLongitude").val = 'Location not found!';
    }
}

// Helper Function //

function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

// Add Page //

function showAddress(address) {
    // Create new geocoding object
    var geocoder = new GClientGeocoder();
    
    smap.clearOverlays();
    lmap.clearOverlays();
    
    if ($("#ctl00_cplBD_txtStreet1").val() != '') {
        address = address + ' ' + $("#ctl00_cplBD_hidState").val();

        // Retrieve location information
        geocoder.getLocations(address, getaddress);
    }  
    else
    {
        $("#mapmsg").innerHTML = '<span style="font-size : 1.1em; font-weight : bold;">Street 1 is Required!</span>!';
        document.getElementById('addressform').className = 'addressincorrect';
    }
}

function getaddress(response) {
    // Retrieve the object
    try
    {
        smap = new GMap2($("#smLocMap")[0]);
        lmap = new GMap2($("#lgLocMap")[0]);
        szm = $("#ctl00_cplBD_hidSZm").val();
        lzm = $("#ctl00_cplBD_hidLZm").val();
        
        //Hide Google and Mapping Copyright
        var CopyrightDiv = document.getElementById('smLocMap').firstChild.nextSibling;
        var CopyrightImg = document.getElementById('smLocMap').firstChild.nextSibling.nextSibling;
    	
        CopyrightDiv.style.display = "none"; 
        CopyrightImg.style.display = "none"; 
        
        CopyrightDiv = document.getElementById('lgLocMap').firstChild.nextSibling;
        CopyrightImg = document.getElementById('lgLocMap').firstChild.nextSibling.nextSibling;
    	
        CopyrightDiv.style.display = "none"; 
        CopyrightImg.style.display = "none"; 

        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
                      
        smap.addControl(new GSmallMapControl())
        smap.setCenter(point, parseInt(szm));
        lmap.addControl(new GSmallMapControl())
        lmap.setCenter(point, parseInt(lzm));
        
        //Add results to the map
        var marker = new GMarker(point);
        smap.clearOverlays();
        smap.addOverlay(marker);
        lmap.clearOverlays();
        lmap.addOverlay(marker);
        
        //Notify
        $("#mapmsg").innerHTML = '<span style="font-size : 1.1em; font-weight : bold;">We located your address.</span>';
        document.getElementById('addressform').className = 'addresscorrect';
        //Allow the user to submit the Yard Sale
        document.getElementById('ctl00_cplBD_Button1').disabled = false;
        document.getElementById('ctl00_cplBD_Button1').value = 'Submit Yard Sale!';
        //Set hidden input fields
        document.getElementById('ctl00_cplBD_longitude').value = place.Point.coordinates[0];
        document.getElementById('ctl00_cplBD_latitude').value = place.Point.coordinates[1];
    }
    catch(ex)
    {
        $("#mapmsg").innerHTML = '<span style="font-size : 1.1em; font-weight : bold;">We could not find your address <b>( ' + 
        $("#ctl00_cplBD_txtStreet1").val() + ' ' + $("#ctl00_cplBD_txtStreet2").val() + ' ' + $("#ctl00_cplBD_ddCity").val().split("|")[0]
        + ' </b>).<br/> Please adjust and try again</span>!';
        document.getElementById('addressform').className = 'addressincorrect';
        
        smap.setCenter(new GLatLng(latitude, longitude), szm);
        lmap.setCenter(new GLatLng(latitude, longitude), lzm);
        
        smap.clearOverlays();
     
        lmap.clearOverlays();
    }
}
