var rotSys = {
    addEvent: function(obj,ev,fn,par) {
    if(!obj) return;
    var wrapper = function(e) {
      if(!e) e = window.event;
      var result = fn(obj,par,e);
      if(result===false) {
        if(event.preventDefault) event.preventDefault();
        event.returnValue = false;
      }
    }; 
  	if(obj.addEventListener) obj.addEventListener(ev, wrapper, false);
  	else if(obj.attachEvent) obj.attachEvent("on"+ev, wrapper);
  },
  href: [],
  pump: function(obj) {
    rotSys.href.push(obj);
    return rotSys.href.length-1;
  },
  getOp: function(obj) {
    return obj.style.opacity;
  },
  setOp: function(obj,val) {
    obj.style.opacity = val;
  },
  preload: function(o,p,e) {
    if(!images) return;
    if(!p) p = [-1,new Image()];
    p[0]++;
    if(!images[p[0]]) return;
    p[1].onload = function() { js.preload(null,p); };
    p[1].src = images[p[0]];
  }
}

function hash(obj) {
  var keys=[], pos=0, len=0;
  for(i in obj) { keys.push(i); len++; }
  this.next = function(key) {
    pos++;
    if(pos>=len) pos = 0;
    if(key) return keys[pos];
    return obj[keys[pos]];
  };
  this.prev = function(key) {
    pos--;
    if(pos<0) pos = len-1;
    if(key) return keys[pos];
    return obj[keys[pos]];
  };
  this.current = function(key) {
    if(key) return keys[pos];
    return obj[keys[pos]];
  }
}
  
var rotor = {
  preload: function(o,p,e) {
    var anchor = document.getElementById(p[0]);
    var imgs = document.getElementById(p[1]);
    var hsh = new hash(p[2]);
    var twins = [];
    for(i in imgs.childNodes) if(imgs.childNodes[i].nodeName=="IMG") twins.push(imgs.childNodes[i]);
    rotSys.setOp(twins[1],0);
    var func = "rotor.flip(%d,%d,%d,%d);".sprintf(
      rotSys.pump(anchor),
      rotSys.pump(twins[1]),
      rotSys.pump(twins[0]),
      rotSys.pump(hsh)
    );
    window.setTimeout(func,5000);
  },
  flip: function(phref,pimg1,pimg2,phsh) {
    rotor.fade(pimg1,pimg2,0,phsh,phref);
    window.setTimeout("rotor.flip(%d,%d,%d,%d)".sprintf(phref,pimg1,pimg2,phsh),5000);
  },
  fade: function(pimg1,pimg2,lvl,phsh,phref) {
    var img1 = rotSys.href[pimg1];
    var img2 = rotSys.href[pimg2];
    var hsh = rotSys.href[phsh];
    rotSys.setOp(img1,lvl/5);
    rotSys.setOp(img2,1-lvl/5);
    if(lvl!=5) window.setTimeout("rotor.fade(%d,%d,%d,%d,%d)".sprintf(pimg1,pimg2,lvl+1,phsh,phref),80);
    else {
      var hrf = rotSys.href[phref];
      var href1 = hsh.current(false);
      var src1 = hsh.next(true);
//      document.getElementById("info").innerHTML = href1;
      hrf.href = href1;
      rotSys.setOp(img1,0);
      rotSys.setOp(img2,1);
      img2.src = img1.src;
      img1.src = src1;
    }
  }
}

