﻿
var geocoder;
var map;
//var myICON = 'images/start.png';
var myICON = 'images/mm_20_yellow.png';


//var mapJSON=[];

var PreWindow=null;

function initialize() 
{    
    geocoder = new google.maps.Geocoder();
    //var latlng = new google.maps.LatLng(-34.397, 150.644);
    var latlng;

    if(location.href.indexOf('LiveMap.aspx')>0)
    {
       latlng = new google.maps.LatLng(38, -150);
    }
    else
    {
       latlng = new google.maps.LatLng(38, -96);
    }

    var myOptions = 
    {
        zoom: zoomLevel,
        center: latlng,
        scaleControl: true,
        navigationControl: true,
        mapTypeControl:true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    //map.enableScrollWheelZoom();
    
    
    for(i=0;i<mapJSON.length;i++)
    {
        
        //codeAddress(mapJSON[i].id,mapJSON[i].title,mapJSON[i].address,mapJSON[i].photo);
        //codeLatLng(mapJSON[i].id,mapJSON[i].title,mapJSON[i].address,mapJSON[i].LatLong,mapJSON[i].photo);
    
    }
    
    codeAddress(mapJSON[0].id,mapJSON[0].title,mapJSON[0].address,mapJSON[0].photo);
    
    
}

var counter = 0;
function codeNextAddress(){
	var i = counter++;
	if(!mapJSON[i]) return;
	codeAddress(mapJSON[i].id,mapJSON[i].title,mapJSON[i].address,mapJSON[i].photo);
}

function codeAddress(id,title,address,photo) 
{
	
	
    var status_code = "";
    if (geocoder)
    {
        geocoder.geocode( { 'address': address}, function(results, status) {
        	
        	codeNextAddress();
        	
            if (status == google.maps.GeocoderStatus.OK)
            {
                if (status != google.maps.GeocoderStatus.ZERO_RESULTS)
                {
                    //map.set_center(results[0].geometry.location);
                    
                    //var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});
                    var marker = new google.maps.Marker({map: map, position: results[0].geometry.location, icon: myICON});

                    
                    var str="";
                    str+="<table width='90%' border='0' cellspacing='0' cellpadding='0'>";
                    str+="  <tr>";
                    str+="    <td colspan='2' nowrap='nowrap'><b style='font-size:14px;'>"+title+"</b></td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td><p>"+address+"</p><p><a href='Property.aspx?Id="+id+"' style='color:green;'>View more details</a></p></td>";
                    str+="    <td>"+photo+"</td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td colspan='2' style='color:blue;' nowrap='nowrap'><a href='http://maps.google.com/?f=d&q="+address+"' style='color:blue; text-decoration:underline;'>Get directions</a> - <a href='Property.aspx?Id="+id+"#morephotos' style='color:blue; text-decoration:underline;'>More photos</a> - <a href='Property.aspx?Id="+id+"#ca' style='color:blue; text-decoration:underline;'>CA agreement</a></td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td colspan='2' style='color:blue;' nowrap='nowrap'><a href='Property.aspx?Id="+id+"#contact' style='color:blue; text-decoration:underline;'>Contact listing broker</a> - <a href='Property.aspx?Id="+id+"#overview' style='color:blue; text-decoration:underline;'>Property overview</a></td>";
                    str+="  </tr>";
                    str+="</table>";
                    
                    var infowindow = new google.maps.InfoWindow({content: str});
                    
                    google.maps.event.addListener(marker, 'click', function() {
                        
                        if(PreWindow)PreWindow.close();
                         
                        infowindow.open(map,marker);

                        PreWindow=infowindow;
                    });
                    
                    //google.maps.event.addListener(marker, 'click', function() {location.href='Property.aspx?Id='+id;});
                }
                else
                {
                    //alert("No results found");
                    
                }
            } 
            else
            {
                //alert("Geocode was not successful for the following reason: " + status);
                    
                
            }
        });
    }

}
function codeLatLng(id,title,address,latlng,photo) {
	var input = latlng;
	var latlngStr = input.split(",",2);
	var lat = parseFloat(latlngStr[0]);
	var lng = parseFloat(latlngStr[1]);
	var latlng = new google.maps.LatLng(lat, lng);
	if (geocoder) {
		geocoder.geocode({'latLng': latlng}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
                if (results[1] && status != google.maps.GeocoderStatus.ZERO_RESULTS)
                {
                    //map.set_center(results[0].geometry.location);
                    
                    //var marker = new google.maps.Marker({map: map, position: results[0].geometry.location});
                    var marker = new google.maps.Marker({map: map, position: latlng, icon: myICON});

                    
                    var str="";
                    str+="<table width='90%' border='0' cellspacing='0' cellpadding='0'>";
                    str+="  <tr>";
                    str+="    <td colspan='2' nowrap='nowrap'><b style='font-size:14px;'>"+title+"</b></td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td><p>"+address+"</p><p><a href='Property.aspx?Id="+id+"' style='color:green;'>View more details</a></p></td>";
                    str+="    <td>"+photo+"</td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td colspan='2' style='color:blue;' nowrap='nowrap'><a href='http://maps.google.com/?f=d&q="+address+"' style='color:blue; text-decoration:underline;'>Get directions</a> - <a href='Property.aspx?Id="+id+"#morephotos' style='color:blue; text-decoration:underline;'>More photos</a> - <a href='Property.aspx?Id="+id+"#ca' style='color:blue; text-decoration:underline;'>CA agreement</a></td>";
                    str+="  </tr>";
                    str+="  <tr>";
                    str+="    <td colspan='2' style='color:blue;' nowrap='nowrap'><a href='Property.aspx?Id="+id+"#contact' style='color:blue; text-decoration:underline;'>Contact listing broker</a> - <a href='Property.aspx?Id="+id+"#overview' style='color:blue; text-decoration:underline;'>Property overview</a></td>";
                    str+="  </tr>";
                    str+="</table>";
                    
                    var infowindow = new google.maps.InfoWindow({content: str});
                    
                    google.maps.event.addListener(marker, 'click', function() {
                        
                        if(PreWindow)PreWindow.close();
                         
                        infowindow.open(map,marker);

                        PreWindow=infowindow;
                    });
                    
                    //google.maps.event.addListener(marker, 'click', function() {location.href='Property.aspx?Id='+id;});
                }
                else
                {
                    //alert("No results found");
                    
                }
            
			} else {
				//alert("Geocoder failed due to: " + status);
			}
		});
	}
}

function pausecomp(millis) 
{
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
} 
