// ==UserScript==
// @name           AllegroTools (Cafe) dla IE i FF
// @namespace      http://www.allegrotoolsie.hopto.org/
// @description    Dodatkowe narzedzia pomocne przy przegladaniu Cafe Allegro // v0.20070817.0
// @include        http*://*allegro.pl/phorum/index_pl.php*
// @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;
}

// In FF childNodes returns also #text nodes that represents
// the whitespaces in the HTML source. IE skips these nodes
// For IE and FF use this method instead od childNodes property
// to return all children of an element.
// NOTE: this method skips ALL #text nodes (not only whitespaces)
function getChildNodes(element)
{
  var children = new Array();
 
  if(element)
  {
    var child = element.firstChild;
    while(child)
    {
      if (child.nodeType == 1)
      {
        children.push(child);
      }
      child = child.nextSibling;
    }
  }
  
  return children;
}

// IE specific functions
function select()
{
  if (arguments.length>1) {
    var node = arguments[0];
    for (var i=1; i<arguments.length; i++) {
      if (typeof(arguments[i])=='string') {
        node = node.getElementById(arguments[i]);
      } else {
        var nodeIdx = arguments[i];
        var children = getChildNodes(node);
        if (nodeIdx<children.length) {
          node = children[nodeIdx];
        } else {
          node = null;
          break;
        }
      }
    }
  }
  return node;
}

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 addSearchShortcuts()
{
  var pagecontentDiv = document.getElementById("pagecontent1");
  if (pagecontentDiv)
  {
    var summary = select(pagecontentDiv, 1, 0, 0 ,0 ,0);
    if (summary && summary.innerHTML.match(/Wyniki szukania: \d+-\d+ z (\d+)/))
    {
      var count = RegExp.$1;
      var origStart = 1;
    
      var link = document.location.toString();
      if (link.match(/&start=(\d+)/))
      {
        origStart = RegExp.$1;
      }
      link = link.replace(/&start=\d+/g, "");
      
      if(count > 20)
      {
        var start = 1;
        var end = 20;
        var numOfPages = Math.ceil(count / 20);
        var text = "";
        var innerHTML = "";
        for(var i = 0; i < numOfPages; i++)
        {
          start = i * 20 + 1;
          end = (i + 1) * 20;
          text = start + "-" + ((end < count) ? end : count);
          if (i % 10 == 0)  
          {
            innerHTML = innerHTML.concat("<br>");
          }
          else
          {
            innerHTML = innerHTML.concat("| ");
          }
          if (start == origStart)
          {
            innerHTML = innerHTML.concat(text + " ");
          }
          else
          {
            innerHTML = innerHTML.concat("<a href='" + link + "&start=" + start + "'>" + text + "</a> ");
          }
        }
        summary.innerHTML = summary.innerHTML.concat(innerHTML);
      } 
    }  
  }
}

function addGlobalSearch()
{
  var tableArray = document.getElementsByTagName("TABLE");
  if(tableArray)
  {
    for(var i = 0; i < tableArray.length; i++)
    {
      if(tableArray[i].className == "info")
      {
        var searchButton = document.createElement("SPAN");
        searchButton.className = "small";
        searchButton.innerHTML = "<br><a class='spec-flex' href='http://allegro.pl/phorum/search.php?f=298&globalsearch=1'><SPAN>Szukaj w calym Cafe Allegro</SPAN></a>";
        getParentNode(tableArray[i], 1).insertBefore(searchButton, getNextSibling(tableArray[i], 1));
        break;
      }
    }
  }
}

function allegrotoolsCafe()
{
  if(document.location.pathname.match("/phorum/search.php"))
  {
    modifySearchParameters();
    if(document.location.toString().match("search="))
    {
      addSearchShortcuts();
    }
  }
  else if(document.location.pathname.match("/phorum/read.php"))
  {
    modifyMenus();
  }
  else if(document.location.pathname.match("/phorum/index_pl.php"))
  {
    addGlobalSearch();
  }

}

/////////////////////////////////////////////////////////////////
// Start Allegro Tools Cafe script
/////////////////////////////////////////////////////////////////

allegrotoolsCafe()
