//Version 1.0
//Developed BY: Shaukat

jQuery.fn.AjaxToolTip = function(PageUrl, id, options)
{
    var Config = {
         backgroundcolor: "#ffffff",
         border         : "2px solid #389bd4",
         color          : "#000",
         padding        : "10px",
         posX           : "20px",
         posY           : "10px",
         width          : "0",
         maxwidth       : "400px",
         height         : "0px",
         maxheight      : "0px",
         event          : 'mousemove'
    };

    if(options)
    {
		jQuery.extend(Config, options);
	};

    $("#" + id).css({"cursor":"pointer"})

    var Width = parseInt(Config.width);
    var maxWidth = parseInt(Config.maxwidth);
    var Height = parseInt(Config.height);
    var maxHeight = parseInt(Config.maxheight);

    if(!document.getElementById('ToolTip_' + id))
    {
       $("body").append('<div id="ToolTip_' + id + '"></div>');
       $("#ToolTip_" + id).css({
        "float":"left",
        "padding": Config.padding,
        "background-color": Config.backgroundcolor, 
        "border": Config.border, 
        "color": Config.color, 
        "position": "absolute", 
        "z-index": "1001", 
        "display": "none"});
        
        //managing width of tool tip
        if(Width > 0)
            $("#ToolTip_" + id).css({"width":parseInt(Config.width)+"px"})
        
        //managing height of tool tip
        if(Height > 0)
            $("#ToolTip_" + id).css({"height":parseInt(Config.height)+"px"})
    }
    
    //$("#ToolTip_" + id).css({"width":"expression(this.width > 500 ? 500: true)"})

    function brwstester()
    {
        return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
    }


    //alert($('#ToolTip_' + id).html())     
    if($('#ToolTip_' + id).html() == "") // if first time load
    {   
        $("#ToolTip_" + id).html('<img id="imgLoad" src="/CommonImages/loading.gif" alt="loading"  style="margin:10px;" />');

        //show with loading image
        $("#ToolTip_" + id).show();
        
        //Fill the tool tip Contents
        $.get(PageUrl, function(response) {
            //var strResponse = response;//$("span#dlProdDetail").html(); 
            //var startIndex = strResponse.indexOf("<!-- start -->");
            //var endIndex = strResponse.indexOf("<!-- end -->");
            //var htmlStr = strResponse.substring(startIndex, endIndex + 12);
            //$("#ToolTip_" + id).html(htmlStr)
            $("#ToolTip_" + id).load(PageUrl + " #ToolTipData");
            $("#ToolTip_" + id).append('<br style="clear:both;" />')
        })
        
    }
    else
    {
    
        //alert($("#ToolTip_" + id).width())
        //show with content already loaded
        $("#ToolTip_" + id).show();
    }

    //alert($("#ToolTip_" + id).width() + "==" + maxWidth)
    if($("#ToolTip_" + id).width() > maxWidth && Width == 0 && maxWidth!=0)
        $("#ToolTip_" + id).css({"width":parseInt(Config.maxwidth)+"px"})

    if($("#ToolTip_" + id).height() > maxHeight && Height == 0 && maxHeight!=0)
        $("#ToolTip_" + id).css({"height":parseInt(Config.maxheight)+"px"})
    
    $("#" + id).bind(Config.event, function(e)
    {                            
        var padding =  parseInt(Config.padding);
        var posX = parseInt(Config.posX);              
        var posY = parseInt(Config.posY);
        var bodyWidth = brwstester().clientWidth;
        var bodyScrollLeft = brwstester().scrollLeft;
        var bodyHeight = brwstester().clientHeight;
        var bodyScrollTop = brwstester().scrollTop;
        //alert(screen.availHeight)
        //alert((document.body.clientHeight) + "|" + (brwstester().clientHeight))
        //alert($("#ToolTip_" + id).height() + ">" + bodyHeight)
        if ($("#ToolTip_" + id).height() > (bodyHeight - 20))
        {
            $("#ToolTip_" + id).css({"height":(bodyHeight - 40)+"px"})
            //$("#ToolTip_" + id).height() = 
        }
        if(bodyWidth + bodyScrollLeft <= (e.pageX + (2*padding + 5) + posX + $("#ToolTip_" + id).width()))
        {
        
           var lfts = e.pageX  - ((2*padding + 5) + posX + $("#ToolTip_" + id).width());
        }

        else
        {
              var lfts = e.pageX + posX;
        }            
        
        if(bodyHeight + bodyScrollTop <= (e.pageY + (2*padding + 5) + posY + $("#ToolTip_" + id).height()))
        {
        
            var tps =  e.pageY + (bodyHeight + bodyScrollTop - (e.pageY +  (2*padding + 5) + $("#ToolTip_" + id).height()));
        }
        else
        {
            var tps = e.pageY - (2*padding + 5) + posY;
        }
        
        if(lfts<0){lfts=0;}
        if(tps<0){tps=0;}

        $("#ToolTip_" + id).css({"left": lfts+"px", "top" : tps +"px"})
        
    });


   $("#" + id).bind("mouseout", function()
   {
        $("#ToolTip_" + id).hide();
   });


}
