﻿$(function() {
    $(document).mousemove(function(e){
        cursor_position.x = e.pageX;
        cursor_position.y = e.pageY;
    });

    var $tooltip = $('\
        <div class="tooltip-outer">\
			<div class="tooltip-wrapper">\
				<div class="tooltip-shadow" style="left: 1px; top: 1px;"></div>\
				<div class="tooltip-shadow" style="left: 2px; top: 2px;"></div>\
				<div class="tooltip-shadow" style="left: 3px; top: 3px;"></div>\
				<div class="tooltip-shadow" style="left: 4px; top: 4px;"></div>\
				<div class="tooltip-shadow" style="left: 5px; top: 5px;"></div>\
				<div class="tooltip-shadow" style="left: 6px; top: 6px;"></div>\
				<div class="cluetip-orion"></div>\
			</div>\
            <div class="tooltip-stem"></div>\
        </div>\
    ');

    var $tooltip_inner = $("div.cluetip-orion", $tooltip);
    var $tooltip_stem = $("div.tooltip-stem", $tooltip);
    var $tooltip_wrapper = $("div.tooltip-wrapper", $tooltip);
    var $tooltip_shadow = $("div.tooltip-shadow", $tooltip).css({ opacity: 0.1 });
    var $tooltip_divs = $tooltip.children().hide();
    var $tooltip_timer;
    var $tooltip_request;
    var $tooltip_parent;

    var is_tooltip_available = false;
    var cursor_position = { x: 0, y: 0 };

    var getId = /netobject=([a-z]+)(:|%3a)(\d+)/i;

    $.swtooltip = function(control, tipText) {
        var $control = $(control);
                    
        control.onmouseover = function() {
                $tooltip_timer = setTimeout(function() {
                    if ($tooltip_request) {
                        $tooltip_request.abort();
                        $tooltip_request = null;
                    }
                    
                    if (!$control.swtooltip)
                        $control.swtooltip = new $tooltipHelper(control, tipText);

                    $control.swtooltip.show();
                }, 200);
            };
        control.onmouseout = function() {
                if ($tooltip_timer) {
                    clearTimeout($tooltip_timer);
                    $tooltip_timer = null;
                }

                if ($tooltip_request) {
                    $tooltip_request.abort();
                    $tooltip_request = null;
                }

                if ($control.swtooltip)
                    $control.swtooltip.hide();
            };
    }

    $tooltipHelper = function(control, tipText) {
        this.control = control;
        this.tipText = tipText;
        this.cache = {};

        if (tipText || this.url()) {
            var _this = this;

            $("img[alt]", control).removeAttr("alt");

            if (!is_tooltip_available) {
                is_tooltip_available = true;
                $("body").append($tooltip);
            }
        }
    }

    $.extend($tooltipHelper.prototype, {
        static_cache: {},

        hide: function() {
            $tooltip.hide();
            $tooltip_divs.hide();
        },

        show: function() {
            if (this.tipText) {
                $tooltip_inner.html(this.tipText);
                this.adjustPosition();
            }
            else {
                $tooltip_request = $.ajax(this.ajaxSettings());
            }
        },

        adjustPosition: function() {
            $tooltip.removeClass("stem-bottom-top stem-top-right stem-bottom-left stem-bottom-right");
            $tooltip_divs.show();
            $tooltip.show()
            var v = viewport();
            var p = position($tooltip_wrapper);
            var r = (v.x + v.cx < p.x + $tooltip_inner.width());
            var b = (v.y + v.cy < p.y + $tooltip_inner.height());

            if (b && r) {
                $tooltip.addClass("stem-bottom-right");
                $tooltip.css('top', p.y - 20);
                $tooltip.css('left', p.x - $tooltip_inner.width() - 40);
            }
            else if (b) {
                $tooltip.addClass("stem-bottom-left");
                $tooltip.css('top', p.y - 20);
                $tooltip.css('left', p.x + 15);
            }
            else if (r) {
                $tooltip.addClass("stem-top-right");
                $tooltip.css('top', p.y);
                $tooltip.css('left', p.x - $tooltip_inner.width() - 40);
            }
            else {
                $tooltip.addClass("stem-top-left");
                $tooltip.css('top', p.y);
                $tooltip.css('left', p.x + 15);
            }

            if ((p.cy + p.y) < $tooltip_stem.height())
                $tooltip_wrapper.css('top', '0');

            $tooltip_shadow.css({
                width: $tooltip_inner.width(),
                height: $tooltip_inner.height()
            });
        },

        ajaxSettings: function() {
            var _this = this;

            return $.extend({
                url: this.url(),

                complete: function() {
                    _this.adjustPosition();
                }
            }, this.ajaxSettingsBase);
        },

        ajaxSettingsBase: {
            dataType: 'html',

            error: function() {
                $tooltip_request = null;
                $tooltip_inner.html('<i>sorry, the contents could not be loaded</i>');
            },

            success: function(data) {
                $tooltip_request = null;
                data = data.replace(/<s(cript|tyle)(.|\s)*?\/s(cript|tyle)>/g, '').replace(/<(link|title)(.|\s)*?\/(link|title)>/g, '');
                $tooltip_inner.html(data);
            }
        },

        width: function() {
            return this.static_cache.width || (this.static_cache.width = this.getWidth());
        },

        getWidth: function() {
            var left = 0;
            var right = 0;

            $tooltip.children().each(function() {
                var p = position($(this));

                if (p.x < left)
                    left = p.x;

                if (p.x + p.cx > right)
                    right = p.x + p.cx;
            });

            return right - left;
        },

        netobject: function() {
            return this.cache.netobject || (this.cache.netobject = this.getNetobject())
        },

        getNetobject: function() {
            return getId.exec(this.control.href);
        },

        url: function() {
            return this.cache.url || (this.cache.url = this.getUrl());
        },

        getUrl: function() {
            var id = this.netobject();
            var tipURL = id && NetObjectTypeToTipPagePath[id[1]];

            if (!tipURL || (this.currentPageId && (id[1] == this.currentPageId[1]) && (id[3] == this.currentPageId[3])))
                return null;

            tipURL += ((tipURL.indexOf('?') < 0) ? '?' : '&') + "NetObject=" + id[1] + ":" + id[3];

            return tipURL;
        },

        currentPageId: getId.exec(document.location)
    });

    function viewport() {
        var $window = $(window);

        return {
            x: $window.scrollLeft(),
            y: $window.scrollTop(),
            cx: $window.width(),
            cy: $window.height()
        };
    }

    function position(control) {
        var p = control.position();
        return {
            x: cursor_position.x, //p.left,
            y: cursor_position.y, //p.top,
            cx: control.width,
            cy: control.heigth
        };
    }

    $("a[tooltip!='processed'][href*='NetObject=']").livequery(function() {
        this.tooltip = 'processed';
        $.swtooltip(this);
    });
});
