// ==UserScript==
// @name           ThreadTools
// @namespace      http://froschlaich.com
// @description    Ersetzt die "New Reply" und "New Thread" Buttons, zeigt die Number der Posts auf der aktuellen Seite an und bietet die Moeglichkeit, Threads zu seinen Favoriten hinzuzufuegen.
// @version        1.1

// @include       http://82.149.226.131/bb/*
// @include       http://forum.counter-strike.de/bb/*
// @include       http://forum.cstrike.de/bb/*
// @include       http://forum.mods.de/bb/*
// ==/UserScript==


// menu command to set the number of max saved threads
GM_registerMenuCommand("ThreadTools: Maximale Anzahl von Favoriten", function(){ 
    if(!GM_getValue('fav-maxthreads')){ var value='200'; } else { var value=GM_getValue('fav-maxthreads'); };
    GM_setValue('fav-maxthreads', (prompt('(ThreadTools) Maximale Anzahl von Favoriten (Default: 200)',value))); 
});

// some object
var butts = new Object;
butts['top'] = new Object;
butts['bottom'] = new Object;
var buttParents = new Object;




SITE = {
//
// THREAD
//
    thread : function() {
        // get thread title and subtitle
        document.getElementsByTagName('title')[0].innerHTML.match(/(.*) \- mods\.de \- Forum/ig);
    
        this.threadTitle = RegExp.$1;
        this.threadSubtitle = RegExp.$2;

        // set the reply count to zero
        this.replyCount = 0;

        // get all links
        var allLinks = document.getElementsByTagName('a');

        // walk through the links
        for(var i=0; i < allLinks.length; i++) {
            if(allLinks[i].href.match(/newthread\.php\?BID=([0-9]*)/ig)) {
                // seems to be newthread button. get it's parent.
                if(!buttParents.top) { 
                   buttParents.top = allLinks[i].parentNode;
                } else {
                    buttParents.bottom = allLinks[i].parentNode;
                }
                
                // now hide the button
                allLinks[i].style.display = 'none';
                
                // and save the board id
                if(!this.boardID) this.boardID = RegExp.$1;
            }
            else if(allLinks[i].href.match(/newreply\.php\?TID=([0-9]*)/ig)) {
                // must be one newreply button. hide it.
                allLinks[i].style.display = 'none';
                
                // save the thread id
                if(!this.threadID) this.threadID = RegExp.$1;
            }
            else if(allLinks[i].name.match(/reply_/ig)) {
                // at least it's a reply anchor, increment the reply coutner
                this.replyCount++;
            }
        }
    
        // set vertical align of top button parent to 'bottom'
        buttParents.top.style.verticalAlign = 'bottom';
    
        // create top buttons
        butts.top.reply = BUTTONS.reply('top');
        butts.top.delfav = BUTTONS.delFav('top');
        butts.top.addfav = BUTTONS.addFav('top');
        butts.top.repcount = BUTTONS.replyCount('top');
        
        // create bottom buttons
        butts.bottom.repcount = BUTTONS.replyCount('bottom');
        butts.bottom.delfav   = BUTTONS.delFav('bottom');
        butts.bottom.addfav   = BUTTONS.addFav('bottom');
        butts.bottom.reply    = BUTTONS.reply('bottom');

        // append top buttons
        buttParents.top.appendChild(butts.top.reply);
        buttParents.top.appendChild(butts.top.delfav);
        buttParents.top.appendChild(butts.top.addfav);
        buttParents.top.appendChild(butts.top.repcount);  

        // insert bottom buttons
        buttParents.bottom.insertBefore(butts.bottom.repcount, buttParents.bottom.firstChild); 
        buttParents.bottom.insertBefore(butts.bottom.delfav, buttParents.bottom.firstChild); 
        buttParents.bottom.insertBefore(butts.bottom.addfav, buttParents.bottom.firstChild);  
        buttParents.bottom.insertBefore(butts.bottom.reply, buttParents.bottom.firstChild);
    
        // hide or display?
        if(FAVORITES.in_list(this.threadID)) {
            butts.top.addfav.style.display = 'none';
            butts.bottom.addfav.style.display = 'none';
            butts.top.delfav.style.display = 'block';
            butts.bottom.delfav.style.display = 'block';
        } else {
            butts.top.delfav.style.display = 'none';
            butts.bottom.delfav.style.display = 'none';
            butts.top.addfav.style.display = 'block';
            butts.bottom.addfav.style.display = 'block';
        }
    },
    
//
// BOARD
//
    board : function() {
        // first get all links.
        var allLinks = document.getElementsByTagName('a');
    
        // now get the thread ids
        var threadIDs = GM_getValue('fav-IDs').split('|');
    
        // walk through the links
        for(var i=0; i < allLinks.length; i++) {
            // we are looking for thread links. they have the class "onblue" and are inside a TD element with the width of 36%
            if(allLinks[i].parentNode.width == '36%') {
                // alright, we found a thread link. now we need it's thread id.
                allLinks[i].href.match(/thread\.php\?TID=([0-9]*)/ig);
                var threadID = RegExp.$1;
            
                // check if the thread id is inside the thread list
                if(in_array(threadIDs, threadID)) {
                    // get the thread image
                    var threadIconParent = allLinks[i].parentNode.parentNode.childNodes[3];

                    // create the icon
                    var favIcon = document.createElement('img');
                    favIcon.alt = "<3";
                    favIcon.title = unescape('Dieser Thread geh%F6rt zu deinen Favoriten');
                    favIcon.src = ICONS.fav;

                    // set bgcolor
                    threadIconParent.style.background = '#222E3A';
                
                    // save this cell in an object (for unmarking)
                    this[threadID] = new Object;
                    this[threadID].iconCell = threadIconParent;
                    this[threadID].favIcon = favIcon;
                
                    // does the thread already have an icon?
                    if(threadIconParent.firstChild) {
                        threadIconParent.firstChild.style.display = 'none';
                        this[threadID].iconImg = threadIconParent.firstChild;
                    }
                    
                    // append the fav icon
                    threadIconParent.appendChild(favIcon);
                }
            }
            else if(allLinks[i].href.match(/newthread\.php\?BID=([0-9]*)/ig)) {
                // seems to be newthread button. get it's parent.
                if(!buttParents.top) {
                    buttParents.top = allLinks[i].parentNode;
                } else {
                    buttParents.bottom = allLinks[i].parentNode;
                }

                // now hide the button
                allLinks[i].style.display = 'none';
                
                // and save the board id
                if(!this.boardID) this.boardID = RegExp.$1;
            }
        }
            
        // set vertical align of top button parent to 'bottom'
        buttParents.top.style.verticalAlign = 'bottom';
        buttParents.top.style.width = '50%';
        
        buttParents.bottom.style.verticalAlign = 'top';
        
        // create top buttons
        butts.top.newthread = BUTTONS.newThread('top');
        butts.top.togglefavs = BUTTONS.toggleFavs('top');
        
        // create bottom buttons
        butts.bottom.newthread = BUTTONS.newThread('bottom');

        // append top buttons
        buttParents.top.appendChild(butts.top.newthread);
        buttParents.top.appendChild(butts.top.togglefavs); 
        
        // insert bottom buttons
        buttParents.bottom.insertBefore(butts.bottom.newthread, buttParents.bottom.firstChild); 
    }
};


