//  JavaScript Document

//  homeZoneSlector (2010-03-17)

//  typical using of this component
//  var homeZoneSlector = new homeZoneSlector();

function homeZoneSlector() {
  var self = this;
  this.containerID = 'home-zoneselector';
  this.containerElem = null;
  this.imgElem = null;
  this.imgArr = Array();
  this.imgCount = 0;
  this.loadedCount = 0;
  this.animSpeed = 500;
  this.autoplayAnimSpeed = 4000;
  this.autoplayTimerID = null;
  this.autoplayInterval = 8000;
  this.autoplayRunning = false;
  this.actualImgPosition = 0;
  //
  this.init = function(formID) {
    self.containerElem = document.getElementById(self.containerID);
    if (!self.containerElem) return false;
    dojo.addClass(dojo.query('div',self.containerElem)[0],'js');
    var tempImgArr = dojo.query('img',self.containerElem);
    self.imgCount = tempImgArr.length;
    dojo.forEach(tempImgArr,self.makeItem);
    return true;
  };
  //
  this.makeItem = function(node,index) {
    var newImg = document.createElement('img');
    var newSrc = node.src;
    newSrc = newSrc.replace('button','selector');
    newSrc = newSrc.replace('.gif','.jpg');
    self.imgArr[index] = newImg;
    newImg.onload = self.imageOnLoad;
    newImg.src = newSrc;
    self.containerElem.insertBefore(newImg,self.containerElem.firstChild);
    node.homeZoneSlector = new Object();
    node.homeZoneSlector.position = index;
    dojo.connect(node,'onmouseover',self.setActive);
    dojo.connect(node,'onmouseout',self.stopActive);
  };
  //
  this.imageOnLoad = function() {
    self.loadedCount++;
    if (self.loadedCount==self.imgCount) {
      self.autoplayTimerID = setInterval(self.playAnim,self.autoplayInterval);
      self.autoplayRunning = true;
    }
  };
  //
  this.playAnim = function() {
    var actualSpeed = self.animSpeed;
    if (self.autoplayRunning) {
      self.actualImgPosition = ((self.actualImgPosition>=self.imgCount-1)?(0):(self.actualImgPosition+1));
      actualSpeed = self.autoplayAnimSpeed;
    }
    dojo.forEach(self.imgArr,function(node,index){
      if (index==self.actualImgPosition) {
        dojo.animateProperty({node:node,properties:{opacity:1},duration:actualSpeed}).play();
      } else {
        dojo.animateProperty({node:node,properties:{opacity:0},duration:actualSpeed}).play();
      }
    });
  };
  //
  this.setActive = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    clearTimeout(self.autoplayTimerID);
    self.actualImgPosition = thisElem.homeZoneSlector.position;
    self.autoplayRunning = false;
    self.playAnim();
  };
  //
  this.stopActive = function(e,thisElem) {
    if (!thisElem) thisElem = this;
    self.autoplayRunning = true;
    self.autoplayTimerID = setInterval(self.playAnim,self.autoplayInterval);
  };
  //
  return self.init();
}

//  end of document