function ImageRandomizer() {

  var self = this;

  this.imgTimer = null;
  this.active = null;

  this.imageList = new Array();

  GDownloadUrl("all_images.xml", function(data, responseCode) {
    var xml = GXml.parse(data);
    var imgs = xml.getElementsByTagName("image");

    for (var i = 0 ; i < imgs.length ; i++) {
    	// get one track after another
    	var img = imgs[i];
    	// now we have the track object, time to get the contents
    	// get the name of the track
    	var imageURL = img.getElementsByTagName("imageURL")[0].firstChild.nodeValue;
    	//alert(imageURL + "|" + this.imageList_x.length);
      self.imageList[i] = imageURL;
    }
  });

	function showOptions(){
		var someNodeList = $('lstEmployees').getElementsByTagName('option');
		var nodes = $A(someNodeList);

		nodes.each(function(node){
				alert(node.nodeName + ': ' + node.innerHTML);
			});
	}

  // acts a timer control to periodically rotate images as music plays
  // has active state true|false which can be toggled.
  // in turn calls function defined in calling document setBackground 
  // since setting document.body.background doesn't seem to work from within this script
  // but timer seems to work much better here than in head of document
  //
  // calls: document function setBackground
  //
  // called by anything that needs to suspend image rotation to allow
  // (an) other process to complete without competition.


  this.start = function () {
    if (self.active==true) return false;
    self.active = true;
    self.imgTimer = window.setInterval("imageRandomizer.pushNextImage()", 10000);
    //alert("background rotation started");
  }

  this.stop = function () {
    if (self.imgTimer) {
      window.clearInterval(self.imgTimer);
      self.imgTimer = null;
      self.active = false;
      //alert("background rotation stopped");
    }
  }
  
  this.changeNow = function() {
    self.stop();
    self.pushNextImage();
    self.start();
  }

  this.getRandomIndex = function(list) {
  // generates a random index based on size of list of urls provided
  //
  // NOTE: this function will alway return idx of at least 0
  //       and one which is less than the length of the list
  //
  // called by: getNewImage
  
    this.i = 1;
    this.n = false;
    this.list = list;
  
        if (typeof this.list != 'undefined') {
            this.n = parseInt(Math.random() * this.list.length);
          }
        if (typeof this.n != 'undefined') {
          if (this.n != false) { // this.n not false as initialized above
            if (this.n < this.list.length) {
              this.i = this.n;
            }
          }
        }
        return this.i; // random index value from imageList
  }


  
  this.getNextImageURL = function() {
  // coordinates delivery of a new image URL from imageList and
  // returns it to calling process.
  // for prototype this will function will use only embedded imageList above.
  // TODO: add ability to read a file. and assure always a valid URL returned
  //
  // calls: ImageRandomizer, imageList
  //
  // called by: various document functions directly or by imageTimer
  //
  // NOTE: requires that array imageList be initialized and available
  
    var imageURL = "http://www.oneworldmix.com/media/images/active_users_block.gif"; // set default
    var i = 0;
    var n = false;
    
    try {
      
      this.n = self.getRandomIndex(this.imageList);
      this.i = this.n;
    } catch(err) {
      // debug
      alert("attempt to obtain image list length failed");
      alert(err);
    }
    
    // successful getRandomIndex assures we have a valid idx, now get image URL
    imageURL = this.imageList[this.i];
    
    return imageURL;
  }
 

  this.pushNextImage = function() {
    if (soundPlayer.playState) {
      if (!soundPlayer.paused) {
        parent.setBackground(self.getNextImageURL());
      }
    }
  }
}

var imageRandomizer = new ImageRandomizer();
