// forum image scaler
// requires phpbb 3.x
// 18/03/2008 1.0
// 28/01/2010 1.0.1

var fis_config = [];
fis_config['img_max_width'] = 801;  //in pixels
fis_config['scaled_text'] = "This image has been resized. To display it at its full size, click this bar.";
fis_config['unscaled_text'] = "Image displayed full size. To scale it down to fit, click this bar.";


//TODO: make recursive and check performance
function fis_scanImages()
{

  //we are only interested in images appearing inside the forum post text body
  var post_body_array = fis_getElementsByClassName('content', 'div');
  if (!post_body_array) return 0;

  for (var i=0; i < post_body_array.length; i++)
  {
    currentPost = post_body_array[i];
    currentNode = currentPost.firstChild;
    while (currentNode && currentNode != currentPost)
    {

      if (currentNode.tagName == 'IMG')
      {
        imgNode = currentNode;
        if(currentNode.nextSibling)
        {
            currentNode = currentNode.nextSibling;
        }
        else
        {
          //this bit hurt
          while (currentNode.parentNode!=currentPost && !currentNode.parentNode.nextSibling)
          {
            currentNode = currentNode.parentNode;
          }
          currentNode = (currentNode.parentNode == currentPost) ? currentNode.parentNode : currentNode.parentNode.nextSibling;
        }
        fis_resizeImg(imgNode);
        continue;
      }

      if(currentNode.childNodes.length > 0)
      {
        currentNode = currentNode.firstChild;
      }
      else
      {
        if(currentNode.nextSibling)
        {
            currentNode = currentNode.nextSibling;
        }
        else
        {
          //this bit hurt
          while (currentNode.parentNode!=currentPost && !currentNode.parentNode.nextSibling)
          {
            currentNode = currentNode.parentNode;
          }
          currentNode = (currentNode.parentNode == currentPost) ? currentNode.parentNode : currentNode.parentNode.nextSibling;
        }
      }
      //continue
    }

  }
}


function fis_resizeImg(imgNode)
{
  //
  if (imgNode.width <= fis_config['img_max_width']) return;

  imgNode.style.width = fis_config['img_max_width'] + "px";
  if (imgNode.parentNode.tagName == "A")
    imgNode = imgNode.parentNode;

  newNode = document.createElement('DIV');	//an outer div to enclose the image and the alert bar
  newNode.style.width = fis_config['img_max_width'] + "px";
  newNode.style.border = "1px solid #333";
  newText = document.createElement('A');
  newText.innerHTML = fis_config['scaled_text']
  newText.style.cursor = "pointer";
  newText.style.textDecoration = "none";
  newText.style.fontSize = "0.8em";
  newText.style.background = "#ffd";
  newText.style.color = "#000";
  newText.style.padding = "4px";
  newText.style.display = "block";
  newText.style.borderBottom = "1px solid #333";
  newText.setAttribute("onclick", "fis_toggleImg(this)");
  newNode.appendChild(newText);
  newImg = imgNode.cloneNode(true);
  newNode.appendChild(newImg);
  newText.parentNode.innerHTML = newText.parentNode.innerHTML;  //this forces IE recognise the setAttribute for onclick, since it's a piece of crap.
  return imgNode.parentNode.replaceChild(newNode, imgNode);
}


function fis_toggleImg(img)
{
  var workingNode;
  if(img.nextSibling.tagName == "IMG")
  {
    workingNode = img.nextSibling;
  }
  else
  { //this covers images which are also anchored (clickable)
    workingNode = img.nextSibling.firstChild;
  }

  natWidth = fis_getNaturalWidth(workingNode);
  if (workingNode.style.width == fis_config['img_max_width'] + "px")
  {
    img.innerHTML = fis_config['unscaled_text']
    workingNode.style.width = natWidth + "px";
  }
  else
  {
    img.innerHTML = fis_config['scaled_text'];
    workingNode.style.width = fis_config['img_max_width'] + "px";
  }

  //make the alert bar expand and contract with the image too
  if (img.parentNode.style.width == fis_config['img_max_width'] + "px" )
  {
    img.parentNode.style.width = natWidth + "px"
  }
  else
  {
    img.parentNode.style.width = fis_config['img_max_width'] + "px";
  }

  return 0;
}


function fis_getElementsByClassName(className, tag, elm)
{
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++)
	{
		current = elements[i];
		if(testClass.test(current.className))
		{
			returnElements.push(current);
		}
	}
	return returnElements;
}


// adapted from http://frakkle.com/entry/98/
function fis_getNaturalWidth(img)
{
  if( img.naturalWidth ) {
    return img.naturalWidth;
  }
  else
  {
    lgi = new Image();
    lgi.src = img.src;
    return lgi.width;
  }
}

onload_functions.push('fis_scanImages;');

