﻿if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', UpdateCheck, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', UpdateCheck);
}

function UpdateCheck() {
    Load();
    setTimeout(Store, 40 * 1000);
}

function Distance(s1, s2, maxOffset) {
    if (s1 == null || s1.length == 0) {
        return (s2 == null || s2.length == 0) ? 0 : s2.length;
    }

    if (s2 == null || s2.length == 0) return s1.length;

    var c = 0;
    var offset1 = 0;
    var offset2 = 0;
    var lcs = 0;

    while ((c + offset1 < s1.length) && (c + offset2 < s2.length)) {
        if (s1[c + offset1] == s2[c + offset2]) {
            lcs++;
        } else {
            c += (offset1 + offset2) / 2;
            if (c >= s1.length) c = s1.length - 1;
            if (c >= s2.length) c = s2.length - 1;
            offset1 = 0;
            offset2 = 0;
            for (var i = 0; i < maxOffset; i++) {
                if ((c + i < s1.length) && (s1[c + i] == s2[c])) {
                    offset1 = i;
                    break;
                }

                if ((c + i < s2.length) && (s1[c] == s2[c + i])) {
                    offset2 = i;
                    break;
                }
            }
        }
        c++;
    }

    return (s1.length + s2.length) / 2 - lcs;
}

if (typeof HTMLElement != 'undefined' && typeof document.documentElement.innerText != 'string') {
    HTMLElement.prototype.__defineSetter__('innerText', function (text) {
        while (this.hasChildNodes()) this.removeChild(this.firstChild);
        this.appendChild(document.createTextNode(text));
    });
    HTMLElement.prototype.__defineGetter__('innerText', function () {
        return this.textContent;
    });
}

function Load() {
    if (localStorage) {
        var key = ArticleName();
        var cookie = localStorage.getItem(key);
        if (cookie) {
            var array = cookie.split(/[\n]/);
            Compare(Read(), array);
        }
    }
}

function Compare(local, stored) {
    for (var l = 0; local[l]; l++) {
        var minDistance = Math.pow(local[l].innerText.length, 2);

        for (var s = 0; stored[s]; s++) {
            var distance = Distance(local[l].innerText, stored[s], 100);
            if (distance < minDistance) {
                minDistance = distance;
                if (distance == 0) break;
            }
        }

        if (minDistance > 20) {
            local[l].className += ' updated';
        }
    }
}

function Read() {
    var array = new Array();
    var paragraphs = document.getElementsByTagName('p');
    for (var i = 0; i < paragraphs.length; i++) {
        var p = paragraphs[i];
        if (p.tagName.toLowerCase() == 'p') {
            var content = p.innerText;
            if (content.length > 150 && content.indexOf('\n') == -1) {
                array.push(p);
            }
        }
    }
    return array;
}

function Store() {
    if (localStorage) {
        var cookie = new String();
        var array = Read();
        for (var i = 0; i < array.length; i++) {
            cookie += array[i].innerText + '\n';
        }
        var key = ArticleName();
        if (key != null) {
            localStorage.setItem(key, cookie);
        }
    }
}

function ArticleName() {
    var location = String(document.location);
    var pos1 = location.lastIndexOf('/') + 1;
    var pos2 = location.length;
    if (location.indexOf('.', pos1) == -1) {
        return location.substring(pos1, pos2);
    } else {
        return null;
    }
}