﻿(function ($) {
    $.fn.igwsqtip = function (options, value) {

        var defaults = {
            cssclass: null,
            maxwidth: null,
            delay: null
        };

        if (typeof options == 'object') {
            options = $.extend(defaults, options);
        }

        return this.each(function () {
            if (typeof options == 'string') {
                command = options.toLowerCase();
                if (command == 'update') {
                    $(".jq-igwsqtip").text(value);
                    $(this).attr('qtiptitle', value);
                }
                if (command == 'disable') {
                    $(".jq-igwsqtip").remove();
                }
                if (command == 'enable') {
                    if ($('.jq-igwsqtip').length == 0) {
                        $("<div class='jq-igwsqtip' style='position: absolute; z-index: 2147483647;'></div>").appendTo('body');
                    }
                }
            }
            else {
                $qt = new $igwsqtip(this, options);
            }
        });

        var $qt;

        function show(item) {
            var panel = $('.jq-igwsqtip');
            if (!$qt.hidden && panel.text() != '')
                item.show();
        };

        // CONSTRUCTOR
        function $igwsqtip(e, o) {

            this.options = o;
            this.obj = $(e);
            this.hidden = false;

            if ($('.jq-igwsqtip').length == 0) {
                $("<div class='jq-igwsqtip' style='position: absolute; z-index: 2147483647;'></div>").appendTo('body');
            }

            this.obj.mouseenter(function () {
                $qt.hidden = false;
                var panel = $('.jq-igwsqtip');
                if (panel.length != 0) {
                    panel.show();
                    var title = $(this).attr('qtiptitle');
                    if (title == null || title == '') {
                        title = $(this).attr('title');
                        $(this).attr('qtiptitle', title);
                        $(this).attr('title', '');
                    }
                    panel.html(title);

                    panel.css('width', 'auto');
                    if ($qt.options.maxwidth != null && panel.outerWidth(false) > $qt.options.maxwidth) {
                        panel.css('width', $qt.options.maxwidth);
                    }

                    if ($qt.options.cssclass != null)
                        panel.addClass($qt.options.cssclass);

                    var panelWidth = panel.outerWidth(false);
                    if ($qt.options.panelWidth != undefined)
                        panelWidth = $qt.options.panelWidth;
                    var panelHeight = panel.outerHeight(false);
                    panel.hide();

                    var width = $(document).width();
                    var height = $(document).height();
                    var targetTop = $(this).offset().top;
                    var targetLeft = $(this).offset().left;

                    var targetWidth = $(this).outerWidth(false);
                    var targetHeight = $(this).outerHeight(false);

                    var positionTop = targetTop - panelHeight;
                    var positionLeft = targetLeft + targetWidth;

                    if (positionTop < 0) {
                        positionTop = targetTop + targetHeight;
                    }
                    if (positionLeft + panelWidth > width) {
                        positionLeft = targetLeft - panelWidth;
                    }

                    if ($qt.options.orientation == 'left')
                        positionLeft = targetLeft - panelWidth;

                    panel.css('top', positionTop);
                    panel.css('left', positionLeft);

                    if ($qt.options.delay != null) {
                        setTimeout(function () { show(panel); }, $qt.options.delay);
                    }
                    else {
                        show(panel);
                    }
                }
            });

            this.obj.mouseleave(function () {
                $qt.hidden = true;
                $('.jq-igwsqtip').hide();
            });


        };
    };
})(jQuery);

