var lat = "36.1146";
var lng = "-115.1728";
var map = null;
var geocoder = null;
var bounds = null;
var addresses = new Array();
var addresses_str = new Array();
var directions;
var fromAddress = null;
var toAddress = null;
var properties_address = new Array();
var blueIcon = new GIcon(G_DEFAULT_ICON);
blueIcon.iconSize = new GSize(26, 33);
markerOptions = { icon:blueIcon };

jQuery(document).ready(function() {
    bounds = new GLatLngBounds;
    initialize();
//    createPopupWindowsDiv(properties_address);
});

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(lat, lng), 13);
        geocoder = new GClientGeocoder();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        if(jQuery(properties_address)){
			for(property in properties_address){
				if(properties_lat[property] != 0 && properties_lng[property] != 0){
					showAddressByLatLng(property,properties_lat[property],properties_lng[property],jQuery('#'+property).html(),properties_image[property]);
				}else{
					showAddressByAddress(property,properties_address[property],jQuery('#'+property).html(),properties_image[property]);
				}
			}
        }
        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.setCenter(bounds.getCenter());
        /*
        var default_place = new GLatLng(lat,lng);
        panoramaOptions = { latlng:default_place };
        streetview = new GStreetviewPanorama(document.getElementById("steetview"), panoramaOptions);
        GEvent.addListener(streetview, "error", handleNoFlash);
        */
    }
}

function showAddressByLatLng(property,addrLat, addrLng, address_str,image_path)
{
    blueIcon.image = image_path;    

    if(addrLat && addrLng){
        point = new GLatLng(addrLat, addrLng);
        var marker = new GMarker(point,markerOptions);
        map.addOverlay(marker);
        bounds.extend(marker.getPoint());
        GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml(address_str);
        });
    }
}

function showAddressByAddress(property,address, address_str,image_path)
{
    blueIcon.image = image_path;
	
    if(address){
    	geocoder.getLatLng(address,function(point) {
            var marker = new GMarker(point,markerOptions);
            map.addOverlay(marker);
            bounds.extend(marker.getPoint());
            GEvent.addListener(marker, "mouseover", function() {
                marker.openInfoWindowHtml(address_str);
            });
        	addlatlng(property,point.lat(),point.lng());
    	});
    }
}

function addlatlng(property,lat,lng){
	jQuery.ajax({
		type:'post',
		url: "/updatelatlng.php",
		data:"&mls="+ property + "&lat=" + lat + "&lng=" + lng,
		success: function(data){
			//alert(data);
      	}
	});	
}

function ViewOnMap (addrLat,addrLng,property) {
	if(addrLat && addrLng){
		point  = new GLatLng(addrLat, addrLng);
		map.setCenter(point, 15,G_HYBRID_MAP);
		var marker = new GMarker(point,markerOptions);
	    map.addOverlay(marker);
	    marker.openInfoWindowHtml(jQuery('#MLS_'+property).html());
	}    
}

function ViewOnMapByAdd (address,property) {
	var geocoder = new GClientGeocoder();
    if (geocoder) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                	//alert(address + " not found");
                } else {
                    map.setCenter(point, 15,G_HYBRID_MAP);
                    
                    var marker = new GMarker(point,markerOptions);
            	    map.addOverlay(marker);
            	    marker.openInfoWindowHtml(jQuery('#MLS_'+property).html());
                }
            }
            );
    }
}

