// ==UserScript==
// @name          Menic text/HTML
// @namespace     http://molhanec.net/lopuch/?n=Main.Okoun
// @description   Meni text na HTML podle obsahu pri psani prispevku
// @include *okoun.cz/*
// @exclude *billboard*
// @require http://code.jquery.com/jquery-2.0.3.min.js
// @grant none
// ==/UserScript==

function text_change() {
    var value = $(this).val();
    var switcher = $(this).parent().parent().parent().find('select[name=bodyType]');
    if (value.indexOf('<') >= 0) {
        switcher.val('html');
    } else {
        switcher.val('plain');
    }
}

try {
    $("textarea").keyup(text_change);
    var config = { childList: true, subtree: true };
    (new MutationObserver(function (records, observer) {
        records.forEach(function (record) {
            for (var node of record.addedNodes) {
                if (node.className == 'post content') {
                    $(node).find("textarea").keyup(text_change);
                }
            }
        });
    })).observe(document, config);
}
catch(exception) {
    alert(exception.message);
}
