/*global document: false, window: false, $: false, wgNamespaceNumber: false, wgUserName: false, alert: false, prompt: false */
/** voting tools ************************************************
 *
 *  by [[w:fa:user:ebraminio]] */

function extpar(par) {
    "use strict";
    return window.location.href.replace(new RegExp(".*?[&?]" + par + "=([^&]*).*"), "$1");
}
$(function () {
    "use strict";
    if (wgNamespaceNumber === 8) {
        return;
    }
    var vote = extpar("vote"),
        why = "",
        subcontent,
        bodycontent;
    if (/^(support|oppose|neutral)$/.test(vote)) {
        subcontent = $('#wpTextbox1').val();
        bodycontent = $('#bodyContent') || $('#mw_contentholder');
        bodycontent.css("display", "none");
        subcontent.replace(/\n$/, "");
        if (subcontent.search(new RegExp(wgUserName, "i")) !== -1) {
            alert("قبلا ثبت شده");
        } else {
            if (extpar("comment") === "yes") {
                why = prompt("دلیل");
            }
            if (why !== "") {
                why = why + " ";
            }
            if (vote === "oppose") {
                vote = "مخالف";
            } else if (vote === "support") {
                vote = "موافق";
            } else if (vote === "neutral") {
                vote = "ممتنع";
            }
            $('#wpTextbox1').val(subcontent + "#{{رای هیئت نظارت سوم|رای=" + vote + "|1=" + wgUserName + "}} " + why + "~~" + "~~");
            $('#wpSummary').val("رای " + vote + "، با استفاده از ابزار رای");
        }
        window.onbeforeunload = null;
        document.editform.submit();
    }
});