﻿//window.onunload = GUnload();

var my_icon_list = new Array();
var a = 0;
var md;

function include(filename) {
    var body = document.getElementsByTagName('body')[0];

    script = document.createElement('script');
    script.src = filename;
    script.type = 'text/javascript';

    body.appendChild(script)
}
function loadGoogleAPI() {
    if (location.href.indexOf("ismt.com.au") > -1) {
        addLoadEvent(include("http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAArNR6QDjkhmbH56L-4XAaPBSZKYfw3ftUh2tRgjdNM_Iou5bHbxToSMSU2wODIM3r5KMAWmlTnKexxg"));
    }
    else if (location.href.indexOf("ismt.net.au") > -1) {
        addLoadEvent(include("http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAArNR6QDjkhmbH56L-4XAaPBRBEkaND7ovEkKbCn-2LwZtdgKY7RTCBlEAE_jF9AsbwWCj0mK3wO-Nxw"));
    }
    else {
        addLoadEvent(include("http://maps.google.com/maps?file=api&v=2&sensor=false&key=ABQIAAAArNR6QDjkhmbH56L-4XAaPBRdZnAl4IsSfVOegv_qo4w8Yztr5hT13mfVS_roH6auioc5zxY42R4E0Q"));
    }
}
function addLoadEvent(func) {   
   var oldonload = window.onload;   
   if (typeof window.onload != 'function') {   
     window.onload = func;   
   } else {   
     window.onload = function() {   
       if (oldonload) {   
         oldonload();   
       }   
       func();   
     }   
   }   
}

function addGoogleMapL(ctlId) {
    if (GBrowserIsCompatible()) {
        if (document.getElementById(ctlId)) {
            var bounds = new GLatLngBounds();
            //var options = new Object();
            //options.clickable = true;
            md = new GMap2(document.getElementById(ctlId));
            md.setCenter(new GLatLng(0, 0), 0);
            //md.setCenter(new GLatLng(-37.91435,145.159645),2);
            md.setUIToDefault();

            var i = 0;
            var is = 0;
            var my_icons = new Array();
            var marker;
            if (my_icon_list.length > 0) {
                for (i = 0; i < my_icon_list.length; i++) {
                    if (my_icon_list[is][0] != 0 && my_icon_list[is][1] != 0) {
                        var customIcon = new GIcon(G_DEFAULT_ICON);
                        marker = createMarker(new GLatLng(my_icon_list[is][0], my_icon_list[is][1]), my_icon_list[is][2], customIcon)
                        md.addOverlay(marker);
                        bounds.extend(marker.getPoint());
                        is++
                    }
                }
            }
            md.setZoom(md.getBoundsZoomLevel(bounds));
            md.setCenter(bounds.getCenter());
        }
    }
}
function createMarkerL(point, html, customIcon) {
    var marker;
    if (customIcon)
        marker = new GMarker(point, { icon: customIcon });
    else
        marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}
function moveL(lat, lng, zoom) {
    md.setCenter(new GLatLng(lat, lng), zoom);
    //md.panTo(new GLatLng(lat, lng));
    //md.setZoom(zoom);
}
function addIconL(lat, lng, address) {
    //alert(lat + " " + lng + " " + address);
    if (my_icon_list) {
        if (lat.length > 0 && lng.length > 0) {
            if (lat != '-1.79769313486232E+308' && lng != '-1.79769313486232E+308') {
                var addNewIcon = true
                for (i = 0; i < my_icon_list.length; i++) {
                    if (my_icon_list[i][0] == lat && my_icon_list[i][1] == lng && my_icon_list[i][2] == address && my_icon_list[i][3] == status) {
                        addNewIcon = false
                    }
                }
                if (addNewIcon) {
                    my_icon_list[a] = new Array(3)
                    my_icon_list[a][0] = lat
                    my_icon_list[a][1] = lng
                    my_icon_list[a][2] = address
                    a++
                }
            }
        }
    }
}
function findAddressL(addressCtlId, localityCtlId, postcodeCtlId, msgCtlId, latCtlId, lngCtlId) {
    if (GBrowserIsCompatible()) {
        if (document.getElementById(localityCtlId).value != '') {
            var address, locality, postcode;
            address = document.getElementById(addressCtlId).value;
            locality = document.getElementById(localityCtlId).value;
            postcode = document.getElementById(postcodeCtlId).value;

            var geocoder = new GClientGeocoder();
            if (geocoder) {
                geocoder.getLatLng(
                      address + " " + locality + " " + postcode,
                      function(point) {
                          //alert(address + " " + locality + " " + postcode);
                          if (!point) {
                              document.getElementById(msgCtlId).innerHTML = "Address not found.";
                          } else {
                              document.getElementById(msgCtlId).innerHTML = "Latitude: " + point.lat() + ", Longitude: " + point.lng();
                              document.getElementById(latCtlId).value = point.lat();
                              document.getElementById(lngCtlId).value = point.lng();
                          }
                      }
                    );
            }
        }
    }
    return true;
}

//<script type="text/javascript" language="javascript">addIcon('<%# Eval("latitude") %>','<%# Eval("longitude") %>','<%# Eval("display_name") %><br><a href="Mapping.aspx?id=<%# Eval("store_name") %>">View Details &raquo;</a>');</script>

