// Tutorial Utilities JavaScript
function init(){
  createLinkImages()
  createFooter();
  applyZoom();
  window.focus()
}
function createLinkImages(){
/* creates the next/previous/menu images in the paging links */
  var classNames=["prev","next"] // check both prev & next items
  for (var i=0;i<classNames.length;i++){
    c=document.getElementsByClassName(classNames[i]);
    for (var j=0;j<c.length;j++){
      var mylinks=c[j].getElementsByTagName("A");
      for (var k=0;k<c.length;k++){
        // This is the link - now create the image - set it to the appropriate source & insert it
        var img=document.createElement("IMG")
		var sp=document.createTextNode(" ")
        if (mylinks[k].href.indexOf("menu.")>0 ) {
          // menu link - use the menu image
          img.src= "../pgArt/menu.gif"
        }else{
          // choose the appropriate prev/next arrow image
          img.src= classNames[i]=="prev" ? "../pgArt/ArrowPrevious.gif" : "../pgArt/ArrowNext.gif"
        }
        // now insert the img into the link, either as the first or last child with sp as a spacer
        if (classNames[i]=="prev") {
          mylinks[k].insertBefore(sp,mylinks[k].firstChild);
          mylinks[k].insertBefore(img,mylinks[k].firstChild);
        } else {
          mylinks[k].appendChild(sp);
          mylinks[k].appendChild(img);
        }
      }
    }
  }
}
function createFooter(){
/* creates a footer that duplicates the next/previous page controls in the header */
  var h=document.getElementById("header")
  var f=document.getElementById("footer")
  
  if (h && f){ 
  	var p=h.firstChild;
	while (p){// && (p.nodeName !="TABLE"){// && (p.nodeName != "TABLE") && (p.getAttribute("className" != "pager") ) {
		if (p.nodeName=="TABLE" && p.className=="pager") break;
		p=p.nextSibling; 
	}
	if (p) {
	  var newP=p.cloneNode(true);
	  f.insertBefore(newP,document.getElementById("legal"));
    }
  }
}
function applyZoom(){
/* This function finds the screenshot image and, if present, 
	adds an onclick event to enlarge the image in a new window  */
  var s=document.getElementById("screen")
  var showImgUrl="../showImage.html"
  if (s) {
    var i=s.firstChild;
    while (i && i.nodeName != 'IMG'){
  	  i=i.nextSibling;
    } 
	// if there is an image in div#screen
    if (i){
      var iName=i.src.substring(i.src.lastIndexOf("/")+1);
      i.height=Math.round(i.height/2,0);
      i.onclick=function(){window.open(showImgUrl + "?" + iName,'screenshot','');}
      var d=document.createElement("DIV");
      d.setAttribute("class","noPrint");
      var t=document.createTextNode("Click image to view full-size in a new window");
      s.insertBefore(d,i)
      d.appendChild(t)
    } else {
      s.style.display="none";
    }
  }
}

document.getElementsByClassName = function(className, parentElement) {
  var children = ($(parentElement) || document.body).getElementsByTagName('*');
  var elements=new Array();
  for (var i=0;i<children.length;i++){
    if (children[i].className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))){
      elements.push(children[i]);
    }
  }
  return elements;
}
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}
if (!Array.prototype.push) {
  Array.prototype.push = function() {
		var startLength = this.length;
		for (var i = 0; i < arguments.length; i++)
      this[startLength + i] = arguments[i];
	  return this.length;
  }
} 
