/**
 * Generic class to hold infobox interaction.
 * @author Guy Whitfield
 */
// InfoBox class
function InfoBox(html) {
    this.container;
    this.html = html;
}

// Displays the infobox
InfoBox.prototype.show = function (e) {
    if (this.container == undefined) {
        $("#map").append('<div class="infobox" style="display:none">' + this.html + '</div>');
        this.container = $("#map").children(".infobox").last();
        $(this.container).fadeIn();

        $(this.container).click(function () {
            $(this).fadeOut(rn.map.FADEOUT_SPEED);
        });

    } else {
        this.container.fadeIn();
    }
    rn.map.setInfoboxPosition(e, this.container);
};

// Hides the infobox
InfoBox.prototype.hide = function (e) {
    if (this.container != undefined) {
        $(this.container).fadeOut(rn.map.FADEOUT_SPEED);
    }
};

InfoBox.prototype.toggle = function (e) {
    if (this.container == undefined) {
        this.show(e);
    } else if ($(this.container).is(':visible')) {
        $(this.container).fadeOut(rn.map.FADEOUT_SPEED);
    } else {
        rn.map.setInfoboxPosition(e, this.container);
        $(this.container).fadeIn('slow');
    }
};

if (typeof (Microsoft) != "undefined" && typeof (Microsoft.Maps.Pushpin) != "undefined") {
    // Extends the Pushpin class to add an InfoBox object.
    Microsoft.Maps.Pushpin.prototype.setInfoBox = function (infoBox, type) {
        if (typeof this.infoBox != undefined && this.infoBox) {
            this.removeInfoBox();
        }

        this.infoBox = infoBox;
        this.type = type;

        // Add handlers for mouse events
        this.clickHandler = Microsoft.Maps.Events.addHandler(
        this, 'click',
        function (e) { infoBox.toggle(e); });
    }

    // Extends the Pushpin class to remove the current InfoBox object.
    Microsoft.Maps.Pushpin.prototype.removeInfoBox = function () {
        this.infoBox = null;

        // Remove handlers for mouse events
        Microsoft.Maps.Events.removeHandler(this.clickHandler);
    }
}
