allow_browser_window_close = false;

cwd=get_home_directory();
cwd.append("downloads");

download_buffer_automatic_open_target=OPEN_NEW_BUFFER_BACKGROUND;
hint_digits="0abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
url_remoting_fn = load_url_in_new_buffer;

add_hook("mode_line_hook", mode_line_adder(downloads_status_widget));
add_hook("mode_line_hook", mode_line_adder(buffer_count_widget), true);
remove_hook("mode_line_hook", mode_line_adder(clock_widget));

define_variable("firebug_url",
    "http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js");

function firebug (I) {
    var doc = I.buffer.document;
    var script = doc.createElement('script');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', firebug_url);
    script.setAttribute('onload', 'firebug.init();');
    doc.body.appendChild(script);
}
interactive("firebug", "open firebug lite", firebug);
define_key(content_buffer_normal_keymap, "f12", "firebug");

define_key(content_buffer_normal_keymap, "d", "follow-new-buffer-background");
define_key(content_buffer_normal_keymap, "y", "block-images-toggle");
define_key(content_buffer_normal_keymap, "j", "javascript-toggle");
define_key(content_buffer_normal_keymap, "W", "content-policy-whitelist-show");
define_key(content_buffer_normal_keymap, "w", "content-policy-whitelist-toggle-current-origin");

define_webjump("crawl", "http://crawl.chaosforge.org/index.php?title=%s");
define_webjump("github", "http://github.com/search?q=%s&type=Everything");
define_webjump("krautchan", "https://krautchan.net/board/%s");
define_webjump("logs", "http://logs.2pktfkt.de/%s");
define_webjump("reddit","http://www.reddit.com/r/%s");
define_webjump("rfc", "http://tools.ietf.org/html/rfc%s.txt");
define_webjump("wayback", function (url) {
    if (url) {
        return "http://web.archive.org/web/*/" + url;
    } else {
        return "javascript:window.location.href='http://web.archive.org/web/*/'+window.location.href;";
    }
}, $argument = "optional");

register_user_stylesheet(
    "data:text/css," + escape(
        "@namespace url(\"http://www.w3.org/1999/xhtml\");" +
        "span.__conkeror_hint { border-radius: 4px; display: block !important; font-size: 16px !important; line-height: 16px !important; padding: 2px !important; }" +
        "body [title] { border-bottom: 2px dotted !important; cursor: help; position: relative; }" +
        "body [title]:hover::after { background-color: white; border: 2px solid !important; color: black; content: attr(title); left: 0; position: absolute; top: 1.5em; z-index: 1; }" +
        "head { display: block; }" +
        "body { position: relative; }" +
        "link[rel][title], link[rel][title]::before { display: inline-block; margin: 0.375em 0 0.375em 0.375em; padding: 0.375em; }" +
        "link[rel][title]::before { background-color: white; border: 2px solid !important; color: black; content: attr(rel) ': ' attr(title); margin: 0; }" +
        "link[rel=alternate][title][type='application/atom+xml']::before, link[rel=alternate][title][type='application/rss+xml']::before { content: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJDSURBVHjajJJNSBRhGMd/887MzrQxRSLbFuYhoUhEKsMo8paHUKFLdBDrUIdunvq4RdClOq8Hb0FBSAVCUhFR1CGD/MrIJYqs1kLUXd382N356plZFOrUO/MMz/vO83+e93n+f+1zF+kQBoOQNLBJg0CTj7z/rvWjGbEOIwKp9O7WkhtQc/wMWrlIkP8Kc1lMS8eyFHpkpo5SgWCCVO7Z5JARhuz1Qg29fh87u6/9VWL1/SPc4Qy6n8c0FehiXin6dcCQaylDMhqGz8ydS2hKkmxNkWxowWnuBLHK6G2C8X6UJkBlxUmNqLYyNbzF74QLDrgFgh9LLE0NsPKxjW1Hz2EdPIubsOFdH2HgbwAlC4S19dT13o+3pS+vcSfvUcq9YnbwA6muW9hNpym/FWBxfh0CZkKGkPBZeJFhcWQAu6EN52QGZ/8prEKW+cdXq0039UiLXhUYzdjebOJQQI30UXp6mZn+Dtam32Afu0iyrgUvN0r+ZQbr8HncSpUVJfwRhBWC0hyGV8CxXBL5SWYf9sYBidYLIG2V87/ifVjTWAX6AlxeK2C0X8e58hOr/Qa2XJ3iLMWxB1h72tHs7bgryzHAN2o2gJorTrLxRHVazd0o4TXiyV2Yjs90uzauGvvppmqcLjwmbZ3V7BO2HOrBnbgrQRqWUgTZ5+Snx4WeKfzCCrmb3axODKNH+vvUyWjqyK4DiKQ0eXSpFsgVvLJQWpH+xSpr4otg/HI0TR/t97cxTUS+QxIMRTLi/9ZYJPI/AgwAoc3W7ZrqR2IAAAAASUVORK5CYII=') ' ' attr(title); }"
    )
);

