﻿"use strict";

// Assume oa exists.
(function (oa) {
    // Get the pages page.
    oa.pages = oa.pages || {};

    // Add events.
    oa.pages.events = oa.pages.events || {};

    // The listing is this page.
    oa.pages.events.listing = (function () {
        // Initialize the filters.
        oa.filters.initialize();

        // Return the structure with an initialize function.
        return {
            initialize: function (mapSelector, feed) {
                // Called on initialization.
                $(document).ready(function () {
                    // Set up the filters date time pickers for the filters.
                    //                    $(".oa-calendar").datepicker({
                    //                        changeMonth: true,
                    //                        changeYear: true
                    //                    });

                    // Set the focus on the location.
                    $("#location").focus();

                    // Set the numeric filter on the radius input.
                    $("#radiusValue").numeric();

                    // The key for the old value.
                    var oldLocationValueKey = "oa-events-listings-location-oldValue";

                    // The geocoded class and selector.
                    var geocodedHiddenInputClass = "oa-events-listings-location-geocoded";
                    var geocodedHiddenInputClassSelector = "." + geocodedHiddenInputClass;

                    // The geocoder.
                    var geocoder;

                    // The latitude and longitude input template.
                    var inputTemplate = $("<input/>").attr("type", "hidden").addClass(geocodedHiddenInputClass);

                    // The location filter form selector.
                    var locationFilterSelector = $("#locationFilter");

                    // On the focus of the location, store the old location.
                    // On blur of location, geolocate with google.
                    $("#location").focus(function (e) {
                        // Get the current selector.
                        var currentTargetSelector = $(e.currentTarget);

                        // Set the data on the item.
                        currentTargetSelector.data(oldLocationValueKey, currentTargetSelector.val());
                    }).blur(function (e) {
                        // Get the target selector.
                        var currentTargetSelector = $(e.currentTarget);

                        // Get the value.
                        var location = currentTargetSelector.val();

                        // Get the old value.
                        // TODO: Create popData method on selector.
                        var oldValue = currentTargetSelector.data(oldLocationValueKey);

                        // Set the old value to null.
                        currentTargetSelector.data(oldLocationValueKey, null);

                        // Remove the geocoded classes.
                        $(geocodedHiddenInputClassSelector).remove();

                        // If the old value is equal to the new value, or the new value
                        // doesn't exist, then get out.
                        if (!location || (location == oldValue)) {
                            // Get out.
                            return;
                        }

                        // Geocode.
                        geocoder.getLocations(location, function (locations) {
                            // Check the status, only continue if the status is G_GEO_SUCCESS
                            if (locations.Status.code == G_GEO_SUCCESS) {
                                // Get the first placemark.
                                var placemark = locations.Placemark[0];

                                // Set the value of the textbox to the address.
                                currentTargetSelector.val(placemark.address);

                                // Get the coordinates.
                                var coordinates = placemark.Point.coordinates;

                                // Now add twice, once for the latitude and once for the longitude.
                                // Clone each time and add the appropriate name attribute.
                                locationFilterSelector.append(inputTemplate.clone().attr("name", "long").val(coordinates[0]));
                                locationFilterSelector.append(inputTemplate.clone().attr("name", "lat").val(coordinates[1]));
                            }

                        });
                    });

                    // If the browser is compatable for maps.
                    if (GBrowserIsCompatible()) {
                        // Set the geocoder for the location.
                        geocoder = new GClientGeocoder();

                        // Initialize the map.
                        var map = new GMap2($(mapSelector)[0]);

                        // Set the center and zoom for North America.
                        map.setCenter(new GLatLng(39, -96), 4);

                        // Set the UI to the default.
                        map.setUIToDefault();

                        // Add the overlay.
                        var gx = new GGeoXml(feed);
                        map.addOverlay(gx);
                    }
                });

                // Set the unload for the window.
                $(window).unload(function () {
                    // Unload google maps.
                    GUnload();
                });
            }
        };
    }());
}(oa));