// ==UserScript==
// @include       http://news.yahoo.com/
// @include       http://mx.noticias.yahoo.com/
// @include       http://es.noticias.yahoo.com/
// @require       utils.js
// ==/UserScript==

// This placeholder gets munged with real data at build time.
var WebappsGettextDict = JSON.parse(unescape(
    "%7B%22GETTEXT%22%3A%22PLACEHOLDER%22%7D"
));

window.Unity = external.getUnityObject(1);

function isCorrectPage() {
    if (!document.getElementById('mediamegatron') && !document.getElementById('mediablistmixednewsforyoucatemp') && !document.getElementById('mediatabs')) {
        return false;
    }

    return true;
}

function messagingIndicatorSetup() {
    var i, res = document.evaluate('//*[@id="mediatabs"]/div[@class="bd"]/div/div/div/div/div/div[@class="bd"]/ul/li/div/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    if (!res.snapshotLength) {
        res = document.evaluate('//*[@id="mediamegatron"]/div[@class="bd"]/div[@class="feature"]/div[@class="meta"]/div[@class="bd"]/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    }

    if (!res.snapshotLength) {
        res = document.evaluate('//*[@id="mediablistmixednewsforyoucatemp"]/div[@class="bd"]/ul/li/div/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    }

    if (!res.snapshotLength) {
        res = document.evaluate('//ul[@class="tpl-thumb_100x75_title yom-list yom-list-large"]/li/div/a', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    }

    for (i = 0; i < res.snapshotLength; i++) {
        var node = res.snapshotItem(i);
        var title = node.textContent;

        if (!localStorage.getItem(title)) {
            localStorage.setItem(title, true);
            Unity.Notification.showNotification(_("Yahoo News"), title, null);
        }
    }
}

if (isCorrectPage()) {
    Unity.init({
        name: _("Yahoo News"),
        domain: 'yahoo.com',
        homepage: 'news.yahoo.com',
        iconUrl: "icon://unity-webapps-yahoonews",
        onInit: wrapCallback(messagingIndicatorSetup)
    });
}
