// ==UserScript==
// @name           Helgon.net workover
// @namespace      http://www.lysator.liu.se/~jhs/userscript
// @description    Adds a "Historik" link to each conversation, even between other people. Adds user avatars to lots of pages that do not usually feature one.
// @include        http://www.helgon.net/*.asp*
// @include        http://helgon.net/*.asp*
// ==/UserScript==

var picturize = true; // whether to add avatar pictures

switch( location.pathname )
{
  case '/':
  case '/userinfo/userinfo.asp':
  case '/GuestBook/Guestbook.asp':
    picturize = false;
    break;
  case '/frameset/meny.asp':
    document.forms.myForm.UserNameSearch.accessKey = 'S';
    break;
  case '/guestbook/guestbook.asp': // ?ID=1330&GuestBookID=202811852
    break;
}

const profileURL = 'http://'+ location.hostname +'/UserInfo/UserInfo.asp?ID=';
const profileRE = new RegExp( '/UserInfo/UserInfo\\.asp\\?ID=(\\d+)$', 'i' );

function Cache()
{
  this.cache = {};
}

function Message( fromUID, toUID, at, message )
{
}

function history( message, at, other )
{
  
}

// Parse out the avatar URL from a user home page, and store it in the cache.
// (Then call the callback, if one was setup in Cache.withAvatar(). )
function gotAvatar( http )
{
  var html = http.responseText; // .replace( /\r\n/g, '' );

  var href = /src="(http:..[^\/]*.helgon.net.UserPicz.*?jpg)"/.exec( html )[1];
  var user = /\?ID=(\d+)/.exec( html )[1];
  top.cache.avatars[user] = href;
  var call = top.cache.callbacks[user];
  if( call )
  {
    call( href, user );
    delete top.cache.callbacks[user];
  }
}

// a document.getElementsByTagAndClassName(), the Method That Does Not Exist
function byClass( nodeName, className )
{
  var nodes = document.getElementsByTagName( nodeName ), result = [], i;
  for( i=0; i<nodes.length; i++ )
    if( nodes[i].className == className )
      result.push( nodes[i] );
  return result;
}

function toggleAvatarSize()
{
  var img = this.firstChild;
  if( img.width == 40 )
  {
    img.width = 100;
    img.height = 140;
  }
  else
  {
    img.width = 40;
    img.height = 56;
  }
  return false;
}

function drawAvatar( href, user )
{
  var nodes = byClass( 'a', user ), avatar, space, link, i;
  for( i=0; i<nodes.length; i++ )
  {
    nodes[i].className = ''; // so we won't modify the node more than once
    avatar = document.createElement( 'img' );
    avatar.setAttribute( 'width', '40' );
    avatar.src = href;
    avatar.className = 'largeimageborder';
    avatar.align = 'middle';

    space = document.createTextNode( ' ' );

    link = document.createElement( 'a' );
    link.href = '/userinfo/userinfo.asp?ID=' + user;
    link.onclick = toggleAvatarSize;
    link.appendChild( avatar );

    avatar.setAttribute( 'height', '56' ); // drop this if uncommenting below
    // Don't show default pictures:
    //if( href.substring(34,73) != '000/{00000000-0000-0000-0000-0000000000' )
    //{
    nodes[i].parentNode.insertBefore( space, nodes[i] );  // Insert spacing
    nodes[i].parentNode.insertBefore( link, space ); // Insert linked image
    //}
  }
}

var node, i, is, id, sex, age, parts, t, e = 0;
for( i = document.links.length-1; i >= 0; i-- )
{
  node = document.links[i];
  if( id = profileRE.exec( node.href ) )
  {
    id = id[1];
    if( (is = node.getElementsByTagName( 'img' )).length ) // register in cache
    {
      top.cache.avatars[id] = is[0].src;
      continue;
    }
/* this bit, if uncommented, will hide all males and below-20 / over-35
    is = node.nextSibling.nodeValue; // " T<age>", " K<age>", " O<age>"
    if( !(parts = / *(\D)(\d+)/.exec( is )) )
      continue;
    sex = parts[1];
    age = parseInt( parts[2], 10 );
    t = node.parentNode.parentNode;
    if( (sex == 'K') || (age < 20) || (age > 35) )
    {
      t.style.display = 'none';
      continue;
    }
    else
    {
      t.style.className = e ? 'bgframe' : 'middleframe';
      e = !e;
    }
*/
    top.cache.withAvatar( node.className = id, drawAvatar );
  }
}

