var isMSIE = (navigator.appName == "Microsoft Internet Explorer");

function createElement(elementName) {
    if (isMSIE) {
        return document.createElement(elementName);
    }
    else {
        return document.createElementNS("http://www.w3.org/1999/xhtml", elementName);
    }
}

function beautifyUnourderedList (listNode) {
        var newList = createElement("table");
        var body = createElement("tbody");
        newList.appendChild(body);
        if (!isMSIE) {
            newList.setAttribute("class", listNode.getAttribute("class")+"-t");
        }
        else {
            newList.className = listNode.className+"-t";
        }
        var items = listNode.getElementsByTagName("li");
        for (var j = 0; j < items.length; j++) {
            var newItem = createElement("tr");
            var marker = createElement("td");
            var contents = createElement("td");
            marker.appendChild(document.createTextNode("—"));
            for (var k = 0; k < items[j].childNodes.length; k++)
                contents.appendChild(items[j].childNodes[k].cloneNode(true));
            if (!isMSIE) {
                contents.setAttribute("class", "list-item");
                marker.setAttribute("class", "list-marker");
            }
            else {
                contents.className = "list-item";
                marker.className = "list-marker";
            }
            newItem.appendChild(marker);
            newItem.appendChild(contents);
            body.appendChild(newItem);
        }
        (listNode.parentNode).replaceChild(newList, listNode);
}

function beautifyUnourderedLists(className) {
    var ulNodes = document.getElementsByTagName("ul");
    var ulArray = new Array();
    for (var i = 0; i < ulNodes.length; i++) {
            if (ulNodes[i].getAttribute("class") != className && // Gecko, Opera
                ulNodes[i].className != className) // MSIE
                continue;
            ulArray.push(ulNodes[i]);
    }

    for (var i = 0; i < ulArray.length; i++)
        beautifyUnourderedList(ulArray[i]);
}

// Написано по мотивам java.awt.Color.HSBtoRGB() из Java SDK

function HSBtoRGB (hue, saturation, brightness) {
    var r = 0;
    var g = 0;
    var b = 0;
    if (saturation == 0.0) {
        r = g = b = Math.floor(brightness * 255.0 + 0.5);
    }
    else {
        var h = (hue - Math.floor(hue)) * 6.0;
        var f = h - Math.floor(h);
        var p = brightness * (1.0 - saturation);
        var q = brightness * (1.0 - saturation * f);
        var t = brightness * (1.0 - (saturation * (1.0 - f)));

        switch (Math.floor(h)) {
            case 0:
            r = Math.floor(brightness * 255.0 + 0.5);
            g = Math.floor(t * 255.0 + 0.5);
            b = Math.floor(p * 255.0 + 0.5);
            break;

            case 1:
            r = Math.floor(q * 255.0 + 0.5);
            g = Math.floor(brightness * 255.0 + 0.5);
            b = Math.floor(p * 255.0 + 0.5);
            break;

            case 2:
            r = Math.floor(p * 255.0 + 0.5);
            g = Math.floor(brightness * 255.0 + 0.5);
            b = Math.floor(t * 255.0 + 0.5);
            break;

            case 3:
            r = Math.floor(p * 255.0 + 0.5);
            g = Math.floor(q * 255.0 + 0.5);
            b = Math.floor(brightness * 255.0 + 0.5);
            break;

            case 4:
            r = Math.floor(t * 255.0 + 0.5);
            g = Math.floor(p * 255.0 + 0.5);
            b = Math.floor(brightness * 255.0 + 0.5);
            break;

            case 5:
            r = Math.floor(brightness * 255.0 + 0.5);
            g = Math.floor(p * 255.0 + 0.5);
            b = Math.floor(q * 255.0 + 0.5);
            break;
        }
    }
    return "rgb("+r+","+g+","+b+")";
}

function colorize (obj, saturation, brightness) {
    var hue = Math.random();
    obj.style.backgroundColor = HSBtoRGB(hue, saturation, brightness);
}

function colorizeHeader () {
    var header = document.getElementById("header");
    if (header != null) {
        colorize(header, 0.4, 0.7);
    }
}

// Да будут таблицы стилей валидными,
// и да получит Mozilla -moz-border-radius!
function mozillize () {
    var mozillaStyleSheet = document.createElementNS("http://www.w3.org/1999/xhtml", "style");
    mozillaStyleSheet.appendChild(document.createTextNode(".distros, pre, .rouble, .process, .note, .bnote { -moz-border-radius: 8px }"));
    // mozillaStyleSheet.appendChild(document.createTextNode(".rss { -moz-border-radius: 3px }"));
    document.getElementsByTagName("head")[0].appendChild(mozillaStyleSheet);
}

function init () {
    beautifyUnourderedLists ("dashedlist");
    colorizeHeader ();
    if (navigator.appName == "Netscape") mozillize ();
}

var isLocalized = false;
function russianLocalization () {
    var footer = document.getElementById("footer");
    if (isLocalized) {
        footer.style.backgroundImage = "none";
        footer.style.backgroundRepeat = "repeat-x";
        footer.style.paddingBottom = "1em";
    }
    else {
        footer.style.backgroundImage = "url('russian.png')";
        footer.style.backgroundRepeat = "repeat-x";
        footer.style.backgroundPosition = "center bottom";
        footer.style.paddingBottom = "120px";
    }
    isLocalized = !isLocalized;
}

function feedbackHdr (str) {
    var subject = document.getElementById("subject");
    subject.value = str;
}