﻿/// <reference path="../../helper/VeJavaScriptIntellisenseHelper.js" />
/// <reference path="../../jquery/jquery-vsdoc.js" />
/// <reference path="vemap.v70.js" />

var loaded = false;
var mapSourceUrl = 'http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=de-de';
var $infobox;
var infoboxWidth = 190;

var errorNoPin = 'Die genaue Adresse dieser Immobilie erfragen Sie bitte direkt beim Anbieter.<div class=\"h_010\">';

function InitLoading(lat, lon, zoom, callback) {
    $.ajax({
        url: mapSourceUrl,
        dataType: "script",
        complete: function () {
            setTimeout(function () { LoadMap(lat, lon, zoom, callback); }, 200);
        }
    });
}

function InitSpecific() {
    Microsoft.Maps.Pushpin.prototype.title = null;
    Microsoft.Maps.Pushpin.prototype.description = null;
    Microsoft.Maps.Pushpin.prototype.isObjectPin = null;

    if (typeof showPin == 'boolean') {
        if (showPin) {
            if (typeof objLat != 'undefined' && typeof objLon != 'undefined' && typeof GetPinDescription == 'function') {
                var pinDesc = GetPinDescription();
                var pushpinOptions = {
                    icon: pinDesc.url,
                    height: 43,
                    width: 34
                };
                var pin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(objLat, objLon), pushpinOptions);
                pin.title = pinDesc.title;
                pin.description = pinDesc.description;
                pin.isObjectPin = true;

                if (pinDesc.showInfoBox) {
                    Microsoft.Maps.Events.addHandler(pin, 'mouseover', pinMouseOver);
                    Microsoft.Maps.Events.addHandler(pin, 'mouseout', pinMouseOut);
                }

                map.entities.push(pin);
            }
        }
        else {
            AddErrorLayer(errorNoPin, "partner");
        }
    }
};

function displayInfobox(e) {
    stopInfoboxTimer(e);
    var pin = e.target;

    if (pin != null) {
        activePin = pin;

        var location = pin.getLocation();
        var $divMap = $("#divMap");

        var options = {
            id: 'infoBox1',
            visible: false,
            showPointer: true,
            showCloseButton: false,
            offset: 0,
            zIndex: 999
        };

        var pinLocation = map.tryLocationToPixel(pin.getLocation(), Microsoft.Maps.PixelReference.control);

        var ib = $('#ibSingle');
        $('#ibTitle').html(pin.title);
        $('#ibDescSingle').html(pin.description);

        var offsetLeft = getOffsetX(pinLocation.x);
        var offsetHeight = getOffsetY(pinLocation.y);
        var offsetPointer = pinLocation.y - offsetHeight - 18;

        $infobox = ib.clone().css({
            left: offsetLeft + "px",
            top: offsetHeight + "px",
            width: infoboxWidth + "px",
            visibility: "visible"
        })
        .bind('mouseenter', pinInfoboxMouseEnter)
        .bind('mouseleave', pinInfoboxMouseLeave);

        $('#infoBox').empty()
        $infobox.appendTo('#infoBox');

        if (offsetLeft >= pinLocation.x) {
            $infobox.find('.ibSingleRightPointer').hide();
            $infobox.css({ left: offsetLeft + "px", top: offsetHeight + "px" }).find('.ibSingleLeftPointer').show().css({ top: offsetPointer });
        }
        else {
            $infobox.find('.ibSingleLeftPointer').hide();
            $infobox.css({ left: offsetLeft + "px", top: offsetHeight + "px" }).find('.ibSingleRightPointer').show().css({ top: offsetPointer });
        }
    }
}

function hideInfobox(e) {
    $('#infoBox').empty();
    activePin = null;
}

function startInfoboxTimer(e) {
    if ($infobox != null && typeof $infobox.pinTimer != 'undefined' && $infobox.pinTimer != null) {
        clearTimeout($infobox.pinTimer);
    }
    if ($infobox != null) {
        $infobox.pinTimer = setTimeout(hideInfobox, 300);
    }
}

function stopInfoboxTimer(e) {
    if ($infobox != null && typeof $infobox.pinTimer != 'undefined' && $infobox.pinTimer != null) {
        clearTimeout($infobox.pinTimer);
    }
}

function mapViewChange(e) {
    stopInfoboxTimer(e);
    hideInfobox(e);
}
function pinMouseOver(e) {
    displayInfobox(e);
}
function pinMouseOut(e) {
    startInfoboxTimer(e);
}
function pinInfoboxMouseLeave(e) {
    hideInfobox(e);
}
function pinInfoboxMouseEnter(e) {
    stopInfoboxTimer(e);
}

function getOffsetX(pinLocationX) {
    var mapWidth = map.getWidth();
    var infoboxWidth = $('#ibSingle').width();

    if (mapWidth - infoboxWidth - 20 < pinLocationX) {
        return pinLocationX - infoboxWidth - 50;
    }

    return pinLocationX;
}

function getOffsetY(pinLocationY) {
    return pinLocationY - 28;
}

