﻿(function($) {
    $.fn.showtimetip = function(options) {
        var defaults = {};

        var options = $.extend({}, defaults, options);
        var $this = this;
        var tip = null;
        var activeShowtime = null;

        this.initialize = function() {
            $(this).mousemove(function(e) {
                var elem = $(e.target);
                if (elem.hasClass('showtime')) {
                    e.stopPropagation();
                    e.preventDefault();
                    if (activeShowtime == elem) {
                        try {
                            tip.css({ left: (e.pageX + 25) + "px", top: (e.pageY - tip.h / 2) + "px" });
                            return;
                        }
                        catch (r) { }

                    }
                    $this.RemoveTip();
                    activeShowtime = elem;

                    var showtimeid = elem.attr("showtimeid");
                    if (typeof (showtimeid) == "undefined")
                        return;

                    var showtimetip = $(document.createElement("div"));
                    showtimetip.addClass("showtimetip");

                    var showtimehtml = "<div>Sal: " + elem.attr("roomname") + "</div>";
                    if (elem.attr("seats") != null) {
                        showtimehtml += "<div>Sæder: " + elem.attr("seats") + "</div>";
                    }
                    else {
                        showtimehtml += "<div>Sæder: Information er ikke tilgængelig</div>";
                    }
                    if (elem.attr("avail") != null) {
                        showtimehtml += "<div>Ledige: " + elem.attr("avail") + "</div>";
                    }
                    else {
                        showtimehtml += "<div>Ledige: Information er ikke tilgængelig</div>";
                    }
                    showtimetip.html(showtimehtml);
                    showtimetip.appendTo(document.body);
                    //document.body.appendChild(showtimetip);
                    var h = showtimetip.height();

                    showtimetip.css({ left: (e.pageX + 25) + "px", top: (e.pageY - h / 2) + "px" });

                    tip = showtimetip;
                    tip.h = h;
                }
                else {
                    $this.RemoveTip();
                    activeShowtime = null;
                }
            });

            this.RemoveTip = function() {
                if (tip != null) {
                    try {
                        $(tip).remove();
                        tip = null;
                    } catch (ex) { }
                }
            };
        };

        return this.initialize();

    };

})(jQuery);