require("block-content-focus-change.js");

require("content-policy.js");

var Set = function() {}
Set.prototype.add = function(o) { this[o] = true; }
Set.prototype.remove = function(o) { delete this[o]; }
Set.prototype.toString = function() {
    var l = [];
    for (var e in this) {
        if (this.hasOwnProperty(e)) {
            l.push(e)
        }
    }
    return l;
};

var content_policy_origin_whitelist = new Set();

function uri_origin(uri) {
    return uri.scheme + ' ' + uri.host + ' ' + uri.port;
}

interactive("content-policy-whitelist-toggle-current-origin",
    "Toggle if current origin is whitelisted for content policy.",
    function (I) {
        var origin = uri_origin(I.buffer.current_uri);
        if (origin in content_policy_origin_whitelist) {
            content_policy_origin_whitelist.remove(origin);
            I.minibuffer.message("Origin " + origin + " removed from content policy whitelist.");
        } else {
            content_policy_origin_whitelist.add(origin);
            I.minibuffer.message("Origin " + origin + " whitelisted for content policy.");
        }
    }
);

interactive("content-policy-whitelist-show",
    "Show content policy origin whitelist.",
    function (I) {
        I.minibuffer.message("Allowed origins: " + content_policy_origin_whitelist.toString())
    }
);

function block_content(content_type, content_location, request_origin) {
    if (uri_origin(request_origin) in content_policy_origin_whitelist) {
        return content_policy_accept;
    } else {
        return content_policy_reject;
    };
};

content_policy_bytype_table.font = block_content;
content_policy_bytype_table.image = block_content;
content_policy_bytype_table.media = block_content;
content_policy_bytype_table.object = block_content;
content_policy_bytype_table.script = block_content;
add_hook("content_policy_hook", content_policy_bytype);

interactive("block-images-toggle",
    "Turn the image blocking off if it is on and on if it is off.",
    function (I) {
        if (content_policy_bytype_table.image == block_content) {
            content_policy_bytype_table.image = function () content_policy_accept;
            I.minibuffer.message("Images are allowed.");
        } else {
            content_policy_bytype_table.image = block_content;
            I.minibuffer.message("Images are blocked.");
        }
    }
);

interactive("javascript-toggle",
    "Turn javascript off if it is on, and on if it is off.",
    function (I) {
        if (get_pref('javascript.enabled')) {
            session_pref('javascript.enabled', false);
            I.minibuffer.message("JavaScript disabled.");
        } else {
            session_pref('javascript.enabled', true);
            I.minibuffer.message("JavaScript enabled.");
        }
    }
);

function content_policy_widget (window) {
    this.class_name = "content-policy-widget";
    text_widget.call(this, window);
    this.add_hook("keypress_hook");
};

content_policy_widget.prototype = {
    constructor: content_policy_widget,
    __proto__: text_widget.prototype,
    update: function () {
        var text = ""
        if (content_policy_bytype_table.image == block_content) {
            text += "noIMG"
        } else {
            text += "IMG"
        };
        text += " ";
        if (get_pref('javascript.enabled')) {
            text += "JS";
        } else {
            text += "noJS";
        };
        this.view.text = text;
    }
};

add_hook("mode_line_hook", mode_line_adder(content_policy_widget));

require("adblockplus.js");

require("favicon");
add_hook("mode_line_hook", mode_line_adder(buffer_icon_widget), true);

session_pref("intl.accept_languages", "de-de, de, en-us, en");
session_pref('javascript.enabled', false);

set_protocol_handler("mailto", find_file_in_path("claws-mail"));