// ==UserScript==
// @name            Sokker-ImageViewer
// @namespace       http://www.mkvale.it/mk/sokker/
// @author          Mkcerusky
// @description     Version 0.2. - Shows images included in [img] tags or linked through [url] in forum topics (works with NEW DESIGN 2008)
// @include         http://online.sokker.org/forum_topic.php*

var contents, allLinks, thisLink, tables, newElement, images;

contents = document.getElementsByTagName('body')[0].innerHTML;
divs = document.getElementsByTagName('div');

images = '';

allLinks = document.evaluate(
			     '//a[@href]',
			     document,
			     null,
			     XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
			     null);

for (var i = 0; i < allLinks.snapshotLength; i++) {
  thisLink = allLinks.snapshotItem(i);
  if(imgTest(thisLink.href.toLowerCase())) images +=  '<a href=' + thisLink.href + '><img src=' + thisLink.href + ' width=100pt alt=' + thisLink.href + '></a> ';
 }


if (contents) {
  var parts, cleanParts;
  parts = contents.split('[img]');
  if (parts.length > 1) {
    for (var i = 0; i < parts.length; i++) {
      cleanParts = parts[i].split('[/img]');
      if (imgTest(cleanParts[0].toLowerCase()) && cleanParts.length > 1) images +=  '<a href=' + cleanParts[0] + '><img src=' + cleanParts[0] + ' width=100pt alt=' + cleanParts[0] + '></a> ';
    }
  }
 }

if (images !== '') {
  divs[0].innerHTML +=  "<p>" + images + 
  "<br><a href='www.mkvale.it/mk/sokker' style='font-size:smaller;' ><i><b>Sokker Image Viewer</b> and other scripts available at MkCerusky's Sokker Page</i></a>" +  
  "</p>";


 }

function imgTest(imgString) {
  if (imgString.match('.jpg') || imgString.match('.gif') || imgString.match('.png') || imgString.match('.jpeg')) return 1;
  else return 0;
}
