﻿// JScript File

//global variables
var mapLocation;
var locationBounds;
var locationGeocoder;
var points = [];
var iPoint=0;
var zoom_level=15;
// arrays to hold copies of the markers and html used by the side_bar
// because the function closure trick doesnt work there

var gmarkers_global = new Array();

function loadMap(id, mapType, zoomLevel1,defaultLocation) {   
    if(zoomLevel1) {
        zoom_level = zoomLevel1;        
    }
    if (GBrowserIsCompatible()) {
        mapLocation = new GMap2(document.getElementById(id));
        //a smaller pan/zoom control used on Google Maps. Appears in the top left corner of the mapLocation by default       
        mapLocation.addControl(new GSmallMapControl());    
        if(mapType && mapType=='hybrid') { 
          //a collapsible overview mapLocation in the corner of the screen   
           mapLocation.addControl(new GOverviewMapControl()); 
        }     
        //buttons that let the user toggle between map types (such as Map and Satellite) 
        mapLocation.addControl(new GMapTypeControl());          
        // enable mouse scroll wheel
        //mapLocation.enableScrollWheelZoom();
        locationGeocoder = new GClientGeocoder(); 
        locationGeocoder.setBaseCountryCode('AU');        
        mapLocation.setCenter(new GLatLng(-35.28, 149.13), zoom_level);  
        if(defaultLocation && defaultLocation!='') {        
            locationGeocoder.getLatLng(
                defaultLocation,
                function(deflatlng) {               
                    if (deflatlng) { 
                        mapLocation.setCenter(deflatlng, zoom_level);
                    } 
                }
            );          
        } 
        if(mapType && mapType=='hybrid') {         
          mapLocation.setMapType(G_HYBRID_MAP);         
          
        }          
       
    }  
}

//show addess based on address array
function showAddresses(addresses, label) {
    locationBounds = new GLatLngBounds();         
    if (addresses && addresses != null && addresses.length > 0) {
        var i = 0;
        for (i = 0; i < addresses.length; i++) {
            showAddress(addresses[i],addresses[i],0,label);
        }         
    }
}   
    
// Creates a marker at the given point with the given number label

function showAddress(address, infoText, property,label, address2) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }    
    locationGeocoder.getLatLng(
            address,
            function(latlng) {               
                if (latlng) {                   
                    if(iPoint == 0) {
                        mapLocation.setCenter(latlng, zoom_level);
                    }
                    var bIcon = new GIcon(G_DEFAULT_ICON);  
                    if(property==0) {
                        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";   
                    } else if(property==1){
                        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";        
                    }else if(property==2) {
                        bIcon.iconSize = new GSize(45, 45);   
                        bIcon.image = "/Public/Base/ImageThumbNailer.aspx?id="+label+"&y=45";          
                    }else{
                        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";  
                    }
                    var markerOptions = { icon:bIcon };  
                   
                    var gmarkers = new GMarker(latlng,markerOptions);          
                    mapLocation.addOverlay(gmarkers);             
                    GEvent.addListener(gmarkers, "mouseover", function() {
                        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
                    });  
                    locationBounds.extend(gmarkers.getPoint());
                    points[iPoint] = gmarkers.getPoint();
                    gmarkers_global[label] = gmarkers;
                    ++iPoint;                      

                } else {                   
                    if (address.indexOf('/') > 0) {                     
                        address = address.substring(address.indexOf('/') + 1);                     
                        showAddress(address,infoText, property,label,address2);
                    } else if (address.indexOf(',') > 0) {                      
                        address = address.substring(address.indexOf(',') + 1);                       
                        showAddress(address,infoText, property,label,address2);
                    }else {                      
                        if(address2!='') {                          
                            showAddress(address2,infoText, property,label,'');
                        }
                    }
                }
            }
            );
} 


    
// Creates a marker at the given point with the given number label

function showAddressIcon(address, infoText, property,label, address2) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }    
    locationGeocoder.getLatLng(
            address,
            function(latlng) {               
                if (latlng) {                   
                    if(iPoint == 0) {
                        mapLocation.setCenter(latlng, zoom_level);
                    }
                    
                    var baseIcon = new GIcon();     
                    baseIcon.iconSize = new GSize(24, 38);       
                    baseIcon.iconAnchor = new GPoint(24, 38);            
                    // for info window
                    baseIcon.infoWindowAnchor = new GPoint(21, 21);                      
                             
                    var bIcon = new GIcon(baseIcon);
                    bIcon.image = "../../images/mm_20_red_"+label+".png";  
                   
                 
                    var markerOptions = { icon:bIcon };  
                   
                    var gmarkers = new GMarker(latlng,markerOptions);          
                    mapLocation.addOverlay(gmarkers);             
                    GEvent.addListener(gmarkers, "mouseover", function() {
                        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
                    });  
                    locationBounds.extend(gmarkers.getPoint());
                    points[iPoint] = gmarkers.getPoint();
                    gmarkers_global[label] = gmarkers;
                    ++iPoint;                      

                } else {                   
                    if (address.indexOf('/') > 0) {                     
                        address = address.substring(address.indexOf('/') + 1);                     
                        showAddress(address,infoText, property,label,address2);
                    } else if (address.indexOf(',') > 0) {                      
                        address = address.substring(address.indexOf(',') + 1);                       
                        showAddress(address,infoText, property,label,address2);
                    }else {                      
                        if(address2!='') {                          
                            showAddress(address2,infoText, property,label,'');
                        }
                    }
                }
            }
            );
} 


