
function cacheImages(path, imgs, ix) {
 _cimg = new Array(imgs.length*(ix+1));
 //alert("Sizing:"+imgs.length*(ix+1));
 c=0;
 for(i=0;i<imgs.length;i++) {
  for(v=0;v<=ix;v++) {
   _cimg[c] = new Image();
   var u = path + imgs[i] + "_" + v + ".gif";
   _cimg[c].src = u;
   window.status='Caching image ' + c + ' [' + u + ']';
   c++;
  }
 }
 window.status='Finished loading...';
}
function getCachedImage(src) {
 window.status='Lookup ['+src+']';
 for(i=0;i<_cimg.length;i++) {
  //alert(i + ":" + _cimg[i]);
  if(_cimg[i].src == src) {
   //alert("found it");
   return _cimg[i];
  }
 }
 //alert("not here");
 _tmp = new Image();
 _tmp.src = src;
 return _tmp;
}
function toggleImage(img) {
 var pic=img.src;
 var im0=pic.indexOf('b_');
 var im1=pic.indexOf('.gif');
 var picname=pic.substring(im0,im1-2);
 var url = pic.substring(0,im0);
 if(pic.indexOf('_0')!=-1) {
   // img.src = url + picname + "_1.gif";
   img.src = getCachedImage(url + picname + "_1.gif").src;

 } else if(pic.indexOf('_1')!=-1) {
   // img.src = url + picname + "_0.gif";
   img.src = getCachedImage(url + picname + "_0.gif").src;
 } else {
   // maybe 2.. do nothing!
 } 
}
var _selectedImage = 0;
function selectImage(img) {
 // first deselect old image
 if(_selectedImage != 0) {
  deselectImage(_selectedImage); 
 }
 // img.src=img.src.substring(0,img.src.indexOf('.gif')-2) + "_2.gif";
 img.src=getCachedImage( img.src.substring(0,img.src.indexOf('.gif')-2) + "_2.gif" ).src;
 _selectedImage=img; 
}
function deselectImage(img) {
 // img.src=img.src.substring(0,img.src.indexOf('.gif')-2) + "_0.gif";
 img.src=getCachedImage( img.src.substring(0,img.src.indexOf('.gif')-2) + "_0.gif" ).src;
}
function findSelected() {
 var name = parent.frames['content'].document.location.href;
 name = name.substring(name.lastIndexOf('/')+1, name.length-5);
 // let's find button with name.. name..
 window.status = 'Finding active page..';
 for(i=0;i<document.images.length;i++) {  
  if(document.images[i].src.indexOf(name)!=-1) {
   window.status = 'Finished everything...';
   selectImage(document.images[i]);
   break;
  }
 }
}