// ==UserScript==
// @name           AllegroTools (Archiwizator) for IE i FF
// @namespace      http://allegrotoolsie.hopto.org/
// @description    Integracja z serwisami archiwizujacymi aukcje (Archi-wizator, Allegromat) // v0.20080523.0
// @include        http*://*allegro.pl/item*
// @include        http*://*allegro.pl/show_item.php?*
// ==/UserScript==

// Menu components constants
ARCHIVER_ENGINE_SEL = "_archiver_engine_sel_";
ARCHIVE_BTN         = "_archive_btn_";
                          
// Allegro Tools parameters constants
var AT_DEFAULT_ARCHIVER  = "AT_default_archiver";

// Archiver data
var ARCHIVERS = new Array(new Array("http://archi.inos.int.pl/add.html?s=1&id=", "Archi-wizator"),
                           new Array("http://www.allegromat.pl/index.php?numer=", "Allegromat"),
                           new Array("http://archi.inos.int.pl/add.html?s=1&imgdb=1&id=", "Archi-wizator (+obrazki)"));

/////////////////////////////////////////////////////////////////
//// IE/FF compatibility
/////////////////////////////////////////////////////////////////

// FF uses addEventListener method while IE uses attachEvent method
// to register event handler
// Call this mehod with the element that generates the event,
// the event name (without 'on' prefix) and the handler routine
// The handler routine should accept an event source
// See getEvent method to see how to get the event source in FF and IE
function registerEventHandler(element, eventName, handler)
{
  if (element.addEventListener)
  {
    element.addEventListener(eventName, handler, false);
  }
  else if (document.attachEvent)
  {
    element.attachEvent("on" + eventName, handler);
  }
} 

// 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 handleArchiveBtn(auctionNumber)
{
  if(confirm("Czy na pewno chcesz zarchiwizowac aukcje nr " + auctionNumber + " w wybranym archiwizatorze?"))
  {
    var engineSel = document.getElementById(ARCHIVER_ENGINE_SEL);
    window.open(engineSel.value);
    GM_setValue(AT_DEFAULT_ARCHIVER, engineSel.selectedIndex);
  }
}

function addArchiverButton()
{
  if(document.location.href.match(/item(\d+).*?.html/) ||
    (document.location.href.match(/show_item.php\?.*?item=(\d+).*?/)))
  {
    var auctionNumber = RegExp.$1;
    
    if(auctionNumber)
    {
      var dataHeaderElement = null;
      var pagecontent = document.getElementById("pagecontent1");

      if(pagecontent)
      {
        var dataHeaderElement = select(pagecontent, 0, 3, 0, 0, 0, 0, 0, 0, 0);
        if(dataHeaderElement)
        {
          dataHeaderElement.removeAttribute('colSpan');
          dataHeaderElement.removeAttribute('width');
          var archiverFormTd = dataHeaderElement.cloneNode(true);
          archiverFormTd.align = 'right';
          var default_archiver = GM_getValue(AT_DEFAULT_ARCHIVER, 0);
          if(default_archiver >= ARCHIVERS.length)
          {
            default_archiver = 0;
          }
          var innerHtml = "<form>" +
                          "<select id='" + ARCHIVER_ENGINE_SEL + "'>";
          for(var i = 0; i < ARCHIVERS.length; i++)
          {
            innerHtml = innerHtml +
                          "<option value='" + (ARCHIVERS[i])[0] + auctionNumber + "'" + ((default_archiver == i) ? " selected>" : ">") + (ARCHIVERS[i])[1] + "</option>";
          }
          innerHtml = innerHtml + 
                          "</select>&nbsp;" +
                          "<input id='" + ARCHIVE_BTN + "' type='button' value='Archiwizuj' style='font-size=8pt' title='Archiwizuje aukcje nr " + auctionNumber + " w wybranym archivizatorze'>" +
                          "&nbsp;</form>";
          archiverFormTd.innerHTML = innerHtml;
          dataHeaderElement.parentNode.appendChild(archiverFormTd);
          registerEventHandler(document.getElementById(ARCHIVE_BTN), "click", function(){handleArchiveBtn(auctionNumber);});
        }  
      }  
    }  
  }
}

function allegrotoolsArchiver()
{
  addArchiverButton();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Start Allegro Tools (Archiver) script
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
allegrotoolsArchiver()
