
jQuery.extend({


    findPosX:function (obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          //alert(obj.tagName + " - " + obj.className + "\n" + obj.id + " - " + obj.offsetLeft);
          curleft += obj.offsetLeft;
          
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  },

  findPosY: function(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  },

    /* Find an object's position on the screen. */
    findPos: function (obj) {
        var curleft = $.findPosX(obj);
        var curtop = $.findPosY(obj);
        return [curleft,curtop];
	}
});

jQuery.fn.extend({

    attachOverlay: function(winId, p) {
        p = $.extend({
            offsetX: 20,
            offsetY: 0
		}, p);

        //var contentWinId = winId;
        var currItem;

        $.fn.openOverlay = function(data){
            var t = this[0];

            var pos = $.findPos(t);
           
             pos[0] += t.p.offsetX;
             $("#"+t.p.contentWinId).dialog('open');
             var dialogDiv = $(".ui-dialog");
             $(".ui-dialog").css("left",pos[0]).css("top",pos[1]);

            // re-position on screen if necessary
            
            browserWidth = $(window).width();
            if (document.documentElement && (document.documentElement.scrollLeft)) {
                browserX = document.documentElement.scrollLeft;
            }
            else {
                browserX = document.body.scrollLeft;
            }
            // reposition if outside the browser window
            if (($(".ui-dialog").offset().left + $(".ui-dialog").width()) > (browserWidth + browserX)) {
                $(".ui-dialog").css('left', (pos[0] + $(t).width() - $(".ui-dialog").width()) + 'px');
            }
            browserHeight = $(window).height();
            if (document.documentElement && (document.documentElement.scrollTop)) {
                browserTopY = document.documentElement.scrollTop;
            }
            else {
                browserTopY = document.body.scrollTop;
            }
            // reposition window if outside the browser window
            //if (($(".ui-dialog").offset().top + $(".ui-dialog").height()) > (browserTopY + browserHeight) ) {
                $(".ui-dialog").css('top', (pos[1] - $(".ui-dialog").height()) + 'px');
            //}
           

        };
        $.fn.closeOverlay = function(){
            var t = this[0];
            $("#"+t.p.contentWinId).dialog('close');
        };

        

        return this.each(function() {
            var t = this;
            t.p = p;

            t.p.contentWinId = winId;

            $("#"+t.p.contentWinId).dialog({
                resizable: false,
                autoOpen: false,
                width: 400
                //,show: 'slide'
                //,hide: 'slide'
            });

            $("#"+t.p.contentWinId).css("display","block");
           
            var hideDelay = 300;
            var hideDelayTimer = null;
            var beingShown = false;
            var shown = false;
            $(t).bind('mouseover', function() {
                //alert("over: " + shown);
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    hideDelayTimer = setTimeout(function () {
                        hideDelayTimer = null;
                        
                            beingShown = true;
                            
                            currItem = this;
                            $(t).openOverlay();
                            beingShown = false;
                            shown = true;
                    }, hideDelay);
                };
                
                return false;
            });
            
            $(t).bind('mouseout', function() {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (shown) {
                    hideDelayTimer = setTimeout(function () {
                        hideDelayTimer = null;
                        $(t).closeOverlay();
                        shown = false;

                    }, hideDelay);
                };

                return false;
                
            });
            
            $("#"+t.p.contentWinId).bind('mouseover', function() {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                //$(t).openOverlay();
            });
            
            
            $("#"+t.p.contentWinId).bind('mouseout', function() {
               if (hideDelayTimer) clearTimeout(hideDelayTimer);

               if (shown) {
                    hideDelayTimer = setTimeout(function () {
                        hideDelayTimer = null;
                        $(t).closeOverlay();
                        shown = false;

                    }, hideDelay);
               };
                return false;
            });
            
        });
    }

});

