﻿var linkRollovers = {
  
	init : function() {
	  if (!document.getElementById || !document.getElementsByTagName) {return;}
	  linkRollovers.rollovers()
	},
	
	rollovers : function() {
	  // get the inputs, images and anchor tags
      var theInputs = getElementsByClass('rollover',document,'input');      
      var theLinks = getElementsByClass('rollover',document,'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 theImgs = [], imgOver = [], imgOff = [], inputOver = [], inputOff = [], suffix, totalLinks = theLinks.length, totalInputs = theInputs.length;    
      
      //loop through the input buttons to apply rollovers
      for (var i=0; i<totalInputs; i++) { 
        suffix = theInputs[i].src.substring(theInputs[i].src.lastIndexOf('.'));
      
        util.addEvent(theInputs[i],'mouseover', function() {            
            this.src = this.src.substring(0,this.src.lastIndexOf('.')) + "_over" + suffix;
         });
         util.addEvent(theInputs[i],'mouseout', function() {
            this.src = this.src.replace('_over.','.');
         });
          util.addEvent(theInputs[i],'focus', function() {            
            this.src = this.src.substring(0,this.src.lastIndexOf('.')) + "_over" + suffix;
         });
         util.addEvent(theInputs[i],'blur', function() {
            this.src = this.src.replace('_over.','.');
         });
      }
      
      //loop through the links to apply rollovers
      for (i=0; i<totalLinks; i++) {
    
          theImgs[i] = new Image();
          theImgs[i] = theLinks[i].getElementsByTagName('img')[0];
      
          // 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;
          

          // use the rollover image as the "normal" state of a selected link
          if (theLinks[i].className == 'rollover selected') {
            imgOff[i].src = imgOver[i].src;
            theImgs[i].src = imgOver[i].src;
          }

          // assigning number properties to each img and link in the document
          theImgs[i].number = i;
          theLinks[i].number = i;     
         
          // using the number to synchronize the regular and rollover images as
          // well as the links and the rollover images
          util.addEvent(theImgs[i],'mouseover', function() {
              if(this.src.indexOf("blank.gif") == -1 ) {
                theImgs[this.number].src = imgOver[this.number].src;
              }
              else {
                var theFilter = this.style.filter;
                this.style.filter = theFilter.replace('.png','_over.png');
              }
          });
          util.addEvent(theImgs[i],'mouseout', function() {
               if(this.src.indexOf("blank.gif") == -1 ) {
                theImgs[this.number].src = imgOff[this.number].src;
               }
               else {
                var theFilter = this.style.filter;
                this.style.filter = theFilter.replace('_over.png','.png');
              }
          });
          util.addEvent(theLinks[i],'focus', function() {
               if(theImgs[this.number].src.indexOf("blank.gif") == -1 ) {
                theImgs[this.number].src = imgOver[this.number].src;
               }
               else {
                var theFilter = theImgs[this.number].style.filter;
                theImgs[this.number].style.filter = theFilter.replace('.png','_over.png');
               }
          });
          util.addEvent(theLinks[i],'blur', function() {
               if(theImgs[this.number].src.indexOf("blank.gif") == -1 ) {
                theImgs[this.number].src = imgOff[this.number].src;
               }
               else {
                var theFilter = theImgs[this.number].style.filter;
                theImgs[this.number].style.filter = theFilter.replace('_over.png','.png');
               }
          });
      }

	}

}

linkRollovers.init();	
