// ==UserScript==
// @name           AllegroTools (Cafe) dla IE i FF
// @namespace      http://www.allegrotoolsie.hopto.org/
// @description    Dodatkowe narzedzia pomocne przy przegladaniu Cafe Allegro // v0.20081126.0
// @include        http*://*allegro.pl/spolecznosc/
// @include        http*://*allegro.pl/phorum/read.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;
}

// 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;
}

// 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;
}

// 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 modifyMenus()
{
  var linksTable = document.links;
  var menuTop = false;
  var menuBottom = false;
  
  if (linksTable)
  {
    for(var i = 0; i < linksTable.length; i++)
    {
      if (linksTable[i].href.match("phorum_search.php") && (linksTable[i].className == "spec-flex"))
      {
        if(menuTop)
        {
          menuBottom = getParentNode(linksTable[i], 2);
          break;
        }
        else
        {
          menuTop = getParentNode(linksTable[i], 5);
        }  
      }
    }  

    if(menuTop && menuBottom)
    {
      // add spacing between bottom menu and message edit box
      var span = document.createElement("SPAN");
      span.innerHTML = "<BR>";
      getParentNode(menuBottom, 1).insertBefore(span, getNextSibling(menuBottom, 1));
      // clone the top menu at the bottom
      menuBottom = getParentNode(menuBottom, 1).replaceChild(menuTop.cloneNode(true), menuBottom);
    }
  }
}

function addGlobalSearch()
{
  var div = document.getElementById("CmutyCafe");
  if(div)
  {
    var searchLink = document.createElement("span");
    searchLink.style.verticalAlign = '100%';
    searchLink.innerHTML = "<strong>&nbsp;&nbsp;<a href='http://allegro.pl/phorum/phorum_search.php?f=298&globalsearch=1'>Szukaj w calym Cafe Allegro</a></strong>";
    getParentNode(div, 1).insertBefore(searchLink, div);
  }
}

function allegrotoolsCafe()
{
  if(document.location.pathname.match("/phorum/read.php"))
  {
    modifyMenus();
  }
  else if(document.location.pathname.match("/spolecznosc"))
  {
    addGlobalSearch();
  }
}

/////////////////////////////////////////////////////////////////
// Start Allegro Tools Cafe script
/////////////////////////////////////////////////////////////////

allegrotoolsCafe()
