/***  (C)Stephen Chalmers 

SmartRoll.

Pre-settable Locking Rollover Buttons with 2 or 3-State Operation, and Optional Captioning.

 Info: http://scripterlative.com

***/

var SmartRoll=/*1*/
{
 imgArray:[], logged:0,
 
 showImage:function(holder, obj, img, restore)
 {
  var arr=this.imgArray[obj][img]; 
  
  if( !arr.locked /*&& ( !restore || !arr.triState) */)
   holder.src=(restore?arr.defSrc:arr.src);
   
  if( this.imgArray[obj].captionHolder && arr.caption )
   this.imgArray[obj].captionHolder.childNodes[0].data=restore ? this.imgArray[obj].defCaption : arr.caption;
 },

 over:function()
 {
  if(document.images)  
  {
   var objName=arguments[0], focusElem=null;  
   this.imgArray[ objName ]=[];
      
   for(var i=1,img; i<arguments.length && (img=(document.images[arguments[i]] || document.getElementById(arguments[i]))); i+=2)
   {
    this.imgArray[ objName ][ arguments[i] ]=new Image();
    this.imgArray[ objName ][ arguments[i] ].defSrc=img.src;
    this.imgArray[ objName ][ arguments[i] ].src=arguments[i+1];
    this.imgArray[ objName ][ arguments[i] ].locked=false;

    if(!!img.parentNode && /a/i.test(img.parentNode.tagName) && img.parentNode.childNodes.length==1)
     focusElem=img.parentNode;  
        
    this.addToHandler(img, 'onmouseover',new Function("if(typeof SmartRoll!='undefined')SmartRoll.showImage(document.images['"+arguments[i]+"'], '"+objName+"','"+arguments[i]+"',false)"));
    
    this.addToHandler(img, 'onmouseout',new Function("if(typeof SmartRoll!='undefined')SmartRoll.showImage(document.images['"+arguments[i]+"'], '"+objName+"','"+arguments[i]+"', true)"));
    
    this.addToHandler(img, 'onclick', new Function("if(typeof SmartRoll!='undefined'){SmartRoll.lock('"+objName+"','"+arguments[i]+"');this.src=SmartRoll.imgArray['"+objName+"']['"+arguments[i]+"'].src;}"));   
    
    if(focusElem)
    {
     this.addToHandler(focusElem, 'onclick', img.onclick);    
     this.addToHandler(focusElem, 'onfocus', img.onmouseover);
     this.addToHandler(focusElem, 'onblur',  img.onmouseout);
    }
   }
   
   if(!this.logged++)
    this.addToHandler(window,'onload',function(){setTimeout(SmartRoll.cont,3000)});
   
   if(i!=arguments.length)
    alert("Image with Name/ID: '"+arguments[i]+"', not found.");
   else
    this.presetButton();
  }
 },
  
 overTriState:function()
 {
  if(document.images)  
  {
   var objName=arguments[0], focusElem=null;
   this.imgArray[ objName ]=[]; 
   
   for(var i=1,img; i<arguments.length && (img=(document.images[arguments[i]]||document.getElementById(arguments[i]))); i+=3)
   {
    this.imgArray[ objName ][ arguments[i] ]=new Image();
    this.imgArray[ objName ][ arguments[i] ].defSrc=img.src;
    this.imgArray[ objName ][ arguments[i] ].triState=true;
    this.imgArray[ objName ][ arguments[i] ].src=arguments[i+1];
    this.imgArray[ objName ][ arguments[i] ].locked=false;
    this.imgArray[ objName ][ arguments[i]+'Active' ]=new Image();
    this.imgArray[ objName ][ arguments[i]+'Active' ].src=arguments[ i+2 ];
    
    if(!!img.parentNode && /a/i.test(img.parentNode.tagName) && img.parentNode.childNodes.length==1)
     focusElem=img.parentNode;  

    this.addToHandler(img, 'onmouseover',new Function("if(typeof SmartRoll!='undefined')SmartRoll.showImage(document.images['"+arguments[i]+"'], '"+objName+"','"+arguments[i]+"',false)"));

    this.addToHandler(img, 'onmouseout',new Function("if(typeof SmartRoll!='undefined')SmartRoll.showImage(document.images['"+arguments[i]+"'], '"+objName+"','"+arguments[i]+"', true)"));

    this.addToHandler(img, 'onclick', new Function("if(typeof SmartRoll!='undefined'){var theElem=this;if(this.firstChild)theElem=this.firstChild;SmartRoll.lock('"+objName+"','"+arguments[i]+"');theElem.src=SmartRoll.imgArray['"+objName+"']['"+arguments[i]+"'+'Active'].src;}"));
    
    if(focusElem)
    {
     this.addToHandler(focusElem, 'onclick', img.onclick);    
     this.addToHandler(focusElem, 'onfocus', img.onmouseover);
     this.addToHandler(focusElem, 'onblur',  img.onmouseout);
    }
    
   }
   
   if(!this.logged++)
    this.addToHandler(window,'onload',function(){setTimeout(SmartRoll.cont,5000)});
   
   if(i!=arguments.length)
    alert("Image with Name/ID: '"+arguments[i]+"', not found.");
   else
    this.presetButton();
  }
 
 }, 

 setCaptions:function()
 {
  var objName=arguments[0], cn;
  
  this.imgArray[objName].captionHolder=document.getElementById?document.getElementById(arguments[1]):null;
  
  if(this.imgArray[objName].captionHolder)
  {
   if(this.imgArray[objName].captionHolder.childNodes.length==0)
    this.imgArray[objName].captionHolder.appendChild(document.createTextNode('\xA0'));//HARD SPACE #160 
     
   cn=this.imgArray[objName].captionHolder.childNodes;
        
   cn[0].data = this.imgArray[objName].defCaption=arguments[2]!=''?arguments[2]:cn.length?cn[0].data:"";
    
   for(var i=3; i<arguments.length; i+=2)
    this.imgArray[objName][arguments[i]].caption=arguments[i+1];
  }
 },
 
 lock:function(objName,elem)
 {
  for( var i in this.imgArray[objName] )
   if( i!=elem && this.imgArray[objName][i].locked )
   {
    this.imgArray[objName][i].locked=false;
    document.images[i].src=this.imgArray[objName][i].defSrc;
   }
  this.imgArray[objName][elem].locked=true;
 },

 presetButton:function() // Do not call
 {
  var searchData,  rxp=/(SmartRoll)=(\w+)/ig;
    
  if(window.location.search && window.location.search.length)
   while( (searchData=rxp.exec(window.location.search)) ) 
    this.setButton( searchData[2] );
 },
  
 setButton:function(imgName)
 {
  var di;  
  
  if( (di=document.images[imgName]||document.getElementById(imgName))!=undefined )
  {  
   if(typeof di.onmouseover=='function' && typeof di.onclick=='function')
   {
    di.onmouseover();
    di.onclick();
   }
  }
 },
 
 addToHandler:function(obj, evt, func)
 {
  if(obj[evt])
   {
    obj[evt]=function(f,g)
    {
     return function()
     {
      f.apply(this,arguments);
      return g.apply(this,arguments);
     };
    }(func, obj[evt]);
   }
   else
    obj[evt]=func;
 }
}
