var fadeColours = ['66', '74', '82', '90', '9e', 'ac', 'b9', 'c7', 'd5', 'e3', 'f1', 'ff'];

function fadeKill(obj, colours, delay) {
    obj.style.color = '#' + colours[0] + colours[0] + colours[0];
    if (colours.length == 1) {
        obj.parentNode.removeChild(obj);
    } else {
        setTimeout(function() {
            fadeKill(obj, colours.slice(1), delay);
        }, delay);
    }
}

function karmaVote(comment_id, vote) {
    var link = 'http://' + document.location.host + '/comments/karma/vote/' + comment_id + '/' + (vote ? 'up' : 'down') + '/';
    if (xmlhttp) {
        xmlhttp.open("GET", link, true);
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status != 404) {
                var karmasummary = document.getElementById('karmasummary' + comment_id);
                var newsummary = xmlhttp.responseText.match(/(\d+) of (\d+)/)[0];
                karmasummary.innerHTML = karmasummary.innerHTML.replace(/(\d+) of (\d+)/, newsummary);
                if (!document.getElementById('voteadded' + comment_id)) {
                    var added = document.createElement('span');
                    added.id = 'voteadded' + comment_id;
                    added.appendChild(document.createTextNode(' Vote added.'));
                    document.getElementById('karmavote' + comment_id).appendChild(added);
                    setTimeout(function() {
                        fadeKill(document.getElementById('voteadded' + comment_id), fadeColours, 20);
                    }, 1000);
                }
            }
        }
        xmlhttp.send(null);
    } else {
        window.open(link, 'karma', 'width=200,height=100');
    }
    return false;
}


