﻿
// Minimum CSS required to support infoboxes
//$('head').append('<style>.infobox{position: absolute; z-index: 1000; display: none;}</style>');


/**
* Use this function to create Pushpins until it can use collection events.
*/
Microsoft.Maps.Map.prototype.AddPushpin = function (location, options) {

    var pin = new Microsoft.Maps.Pushpin(location, options);
    this.entities._tryLocationToPixel = this.tryLocationToPixel;
    this.entities.push(pin);
    this.pins.push(pin);

    Microsoft.Maps.Events.addHandler(pin, 'click', pin._infoboxToggle);
    Microsoft.Maps.Events.addHandler(pin, 'mouseover', pin._toggleImage);
    Microsoft.Maps.Events.addHandler(pin, 'mouseout', pin._toggleImage);

    var infoboxOptions = { width: 200, height: 100, showCloseButton: true, zIndex: 0, visible: false, offset: new Microsoft.Maps.Point(0, 10), showPointer: true };
    var infobox = new Microsoft.Maps.Infobox(location, infoboxOptions);
    infobox.setHtmlContent('<div class="infobox">' + options.infobox + '</div>');
    //infobox.setHtmlContent('<div class="Infobox" style="position: absolute; overflow: hidden; background-color: white; border: 1px solid rgb(136, 136, 136); left: 0px; top: 0px; width: 200px; height: 100px;"><a class="close" href="#">x</a><div class="content"></div></div>');
    //infobox.setHtmlContent('<div class="infoboxText"><b class="infoboxTitle">myTitle</b><a class="infoboxDescription">Description</a><a class="close" href="#">Close</a></div>');
    pin._infoBox = infobox;
    this.entities.push(infobox);

    return pin;
}

Microsoft.Maps.Map.prototype.clearAll = function () {
    map.pins = new Array();
    map.entities.clear();
}



/*********************************************************************************************/

Microsoft.Maps.Pushpin.prototype.ToggleInfoBox = function () {
    if (this._infoBox._visible)
        this.HideInfoBox();
    else 
    {
        map._hideOpen();
        this.ShowInfoBox();
    }
};

Microsoft.Maps.Pushpin.prototype.ShowInfoBox = function () {
    this._infoBox.setOptions({ visible: true });
    this._infoBoxShown = true;
};

Microsoft.Maps.Pushpin.prototype.HideInfoBox = function () {
    this._infoBox.setOptions({ visible: false });
    this._infoboxShown = false;
};

/*********************************************************************************************/

Microsoft.Maps.Pushpin.prototype._infoboxShown = false;

Microsoft.Maps.Pushpin.prototype._infoboxToggle = function (e) {
    e.target.ToggleInfoBox();
};

Microsoft.Maps.Pushpin.prototype._infoboxShow = function (e) {
    e.target.ShowInfoBox();
};

Microsoft.Maps.Pushpin.prototype._toggleImage = function (e) {
    var img = $(e.target.cm1001_er_etr.dom).children(0);
    if (img.attr("src").match(e.target.hoverImage + "$") == e.target.hoverImage)
        img.attr("src", e.target._icon);
    else
        img.attr("src", e.target.hoverImage);
};

Microsoft.Maps.Pushpin.prototype._infoboxHide = function (e) {
    e.target.HideInfoBox();
};

/*********************************************************************************************/

/*********************************************************************************************/

Microsoft.Maps.Pushpin.prototype._setOptions = Microsoft.Maps.Pushpin.prototype.setOptions;
Microsoft.Maps.Pushpin.prototype.setOptions = function (options) {
    // Pushpin options
    this._setOptions(options);

    if (options.hoverImage) this.hoverImage = options.hoverImage;
    if (options.title) this.title = options.title;
    if (options.description) this.description = options.description;
};

/*********************************************************************************************/

Microsoft.Maps.Map.prototype._hideOpen = function () {
    var entities = this.pins;
    var l = entities.length;
    for (var i = 0; i < l; i++) {
        var entity = entities[i];
        if (entity._infoBox._visible) {
            entity.HideInfoBox();
        }
    }
}

/*********************************************************************************************/

/**
* Extended "Map" constructor to listen to collection changes and map movements.
*/
Microsoft.Maps._Map = Microsoft.Maps.Map;
Microsoft.Maps.Map = function (container, options) {
    var map = new Microsoft.Maps._Map(container, options);
    map.pins = new Array();
    return map;
};

/*********************************************************************************************/