/* FAVORITES
***************************************************************/

FAVORITES = {
//
// ADD FAVORITE
//
    add : function(threadID, threadTitle, threadSubtitle, boardID) {
        // get fav list and ids and split them into arrays
        if(!GM_getValue('fav-list') || !GM_getValue('fav-IDs')) {
            var favList = new Array;
            var favIDs = new Array;
        } else {
            var favList = GM_getValue('fav-list').split('|');
            var favIDs  = GM_getValue('fav-IDs').split('|');
        }
    
        // check if we have a max id value. if not, set one!
        if(!GM_getValue('fav-maxthreads')) { GM_setValue('fav-maxthreads', '200'); }
    
        // add thread to arrays
        favList.push(threadID+'('+escape(threadTitle)+'['+escape(threadSubtitle)+']{'+boardID+'})');
        favIDs.push(threadID);
    
    
        // do we have too many ids in the list?
        if(favIDs.length > GM_getValue('fav-maxthreads')) {
            // slice the array
            favIDs = favIDs.slice(favIDs.length-GM_getValue('fav-maxthreads'));
            favList = favList.slice(favList.length-GM_getValue('fav-maxthreads'));
        }
    
        // join the arrays and save
        GM_setValue('fav-list', favList.join('|'));
        GM_setValue('fav-IDs', favIDs.join('|'));
    
        // log it
        GM_log('Thread ID '+threadID+' added to favorites');
    },

//
// DELETE FAVORITES
//
    del : function(threadID) {
        // get fav list and ids and split them into arrays
        var favList = GM_getValue('fav-list').split('|');
        var favIDs  = GM_getValue('fav-IDs').split('|');
    
        // set new arrays
        var newFavList = new Array;
        var newFavIDs = new Array;
    
        // walk through thread ids
        for(var i=0; i < favIDs.length; i++) {
            if(favIDs[i] != threadID) {
                // this is not the thread id we want to delete, pass it to the new array
                newFavList.push(favList[i]);
                newFavIDs.push(favIDs[i]);
            }
        }
    
        // join the arrays and save
        GM_setValue('fav-list', newFavList.join('|'));
        GM_setValue('fav-IDs', newFavIDs.join('|'));
    
        // log it
        GM_log('Thread ID '+threadID+' deleted from favorites');
    },

//
// TOGGLE FAV LIST ON BOARD OVERVIEW
//
    toggle : function(boardID) {
        if(!this.favTable) {
            // this is the first call, we have to create the fav table first. get the fav list.
            var favList = GM_getValue('fav-list').split('|');
            favList.reverse();
            
            // create the table
            var favTable = document.createElement('table');
            this.favTable = favTable;
            
            // set some attributes
            this.favTable.innerHTML = '';
            this.favTable.style.background = '#091827';
            this.favTable.style.width      = '450px';
            this.favTable.style.margin     = '0 0 0 auto';
            
            // now look for favorites with appropriate board id
            for(var i=0; i < favList.length; i++) {
                // the regular expression
                favList[i].match(/([0-9]*)\((.*)\[(.*)\]\{([0-9]*)}\)/ig);
                
                // collect the variables
                var favThreadID = RegExp.$1;
                var favThreadTitle = unescape(RegExp.$2);
                if(RegExp.$3) { var favThreadSubtitle = ' <span style="font-size: 7pt">('+unescape(RegExp.$3)+')</span>'; } else { var favThreadSubtitle = ''; }
                var favBoardID = RegExp.$4;
                
                // look for a match
                if(boardID == favBoardID) {
                    // add the fav to the table
                    var favRow = document.createElement('tr');
                    
                    // create first cell
                    var favCell1 = document.createElement('td');
                    favCell1.style.padding = '4px';
                    favCell1.style.width   = '95%';
                    favCell1.innerHTML     = '<a href="thread.php?TID='+favThreadID+'">'+favThreadTitle+'</a>'+favThreadSubtitle;
                    
                    // create second cell
                    var favCell2 = document.createElement('td');
                    favCell2.style.padding = '4px';
                    favCell2.style.width   = '5%';
                    
                    // create delete button
                    var favDelButton = document.createElement('a');
                    favDelButton.href  = 'javascript: void(0);';
                    favDelButton.title = unescape('Thread aus meinen Favoriten l%F6schen');
                    favDelButton.rel   = favThreadID;
                    
                    // add event listener
                    favDelButton.addEventListener('click', function(event) {
                        // delete the row
                        var theTable = this.parentNode.parentNode.parentNode;
                        var theRow   = this.parentNode.parentNode;
                        theTable.removeChild(theRow);
            
                        // unmark the thread if it's on the current page
                        FAVORITES.unmark(this.rel);
                        
                        // check if there are favorites left for this board
                        if(theTable.innerHTML == '') {
                            // now favs left, delete table and button
                            theTable.parentNode.removeChild(theTable);
                            //butts.top.togglefavs.style.display = 'none';
                        }
        
                        // delete from favs
                        FAVORITES.del(this.rel);
            
                        event.stopPropagation();
                        event.preventDefault();
                    }, true);
    
                    
                    // create delete image
                    var favDelImg = document.createElement('img');
                    favDelImg.src =  ICONS.fav_del;
                    favDelImg.style.border = 'none';
                    
                    // append img to del button
                    favDelButton.appendChild(favDelImg);
                    
                    // append del button to cell 2
                    favCell2.appendChild(favDelButton);
                    
                    // append cell 1 and 2 to tr
                    favRow.appendChild(favCell1);
                    favRow.appendChild(favCell2);
                    
                    // append row to table
                    favTable.appendChild(favRow);
                }
            }
            
            // now we need a place to display our favs. collect all tables in an array.
            var allTables = document.getElementsByTagName('table');
            
            // search the table the threads are listed in
            for(var k=0; k < allTables.length; k++) {
                if(allTables[k].width == '100%' && allTables[k].innerHTML.match(/Moderiert von/ig)) {
                    // ok, we got the table. insert the favorites before.
                    allTables[k].parentNode.insertBefore(this.favTable, allTables[k]);
                
                    // we found the table, no need to look for more
                    k = allTables.length;
                }
            }
        } 
        else {
            // the table does already exist, just toggle it's visibility
            if(this.favTable.style.display == 'none') {
                this.favTable.style.display = 'block';
            } else {
                this.favTable.style.display = 'none';
            }
        }
    },

//
// UNMARK A THREAD
//
    unmark : function(threadID) {
        // check if the thread is on the current page first
        if(SITE[threadID]) {
            // alright, revert the icon cell's bg color to normal
            SITE[threadID].iconCell.style.background = '#394E63';
            
            // hide the fav icon
            SITE[threadID].favIcon.style.display = 'none';
            
            // revert the old icon if there was one
            if(SITE[threadID].iconImg) SITE[threadID].iconImg.style.display = 'inline';
        }
    },

//
// CHECK IF IN FAVORITES LIST
//
    in_list : function(threadID) {
        if(!GM_getValue('fav-IDs')) return false;

        // split the list string into an array
        var favIDs = GM_getValue('fav-IDs').split('|');
    
        for(var i=0; i < favIDs.length; i++) {
            if(favIDs[i] == threadID) { return true; }
        }
    
        return false;
    }
};