function showAddressFromLatLon(lat, lon, infoText, property,label) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }      
    var latlng = new GLatLng(lat, lon);      
    //setthe center to first point   
    if(iPoint == 0) {
        mapLocation.setCenter(latlng, zoom_level);
    }
    // these icons can also come from local servers
    
    var bIcon = new GIcon(G_DEFAULT_ICON);  
    if(property==0) {
        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";   
    } else if(property==1){
        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";        
    }else if(property==2) {
        bIcon.iconSize = new GSize(45, 45);   
        bIcon.image = "/Public/Base/ImageThumbNailer.aspx?id="+label+"&y=45";          
    }else{
        bIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";  
    }
    var markerOptions = { icon:bIcon };  
   
    var gmarkers = new GMarker(latlng,markerOptions);          
    mapLocation.addOverlay(gmarkers);             
    GEvent.addListener(gmarkers, "mouseover", function() {
        //hardcoding the font
        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
    });  
    locationBounds.extend(gmarkers.getPoint());
    points[iPoint] = gmarkers.getPoint();
    gmarkers_global[label] = gmarkers;
    ++iPoint;  
} 
function fnShowPopUp_GoogleMap_LatLon(label)
{    
    try
    {
        showMapDetails(label);      
        if(window.parent.document.getElementById('footer'))
        {        
            window.parent.document.getElementById('footer').focus();
        }
    }    
    catch(e)
    {
    }
}
function fnShowPopUp_GoogleMap_Address(label) 
{
    try
    {
        showMapDetails(label);      
        if(window.parent.document.getElementById('footer'))
        {        
            window.parent.document.getElementById('footer').focus();
        }
    }    
    catch(e)
    {
    }
}
function showAddressFromLatLonIcon(lat, lon, infoText, property,label) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }      
    var latlng = new GLatLng(lat, lon);      
    //setthe center to first point   
    if(iPoint == 0) {
        mapLocation.setCenter(latlng, zoom_level);
    }
    // these icons can also come from local servers
    var baseIcon = new GIcon();     
    baseIcon.iconSize = new GSize(24, 38);       
    baseIcon.iconAnchor = new GPoint(24, 38);            
    // for info window
    baseIcon.infoWindowAnchor = new GPoint(21, 21);                      
             
    var bIcon = new GIcon(baseIcon);
    bIcon.image = "../../images/mm_20_red_"+label+".png";  
                   
    
    var markerOptions = { icon:bIcon };  
   
    var gmarkers = new GMarker(latlng,markerOptions);          
    mapLocation.addOverlay(gmarkers);             
    GEvent.addListener(gmarkers, "mouseover", function() {
        //hardcoding the font
        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
    });  
    locationBounds.extend(gmarkers.getPoint());
    points[iPoint] = gmarkers.getPoint();
    gmarkers_global[label] = gmarkers;
    ++iPoint;  
} 


function showAddressNumber(address, infoText, property,label, address2) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }    
    locationGeocoder.getLatLng(
            address,
            function(latlng) {               
                if (latlng) {                   
                    if(iPoint == 0) {
                        mapLocation.setCenter(latlng, zoom_level);
                    }
                    
                    var baseIcon = new GIcon();     
                    baseIcon.iconSize = new GSize(24, 38);       
                    baseIcon.iconAnchor = new GPoint(24, 38);            
                    // for info window
                    baseIcon.infoWindowAnchor = new GPoint(21, 21);                      
                             
                    var bIcon = new GIcon(baseIcon);
                    bIcon.image = "../../images/mm_20_red_number_"+label+".png";  
                   
                 
                    var markerOptions = { icon:bIcon };  
                   
                    var gmarkers = new GMarker(latlng,markerOptions);          
                    mapLocation.addOverlay(gmarkers);             
                    GEvent.addListener(gmarkers, "mouseover", function() {
                        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
                    });  
                    locationBounds.extend(gmarkers.getPoint());
                    points[iPoint] = gmarkers.getPoint();
                    gmarkers_global[label] = gmarkers;
                    ++iPoint;                      

                } else {                   
                    if (address.indexOf('/') > 0) {                     
                        address = address.substring(address.indexOf('/') + 1);                     
                        showAddress(address,infoText, property,label,address2);
                    } else if (address.indexOf(',') > 0) {                      
                        address = address.substring(address.indexOf(',') + 1);                       
                        showAddress(address,infoText, property,label,address2);
                    }else {                      
                        if(address2!='') {                          
                            showAddress(address2,infoText, property,label,'');
                        }
                    }
                }
            }
            );
} 

