﻿function EroHide(map)
{
    this.map = map;
    this.hide;
    this.isPanning = false;
    this.currentShape = null;
    this.currentShapeID;
    this.onMouseOverArgs = null;
}

EroHide.prototype = {

    Init: function()
    {
        this.hide = window.ero.hide;

        this.map.AttachEvent("onmousedown", createRef(this, this.OnMouseDown));
        this.map.AttachEvent("onmousemove", createRef(this, this.OnMouseMove));
        this.map.AttachEvent("onmouseup", createRef(this, this.OnMouseUp));
        this.map.AttachEvent("onclick", createRef(this, this.OnClick));
        this.map.AttachEvent("onstartpan", createRef(this, this.OnStartPan));
        this.map.AttachEvent("onstartzoom", createRef(this, this.OnStartZoom));
    },

    //////////////////////////////////////////////////
    OnMouseDown: function(e)
    {
        if (e.leftMouseButton)
            this.isPanning = true;
    },

    //////////////////////////////////////////////////
    OnMouseMove: function(e)
    {
        if (this.isPanning)
            this.CustomHideInfoBox(this.currentShape);
    },

    //////////////////////////////////////////////////
    OnMouseUp: function(e)
    {
        this.isPanning = false;
    },

    //////////////////////////////////////////////////
    OnClick: function(e)
    {
        if (e.elementID)
        {
            var clickedShape = this.map.GetShapeByID(e.elementID);

            if (clickedShape)
            {
                if (window.ero.isVisible() && (clickedShape == this.currentShape))
                {
                    // We clicked on the shape which has an infobox open.
                    this.CustomHideInfoBox(this.currentShape);
                }
                else if (window.ero.isVisible() && this.currentShape != null)
                {
                    // We clicked on a different shape. Close the infobox first,
                    // then show it for the new shape.
                    this.CustomHideInfoBox(this.currentShape);
                    this.CustomShowInfoBox(clickedShape);
                }
                else if (true == false)
                {
                    this.CustomShowInfoBox(null);
                }
                else
                {
                    this.CustomShowInfoBox(clickedShape);
                }
            }
        }
    },

    //////////////////////////////////////////////////
    OnStartPan: function(e)
    {
        if (e.leftMouseButton)
        {
            // We don't want to handle this here, because a simple mouse click on a shape,
            // could be interpreted as a "start pan" action.
            return;
        }

        this.CustomHideInfoBox(this.currentShape);
    },

    //////////////////////////////////////////////////
    OnStartZoom: function(e)
    {
        this.CustomHideInfoBox(this.currentShape);
    },

    //////////////////////////////////////////////////
    DisableEroHide: function()
    {
        this.hide = window.ero.hide;

        window.ero.hide = function(a)
        {
            return;
        }
    },

    //////////////////////////////////////////////////
    EnableEroHide: function()
    {
        window.ero.hide = this.hide;
    },

    //////////////////////////////////////////////////
    CustomShowInfoBox: function(shape)
    {
        if (!shape)
            return;

        this.DisableEroHide();
        this.map.ShowInfoBox(shape);
        this.currentShape = shape;
    },

    //////////////////////////////////////////////////
    CustomHideInfoBox: function()
    {
        if (!window.ero.isVisible())
            return;

        // Before actually calling the hide method, we need to restore the ero hide function,
        this.EnableEroHide();
        this.map.HideInfoBox();
        this.currentShape = null;
    }
};

function createRef(instance, method)
{
    return function() { method.apply(instance, arguments); }
}
