/*
    Copyright 2009 Graham King (graham@gkgk.org)
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/
FED = {
    PIC_RE: /[\[|\(](pic|gif)s?[\]|\)]/i,
    NAVEL_RE: /reddit/i,
    POLITICS_RE: /(obama|republican|democrat|congress|senat|mccain|governor)/i,
    RUDE_RE: /(fuck|shit)/i,
    WTF_RE: /(omg|wtf)/i,
    
    init:
        function() {
            
            FED.getNextPage();
            
            var css = '<style type="text/css">';
            css += '#controls {';
            css += 'opacity: 0.7;';
            css += 'filter: alpha(opacity=70);';
            css += 'position: fixed;';
            css += 'top: 175px;';
            css += 'right: 20px;';
            css += 'background: #000;';
            css += 'float: right;';
            css += 'padding: 7px 10px;';
            css += 'color: #fff;';
            css += 'border: solid 2px #fff;';
            css += 'textDecoration: none;';
            css += 'textAlign: left;';
            css += 'font: 12px Lucida Grande,Helvetica,Tahoma;';
            css += 'MozBorderRadius: 5px;';
            css += 'WebkitBorderRadius: 5px;';
            css += 'WebkitBoxShadow: 0px 0px 20px #000;';
            css += 'MozBoxShadow: 0px 0px 20px #000;';
            css += '}';
            css += '#controls label { float: left; width: 5em; } ';
            css += '#about { color: grey; }';
            css += '</style>';
            
            var formHTML = '<form id="controls" action="/" method="post">';
            formHTML += '<ul>';
            
            formHTML += '<li><label for="pics">Pics</label><input id="pics" type="checkbox" checked="true" /></li>';
            formHTML += '<li><label for="politics">Politics</label><input id="politics" type="checkbox" checked="true" /></li>';
            formHTML += '<li><label for="rude">Curse</label><input id="rude" type="checkbox" checked="true" /></li>';
            formHTML += '<li><label for="navel">Reddit</label><input id="navel" type="checkbox" checked="true" /></li>';
            formHTML += '<li><label for="wtf">OMG WTF</label><input id="wtf" type="checkbox" checked="true" /></li>';
            
            formHTML += '</ul>';
            formHTML += '<a id="about" href="http://www.darkcoding.net/software/eddit-reddit-without-the-rrrr/">about</a>';
            formHTML += '</form>';
            
            $(css).appendTo("head");
            $(formHTML).appendTo("body");
            
            $('#pics').click(function(event) {FED.toggleEntries(event, this, FED.PIC_RE)} );
            $('#politics').click( function(event) {FED.toggleEntries(event, this, FED.POLITICS_RE)} );
            $('#rude').click( function(event) {FED.toggleEntries(event, this, FED.RUDE_RE)} );
            $('#navel').click(function(event) {FED.toggleEntries(event, this, FED.NAVEL_RE)} );
            $('#wtf').click( function(event) {FED.toggleEntries(event, this, FED.WTF_RE)} );
            
            setTimeout(FED.clickAll, 1500);
        },

    clickAll:
        function() {
            $('#pics').click();
            $('#politics').click();
            $('#rude').click();
            $('#navel').click();
            $('#wtf').click();
        },

    toggleEntries:
        function(event, target, regexp) {
            
            var funcName = 'hide';
            if (event.pageX && $(target).attr('checked') ) {    // pageX is not set when we call the click function
                funcName = 'show';
            }

            $('a.title').each(
              function(i) {
                var content = $(this).text();
                if (content && content.search(regexp) != -1) {
                    $(this).parent().parent().parent()[funcName]("slow");
                }
              }
            );
        },

    getNextPage:
        function() {
            var entries = $('.link');
            var lastEntry = entries[ entries.length -1 ];
            var entryIdMatch = lastEntry.className.match(/id-([A-Za-z0-9_]+)/);
            var entryId = entryIdMatch[1];
            
            var containerHTML = '<div id="siteTable2" class="sitetable linklisting"></div>';
            $(containerHTML).insertAfter('#siteTable');
            
            var url = '?count=25&after='+ entryId;
            $('#siteTable2').load(url +' .link', null, FED.onPage2Loaded);
            
            $('.nextprev').hide();
            $('.organic-listing').hide();
        },

    onPage2Loaded:
        function() {
            $('div.link span.rank:empty').each(
                function() { $(this).parent().hide(); }  
            );
        }
        
}
FED.init();