﻿/// <reference path="VeJavaScriptIntellisenseHelper.js" />
/* ************************************************************************************ */
/* VEToolkit v6.2.012509.1053 BETA - http://codeplex.com/VEToolkit                      */
/* Copyright (C) 2008 Chris Pietschmann (http://pietschsoft.com). All Rights Reserved.  */
/* This project is licensed under the Microsoft Public License (Ms-PL)                  */
/* This license can be found here: http://www.codeplex.com/VEToolkit/license            */
/* ************************************************************************************ */
VEToolkit.MiniMapVerticalAlignment = function() {
    /// <summary>The Vertical Alignment of the Mini Map.</summary>
    /// <field name="Top" type="number">Align to the Top edge of the VEMap.</field>
    /// <field name="Middle" type="number">Align to the Middle of the VEMap.</field>
    /// <field name="Bottom" type="number">Align to the Bottom edge of the VEMap.</field>
};
VEToolkit.MiniMapVerticalAlignment.Top = 1;
VEToolkit.MiniMapVerticalAlignment.Middle = 2;
VEToolkit.MiniMapVerticalAlignment.Bottom = 3;

VEToolkit.MiniMapHorizontalAlignment = function() {
    /// <summary>The Horizontal Alignment of the Mini Map.</summary>
    /// <field name="Left" type="number">Align to the Left edge of the VEMap.</field>
    /// <field name="Center" type="number">Align to the Center of the VEMap.</field>
    /// <field name="Right" type="number">Align to the Right edge of the VEMap.</field>
};
VEToolkit.MiniMapHorizontalAlignment.Left = 1;
VEToolkit.MiniMapHorizontalAlignment.Center = 2;
VEToolkit.MiniMapHorizontalAlignment.Right = 3;

