document.body.className += ' js';

if (!location.host.indexOf('www.')) {
	location.replace(location.href.replace('www.', ''))
}

if (typeof $ == 'undefined') {
	function $(el) {
		return document.getElementById(el);
	}
}

if (typeof document.head == 'undefined') {
	document.head = document.getElementsByTagName('head')[0];
}

/*@cc_on
document.body.className += ' ie';
@*/


//= Stylish
function getOptions(promptOnIncomplete) {
    var params = [];
    var names = [];
    var styleOptions = document.getElementById("style-options");
    if (!styleOptions) {
        return "";
    }
    var inputs = styleOptions.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (names.indexOf(inputs[i].name) == -1) {
            names.push(inputs[i].name);
        }
        if (inputs[i].checked) {
            params.push(inputs[i].name + "=" + inputs[i].value);
        }
    }
    if (names.length != params.length) {
        if (promptOnIncomplete) {
            alert("Choose a value for every setting first.");
        }
        return null;
    }
    return params.join("&");
}

function stylishInstallChrome() {
    var options = getOptions(true);
    if (options != null) {
        var link = document.querySelector("link[rel='stylish-code-chrome']");
        var url = link.href.split("?")[0];
        if (options != "") {
            link.setAttribute("href", url + "?" + options);
        } else {
            link.setAttribute("href", url);
        }
        fireCustomEvent("stylishInstallChrome");
    }
}
function fireCustomEvent(name) {
    if (document.createEvent) {
        var stylishEvent = document.createEvent("Events");
        stylishEvent.initEvent(name, false, false, window, null);
        document.dispatchEvent(stylishEvent);
    } else {
        var communicationElement = document.getElementById("stylish-event-element");
        communicationElement.setAttribute("stylish-data", name);
        communicationElement.click();
    }
}
