﻿function MapInfoWindow(marker, html)
{
    this._map = null;
    this._marker = marker;
    this._html = html;
    this._div = null;
    this._timeout = null;
    this._visible = null;

    MapInfoWindow.prototype.initialize = function(map)
    {
        var div = document.createElement("div");
        div.className = 'mapInfoWindow';
        div.innerHTML = this._html;

        this._map = marker.map;
        this._div = div;
        this._visible = true;

        div.onmouseover = function()
        {
            clearInfowindowTimeout();
        }
        div.onmouseout = function()
        {
            setInfowindowTimeout(0);
        }

        this._map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    }

    MapInfoWindow.prototype.setTimeout = function(duration)
    {
        if (this._timeout)
            this._timeout.reset(duration);
        else
            this._timeout = $.timer(duration, clearInfowindow);
    }

    MapInfoWindow.prototype.clearTimeout = function()
    {
        if (this._timeout)
            this._timeout.stop();
    }

    MapInfoWindow.prototype.remove = function()
    {
        this.setTimeout(0);
    }

    MapInfoWindow.prototype.clear = function()
    {
        this.clearTimeout();

        if (this._div && this._div.parentNode)
            this._div.parentNode.removeChild(this._div);

        this._visible = false;
    }

    MapInfoWindow.prototype.unload = function()
    {
        this.clear();

        this._div = null;
        this._map = null;
        this._marker = null;
        this._timeout = null;
        this._visible = null;
    }

    MapInfoWindow.prototype.redraw = function()
    {
        var mapCenter = this._map.getCenter();
        var mapX = this._map.fromLatLngToDivPixel(mapCenter).x;
        var mapY = this._map.fromLatLngToDivPixel(mapCenter).y;

        var markerPoint = marker.getPoint();
        var markerX = this._map.fromLatLngToDivPixel(markerPoint).x;
        var markerY = this._map.fromLatLngToDivPixel(markerPoint).y;

        // CSS-Size of MapInfoWindow
        width = this._div.offsetWidth;
        height = 50;

        this._div.style.top = ((markerY < mapY) ? markerY : markerY - height) + 'px';
        this._div.style.left = ((markerX < mapX) ? (markerX + ((markerY < mapY) ? 0 : 10)) : markerX - (width + ((markerY < mapY) ? 0 : 10))) + 'px';
    }
}

var _infoWindow = null;

function setInfowindowTimeout(duration)
{
    if (_infoWindow)
        _infoWindow.setTimeout(duration);
}

function clearInfowindowTimeout()
{
    if (_infoWindow)
        _infoWindow.clearTimeout();
}

function clearInfowindow()
{
    if (currentMarker && currentMarker.overlay._visible)
    {
        if (currentMarker.overlay._timeout)
            currentMarker.overlay._timeout.stop();

        currentMarker.map.removeOverlay(currentMarker.overlay);
        currentMarker.overlay.clear();

        currentMarker.highlight(false, currentMarker.cluster);
    }
}