/* BUTTONS
***************************************************************/
BUTTONS = {
    create : function(href, title, img, text, position, width) {
        if(!position) { var position = 'top'; }
        
        //ok, thats everything we need. let's create the button.
        var button = document.createElement('a');
    
        // set some attributes
        button.href = href;
        button.title = unescape(title);
        button.style.cssFloat = 'right';
        button.style.display = 'block';
        button.style.padding = '5px';
        button.style.background = '#091827';
        button.style.textDecoration = 'none';
        button.style.textAlign = 'center';
        button.style.verticalAlign = 'middle';

        if(!width) {
            button.style.width = '105px';
        } else {
            button.style.width = width+'px';
        }

        if(position == 'top') {
            button.style.margin  = '10px 0 -2px 10px';
            button.style.MozBorderRadiusTopleft = '8px';
            button.style.MozBorderRadiusTopright = '8px';
        } else {
            button.style.margin  = '-2px 0 10px 10px';
            button.style.MozBorderRadiusBottomleft = '8px';
            button.style.MozBorderRadiusBottomright = '8px';
        }

        // create the image and append it to the link
        var buttonImg = document.createElement('img');
        buttonImg.src = img;
        buttonImg.style.border = 'none';
        buttonImg.style.verticalAlign = 'middle';
        button.appendChild(buttonImg);

        // create the text node and append it to the link
        var buttonTxt = document.createTextNode(' '+unescape(text));
        button.appendChild(buttonTxt);
    
        return button;
    },
    
    replyCount : function(pos) {
        if(pos == 'bottom') {
            var href = '#top';
            var title = 'zum Anfang dieser Seite springen';
        } else {
            var href = '#last_reply';
            var title = 'zum letzten Post dieser Seite springen';
        }
        
        return this.create(href, title, ICONS.posts, SITE.replyCount+'/30 Posts', pos);
    },
    
    addFav : function(pos) {
        // create button
        var button = this.create('javascript: void(0);', 'Thread zu meinen Favoriten hinzuf%FCgen', ICONS.fav_add, 'Hinzuf%FCgen', pos);
    
        // add onclick event
        button.addEventListener('click', function(event) {
            // hide add button and show del button
            butts.top.addfav.style.display = 'none';
            butts.bottom.addfav.style.display = 'none';
            butts.top.delfav.style.display = 'block';
            butts.bottom.delfav.style.display = 'block';
            
            // add to favs
            FAVORITES.add(SITE.threadID, SITE.threadTitle, SITE.threadSubtitle, SITE.boardID);
            
            event.stopPropagation();
            event.preventDefault();
        }, true);
    
        button.style.display = 'none';
    
        // return button
        return button;
    },
    
    delFav : function(pos) {
        // create button
        var button = this.create('javascript: void(0);', 'Thread aus meinen Favoriten l%F6schen', ICONS.fav_del, 'Entfernen', pos);
    
        // add onclick event
        button.addEventListener('click', function(event) {
            // hide del button and show add button
            butts.top.delfav.style.display = 'none';
            butts.bottom.delfav.style.display = 'none';
            butts.top.addfav.style.display = 'block';
            butts.bottom.addfav.style.display = 'block';
            
            // delete from favs
            FAVORITES.del(SITE.threadID);
            
            event.stopPropagation();
            event.preventDefault();
        }, true);
    
        button.style.display = 'none';
    
        // return button
        return button;
    },
    
    toggleFavs : function(pos) {
        var button = this.create('javascript: void(0);', 'Favoriten fır dieses Board anzeigen', ICONS.fav, 'Favoriten', pos);
        
        // add onclick event
        button.addEventListener('click', function(event) {
            FAVORITES.toggle(SITE.boardID);
            event.stopPropagation();
            event.preventDefault();
        }, true);
        
        // return button
        return button;
    },
    
    reply : function(pos) {
        return this.create('newreply.php?TID='+SITE.threadID, 'Neue Antwort erstellen', ICONS.reply, 'Antworten', pos);
    },
    
    newThread : function(pos) {
        return this.create('newthread.php?BID='+SITE.boardID, 'Neuen Thread erstellen', ICONS.new_thread, 'Neuer Thread', pos, '115');
    }
};



