﻿/// <reference path="jquery-1.4.2-vsdoc.js"/>
/// <reference path="jquery.hoverIntent.js"/>

(function() {

    var atCache = new Array();

    function showabiszTooltip() {

        var $this = $(this);
        var $tt = $("#abisztooltip");
        if ($tt.length == 0) { $tt = $('<div id="abisztooltip"></div>'); }

        $this.append($tt);

        if ($.browser.msie) { $tt.css("left", 0); }
        else { $tt.css("left", $this.position().left); }

        $tt.css({ top: $this.outerHeight(), width: "auto", height: "auto" });
        $tt.html('<img src="/images/ajax-loader.gif">&nbsp;Wird&nbsp;geladen');

        var c;
        c = $("a", $this).text();

        if (atCache[c]) {
            $tt.html(atCache[c]);
            $tt.css({ width: "auto", height: "auto" });
        }

        $tt.fadeIn(100);
        fixPosition($tt);

        if (atCache[c]) { return; }

        $.get('/ashx/abisztooltip.ashx', { q: c }, function(data) {
            atCache[c] = data;
            $tt.html(data);
            $tt.show();
            $tt.css({ width: "auto", height: "auto" });
            fixPosition($tt);
        });
    }

    function fixPosition(elm) {
        $elm = $(elm);
        var w = $elm.outerWidth(true);
        var h = $elm.outerHeight(true);
        //unten testen
        var ofs = $elm.offset();
        var posi = $elm.position();
        var diff = (ofs.top + h) - $(window).scrollTop() - $(window).height();
        if (diff > 0) { $elm.css("top", posi.top - diff - 5); }
        //oben testen
        ofs = $elm.offset();
        posi = $elm.position();
        if ((ofs.top - $(window).scrollTop()) < 0) {
            $elm.css("top", posi.top + ($(window).scrollTop() - ofs.top) + 5);
        }
        //rechts
        var diff = (ofs.left + w) - $(window).scrollLeft() - $(window).width();
        if (diff > 0) { $elm.css("left", posi.left - diff - 5); }

    }


    function hideTooltip() {
        $("#abisztooltip").fadeOut(100);
    }


    $(document).ready(function() {
        $("#abisztable td").hoverIntent({ interval: 250, over: showabiszTooltip, out: hideTooltip, timeout: 250 });
    })

})()

