(function($) {
    $.fn.extend({
        tooltip: function(strings, tipContent) {

            function findText(node, string) {
                if (node.nodeType == 3)
                    return searchText(node, string);
                else if (node.nodeType == 1 && node.childNodes && !(/(script|style)/i.test(node.tagName))) {
                    for (var i = 0; i < node.childNodes.length; ++i) {
                        i += findText(node.childNodes[i], string);
                    }
                }
                return 0;

            }

            function searchText(node, string) {
                var position = node.data.toUpperCase().indexOf(string);
                if (position >= 0) {
                    return highlight(node, position, string);
                }
                else {
                    return 0;
                }

            }

            function highlight(node, position, string) {

                // Set Tooltip
                var tip = document.createElement("div");
                    tip.id = 'tooltip';
                    tip.innerHTML = "<div style='width:310px; margin-right:10px; margin-top:2px'>"  + tipContent.toString() + "</div>";
                    tip.style.textDecorationNone = true;
                    tip.style.border = "none";

                // create surround anchor element to the specific word and give it properties
                var word = document.createElement("a");
                    word.setAttribute("rel", "tooltip");
                    word.style.color = "Black";
                    word.style.margin = "0px";
                    word.style.fontWeight = "Bold";
                    word.style.textDecoration = "none";
                    word.style.cursor = "Pointer";
                    word.setAttribute("title", tipContent.toString());
                
                //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
                if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {

                    var middlebit = node.splitText(position);
                    var endbit = middlebit.splitText(string.length);
                    var middleclone = middlebit.cloneNode(true);
                    word.appendChild(middleclone);
                    middlebit.parentNode.replaceChild(word, middlebit);
                    var tipTop = word.offsetTop - 100;
                    var tipLeft = word.offsetLeft;
                    tip.style.top = tipTop + "px";
                    tip.style.left = tipLeft + "px";

                    $(word).mouseenter(function(e) {
                        $(this).stop();
                        $(this).attr('title', '');
                        $(this).append(tip);

                        //Show the tooltip with faceIn effect
                        $('#tooltip').fadeIn('fast');
                        $('#tooltip').fadeTo('100', 1);

                    }).mouseout(function() {
                        $(this).stop();
                        //Remove the appended tooltip template
                        $(this).attr("title", $('#tooltip').html());

                        //Put back the title attribute's value
                        $("#tooltip").remove();
                    });
                }
                else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;

                    var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
                    if (ieversion >= 8) {
                        
                        var middlebit = node.splitText(position);
                        var endbit = middlebit.splitText(string.length);
                        var middleclone = middlebit.cloneNode(true);
                        word.appendChild(middleclone);
                        middlebit.parentNode.replaceChild(word, middlebit);

                        // Set Tip Location To The Ratio Of The Word
                        var tipTop = word.offsetTop - 100;
                        var tipLeft = word.offsetLeft;
                        tip.style.top = tipTop + "px";
                        tip.style.left = tipLeft + "px";

                        $(word).mouseenter(function(e) {
                            $(this).stop();
                            $(this).attr('title', '');
                            $(this).append(tip);

                            //Show the tooltip with faceIn effect
                            $('#tooltip').fadeIn('fast');
                            $('#tooltip').fadeTo('100', 1);

                        }).mouseout(function() {
                            $(this).stop();
                            //Remove the appended tooltip template
                            $(this).attr("title", $('#tooltip').html());

                            //Put back the title attribute's value
                            $("#tooltip").remove();
                        });
                    }

                    else if (ieversion >= 7) {

                        var middlebit = node.splitText(position);
                        var endbit = middlebit.splitText(string.length);
                        var middleclone = middlebit.cloneNode(true);
                        word.appendChild(middleclone);
                        middlebit.parentNode.replaceChild(word, middlebit);
                        var tipTop = word.offsetTop + 200;
                        var tipLeft = word.offsetLeft + 210;
                        tip.style.top = tipTop + "px";
                        tip.style.right = "0px";
                        tip.style.marginRight = tipLeft + "px";

                        $(word).mouseenter(function(e) {
                            $(this).attr('title', '');
                            $(this).append(tip);

                            //Show the tooltip with faceIn effect
                            $('#tooltip').fadeIn('fast');
                            $('#tooltip').fadeTo('100', 1);

                        }).mouseout(function() {
                            //Remove the appended tooltip template
                            $(this).attr("title", $('#tooltip').html());

                            //Put back the title attribute's value
                            $("#tooltip").remove();
                        });
                    }


                }

                return 1;
            }

            return this.each(function() {
                if (typeof strings == 'string') {
                    findText(this, strings.toUpperCase());
                }
                else {
                    for (var i = 0; i < strings.length; ++i) {
                        findText(this, strings[i].toUpperCase());
                    }
                }
            });
        }
    });
})(jQuery);

