// ==UserScript==
// @name         Tag list
// @namespace    http://stackapps.com/questions/4207/burninator-toolkit
// @author       Will Sullivan (Will)
// @developer    David Bingham (Mogsdad)
// @version      1.2.4
// @grant        none
// @description  Adds an expandable list containing a link to every question on the current tag page.  Useful when you want to open many questions with the same tag at the same time.
// @include      /^https?://(meta\.)?(stackoverflow|stackexchange|serverfault|superuser|askubuntu|stackapps)\.com/(questions/(new|popular|need-answers)|search).*/
// @require      http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function exec(fn) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.body.appendChild(script); // run the script
    document.body.removeChild(script); // clean up
}
window.addEventListener("load", function() {
    // script injection
    exec(function() {
        var App = this;

        userscriptCSS();
        
        // Some question lists have a question-mini-list div, searches don't. To simplify, we'll insert our own if needed.
        App.linkytarget = $('.question').length ? '.question' : $('.welovestackoverflow').length ? '.welovestackoverflow' : '.search-results';
        if (!($('#question-mini-list').length)) $('<div id=question-mini-list></div>').insertAfter(App.linkytarget);

        // re-sort list; move all the already handled cruff to the top
        $.each(["closed", "on hold", "migrated", "duplicate"], function(index) {
            App.items = $('.question-summary .summary:contains(' + this + ')');
            App.items.closest('.question-summary').detach().appendTo('#question-mini-list');
        });

        // get all the hrefs
        App.root = $('#question-mini-list');
        App.hrefs = App.root.find('.question-hyperlink,.result-link');  // .result-link on searches, .question-hyperlink everywhere else
        if (hrefs.length === 0) {
            console.log("No post hyperlinks found");
            return;
        }

        App.listroot = $('<div class="linkylist"/>');
        App.listhead = $('<p class="linkyhead">Click here to <span id="linkyaction">open</span> a list of all links on this page</p>');
        App.listbody = $('<div class="linkybody"/>');
        $(App.linkytarget).before(App.listroot);
        App.listroot.prepend(listbody);
        App.listroot.prepend(listhead);
        App.listbody.hide();
        App.listhead.click(function() {
            App.listbody.slideToggle(600);
            $('#linkyaction').text(function (index, text) {
               return (text == 'open' ? 'close' : 'open');
            });
        });
        // note that when you click a link it gets removed from the list.  
        App.hrefs.each(function() {
            App.parent = $('<span class="linkeyspan"/>');
            App.linkey = $(this).clone().attr('target', '_blank');
            App.linkey.click(function() {
                $(this).closest('.linkeyspan').remove();
            });
            App.parent.prepend(App.linkey);
            App.parent.prepend($('<br/>'));
            App.listbody.prepend(App.parent);
        });

        /**
         * Define "CSS" for this user script
         */
        function userscriptCSS() {
            addCss(
                '.linkylist {margin: 0px;padding: 0px;}' +
                '.linkyhead {text-align: right;padding: 5px 10px;cursor: pointer;position: relative;background-color:#FFCCCC;margin:1px;}' +
                '.linkybody {padding: 5px 10px 15px;background-color:#F4F4F8;}'
            );
        }

        /**
         * Stuff string of styles into doc head.
         */
        function addCss(cssString) {
            var head = document.getElementsByTagName('head')[0];
            if (!head) return;
            var newCss = document.createElement('style');
            newCss.type = "text/css";
            newCss.innerHTML = cssString;
            head.appendChild(newCss);
        }
    });
}, false);