// ==UserScript==
// @name           Helgon.net avatars in user listings
// @namespace      http://henrik.nyh.se
// @description    Show avatars next to user names on Helgon.net user listings, by default most listings except for friends pages. Requires a Greasemonkey version that allows GM_xmlhttpRequest, such as 0.5.
// @include        http://*helgon.net/*Visitors.asp?*
// @include        http://*helgon.net/*Views.asp?*
// @include        http://*helgon.net/search/Search.asp?*
// @include        http://*helgon.net/Start/online.asp*
// @include        http://*helgon.net/Start/BirthDay.asp*
// @include        http://*helgon.net/Start/lastreg.asp
// @include        http://*helgon.net/UserInfo/Userinfo_Lastvisits.asp
// @include        http://*helgon.net/diary/diary_whomonitors.asp
// @include        http://*helgon.net/Diary/Diary_readOK.asp
// ==/UserScript==

if( !GM_xmlhttpRequest )
  return alert('Please upgrade to the latest version of Greasemonkey.');

function get( url, callback )
{
  GM_xmlhttpRequest( { method:'GET', url:url, onload:callback } );
}

function gotAvatar( http )
{
  var html = http.responseText.replace( /\r\n/g, '' ), avatar, space;
  var img = /src="(http:..files.helgon.net.UserPicz.*?jpg)"/.exec( html )[1];
  var id = /\?ID=(\d+)/.exec( html )[1]; // needed by gotAvatar

  var node = document.getElementById( id );

  avatar = document.createElement( 'img' );
  avatar.src = img;
  avatar.className = 'largeimageborder';
  avatar.style.width = '40px';
  avatar.align = 'middle';

  space = document.createTextNode( ' ' );

  link = document.createElement( 'a' );
  link.href = 'http://www.helgon.net/userinfo/userinfo.asp?ID=' + id;
  link.appendChild( avatar );

  // Don't show default pictures:
  if( img.substring(34,73) != '000/{00000000-0000-0000-0000-0000000000' )
  {
    node.parentNode.insertBefore( space, node ); // Insert spacing
    node.parentNode.insertBefore( link, space ); // Insert linked image
  }
}

var node, i, is, sex, age, parts, t, e=0;
for( i=0; node = document.links[i]; i++ )
{
  if( node.target == 'helgonmain') // signifies a profile link
  {
    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;
/* adds a filter on what you see -
    if( (sex == 'K') || (age < 20) || (age > 35) )
    {
      t.style.display = 'none';
      continue;
    }
    else
    {
      t.style.className = e ? 'bgframe' : 'middleframe';
      e = 1 - e;
    }
*/
    node.id = /\?ID=(\d+)/.exec( node.href )[1]; // needed by gotAvatar
    get( node.href, gotAvatar );
  }
}

