jQuery.fn.kHoverBox = function(options)
{
	var o = jQuery.extend(
	{
    	loadContent: function(){},
		timeOutStart: 300,
		timeOutEnd: 150,
		positionY: 100,
		useIframeMask: true
	}, options);

	
	return this.each(function()
	{
		var isMouseInBox = false;
		var isMouseInLink = false;
	    var timeOut = false;
	    
		init(this);

        function init(e) 
        {
			jQuery(e).bind('mouseover', function(event)
			{
				var scroll = getScrollXY();										
				var left = event.clientX + scroll[0];
				var top =  event.clientY + scroll[1];
				
				isMouseInLink = true;
				
				if(!timeOut) setTimeout(function()
				{						
					if(isMouseInLink)
					{
						jQuery('#kHoverBox').remove();							
						jQuery('body').append('<div id="kHoverBox"></div>');
						var khb = jQuery('#kHoverBox');							
						khb.hide()
						.bind('mouseover', function(event){							
							isMouseInBox = true;
							return false;
						})
						.bind('mouseout', function(event){
							isMouseInBox = false;
							hide();				
							return false;				
						});
						
						o.loadContent(e);
						
						// Create masking iframe to hide plug-in objects and select in IE6:
						if(o.useIframeMask)
						{  
							iframe = document.createElement('iframe');
							iframe.className = "mask";
							iframe.border = "0";
							iframe.frameborder = "0";	
							iframe.src = "#";						
							khb.append(iframe);
						}
						
						khb.css({ position: 'absolute', left: '-2000px', top: '0', display: 'block', visibility: 'visible'});
						var hheight = 0;
						
						hheight = khb.get(0).offsetHeight;
						
						// If bottom of the box is lower than body, align bottom of the box with bottom of body:
						var positionY = o.positionY;
						
						var bodyHeight = jQuery('body').height();							 
						if(top - (hheight * positionY/100.0) + hheight > bodyHeight ) positionY =  (top + hheight - bodyHeight + 3) / hheight * 100.0;

						if(o.useIframeMask)
						{  
							jQuery(iframe).css({ width: khb.get(0).offsetWidth + 1 + 'px', height: hheight + 2 - 9 + 'px' });
						}

						khb.css({ left: left + 2 + 'px', top: top - (hheight * positionY/100.0) + 'px' }); 							
					
					}
				} , o.timeOutStart);
				return false;
			});
			jQuery(e).bind('mouseout', function()
			{	
				isMouseInLink = false;	
				hide();				
				return false;
			});
			
        };
        
		function hide() 
		{
			timeOut = true;
			setTimeout(function()
			{
				timeOut = false;
				if(!isMouseInLink && !isMouseInBox)
				{
					jQuery('#kHoverBox').remove();
				}
			} , o.timeOutEnd);
		};

		function getScrollXY() 
		{
			var scrOfX = 0, scrOfY = 0;
			if( typeof( window.pageYOffset ) == 'number' ) {
				//Netscape compliant
				scrOfY = window.pageYOffset;
				scrOfX = window.pageXOffset;
			} 				
			else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
			if(jQuery.browser.safari)
			{
				scrOfY = 0;
				scrOfX = 0;					
			}
			return [ scrOfX, scrOfY ];
		};
	});
};