function showAddressFromLatLonNumber(lat, lon, infoText, property,label) {   
    if(!locationBounds || locationBounds==null || locationBounds == undefined) {
        locationBounds = new GLatLngBounds();
    }      
    var latlng = new GLatLng(lat, lon);      
    //setthe center to first point   
    if(iPoint == 0) {
        mapLocation.setCenter(latlng, zoom_level);
    }
    // these icons can also come from local servers
    var baseIcon = new GIcon();     
    baseIcon.iconSize = new GSize(24, 38);       
    baseIcon.iconAnchor = new GPoint(24, 38);            
    // for info window
    baseIcon.infoWindowAnchor = new GPoint(21, 21);                      
             
    var bIcon = new GIcon(baseIcon);
    bIcon.image = "../../images/mm_20_red_number_"+label+".png";  
                   
    
    var markerOptions = { icon:bIcon };  
   
    var gmarkers = new GMarker(latlng,markerOptions);          
    mapLocation.addOverlay(gmarkers);             
    GEvent.addListener(gmarkers, "mouseover", function() {
        //hardcoding the font
        gmarkers.openInfoWindowHtml("<div style='width: 120px;font-family: Verdana, Helvetica, Tahoma, Arial, sans-serif;font-size: 10px;'>"+infoText+"</div>");
    });  
    locationBounds.extend(gmarkers.getPoint());
    points[iPoint] = gmarkers.getPoint();
    gmarkers_global[label] = gmarkers;
    ++iPoint;  
} 

function showMapDetails(i) 
{          
    try{
       GEvent.trigger(gmarkers_global[i], "mouseover");    
     }catch(e) {
     
     } 
}  

//set center and zoom based bounds
function setCenterAndZoom() {
    // ===== determine the centre from the bounds ====== 
    if (locationBounds) {    
        mapLocation.setZoom(mapLocation.getBoundsZoomLevel(locationBounds)-1);         
        mapLocation.setCenter(locationBounds.getCenter());
        window.setTimeout(function() {  mapLocation.panTo(locationBounds.getCenter());}, 0); 
        window.setTimeout(function() {   showMarkersFromData(mapLocation,gmarkers_global);}, 50);          
    }  
   
}   


function showMarkersFromData(mapLocation, localitiesArray) {
    try{
    var minlat = 0;
    var maxlat = 0;
    var minlon = 0;
    var maxlon = 0;
    var i = 0;
    var markerPoint;
    if (localitiesArray != null)
    {
        minlat = localitiesArray[0].lat;
        maxlat = localitiesArray[0].lat;
        minlon = localitiesArray[0].lon;
        maxlon = localitiesArray[0].lon;
        var length = localitiesArray.length;
        for (i = 0; i < length; i++)
        {           
            var latStr = localitiesArray[i].latitude;
            var lonStr = localitiesArray[i].longitude;
            var latitude = parseFloat(latStr);
            var longitude = parseFloat(lonStr);
            if (Math.abs(latitude) < Math.abs(minlat)) {
                minlat = latitude;
            }
            if (Math.abs(longitude) < Math.abs(minlon)) {
                minlon = longitude;
            }
            if (Math.abs(latitude) > Math.abs(maxlat)) {
                maxlat = latitude;
            }
            if (Math.abs(longitude) > Math.abs(maxlon)) {
                maxlon = longitude;
            }           
        }
        var loopCount = 0;
        while (!isVisible(localitiesMap, minlat, maxlat, minlon, maxlon) && loopCount < 5 && mapLocation.getZoom()>6)
        {
            localitiesMap.zoomOut();
            loopCount = loopCount + 1;
        }        
    }
    }catch(e) {
    //
    }
}     

function setupMarkers() {
    try{
       var mgrOptions = { borderPadding: 50, maxZoom: 15, trackMarkers: true };
       var mgr = new MarkerManager(mapLocation, mgrOptions);   
       mgr.addMarkers(gmarkers_global,mapLocation.getZoom());
       mgr.refresh();
   }catch(e){
   }
} 

function isVisible(map, minlat, maxlat, minlon, maxlon)
{
    
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    maxmaplat = northEast.lat();
    minmaplat = southWest.lat();
    maxmaplon = southWest.lng();
    minmaplon = northEast.lng();

    if (Math.abs(minlat) > Math.abs(minmaplat) && Math.abs(maxlat) < Math.abs(maxmaplat) && Math.abs(minlon) > Math.abs(minmaplon) && Math.abs(maxlon) < Math.abs(maxmaplon))
        return true;
    else
        return false;

}