VEToolkit.MiniMapExtender = function(map) {
    /// <summary>Extends Virtual Earth with easier to use functionality to manipulate/control the Mini Map.</summary>
    /// <param name="map" type="VEMap">The VEMap object instance for this MiniMapExtender object instance to connect to.</param>
    this._map = map;
    this._ShowMiniMap = true;
    this._ShowMiniMapResizer = true;
    this._miniMapSize = VEMiniMapSize.Small;
    this._verticalAlignment = VEToolkit.MiniMapVerticalAlignment.Top;
    this._horizontalAlignment = VEToolkit.MiniMapHorizontalAlignment.Right;
    this._verticalOffset = 0;
    this._horizontalOffset = 0;
    this._minimap_resizer_onclick$delegate = null;
    this._map_resize$delegate = VEToolkit.createDelegate(this, this._map_resize);
    this._map.AttachEvent("onresize", this._map_resize$delegate);
};
VEToolkit.MiniMapExtender.prototype = {
    Show: function() {
        /// <summary>Shows the MiniMap</summary>
        this._ShowMiniMap = true;
        this._realignMiniMap();
    },
    Hide: function() {
        /// <summary>Hides the MiniMap</summary>
        this._ShowMiniMap = false;
        this._realignMiniMap();
    },
    ShowResizer: function() {
        /// <summary>Shows the MiniMap Resizer control.</summary>
        this._ShowMiniMapResizer = true;
        this._realignMiniMap();
    },
    HideResizer: function() {
        /// <summary>Hides the MiniMap Resizer control.</summary>
        this._ShowMiniMapResizer = false;
        this._realignMiniMap();
    },
    SetMiniMapSize: function(size) {
        /// <summary>Sets the MiniMap Size.</summary>
        /// <param name="size" type="VEMiniMapSize">A VEMiniMapSize Enumeration to set the MiniMap Size to.</param>
        this._miniMapSize = size;
        this._realignMiniMap();
    },
    SetVerticalAlignment: function(v) {
        /// <summary>Sets the Vertical Alignment of the Mini Map.</summary>
        /// <param name="v" type="VEToolkit.MiniMapVerticalAlignment">A VEToolkit.MiniMapVerticalAlignment Enumeration to set the MiniMap's Vertical Alignment to.</param>
        this._verticalAlignment = v;
        this._realignMiniMap();
    },
    SetHorizontalAlignment: function(v) {
        /// <summary>Sets the Horizontal Alignment of the Mini Map.</summary>
        /// <param name="v" type="VEToolkit.MiniMapHorizontalAlignment">A VEToolkit.MiniMapHorizontalAlignment Enumeration to set the MiniMap's Vertical Alignment to.</param>
        this._horizontalAlignment = v;
        this._realignMiniMap();
    },
    SetVerticalOffset: function(v) {
        /// <summary>Sets the Vertical Offset of the Mini Map.</summary>
        /// <param name="v" type="number">The distance (in pixels) to offset/space the Mini Map from the vertical edge of the VEMap.</param>
        this._verticalOffset = v;
        this._realignMiniMap();
    },
    GetVerticalOffset: function() {
        /// <summary>Gets the Vertical Offset of the Mini Map.</summary>
        return this._verticalOffset;
    },
    SetHorizontalOffset: function(v) {
        /// <summary>Sets the Horizontal Offset of the Mini Map.</summary>
        /// <param name="v" type="number">The distance (in pixels) to offset/space the Mini Map from the horizontal edge of the VEMap.</param>
        this._horizontalOffset = v;
        this._realignMiniMap();
    },
    GetHorizontalOffset: function() {
        /// <summary>Gets the Horizontal Offset of the Mini Map.</summary>
        return this._horizontalOffset;
    },
    _getMapElement: function() { return this._map.mapelement; },
    _realignMiniMap: function() {
        if (this._map != null) {
            if (!this._ShowMiniMap) {
                this._map.HideMiniMap();
            } else {
                this._map.ShowMiniMap(0, 0, this._miniMapSize);
                /// Show the MiniMap in the aligned location we want it
                var minimap = VEToolkit.getChildById(this._getMapElement(), "MSVE_minimap");
                var xoffset = 0;
                var yoffset = 0;

                if (this._verticalAlignment == VEToolkit.MiniMapVerticalAlignment.Top) {
                    yoffset = 0;
                    yoffset += this._verticalOffset;
                } else if (this._verticalAlignment == VEToolkit.MiniMapVerticalAlignment.Middle) {
                    yoffset = (this._getMapElement().offsetHeight / 2) - (minimap.offsetHeight / 2);
                } else {
                    yoffset = this._getMapElement().offsetHeight - minimap.offsetHeight;
                    yoffset -= this._verticalOffset;
                }
                if (this._horizontalAlignment == VEToolkit.MiniMapHorizontalAlignment.Left) {
                    xoffset = 0;
                    xoffset += this._horizontalOffset;
                } else if (this._horizontalAlignment == VEToolkit.MiniMapHorizontalAlignment.Center) {
                    xoffset = (this._getMapElement().offsetWidth / 2) - (minimap.offsetHeight / 2);
                } else {
                    xoffset = this._getMapElement().offsetWidth - minimap.offsetWidth;
                    xoffset -= this._horizontalOffset;
                }

                this._map.ShowMiniMap(xoffset, yoffset, this._miniMapSize);

                var resizer = VEToolkit.getChildById(minimap, "MSVE_minimap_resize");
                if (this._ShowMiniMapResizer) {
                    /// Show the Mini Map resizer so the Mini Map can be resized
                    resizer.style.display = "";
                    if (this._minimap_resizer_onclick$delegate == null) {
                        this._minimap_resizer_onclick$delegate = VEToolkit.createDelegate(this, this._minimap_resizer_onclick);
                        VEToolkit.attachEvent(resizer, "click", this._minimap_resizer_onclick$delegate);
                    }
                } else {
                    /// Hide the Mini Map resizer so the Mini Map cannot be resized
                    resizer.style.display = "none";
                }
            }
        }
    },
    _minimap_resizer_onclick: function() {
        this._miniMapSize = (this._miniMapSize == VEMiniMapSize.Small ? VEMiniMapSize.Large : VEMiniMapSize.Small);
        var me = this;
        window.setTimeout(function() { me._realignMiniMap(); }, 100);
    },
    _map_resize: function() { this._realignMiniMap(); }
};