var selectedThread = 0;

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

function markThread(which){
    var toBeAffected = new Array();
    var toNotBeAffected = new Array();
    if(selectedThread !== 0){
        toBeAffected = getElementsByClass("tm"+selectedThread);
        for(var i=0; i<(toBeAffected.length); i++){
            toBeAffected[i].className = ("tm"+selectedThread+" threadmarker");
        }
        for(var j=1; j>-1; j++){
            toBeAffected = getElementsByClass("t"+j);
            for(var i=0; i<(toBeAffected.length); i++){
                toBeAffected[i].className = ("comment postcard t"+j);
            }
            if(toBeAffected.length==0){j=-3;}
        }
    }
    
    if(which==selectedThread){
        selectedThread=0;
        return;
    }
    
    toBeAffected = getElementsByClass("tm"+which);
    for(var i=0; i<(toBeAffected.length); i++){
        toBeAffected[i].className = ("tm"+which+" threadmarkersel");
    }
    for(var j=1; j>-1; j++){
        toBeAffected = getElementsByClass("t"+j);
        for(var i=0; i<(toBeAffected.length); i++){
            if(j!==which){toBeAffected[i].className = ("comment postcard desel t"+j);}
        }
        if(toBeAffected.length==0){j=-3;}
    }
    selectedThread=which;
}