bffstff = new Object();
bffstff.hideImprint = function() {
    $('#info').hide();
};
bffstff.initSearchLayout = function() {
    $("#phrase").focus();
    $('#propose_navi li').click(function() {
        $('#ear').html('<div class="loader">&nbsp;</div>');
        $('#propose_navi li').toggleClass('active');
        var c = $(this).attr('data-cat');
        $('#ear').load('/propose/' + c, function() {
            });
    });
};

bffstff.tabit = function(el) {
    $(el).click(function(event) {
        $(el+'.active').each(function() {
            $(this).removeClass("active").addClass("inactive");
        });
        $(this).removeClass("inactive").addClass("active");
    });
};

bffstff.initProposeFilter = function() {
    bffstff.tabit('#proposals li');
    $.address.change(function(event) {  
        f = event.value.substr(1);
        if(f != '' && f.indexOf('propose_') != -1){
            bffstff.proposeOverlay();
            bffstff.renderProposeFilter(f);
        }
    });
};

bffstff.initSearchFilter = function() {
    bffstff.tabit('#filter li');
    $.address.change(function(event) {  
        f = event.value.substr(1);
        if(f != '' && f.indexOf('propose_') == -1){
            bffstff.renderTabFilter(f,'.feednapi');
        }
    });
};

bffstff.renderProposeFilter = function(f) {
    $('.proposals li').each(function() {
        if (f.indexOf('_all') != -1) {
            $(this).removeClass("hide");
        } else {
            var cl = f.substr(f.indexOf('_')+1);
            if ($(this).hasClass(cl)) {
                $(this).removeClass("hide");
            } else {
                $(this).addClass("hide");
            }
        }
    });
};
bffstff.renderTabFilter = function(f,el) {
   
    $(el).each(function() {
        if (f == 'all') {
            $(this).removeClass("hide");
        } else {
            if ($(this).hasClass(f)) {
                $(this).removeClass("hide");
            } else {
                $(this).addClass("hide");
            }
        }
    });
};
bffstff.fetchImprint = function () {
    $.ajax({
        type: "GET",
        url: '/imprint',
        success: function(data){
            $('#info').removeClass().addClass('info');
            $('#info').animate({
                opacity: 'show', 
                height: 'show'
            }, 1000);
            $('#info').html(data);
            var targetOffset = $('#head').offset().top;
            $('html,body').animate({
                scrollTop: targetOffset
            }, 1000);
        }
    });
};

bffstff.autocompleteSearch = function() {
    $( "#phrase" ).autocomplete({
        source: "/suggest"
    });
};
bffstff.proposeOverlay = function() {
    $( "#proposals" ).overlay({

        // custom top position
        top: 160,

        // some mask tweaks suitable for facebox-looking dialogs
        mask: {

            // you might also consider a "transparent" color for the mask
            color: '#000000',

            // load mask a little faster
            loadSpeed: 200,

            // very transparent
            opacity: 0.5
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: true,

        // load it immediately after the construction
        load: false,
        
        onClose: function(event, tabIndex) {
            window.location.hash = '';
        }

    });
    $("#proposals").data("overlay").load();
};
bffstff.initProposeOverlay = function() {
    $(".propose .overlaytrigger").click(function(event) {
        bffstff.proposeOverlay();
    });           
};

feednapi = new Object();
feednapi.bindAfterAjax = function(id) {
    var ct = $(id + " .divBar").children().size();
    if (ct <= 0) {
        $(id).height(50);
    } else {
        $(id + " .divBar").hover(function() {
            $(this).find('div:first').removeClass().addClass('legendeshow');
        }, function() {
            $(this).find('div:first').removeClass().addClass('legendehidden');
        });
    }
};
feednapi.trackRef = function(id) {   
    $(id + " a").click(function(event) {
        var s = $(this).attr('data-feednapi-site');
        var q = $(this).attr('data-feednapi-query');
        var u = $(this).attr('href');
        $.post('/go/' + s + '/' + q + '?url=' + u, function(data) {
            });
    // var ga = $(this).parents('[data-ga]').andSelf()
    // .map(function(){return $(this).attr('data-ga');});
    // ga = $.makeArray(ga).join('');
    // _pageTracker._trackPageview(ga);
    });
}

feednapi.news = function(m, container) {
    var searcher = new google.search.NewsSearch();
    searcher.setNoHtmlGeneration();
    searcher.execute(m);
    searcher.setSearchCompleteCallback(this, feednapi.renderSearch, [ searcher,
        container ]);
}
feednapi.blogs = function(m, container) {
    var searcher = new google.search.BlogSearch();
    searcher.setNoHtmlGeneration();
    searcher.execute(m);
    searcher.setSearchCompleteCallback(this, feednapi.renderSearch, [ searcher,
        container ]);
}
feednapi.renderSearch = function(searcher, container) {
    $(container).html('');
    var results = $(container);
    if (searcher.results && searcher.results.length > 0) {
        for ( var i = 0; i < searcher.results.length; i++) {
            var result = searcher.results[i];
            searcher.createResultHtml(result);
            if (result.html) {
                div = result.html.cloneNode(true);
            } else {
                div = $("<div>").html("** failure to create html **");
            }
            results.append(div);
        }
    }
}
