// #REQUIRE: events/core.js

function isPNGHackNeeded() {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])
  return (version > 5.5 && version < 7.0 && document.body.filters);
}

// correctly handle PNG transparency in Win IE 5.5 & 6.
function correctPNG() {
  if (!isPNGHackNeeded()) { return; };

  for (var i=0; i<document.images.length; i++) {
    var img = document.images[i]
    var imgName = img.src.toUpperCase()
    if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
      if (correctPNGNode(img)) { i = i-1; };
    }
  }

  var inputs = document.getElementsByTagName('input');
  for (var i=0; i<inputs.length; i++) {
    var input = inputs[i];
    if (input.type == "image") {
      var imgName = input.src.toUpperCase()
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
        if (correctPNGNode(input)) { i = i-1; };
      };
    };
  };
}

function correctPNGNode(node) {
  if (!isPNGHackNeeded()) { return false; };

  switch (node.tagName) {
    case "IMG":
      return correctPNGNodeImg(node);
    case "INPUT":
      if (node.type != "image") { return false; };
      return correctPNGNodeInput(node);
    default:
      return false;
  };

  if (img.tagName != "IMG") {
    return false;
  };
}

function correctPNGNodeImg(img) {
  if (!isPNGHackNeeded()) { return false; };

  var width = (img.width || parseInt(img.currentStyle.width));
  var height = (img.height || parseInt(img.currentStyle.height)); 

  if (isNaN(width) || isNaN(height)) {
    return false;
  };  
 
  // Note the font-size: 1px; this prevents short images from being expanded to the current 
  // font size

  var newNode = document.createElement("span");
  newNode.id = img.id;
  newNode.className = img.className;
  newNode.title = img.title || img.alt;
  newNode.style.cssText = img.style.cssText;
  newNode.style.display = "inline-block";
  if (img.align == "left") { newNode.style.styleFloat = "left"; };
  if (img.align == "right") { newNode.style.styleFloat = "right"; };
  newNode.style.width = width.toString() + "px";
  newNode.style.height = height.toString() + "px";
  newNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale')";
  newNode.style.cursor = "pointer";
  newNode.style.fontSize = "1px";
  img.parentNode.replaceChild(newNode, img);

  return true;
}

function correctPNGNodeBg(div) {
  if (!isPNGHackNeeded()) { return false; };

  var fullSrc = div.currentStyle.backgroundImage;
  var src = fullSrc.match(/\(["'](.*)["']\)/)[1];
  div.style.backgroundImage = "none";  
  div.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + src + "\', sizingMethod='crop')";

  return true;
}

function correctPNGNodeInput(img) {
  if (!isPNGHackNeeded()) { return false; };

  var form = img.form;

  var width = (img.width || parseInt(img.currentStyle.width));
  var height = (img.height || parseInt(img.currentStyle.width)); 

  if (isNaN(width) || isNaN(height)) {
    return false;
  };

  // Note the font-size: 1px; this prevents short images from being expanded to the current 
  // font size

  var newNode = document.createElement("span");
  newNode.id = img.id;
  newNode.className = img.className;
  newNode.title = img.title || img.alt;
  newNode.style.cssText = img.style.cssText;
  newNode.style.display = "inline-block";
  if (img.align == "left") { newNode.style.styleFloat = "left"; };
  if (img.align == "right") { newNode.style.styleFloat = "right"; };
  newNode.style.width = width.toString() + "px";
  newNode.style.height = height.toString() + "px";
  newNode.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale')";
  newNode.style.cursor = "pointer";
  newNode.style.fontSize = "1px";
  img.parentNode.replaceChild(newNode, img);

  addEvent(newNode, "click", function() { form.fireEvent("onsubmit"); });

  return true;
}