/* ICONS
***************************************************************/
ICONS = {
    posts : "data:image/gif,GIF89a%10%00%10%00%E6D%00%FD%FE%FE%E9%F2%FEW%86%B6%9D%B8%D5%12%258%0D%1D.%0F%201%89%AA%CD%A9%C1%DA%3C"+
        "%60%85%DB%EA%FDl%95%C0%E2%EE%FEMw%A4%40e%8C%209R%E0%ED%FE%14(%3B%D7%E8%FD%B4%C9%DF%D4%E6%FD%D6%E7%FD%E4%EF%FE%DF%EC%FE%DA%E9%FD"+
        "%14'%3A%E7%EE%F5%FB%FC%FD%D5%E6%FD%99%B6%D4%D0%E3%FD%E3%EF%FEU%82%B2%0C%1C%2C%DD%EB%FE%EF%F3%F8V%84%B5%E6%F1%FE%D0%E4%FD%D1%E4%"+
        "FC%D1%E4%FD%E6%F0%FEJt%9F5Uw%DD%EC%FE%2CIg%DC%EA%FDCi%91Mx%A4%D9%E9%FD%E8%F1%FE%1A0F%E5%F0%FE%7B%A0%C70NnNy%A5%D3%E6%FD%F0%F6%F"+
        "E%1B1G%0B%1B*(D%60%E1%EE%FE%DC%EB%FDV%84%B3%DE%EC%FE%7D%A2%C8X%87%B8%FF%FF%FF%09%18'%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"+
        "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%00%00D%00"+
        "%2C%00%00%00%00%10%00%10%00%00%07%80%80D%82%83%84%85%86%87%86%06%09%24B%8D%8EB%2F%11%84%0E%08%1BC%97%98C%13%0D%3B%82%05%02%009%"+
        "01%25%16%3D%40%0A.C53%82%04%0BC2)%1F%10%2C%0A%12%1CC%07%0F%AD%AF4%0C%10%22%18%12%14(%B9%BBD%9F%00%01%17%3E1%158%26'CA%3A%83%94%"+
        "00%98%0C%1E%01%9A%0D!%83%8A%02%8D%03%97%23%03*%92%880%1A%03%3F6%88%82%2B%1D%20-%F0%82%197%3C%F6%FB%FC%FD%F6%81%00%3B",

    fav : "data:image/gif,GIF89a%10%00%10%00%F7%A4%00%FF%85%85%FF%96%96%FF%91%8F%FF%92%92%FF%9D%9D%FF%9F%9F%FF%9A"+
        "%9A%FF%B5%B4%FF%94%94%FF%96%94%FF%D6%D4%FF%7D%7C%FF%AB%A5%FFnm%FF%C4%C0%C6%7B%7D%FF%86%83%1E%1B(%FE%DB%D8%FF%93%93%FE"+
        "%BE%B9%FF%BF%BA%FF%82~%FB%C6%C3%FF%AF%AE%C9%40A%FF%84%84nYb%FC%CA%C7%FC%C3%C0%2C%2B8%FD%CB%C6%FF%AB%AB%FE%CD%CA%23%1C"+
        ")%FF%B9%B7%E0%9D%9F%FF%9D%95%FF%98%98%FD%C5%C1%AD36%FF%83%82%EF%95%93%FF%8A%8A%CAFGSHR%F7%B2%B0%B3jm%CC%3D%3D%80-3%FF"+
        "%AE%A6%FA%B1%AF%D1%7F%81%FF%B2%B2%FF%7Dy%F2%91%8D%E8vrq%5Cd%F4%A2%A0%C5QS%16%1C*%FD%C8%C5%CD%8A%8CjKS()6%B9%88%8CJ%25"+
        "%2F%F2%9C%99.0%3D%FF%C6%C3%C189%E9%85%83%FF%C9%C5%FF%9A%97%FD%D4%D1%FF%88%87%F1%98%95%FC%BB%B9%FA%BF%BD%D6%86%88%95gm"+
        "%EE%8F%8Dx*1%18!%2F%FFom%FF%AD%AB%FF%AE%AE%FF%B1%B0%16%1E%2C%FF%DC%DA%FF%8B%8BK6%40%FE%D8%D5%FE%D9%D6%F9%AD%AA%CDMN%F"+
        "F%91%90%FF%A2%A2%FF%9C%9C%FF%D4%D2%FFzy%FF%80%7F%D3GG%FF%7Bz%3E%20%2B%FF%82%81%FF%A8%A7%FC%B2%B0%D6fg%FF%8A%88%FF%8C%"+
        "8C%5DLU%FD%C3%BF%ADLP%FF%80%80%E5d%60%EB%80%7C%FC%BD%BC%EC%A9%AAw%3DE%FF%A5%A2%ED%89%87%FF%B1%AB%F8%B9%B6%7F7%3D%60'0"+
        "%FF%90%90%D5%8F%917%1F*%12%1B*%D4~%80%18%1A(%FF%B8%B54'3%FFut%FFxw%FF%9C%9A%FF%CB%C9%FF%A6%A6%9005%F7%AD%AA%EB%7Cy%FF"+
        "%A1%A061%3D%FF%B7%B2%FF%BC%B6%FD%C4%C0%FF%DB%D9%FF%C5%C1%FFvu%FF%89%89%C8%7B~yFM%FF%CF%CC%FF%B0%B0%E3%9C%9D2%2C8%FF%C"+
        "F%CB%FF%A9%A9%B58%3A%7Fbj%94PU%BBkn%18%20.%09%18'%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%00%00%A4%00%2C%00%00%00%00%10%00%10%00%00%08%DE%00I%09%1C"+
        "H%B0%E0%40%22%1B%40%BD%99%22p%D4%0FA%2F%B6%0C%CCa%A7I%9D%19%3E%1Eyx%A0%E3%C2%9E%3C%A1%02%B5X%D3e%92%82%2CN%9E%5Cr1%C6"+
        "A%11%05Q%EE%04%09%91%E9%40%0D%2B%9D%3EtHT%80%80%09%3C%1Cv%90%900B%13%06%10%8A%0E%10%12%83(%40%020%3D%BEl%E2r%C5%93%9A"+
        "0%05%0C%04%400%40%C0%8A%25pX%FCQR%C5%11%01%AD%13%FC%B8%B1%04%20%0D%84%13%19%A0xA%92%80%AB%00-m4%C8Y%60%88%C1%91%18%40"+
        "h0%A2%94%04%00%80%14e%16T%B2%A0G%85%91%08%A48%89%BA!I%06%993%87%1A%94%A0%80%03%05%A0%81X0%B1%19%02%C9%06%15%06L%60H%1"+
        "9d%B0P%9CF%15%22%D1%F9%84%C6%E0%40%1E%7C%CC%CCY%24%C2vA!%7D%7C%0B7%18%10%00%3B",

    fav_add : "data:image/gif,GIF89a%10%00%10%00%F7%B0%00%FF%85%85%FF%96%96%22D%20%8F%BCg%FF%9A%9A%FF%91%8F%FF%92%92%FF%9D%9D%FF%B5"+
        "%B46h%197k%19%FF%D6%D4S%8C(%B2%D2%95%8D%BAd%22F%1F%FF%94%94%FF%96%94%FF%9F%9F5g%1A%FF%7D%7C%FF%B2%B2!C%20%FF%B1%B0%FF%A2%A2%FF%"+
        "A1%A0%FF%80%7F%FF%B9%B7jKS%F9%AD%AA%FD%D4%D1%E0%9D%9F%FE%CD%CA%95gm%FF%AB%A5%FF%83%82%FF%98%98%FF%80%80%E5d%60%FF%86%83SHR%F2%9"+
        "1%8D%8C%BCe%C6%7B%7D%BBkn%B3jmnYb%F2%9C%99%FC%B2%B0%FF%A9%A9%FE%DB%D8W%8F-%FF%9C%9CyFM%FF%AE%A6%FF%C9%C5%FF%A6%A6%FB%C6%C3%D3GG"+
        "%FF%9C%9A%FF%C6%C3%1E%1B(%8F%BBj%FF%CF%CC%B6%D4%9C%C8%7B~%81%ABa%FE%D9%D64'3%FFxwa%85D%FF%A5%A2b%88G%18!%2F%FA%BF%BD.0%3D%7F7%3"+
        "D%FF%8C%8C%CDMN%FC%BB%B9K6%40%FF%89%89w%3DE%FF%BC%B6f%8EO%3Bj%1C%E9%85%83%D5%8F%91L%7B1%16%1E%2C%FFzy%FF%AE%AE%80-3%FF%7Bz%FD%C"+
        "5%C1%12%1B*%2C%2B8q%5Cd%87%B9%60%FC%CA%C7%FE%D8%D5%D1%7F%81%FF%C4%C0%FF%AB%AB%FF%84%84%FF%A8%A7%FC%C3%C0%D6fg%FF%91%90%CAFG%FD%"+
        "C4%C0%FF%BF%BA%FF%CF%CB%FD%CB%C6%F7%AD%AA%B1%D6%92%FFut%FF%DB%D9%FFom%FF%DC%DA%FF%CB%C9J%25%2F%FD%C3%BF%D4~%80%FF%AD%AB%ADLP%8B"+
        "%BAb%B4%D4%95%B3%7F_%B1%D3%90%EE%8F%8D%EC%A9%AA%F7%B2%B0%ED%89%87%D6%86%88%FF%8A%8A%FF%B7%B2%FF%B8%B5%EB%7Cy%18%20.%FF%AF%AE%FF"+
        "%7Dy%FF%9A%97%7Fbj%FF%90%90%FD%C8%C5%D0%E6%BA%C5QS%F4%A2%A0%FF%93%93%C9%40A%FF%D4%D2%86W4%F8%B9%B6%FF%8B%8B%16%1C*%FF%88%87()6%"+
        "FF%82%81%E3%9C%9D%CD%8A%8C%B9%88%8C%B9%85g61%3D%94PU%FF%8A%88u%93Z2%2C8%FF%B0%B0%60'0%FA%B1%AF%FF%C5%C1%5DLU%FC%BD%BC%87%BA%60%"+
        "FF%FF%FF%09%18'%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"+
        "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%00%00%B0%00%2C%00%00%00%0"+
        "0%10%00%10%00%00%08%F6%00a%09%1CH%B0%E0%C0%25.%1E%B1J%22p%11%87%3D-%A0%0C%0C3%E8I%2BU%A0F%81YA)G%A6B%A4%BE%A0%801%A4%CE%82%3BJ%"+
        "0C%05!t%C9%0C%8F%05%82%A4%84%02%F1%03A%85-p%E2%A8%C1%23%E1%00%89%23c%26%7D%90%B1%01%15%A338%10%24%A2%B1%23%40%046%91%9C%7C%22s!"+
        "F%1A%0C%12%08%04%80%60%A0%C0!Nz%DA%5C%F1%C0'%C3%01%AD%95%205%89%02%C0%D3%09%2F%96Bt%B8%11%81k%01M%A5%D0%94%A0%40G%84%15.%9D%CA%"+
        "C8Y%E5%08%00%80%11%1A(%882E%C5%08%A6%1E%B0N%B1H%E1%C6%86%96.E%00%09%F9%23%C9%C7%8C%07%02%B3%D4X%F3%02Q%23%3BH%02%CDy%A5b%40%02%"+
        "82D%FA(z3%05%0B%90W%B8%1B((%B8%89%89%0E%13U%FC%B8z%25%C6%B5AXyRY%60%E0%A0%81%03%06%A0%8F%0B%140A%C1%04%01%B0%02%02%00%3B",
    
    fav_del : "data:image/gif,GIF89a%10%00%10%00%F7%B8%00%FF%85%85%FF%96%96%FF%92%92%BBB%04%5D.%16%FF%9D%9D%FF%B5%B4%FF%9A%9A%FF%7D%"+
        "7C%FF%91%8F%FF%96%94%FF%94%94%FF%D6%D4%A6%40%07%FF%9F%9F%FF%C4%C0%FF%B7%B2%F9%AD%AA%12%1B*%C8%7B~%FF%DB%D9%FD%CB%C6f8%20%FE%DB%D"+
        "8%60'0%BCB%05%1E%1B(%FF%84%84%FA%B1%AF%FF%A1%A0%FF%A2%A2%FE%CD%CA%FF%D4%D2%FF%A9%A9%F4%A2%A0%FF%AB%AB%FF%BC%B6%F7%B2%B0%FF%CF%CB"+
        "%FF%83%82%FF%DC%DA%FF%9C%9A%A2%3E%08%FF%8A%88SHR%FF%80%80%5DLU%C9%40A%FF%89%89%E3cH%FF%80%7F%FF%B2%B2%FF%93%93%FFw%11%FC%B2%B0%F"+
        "F%9A%97%FF%7Dy%FF%7Bz%BBL%04yFM%FFxw%80-3%16%1E%2C%FF%C9%C5%C2R%19%FF%92M%F2%9C%99%FE%D8%D5%D2t%3A%FF%CF%CC.0%3D%ADLP%FF%8C%8C%C"+
        "5QS%FC%BD%BC%94PU%FC%BB%B9%FD%D4%D1%18!%2F%B5B%08%FF%86%83%7F7%3D%EE%8F%8D%EC%A9%AA%E9%85%83%E1h%03%D6%86%88%D5%8F%91%F8v%00a6%2"+
        "2%DDd%01%F2%91%8D%F6%B5%8D%A3%3F%08%BCB%04%FF%AF%AE%FF%B1%B0%FF%AE%AE%FF%BF%BA%FF%86!%FF%8B%8B()6%FE%D6%B82%2C8%FE%D9%D6%D1%7F%8"+
        "1%E0%9D%9F%FF%B9%B7%FF%9C%9C%EB%8A%404'3%E3%9C%9DU*%18%FF%AE%A6%FF%A8%A7%B3jm%D6fg%E3hL%FF%7F%18%FD%C4%C0%FF%AD%AB%D3GG%FD%C8%C5"+
        "w%3DE%FE%CD%AE%EB%7Cy%FD%C5%C1%FF%88%87%FF%CB%C9K6%40%CDMN%FF%A5%A2%FC%CA%C7%F8%B9%B6%CD%8A%8C%E5d%60%ED%89%87%C6%7B%7D%7Fbj%FF%"+
        "91%90%2C%2B8%EAy%05%BD%3D%1F%F7%B5%8D%FF%90%90%C6c%25%F5%A8r%FF%B8%B5%FF%AB%A5%BFN%13%FF%82%81%C9j-%FFut%FFzy%FF%C5%C1%FF%C6%C3%"+
        "95gm%B9%88%8C%FF%A6%A6%EF%7F%18%FA%BF%BD%D4~%80J%25%2F%16%1C*%FF%98%98q%5Cd%18%20.%C7S%1B%FF%B0%B0%FC%C3%C0%FFomjKS%EEv%00%F7%AD"+
        "%AA%F4%9Dc%FD%C3%BF%BBkn%E6w!61%3D%FF%8A%8A%CAFG%FB%C6%C3nYb%FF%FF%FF%09%18'%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"+
        "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
        "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%"+
        "00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"+
        "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%00%00%B8%00%2C%00%00%00%00%10%00%10%00%00%"+
        "08%F8%00q%E12bK%91%0B'%02M%AD%0A5'%90%40%5C%A5%A60Q%C2%C1%90%2CF%89D%D4*%84h%89%04%166%D0P%60%80%02%94%95%09%25%40%3C%D8%C4%40%C"+
        "A%9EN%1F%8A%18%98%11%C6D%85T%80%1C%14%205%88P%125%17%D6%A0%FA2%C2%93%81IlR%04P%B0H%8F%A07C%C0%84%90%E3%C1%C1%81%00%0B%04%24%98%F"+
        "5%E7%15%AD%2BM%F0t(p%95%06%24%240%00X%82%E2%E7%05%A7%08%3F%14dM%40f%C5%86%16%080Q%A2%D2%A3L%9AV%9An%00%00pB%06%82%3A%A72Tr%A4%01"+
        "%D7%19X%5B%EE%C4%C9%94%83G%0C%22%8F%CCH%BA%04G%A0%8F%1Dt%84%40%C0%A1%0A%08%97%205%C6%B4Q%F1%10%97%9B%23%7D%C4%90%F0%C2%E7%96%EDO"+
        "%03Z%E3%1A%15%25%CF%A1'%AE%EC%60aU%A5%81n%81%A20X%88%14%AB%91%16%1D%04%8E%B7%CE%D2e%40%83%E8%D2%B3%E3%0A%08%00%3B",
    
    reply : "data:image/gif,GIF89a%10%00%10%00%E6v%00X%87%B8%FD%FE%FE%9D%B8%D5W%86%B6%E9%F2%FE%A9%C1%DA%0F%201%FD%F7%C7%89%AA%CDl%"+
        "95%C0%3C%60%85%209R%E2%EE%FE%40e%8CSF(Ny%A5V%84%B5%F9%C9%91%F7%E1Z%C9%BC%A7%84g.%F6%D3%AE%DD%9DQ%1A0F%D7%A3c%F0%D3y%D7%BAv%E5%F0"+
        "%FE%FF%F3%E5%EE%D3y%FF%F2%E0%E7%BEw%88a%2B..%2C%99%B6%D4%E7%EE%F5%C3%8CH%BD%89LV%84%B3Jt%9F%F6%DDlMx%A4%F9%E3%B2%D1%B7%90%D1%8FA"+
        "%E7%C0%8BVP%255Uw%D0%9DF30*%D5%B6z0Nn%2CIg%E9%D1%7CNI%2B%F2%D6z%D8%9AT%7D%A2%C8%F0%F6%FE%F0%D2%B4QL%25%E7%D7%B4%F7%EA%DC%D1%E4%F"+
        "C%F7%E2%5B%CC%D5%E1%D0%E3%FD%E8%F1%FEGE%26%F1%D4%7B%F9%C7%94%C2%A3o%ED%D0x%C3%91Y%26*-%CD%D0%D7%CC%8D%3E%D6%95G%F5%DF%5B%BEz%1A%"+
        "FB%FC%FD%E6%F0%FEU%82%B2%EF%F3%F8%DB%B1%85%ACj'%F7%DFcYB%18%0C%1C%2C%B4%C9%DF%EC%D3%7BEC%25%14(%3B%FC%E0%C4(D%60%ED%CAm%EF%D2%AC"+
        "%D8%A5X%B6%82E%C9%85A%87%9D%B4x%86%82%C5%98I%E4%C9U%E7%CBw%F8%E2%5DMw%A4%FD%F6%C4%1B1G%FD%F6%C2%AA%89C%DF%EC%FE%F5%DC%5E%F5%DC_%"+
        "14'%3A%25)%2C%CD%9Bd%FF%FF%FF%09%18'%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%01%"+
        "00%00v%00%2C%00%00%00%00%10%00%10%00%00%07%B0%80v%82%83%84v6aT0%0E%85%85.%1F%3B%1C%60b%8C%83%3C5*-%15%5Dt%94vDZk(_%18IJ%94%5B%1D"+
        "%07q%407L!v%06%0A%10%00%B4eh%07p%12E%2C1%82%0D%05Puu%3D%1AmVi%19Ms%83%03%01%3A%04%132%2BgNH%16d%17%83%09uCQn%1E8Of%24%3E%08%0B%D"+
        "Au%1B%0C%14F%11cUKu%E6%CC%01%04oW%20%25GA%3Fu9l%83~%05%10V%87%81%10%02u%B2%A8%C12(%D6%00Z%02%84M%11p%82K'%3B)F%0801%E3%A2%9D%17%"+
        "22%A4%D0%F0hG%CE%03%2F%1E%03%01%00%3B",

    new_thread : "data:image/gif,GIF89a%10%00%10%00%E6%00%00%F8%F8%F8%F7%F7%F7%08%15%23%F5%F5%F5%FD%F7%C7%08%15%22%D7%A3c%26*-%F3%F3%F3%F"+
    "1%D4%7B%F2%D6z%E7%C0%8BZC%18%F2%F2%F2%D7%BAv%CB%A8o%F4%F4%F4%F6%F6%F6%F6%D3%AE%2C*%26%F2%E9%DE%F7%E1Z%F2%E5%BD%F1%E9%E0%FD%F6%C4%FF%"+
    "F3%E5%E4%C9U%C3%91Y%F0%F0%F1%EE%D4%7D%C9%85A%AEk%26%E2%A2Q%FD%F6%C2%F6%DDl%D1%B7%90%ED%CAm%F9%C7%94%E7%CBw%EE%D6%81%F1%F1%F1%EF%D2%A"+
    "C%E7%BEw%EA%EB%EC%E5%D5%B2%EE%E2%D4%D7%C3%A5%F3%E4%AF%C7%8EH%EE%EE%EF%EA%EA%EA%DC%98I%ED%D0x%E5%D4%AF%C5%98I%E4%D5%AC%F0%D2%B4%85g.%"+
    "D8%9AT%F5%DF%5B%F8%E2%5D%DD%9DQ%FF%F2%E0%AA%89C%B6%82E%CD%9Bd%D0%9DF%DB%B1%85%BEz%1A%F5%E5%D4%F0%E3%BA%EE%D3y%F5%EB%DF%F7%E2%5B%F0%F"+
    "0%F0%89a%2B%DD%9AKX_e%D5%B6z%F7%DFc%F9%E3%B2%E8%D9%A8%FC%E0%C4%E9%E9%E9%F9%C9%91%BE%8AL%F0%D3y%EF%EF%EF%D8%A5X%F5%DC%5ESF(%F3%E6%D8%"+
    "F5%DC_%F9%F9%F9%FA%FA%FA%FB%FB%FB%09%18'%07%13%1F%FC%FC%FC%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
    "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%0"+
    "0%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00!%F9%04%00%00%00%00%00%2C%00%00%00%00%10%00%10%00%00%07%B4%80%60%02a%84%85%85"+
    "%05%60%89%89a%2B%5Db%8F%90%00M%88%8Aa%5D%10%03%99(WSb7XCBZ%60ab%01%5D%01%01%08J2%2F*8%19)%40%A4b%5D%B6%01%11%0DQ'P%0B%12RA%B4_%8F_%0"+
    "0F%1D%18%22%24%06%1B%07%B4%90b%16G%04%5CI%0A%20%13%8B%D05%26%04Y%15%09LEa%DA%8F%2C%0E!O%3CV3%14b%E4%CF.N%23%1A%3B4%3DH%00%EF%E5%3F%3"+
    "E%3AD6%60l%01%A0%0F%5E)19JP%F1%F0%A1%85%97%87%FB%9E1XR%E5%C1%85%2F%18%87%19%1C%06%AD%A3%17%83%1C%BCt%84%E4%25%06%BCA%86R%12%12%00%26%10%00%3B"
};


/* MISC
***************************************************************/

// function to check if an array has a certain value
function in_array(array, value) {
    for(var i=0; i < array.length; i++) {
        if(array[i] == value) { return true; }
    }
    
    return false;
};



/**************************************************************/


// this is debug information. you may uncomment these lines if you're interested in
// some status messages inside the javascript console
GM_log('List: '+GM_getValue('fav-list'), 0);
GM_log('IDs: '+GM_getValue('fav-IDs'), 0);
GM_log('Max: '+GM_getValue('fav-maxthreads'), 0);


// call the main functions
if(window.location.href.match(/thread\.php/i)) {
    SITE.thread();
}
else if(window.location.href.match(/board\.php/i)) {
    SITE.board();
}