﻿$(document).ready(function() {
    var map = null;
    var markers = [];

    // Call this function when the page has been loaded
    $("#stores_map").each(function() {
        var s = $(this);
        map = new google.maps.Map2(this);

        // Set map options
        map.disableDoubleClickZoom();
        map.addControl(new GSmallMapControl());
        var VEicon = new GIcon(G_DEFAULT_ICON);
        VEicon.image = "/common/images/icon-gmaps-marker.png";
        VEicon.iconSize = new GSize(27, 28);
        markerOption = { icon: VEicon };

        // Get locations
        var locations = $("#DefaultLocation input").val().split("|");

        for (var l in locations) {
            var coordinates = locations[l].split(";");
            var location = new GLatLng(parseFloat(coordinates[0]), parseFloat(coordinates[1]));
            var marker = new GMarker(location, markerOption);
            map.addOverlay(marker);
            var html = "<div id=\"map_infowindow\"><h4>" + coordinates[2] + "</h4><p>" + coordinates[3] + "</p><p><a href=\"/Stores/" + coordinates[4] + ".aspx\">Full store details</a></p></div>"; // store name and address
            if (locations.length == 1) html = "<div id=\"map_infowindow\"><h4>" + coordinates[2] + "</h4><p>" + coordinates[3] + "</p></div>";
            marker.bindInfoWindowHtml(html);
            markers[l] = marker;
        }
        map.setCenter(markers[0].getLatLng(), 13);
    });

    $(".closest_stores input").click(function() {
        GEvent.trigger(markers[$('.closest_stores input').index(this)], 'click');
    }).eq(0).trigger('click');

    $(".searchMap input[type=image]").click(function() {
        var address = $(".searchMap input[type=text]").val() + ", Australia";
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                  alert(address + " not found");
                } else {
                  $.ajax({
                      type: "POST",
                      url: "/services/storelocator.asmx/FindNearestStoreByLongLat",
                      data: "longitude=" + point.x + "&latitude=" + point.y + "&count=1",
                      success: function(data) {
                          map.clearOverlays();
                          $(data).find('StoreProfile').each(function() {
                              $this = $(this);
                              var location = new GLatLng($this.find("latitude").text(), $this.find("longitude").text());
                              var VEicon = new GIcon(G_DEFAULT_ICON);
                              VEicon.image = "/common/images/icon-gmaps-marker.png";
                              VEicon.iconSize = new GSize(27, 28);
                              markerOption = { icon: VEicon };
                              map.addOverlay(new GMarker(location, markerOption));
                              map.panTo(location);
                          });
                      }
                  });
                }
            }
        );
        return false;
    });
});
               