/* Various Javascript utilities
 * Colin Jeanne, March 2008
 */

// Calls all necessary functions
function InitPage() {
   if (InitPage.arguments.length > 0)
   {
      var ir = new ImageRotator('asmast', InitPage.arguments);
      ir.AdvanceImage();
   }

   PreloadImages('ts-button', '/images/buttons/button_ts_over.jpg',
                 'tut-button', '/images/buttons/button_tutoring_over.jpg',
                 'aa-button', '/images/buttons/button_aa_over.jpg',
                 'sa-button', '/images/buttons/button_sa_over.jpg',
                 'tb-button', '/images/buttons/button_tb_over.jpg',
                 'fa-button', '/images/buttons/button_fp_over.jpg');
   
   InitFAQ('/images/widgets/open-button.png',
           '/images/widgets/close-button.png');
}

// An array of Image objects (for maping ID to src)
var preloadedImages = new Array();

// Loads the images into preloadedImages
// Each element of preloadedImages is an array where the first element
// is the ID of the element that the rollover pertains to and the
// second element is the src of the image that will be used the next
// time the user rolls over the element
function PreloadImages() {
   var args = PreloadImages.arguments;
   
   if (document.getElementById) {
      for (var i = 0; i < args.length; i += 2) {
         var elm = document.getElementById(args[i]);
         
         if (elm) {
            var img = new Image;
            img.src = args[i + 1];
            
            preloadedImages.push(new Array(args[i], img));
            elm.onmouseover = SwapImage;
            elm.onmouseout = SwapImage;
         }
      }
   }
}

// Does a search based on the ID of the element
// Returns the index of the ID in the preloadedImages array
function FindID(id) {
   for (var i = 0; i < preloadedImages.length; ++i) {
      if (id == preloadedImages[i][0])
         return i;
   }
   
   return -1;
}

// Swaps the image of the element that called this function
function SwapImage() {
   var loc = FindID(this.id);
   
   if (loc != -1) {
      if (document.getElementById) {
         var temp = preloadedImages[loc][1].src;
         preloadedImages[loc][1].src = this.src;
         this.src = temp;
      }
   }
}

// Loads the images which will be rotated
// The first argument to this function is the ID of the link which contains the
// image that is being used in the rotation
// The second argument (and every second argument afterward) is the src
// of an image
// The third argument (and every second argument afterward) is the alt
// text for the image that will act as the rotator
// The fourth argument (and every second argument afterward) is the URI that
// this image links to
function ImageRotator(elementID, args) {
   this.elementID = elementID;
   this.maxImages = args.length / 3;
   this.imageList = new Array();
   this.current = 0;
   this.AdvanceImage = function () { AdvanceImage(this); }
   
   for (var i = 0; i < args.length; i += 3) {
      var img = new Image;
      
      img.src = args[i];
      img.alt = args[i + 1];
      img.title = args[i + 1];
      
      var imgLink = new Array(img, args[i + 2]);
      this.imageList.push(imgLink);
   }
   
   if (window.setTimeout) {
      this.timerID = window.setTimeout(this.AdvanceImage, 10000);
   }
}

// Advances to the next image in the rotator array
// If isadvance is true then the next image in the array is used
// otherwise the previous image is used
function AdvanceImage(ir) {
   if (window.clearTimeout) {
      window.clearTimeout(ir.timerID);
   }
   
   if (ir.current == ir.maxImages) {
      ir.current = 0;
   }
   
   if (document.getElementById) {
      var link = document.getElementById(ir.elementID);
      
      if (link) {
         link.href = ir.imageList[ir.current][1];
         
         if (link.getElementsByTagName) {
            img = link.getElementsByTagName('img').item(0);
            
            if (img) {
               img.src = ir.imageList[ir.current][0].src;
               img.alt = ir.imageList[ir.current][0].alt;
               img.title = ir.imageList[ir.current][0].title;
            }
         }
      }
   }
   
   if (window.setTimeout) {
      ir.timerID = window.setTimeout(ir.AdvanceImage, 10000);
   }
}

var openButton;
var closeButton;

// Initializes all the FAQ answers to be hidden
function InitFAQ(openBut, closeBut) {
   openButton = openBut;
   closeButton = closeBut;
   
   if (document.getElementsByName) {
      // XXX Too bad the below line wont work in XHTML
      // XXX getElementsByName() will only grab form controls
      //var elems = document.getElementsByName('FAQItem');
      var elems = document.getElementsByTagName('div');
      
      if (elems) {
         for (var i = 0; i < elems.length; ++i) {
            if (elems.item(i).className == 'FAQItem') {
               elems.item(i).onclick = OpenClose;
               
               var quest = elems.item(i).getElementsByTagName('p').item(0);
               var ans = quest.nextSibling;
               
               while (ans) {
                  if (ans.nodeName != '#text') {
                     ans.style.display = 'none';
                  }
                  
                  ans = ans.nextSibling;
               }
            }
         }
      }
   }
}

// Opens or closes a FAQ item
function OpenClose() {
   var quest = this.getElementsByTagName('p').item(0);
   var ans = quest.nextSibling;
   var button = this.getElementsByTagName('img').item(0);
   //var ans = this.nextSibling;
   //var button = this.parentNode.getElementsByTagName('img').item(0);
   
   while (ans) {
      if (ans.nodeName != '#text') {
         if (ans.style.display == 'none') {
            ans.style.display = '';
            button.src = closeButton;
            button.alt = '[Close]';
         } else {
            ans.style.display = 'none';
            button.src = openButton;
            button.alt = '[Open]';
         }
      }
      
      ans = ans.nextSibling;
   }
}
