// ==UserScript==
// @name           AllegroTools (Cafe) dla IE i FF
// @namespace      http://www.allegrotoolsie.hopto.org/
// @description    Dodatkowe narzedzia pomocne przy przegladaniu Cafe Allegro // v0.20070304.0
// @include        http*://*allegro.pl/phorum/read.php?*
// @include        http*://*allegro.pl/phorum/search.php?*
// ==/UserScript==

/////////////////////////////////////////////////////////////////

// Returns the number-th parent node of the element
function getParentNode(element, number)
{
  var parent = element;
  while((number > 0) && (parent))
  {
    parent = parent.parentNode;
    number--;
  }
  
  return parent;
}

// Returns the number-th next sibling of the element
// for IE/FF compatibility skips #text nodes
function getNextSibling(element, number)
{
  var sibling = element;
  while((number > 0) && (sibling))
  {
    sibling = sibling.nextSibling;
    if (sibling && (sibling.nodeType == 1))
    {
      number--;
    }
  }
  
  return sibling;
}

function addLink(element, link)
{
  var linkElement = link.cloneNode(true);
  var separatorElement = element.previousSibling.cloneNode(true);
  element.parentNode.insertBefore(linkElement, element);
  element.parentNode.insertBefore(separatorElement, element);
}

function modifySearchParameters()
{
  var selectElements = document.getElementsByTagName("select");
  if (selectElements)
  {
    for(var i = 0; i < selectElements.length; i++)
    {
      var select = selectElements[i];
      if (select && select.name == "date")
      {
        var option7days = document.createElement("OPTION");
        option7days.innerHTML = "ostatnich 7 dni";
        option7days.value = "7";
        select.insertBefore(option7days, select.firstChild);
        var optionNoLimit = document.createElement("OPTION");
        optionNoLimit.innerHTML = "bez limitu";
        optionNoLimit.value = "0";
        select.insertBefore(optionNoLimit, getNextSibling(select.lastChild, 1));
        break;
      }
    }
  }
}

function modifyMenus()
{
  var linksTable = document.links;
  var searchLink = false;
  var flatLink = false;
  var treeLink = false;
  var menu = false;
  
  var reply = document.getElementsByName("REPLY");

  if (linksTable)
  {
    for(var i = 0; i < linksTable.length; i++)
    {
      if (linksTable[i].href.match("allegro.pl/phorum/search.php") && (linksTable[i].className == "spec-flex"))
      {
        searchLink = linksTable[i];
        menu = getParentNode(searchLink, 5);
      }
      else if (linksTable[i].href.match("v=f") && (linksTable[i].className == "spec-flex"))
      {
        flatLink = linksTable[i];
      }
      else if (linksTable[i].href.match("v=t") && (linksTable[i].className == "spec-flex"))
      {
        treeLink = linksTable[i];
      }

      if(searchLink)
      {
        if (flatLink)
        {
          addLink(searchLink, flatLink);
          if (reply && menu)
          {
            reply[0].parentNode.insertBefore(menu.cloneNode(true), getNextSibling(reply[0], 2));
          }
          break;
        }
        else if (treeLink)
        {
          addLink(searchLink, treeLink);
          if (reply && menu)
          {
            getParentNode(treeLink, 6).replaceChild(menu.cloneNode(true), getParentNode(treeLink, 5));
          }
          break;
        }
      }
    }
  }
}

function allegrotoolsCafe()
{
  if(document.location.pathname.match("/phorum/search.php"))
  {
    modifySearchParameters();
  }
  else if(document.location.pathname.match("/phorum/read.php"))
  {
    modifyMenus();
  }

}

/////////////////////////////////////////////////////////////////
// Start Allegro Tools Cafe script
/////////////////////////////////////////////////////////////////

allegrotoolsCafe()
