﻿if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', InsertSideText, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', InsertSideText);
}

function InsertSideText() {
    var h1s = document.getElementsByTagName('h1');
    for (var i = 0; i < h1s.length; i++) {
        var h1 = h1s.item(i);
        if (h1.className != 'entry-title') continue;
        if (localStorage) {
            var lastVisitInt = parseInt(localStorage.getItem('last-visit'));
            if (lastVisitInt != null && !isNaN(lastVisitInt)) {
                var lastVisit = new Date();
                lastVisit.setTime(lastVisitInt);
                var abbrPub = h1.nextSibling;
                if (abbrPub == null) continue;
                if (abbrPub.nodeName.toLowerCase() != 'abbr') continue;
                var abbrUpd = h1.nextSibling.nextSibling;
                if (abbrUpd == null) continue;
                if (abbrUpd.nodeName.toLowerCase() != 'abbr') continue;
                var published = AbbrTitleToDatetime(abbrPub);
                var updated = AbbrTitleToDatetime(abbrUpd);
                if (published > lastVisit) {
                    InsertNote(h1, 'new');
                } else if (updated > lastVisit) {
                    InsertNote(h1, 'edited');
                } else {
                    InsertNote(h1, 'read');
                }
            }
            var d = new Date();
            localStorage.setItem('last-visit', d.getTime().toString());
        } else {
            InsertNote(h1, 'read');
        }
    }
}

function AbbrTitleToDatetime(abbr) {
    var a = abbr.attributes.getNamedItem('title').value;
    var d = a.substring(0, a.indexOf(' ')).split('.');
    var t = a.substring(a.indexOf(' ') + 1, a.length).split(':');
    return new Date(d[2], d[1] - 1, d[0], t[0], t[1], 0);
}

function InsertNote(h1, note) {
    var a = h1.parentNode;
    if (a.addEventListener) {
        a.addEventListener('mouseover', function () {
            NoteAdd(h1, note);
        }, false);
        a.addEventListener('mouseout', function () {
            NoteRemove(h1);
        }, false);
    } else if (a.attachEvent) {
        a.attachEvent('onmouseover', function () {
            NoteAdd(h1, note);
        });
        a.attachEvent('onmouseout', function () {
            NoteRemove(h1);
        });
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function NoteAdd(h1, note) {
    var a = h1.parentNode;
    var main = document.getElementById('main');
    var posy = findPos(a);
    var posx = findPos(main);
    var div = document.createElement('div');
    div.className = note;
    div.setAttribute('style', 'position: absolute; top: ' + posy[1] + 'px; left: ' + (posx[0] + 5) + 'px;');
    div.id = h1.innerText;
    document.body.appendChild(div);
}

function NoteRemove(h1) {
    var div = document.getElementById(h1.innerText);
    if (div != null) document.body.removeChild(div);
}
