// JavaScript Document for Mike Behnke, Homework 6, INP271 
var jslib = {

	createCookie : function (name,value,expiration,path,domain,secure) {
  	var data = name + "=" + escape(value);
  	if (expiration) { data += "; expires=" + expiration.toGMTString(); }
  	if (path) { data += "; path=" + path; }
  	if (domain) { data += "; domain=" + domain; }
  	if (secure) { data += "; secure"; }
  	document.cookie = data;
	},
	
	findCookie : function(name) {
    var query = name + "=";
    var querylength = query.length;
    var cookielength = document.cookie.length;
    var i=0;
    while (i < cookielength) {
      var startValue = i + querylength;
      if (document.cookie.substring(i,startValue) == query) {
         return this.findValue(startValue);
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) {break;}
    }
    return null;
  },

  findValue : function(startValue) {
    var endValue = document.cookie.indexOf(";", startValue);
    if (endValue == -1) {
       endValue = document.cookie.length;
    }
    return unescape(document.cookie.substring(startValue,endValue));
  },
  
	// improve performance by reducing object lookups 
	getObj : function(idvalue) {
		return document.getElementById(idvalue);
	},
		
	getElementsByClass : function (searchClass,node,tag) {
    // this array will hold the nodes that have the desired class
    var classElements = [];
    // if we did not pass the node parameter, assume document
    if (node == null) {node = document;}
    // if we did not pass the tag parameter, grab every node
    if (tag == null) {tag = '*';}
    // gather all the element nodes to look through; by default is everything in document
    var els = node.getElementsByTagName(tag);
    // to improve loop performance, determine the length ahead of time
    var elsLen = els.length;
    // establish the pattern to search for within className
    var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
    // look through all the class properties to see if there is a match
    // the j variable is the counter variable that increments each time a
    // match is found and becomes the next item in classElements
    for (var i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
           classElements[j] = els[i];
           j++;
        }
    }
    // send back the array of elements to whatever variable called this function
    return classElements;
	},	
	
	// associate nodes with events and functions
  addEvent : function(obj, type, func) {
       if (obj.addEventListener) {obj.addEventListener(type, func, false);}
       else if (obj.attachEvent) {
           obj["e" + type + func] = func;
           obj[type + func] = function() {obj["e" + type + func] (window.event);}
           obj.attachEvent("on" + type, obj[type + func]);
       }
       else {obj["on" + type] = func;}
   },
   
   roll : function() {

    if (!document.getElementById && !document.getElementsByTagName) { return; }

    // locating document nodes
    var nav = document.getElementById('nav');
    var theImgs = nav.getElementsByTagName('img');
    var theLinks = nav.getElementsByTagName('a');

    // creating arrays to hold rollover images, plus a variable for image file
    // extension and a variable to hold the total number of images to speed up the loop
    var imgOver = [], imgOff = [], suffix, totalImgs = theImgs.length;

    for (var i=0, j=0; i<totalImgs; i++) {
			if (theImgs[i].parentNode.nodeName != 'LI') {				 
	      // extract the image's extension by determining the position of the last
	      // dot in the file name and then getting the substring after that dot
	      suffix = theImgs[i].src.substring(theImgs[i].src.lastIndexOf('.'));

	      // creating a new Image() object and storing it in the imgOff array with
	      // a src that is the same as the image currently hard-coded in the page
	      imgOff[i] = new Image();
	      imgOff[i].src = theImgs[i].src;
	
	      // creating a new Image() object for the rollover image and giving it a
	      // src that is the regular image name with '-over' before the extension
	      imgOver[i] = new Image();
	      imgOver[i].src = theImgs[i].src.substring(0,theImgs[i].src.lastIndexOf('.')) + "_over" + suffix;

  	    // assigning number properties to each img and link in the document
				theImgs[i].number = i;
 	    	theLinks[j].number = i;

 	    	// using the number to synchronize the regular and rollover images as
	     	// well as the links and the rollover images
	     	jslib.addEvent(theImgs[i],'mouseover', function() {
	       	theImgs[this.number].src = imgOver[this.number].src;
	     	});
	     	jslib.addEvent(theImgs[i],'mouseout', function() {
	       	theImgs[this.number].src = imgOff[this.number].src;
	     	});
	     	jslib.addEvent(theLinks[j],'focus', function() {
	       	theImgs[this.number].src = imgOver[this.number].src;
	     	});
 	    	jslib.addEvent(theLinks[j],'blur', function() {
 	      	theImgs[this.number].src = imgOff[this.number].src;
	     	});
				j++;
    	}
		}
  }

}
jslib.addEvent(window,'load',jslib.roll);