jQuery.fn.extend(
{
    /**
     * 
     */
    kHoverBox: function(o) {
        return this.each(function() {
            new jQuery.kHoverBox(this, o);
        });
    }
});

var positionLeft = false;

jQuery.extend(
{
    kHoverBox: function(e, o) 
    {
        var publ = this;
        

        // Private methods/variables
        var priv = 
        {
            o: 
            {
            	loadContent: function(){},
				timeOut: 300,
				timeOutOut: 500      	
            },
            isMouseInBox: false,
            isMouseInLink: false,
            timeOut: false,

            init: function(e, o) 
            {
                if(o) jQuery.extend(priv.o, o);

				jQuery(e).bind('mouseover', function(event)
				{
					
					width = jQuery(document).width();										
					
					var left = event.pageX;
					var top =  event.pageY;
					
					if($.browser.msie){
						top += 10;
					}
					
					priv.isMouseInLink = true;
					
					if(!priv.timeOut)
					setTimeout(function()
					{						
						if(priv.isMouseInLink)
						{
							jQuery('#kHoverBox').remove();
							jQuery('body').append('<div id="kHoverBox"></div>');
							var khb = jQuery('#kHoverBox');							
							khb.hide()
							.bind('mouseover', function(event){
								priv.isMouseInBox = true;
							})
							.bind('mouseout', function(event){
								priv.isMouseInBox = false;
								priv.hide();								
							});
							
							priv.o.loadContent(e);
							khb.css({ position: 'absolute', left: '0', top: '0', display: 'block', visibility: 'hidden'});
							var hheight = 0;
							
							hheight = khb.get(0).offsetHeight;
							// pripadne posunuti doleva, kdyz vpravo neni misto
							if (left + 215 > jQuery(document).width()) {
								khb.css({ position: 'absolute', left: (left - 200) + 'px', top: top - hheight + 30 + 'px', display: 'none', visibility: 'visible'}).show();
								positionLeft = true;
							}							
							else {
								khb.css({ position: 'absolute', left: left + 'px', top: top - hheight + 30 + 'px', display: 'none', visibility: 'visible'}).show();
								positionLeft = false;
							}
																											
						}
					} , priv.o.timeOut);
					return false;
				});
				
				/*
				jQuery(e).bind('mousemove', function(event)
				{
					if(!priv.timeOut)
					setTimeout(function()
					{	
						var scroll = priv.getScrollXY();										
					
						var left = event.clientX + scroll[0];
						var top =  event.clientY + scroll[1];
									
						var khb = jQuery('#kHoverBox');												
						var hheight = 0;						
						// pripadne posunuti doleva, kdyz vpravo neni misto
						if (left + 210 > jQuery(document).width()) {
							khb.css({ position: 'absolute', left: (left - 200) + 'px', top: top - hheight + 30 + 'px', visibility: 'visible'});
							
							$(".corner").css('left','auto');
							$(".corner").css('right','0');
							$(".corner").css('background','url(/img/hoverbox-corner-right.png) right');
						}							
						else {													
							khb.css({ position: 'absolute', left: left + 'px', top: top - hheight + 30 + 'px', visibility: 'visible'});
							
							$(".corner").css('left','0');							
							$(".corner").css('background','url(/img/hoverbox-corner.png) left');
						}						
					} , priv.o.timeOut);
					return false;
				});
				*/ 
				
				
				jQuery(e).bind('mouseout', function()
				{	
					priv.isMouseInLink = false;	
					priv.hide();				
					return false;
				});
				
            },
            
			hide: function() 
			{
				priv.timeOut = true;
				setTimeout(function()
				{
					priv.timeOut = false;
					if(!priv.isMouseInLink && !priv.isMouseInBox)
					{
						jQuery('#kHoverBox').remove();
					}
				} , priv.o.timeOutOut);
			}

        };

        // Initialize
        priv.init(e, o);
    }
});

function loadComents(e)
{
	$.ajax({
		type: "GET",
		url: "/ajax/getComments.php",
		data: "prodID="+$('#productID').text()+"&comment="+$(e).text(),
		success: function(xml){
			var html = xml;
			$('#kHoverBox').html('<div class="hoverbox"><div id="corner"></div>' + html + '</div>');
			$('#kHoverBox .hoverbox').width(550);
		}		
	});
}



$(document).ready(function()
{
	$('.comments-list .adlink a').kHoverBox({loadContent: loadComents});
});
