$(document).ready(function () {
    $.ajax({
        url: '/script/colortip-1.0/sozluk.xml', //xml dosyamız
        type: 'GET',
        dataType: 'xml',
        timeout: 1000,
        success: function (xml) {
            $(xml).find('kelime').each(function () { //kelime node ve altında ki verileri alıyoruz
                var item_text = $(this).text(); // xml belgenin içeriğini text olarak alıyoruz
                findAndReplace('(' + $(this).attr("baslik") + ')', '<span old="' + $(this).attr("baslik") + '" title="' + item_text + '">' + $(this).attr("baslik") + '</span>');

                old = $(".dizin " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h1 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h2 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h3 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h4 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h5 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);

                old = $("h6 " + $(this).attr("baslik")).attr("old");
                $(".dizin " + $(this).attr("baslik")).html(old);
            });
            /* Adding a colortip to any tag with a title attribute: */
            $('[title]').colorTip({ color: 'yellow' });


        },
        error: function () {
            alert('Error loading XML document');
        }
    });
});

function findAndReplace(searchText, replacement, searchNode) {
    if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        return;
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = (searchNode || document.body).childNodes,
        cnLength = childNodes.length,
        excludes = 'html,head,style,title,link,meta,script,object,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data)) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function () {
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}
