// ==UserScript==
// @include       https://www.google.*/calendar/*
// @include       http://www.google.*/calendar/*
// @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() {
    var i, ids = ['calcontent', 'nav', 'sidebar'];

    for (i = 0; i < ids.length; i++) {
        if (!document.getElementById(ids[i])) {
            return false;
        }
    }

    return true;
}

function doIntegration() {
    var createButton = document.evaluate('//div[@id="sidebar"]/div[1]/div[1]', document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
    Unity.Launcher.addAction(_("Create an event"), function () { launchClickEvent(createButton); });
}

if (isCorrectPage()) {
    Unity.init({name: _("Google Calendar"),
        domain: 'google.com',
        homepage: 'https://www.google.com/calendar/render',
        iconUrl: "icon://unity-webapps-google-calendar",
        onInit: wrapCallback(doIntegration)});
}
