// ==UserScript==
// @name           vB New Topics
// @namespace      http://userscripts.org/forums/2/topics/1801
// @description    Open all forum new topics in tabs
// @include        */forumdisplay.php*
// ==/UserScript==

/*
  Created by Gollum from an idea/suggestion/request at us.o by @Seph_VIII
  Expanded upon by @Mithrandir(us.o)
      - with help from @Dr. Evil(de ff-user-group)
          ... max links to open, conditional button display, timeout delay, extra button styling, windowOpen fallback for GM_openInTabs
   Gollum: added quick element check for vB board, plus x-browser support - tested in Opera(9.25) and IE7(IEPro), plus option to ignore timeout delay.
*/

  if (document.getElementById('threadslist') && location.href.indexOf('php?searchid') > 0) // early return for non-vB boards
{
// configuration
var maxlnks = 0;  // 0=open all new threads, 10=open first 10 new threads
var showBtnIf = 1; // 0=button will always be shown, 5=show only, if there are more than 4 new threads
var useTimeout = true; // don't attempt delay on thread-read cookie setting - YMMV - the timeout delay didn't work for me, so =false
var tOut = 3000;   // set timeout in milliseconds for opening (cookies won't be stored if too low!)
var oMode = false; // =true: show first *unread* post, =false: show first post/beginning of thread
// end configuration

  /* x-browser event register */
  function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true; }
    else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r; }
    else { elm['on' + evType] = fn; }
  }

  /* x-browser open tab */
  function openTab(url) {
    if (typeof GM_openInTab != 'undefined') GM_openInTab(url);
    else if (typeof PRO_openInTab != 'undefined') PRO_openInTab(url,2);
    else
    {
	window.open(url);
    }
  }
  
var f = 0;
var newposts = new Array();
var lnks = document.getElementById('threadslist').getElementsByTagName('a');
for (var i=0; i < lnks.length; i++) {
  if (/-new-post.html/.test(lnks[i].href)) {
    newposts.push(lnks[i].href);
    f++
    if (f+1 > maxlnks && maxlnks > 0) break;
  }
}

if (f >= showBtnIf) {
  addNum = document.getElementById('inlinemodform').getElementsByTagName('td').length;
  for (var i=1; i < addNum; i++)
  {
    if(document.getElementById('inlinemodform').getElementsByTagName('td')[addNum - i].innerHTML.indexOf('>Showing results') > 0)
    {
        var offset = i;
    }
  }
  var theParent = document.getElementById('inlinemodform').getElementsByTagName('td')[addNum - offset].getElementsByTagName('span')[0];
  theParent.innerHTML = "";
  var btn = theParent.insertBefore(document.createElement('a'), theParent.firstChild);
  btn.innerHTML = "Sexter All Above Posts (" + f + ")";
  btn.setAttribute('href', '#');
  btn.style.marginLeft = '4px';
  btn.style.position = 'relative';
  btn.style.background='#000000';
  btn.style.color='#ffff00';
  btn.style.fontSize='0.9em';
  addEvent(btn, "click",
    function(e) {
      if (maxlnks==0) this.style.display = 'none';
      if (e && e.target) e.preventDefault();
      else window.event.returnValue = false;
      if (useTimeout) {
        var i = -1;
        function inner() {
          if(++i < f) openTab(newposts[i]);
          else clearInterval(timer);
        }
        var timer = setInterval(inner, tOut);
      } else {
        for (var i=0; i < newposts.length; i++)
          openTab(newposts[i]);
      }
    }
  , false);
  }
}

