﻿/// <reference path="jquery-1.4.2-vsdoc.js"/>
/// <reference path="jquery.hoverIntent.js"/>

(function() {

    var atCache = new Array();

    function showTooltip() {
        var $this = $(this);
        var $tt = $("#artikeltooltip");
        if ($tt.length == 0) { $tt = $('<div id="artikeltooltip"></div>'); }
        $this.append($tt);
        $tt.css("left", (parseInt($this.outerWidth()) + 10) + "px");
        $tt.css({ top: "0px", width: "200px", height: "100px" });
        $tt.html('<img src="/images/ajax-loader.gif"> Wird geladen');

        var artnr;
        var classes = $this.attr("class").split(" ");
        for (i = 0; i < classes.length; i++) {
            if (classes[i].indexOf("art_") > -1) { artnr = classes[i].substr(4) };
        }
        if (artnr == "") return false;
        if (atCache[artnr]) {
            $tt.html(atCache[artnr]);
            $tt.css({ width: "auto", height: "auto" });
        }
        $tt.fadeIn(100);
        fixPosition($tt);
        if (atCache[artnr]) { return; }
        $.get('/ashx/pictooltip.ashx', { q: artnr }, function (data) {
            atCache[artnr] = 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);    
        }
    }

    function hideTooltip() {
        $("#artikeltooltip").fadeOut(100);
    }

    $(document).ready(function() {
        $(".artikeltt").hoverIntent({ interval: 250, over: showTooltip, out: hideTooltip, timeout: 250 });
    })

})()

