﻿var courseMap = {
    map: null,
    postalCodeMarker: null,
    init: function () {

        $("#searchPostalForm").submit(function (e) {
            e.preventDefault();

            courseMap.searchMap();
        });

        $("#mapArea").show();

        myLatlng = new google.maps.LatLng(46.5660280, -84.3765930);
        var myOptions = {
            zoom: 3,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        this.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


        // get markers
        //  $.ajaxSetup({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json" });
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            url: "/service/mapping.asmx/GeoCodeFacilities",
            data: {},
            success: function (data) {

                if (data.d) {
                    
                    $.each(data.d, function () {

                        var that = this;

                        var marker = new google.maps.Marker({
                            map: courseMap.map,
                            draggable: false,
                            title: that.Facility,
                            animation: google.maps.Animation.DROP,
                            position: new google.maps.LatLng(that.Lng, that.Lat),
                            icon: "/images/red.png"
                        });

						//console.log(that.TAK);
                        var infowindow = new google.maps.InfoWindow({
                            content: "<strong>" + that.Facility + "</strong><br /> <br /> " + that.Address1 + "<br />" + that.City + "<br />" + that.Province + "<br />" + that.Postal + "<br /><br />" + that.Phone + "<br /><a href='" + that.Website + "' target='_blank'>View Web Site</a><br /><br />" + ((that.TAK == null) ? "" : that.TAK)
                        });

                        google.maps.event.addListener(marker, 'click', function () {
                            infowindow.open(courseMap.map, marker);
                        });

                    });

                }
                else {
                    alert('There are currently not courses participating.');
                }
            },
            error: function (param1) {

            }
        });


    },
    //Search
    searchMap: function () {
        //courseMap.init();
        var postalCode = $("#searchPostal").val();
        geocoder = new google.maps.Geocoder();

        geocoder.geocode({ 'address': postalCode }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                courseMap.map.setCenter(results[0].geometry.location);

                if (courseMap.postalCodeMarker != null) {
                    courseMap.postalCodeMarker.setMap(null);
                }
                courseMap.postalCodeMarker = new google.maps.Marker({
                    map: courseMap.map,
                    title: "Your postal code " + postalCode,
                    position: results[0].geometry.location,
                    icon: "/images/blue.png"
                });

                courseMap.map.setCenter(results[0].geometry.location);
                courseMap.map.setZoom(10);

            } else {
                alert("Could not find the postal code provided");
            }
        });

    }
};
