﻿Cwo.RegisterNamespace("Cwo.Maps.Mini");

Cwo.Maps.Mini = function (ContainerID, ListID, Latitude, Longitude, MapControlID, ShowButtonID, HideButtonID, BingMapKey, HasRoofTopLatLongs) {
    // private variables
    var cwoMapsMini = this,
        map = null,
        containerID = ContainerID,
        mapProvider = null,
        listID = ListID,
        latitude = Latitude,
        longitude = Longitude,
        mapControlID = MapControlID,
        showButtonID = ShowButtonID,
        hideButtonID = HideButtonID,
        list = $("#" + listID),
        showButton = $("#" + showButtonID),
        hideButton = $("#" + hideButtonID),
        showCssClass = "Selected",
        hasRoofTopLatLongs = HasRoofTopLatLongs,
        bingkey = $("#BingKeyHiddenInput").val();

    // public methods
    this.LoadMap = function () {

        if (mapProvider === null) {
            mapProvider = new Cwo.MapProvider(bingkey);
        }

        map = mapProvider.RenderMap(document.getElementById(mapControlID), { centerLat: latitude, centerLong: longitude, zoom: 12 });
        //map.onLoadMap = cwoMapsMini.MapLoaded;

        //Cwo.Page.EventController.Subscribe("Maps.MapRendered");
        cwoMapsMini.MapLoaded();
        if (Cwo.Common.GetCookie("PropertyWideMapToken") !== "") {
            map.SetClientToken(Cwo.Common.GetCookie("PropertyWideMapToken"));
        }

    };
    this.MapLoaded = function () {

        map.AddMarker(latitude, longitude, { icon: "", typeName: "mapPoint", width: 19, height: 21 });
    };
    this.ClickButton = function () {
        if (list.hasClass(showCssClass)) {
            // The mini map is already visible so hide it
            cwoMapsMini.HideMap();
        } else {
            cwoMapsMini.ShowMap();
        }
    };
    this.ShowMap = function () {

        cwoMapsMini.RegisterRoofTopLatLongUseage();

        // Change the text on any other previously opened mini maps buttons
        $("#" + containerID + " ul." + showCssClass + " li.ShowMiniMap span a").each(function (i) {
            $(this).text("Show Mini Map");
        });

        // Hide any other previously opened mini maps
        $("#" + containerID + " ul." + showCssClass).each(function (i) {
            $(this).removeClass(showCssClass);
        });

        // Show the map
        list.addClass(showCssClass);

        // Change the button text
        if ($("#" + showButtonID).text() === "Show Mini Map") {
            $("#" + showButtonID).text("Hide Mini Map");
        }

        /*
        if (!Cwo.Maps.IsScriptLoaded()) {
        Cwo.Page.EventController.Subscribe("Maps.BingScriptLoaded", cwoMapsMini.LoadMap);
        } else {
        */
        cwoMapsMini.LoadMap();
        //}
    };
    this.HideMap = function () {
        // Hide the map
        list.removeClass(showCssClass);

        // Change the button text
        if ($("#" + showButtonID).text() === "Hide Mini Map") {
            showButton.text("Show Mini Map");
        }
    };

    this.RegisterRoofTopLatLongUseage = function () {
        Cwo.AjaxCall("/webservices/Map.asmx/RegisterRooftopLatLongUseage", "{\"hasRoofTopLatLongs\":" + hasRoofTopLatLongs + "}", function () { /*do nothing*/ }, null, true);
    };

    // bind the events
    showButton.bind("click", this.ClickButton);
    hideButton.bind("click", this.HideMap);
};

