// Module author: Raul Parolari
//    following examples from book "DOM Scripting" by Jeremy Keith

function addLoadEvent(func) {
  var oldonload = window.onload;

  /*
     if first time called, set onload event handler to new function;
     else, set it to a function calling old + new functions
  */
  
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newEl, targetEl) {
   /* 
       if targetEl is the last child of its parent, append newEl as child
   */
   var parent = targetEl.parentNode;
   if (parent.lastChild == targetEl) {
      parent.appendChild(newEl);
   }
   else {
      /* targetEl not the last child; thus, append newEl before sibling */
      parent.appendBefore(newEl, targetEl.nextSibling);
   }
}

function prepareEventHandlers() {
  /*
     this function inserts the event handlers in the element "image_handler":
       <a id="image_handler" href="buon_appetito.html"
            onmouseover="document.molly.src='Images/ralph_biscotti.jpg'"
            onmouseout ="document.molly.src='Images/ralph_molly.jpg'">
          <img id="image" src="Images/ralph_molly.jpg" name="molly"
                      alt="Ralph in the kitchen making Cartelatte" />
       </a>     
  */
  
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  
  var el_ref_image = document.getElementById("ref_image");
  var el_image     = document.getElementById("image");
  var el_caption   = document.getElementById("caption");

  if (! (el_ref_image && el_image && el_caption) ) {
    return false; // comment out this line to test and get alerts
    var str = "";
    str += el_ref_image ? "" : "el_ref_image, ";
    str += el_image     ? "" : "el_image, ";
    str += el_caption   ? "" : "el_caption ";
    str += "not found";
    alert(str);
    return false;
  }

  var regex = /Internet.*Explorer/i;
  var IE = regex.test (navigator.appName) ? true : false;
  // alert("browser is IE? " + IE); 

  // for IE, add the CSS Filter for the image 
  if (IE) {
     el_image.style.filter = "blendTrans(duration=2)";
     // alert(el_image.style.filter);
  }

  var source_Molly    = "./Images/ralph_molly.jpg";
  var source_Biscotti = "./Images/ralph_biscotti.jpg";

  var str_caption_out  = "Molly and Ralph in the kitchen, making biscotti";
  var str_caption_over = "Ralph in the kitchen making Cartelatte";

  el_image.onmouseover = function() {
    if (IE) { el_image.filters["blendTrans"].apply(); }
    el_image.setAttribute("src", source_Biscotti);
    el_caption.childNodes[0].nodeValue = str_caption_over;
    if (IE) { el_image.filters["blendTrans"].play(); }
  }
  
  el_image.onmouseout = function() {
    if (IE) { el_image.filters["blendTrans"].apply(); }
    el_image.setAttribute("src", source_Molly);
    el_caption.childNodes[0].nodeValue = str_caption_out;
    if (IE) { el_image.filters["blendTrans"].play(); }
  }

  if (IE) { el_image.filters["blendTrans"].apply(); }
  el_image.setAttribute("src", source_Molly);
  el_caption.childNodes[0].nodeValue = str_caption_out;
  if (IE) { el_image.filters["blendTrans"].play();  }
  
}

addLoadEvent(prepareEventHandlers);

