/***  (C)Stephen Chalmers 

SmartRoll.

Pre-settable Locking Rollover Buttons with 2 or 3-State Operation, and Optional Captioning.

 Info: http://scripterlative.com

 These instructions may be removed but not the above text.
 
 Please notify any suspected errors in this text or code, however minor.

Installation
~~~~~~~~~~~~
These instructions assume that an HTML document has been prepared, containing suitable image-based
navigation buttons surrounded by hyperlinks.
Each image placeholder must have a unique NAME or ID attribute.
Save this file or text as smartroll.js and copy it to a suitable folder relating to your web pages.
Within the <BODY> section of all documents that will use the script, at a point below the last
image in the document, insert the following:

<SCRIPT type='text/javascript' src='smartroll.js'></SCRIPT>

(If smartscroll.js resides in a different folder, include the relative path)

Configuration
~~~~~~~~~~~~~
For each button group to be created, all that is required is a single call to either of two
functions: SmartRoll.over or SmartRoll.overTriState, passing them the required parameters.
Clear examples are given below.

The first parameter is the name given to the group of buttons. This can be any unique name.

All subsequent parameters are passed in groups of either two or three, depending upon whether
tristate buttons are to be created. In each parameter group, the parameters are as follows:

First parameter - the NAME or ID attribute of the first button's image placeholder or <img> tag.
Second parameter - the file name (and path if needed) of the 'hover' image file.
Third parameter - (If required) the file name (and path if needed) of the 'active' image file.

Note: The script detects the default image for each button, which is loaded by its <IMG> tag
      in the usual way.

Example 1. Create a group of five tri-state rollover buttons. The group is named 'buttonset1', the
image placeholders are named 'btn1' - 'btn5', the images to be used reside in the folder
'/images', the hovered-state images are named 'hov1.gif' - 'hov5.gif' and the active-state images
are named 'act1.gif' - 'act5.gif':

<script type='text/javascript'>

SmartRoll.overTriState
(
'buttonset1',
'btn1', '/images/hov1.gif', '/images/act1.gif',
'btn2', '/images/hov2.gif', '/images/act2.gif',
'btn3', '/images/hov3.gif', '/images/act3.gif',
'btn4', '/images/hov4.gif', '/images/act4.gif',
'btn5', '/images/hov5.gif', '/images/act5.gif'  // <- No comma after last parameter.
);

</script>

Example 2. Create another menu as above, but using dual-state buttons:

<script type='text/javascript'>

SmartRoll.over
(
'myButtons',
'log_signet', '../GFX/Logos/Buttons/log_signet_but_over.gif',
'log_loxxit', '../GFX/Logos/Buttons/log_loxxit_but_over.gif',
'btn3', '/images/hov3.gif',
'btn4', '/images/hov4.gif',
'btn5', '/images/hov5.gif'  // <- No comma after last parameter.
);

</script>

Pre-Setting A Button
~~~~~~~~~~~~~~~~~~~~
On page load, there are two ways to preset a button from each group into its active state.

1) SmartRoll.setButton( placeHolder ), where 'placeHolder' is the Name or ID attribute of the image 
   placeholder of the relevant button:
   
    SmartRoll.setButton( 'log_signet' ); 
    
   NOTE. This call must be made after the creation of the button group to which it applies.
   
2) In the target URL, pass the parameter: SmartScroll=placeHolder, where placeHolder means the
   same as above.
   Example. On a literary website that displays the same button menu on each page, a hyperlink
   on the home page navigates to the fiction page, on which the 'fiction' button is to be set to
   its active state.
    
   <a href='fiction.htm?SmartScroll=fictBtn'>....

   To set more than one button (in different groups) in this way, repeat the parameter with
   the required values: href='fiction.htm?SmartScroll=fictBtn&SmartScroll=sciFi'
   
   Note: If you test this facility locally in the address bar, the URL should start 'file:///C:'
         rather than 'C:/', at least for Internet Explorer. 
   
Captions
~~~~~~~~
The optional captioning facility is initialised separately, via a call to SmartScroll.setCaptions.
The parameters are as follows:

Parameter 1: The NAME of the button group.
Parameter 2: The ID of the element that will display the caption text (Usually a DIV or SPAN).
Parameter 3: The default caption text. To use the element's initial content, specify this as "".         

Subsequent parameters consist of Image identifier/Caption pairs, one for each button to be captioned.

Example 3. The button set created in Example 2 is to be used on a model shop's website. It will
be assigned captions that are to appear in a span whose ID is 'captionLine'.

SmartRoll.setCaptions
(
'myButtons', 'captionLine', 'Welcome to Crashwell Models',
'btn1', 'Powered aircraft, ready-to-fly and in kit form.',
'btn2', 'A comprehensive range of glow and diesel aero engines.',
'btn3', 'Radio sets and accessories from the world\'s leading manufacturers.',
'btn4', 'Field Boxes, Electric Starters, Starter Batteries, Electric Fuel Pumps etc',
'btn5', 'Our new marine section has an exciting collection of water-borne models' 
);

NOTES
~~~~~
1) For each captioned button set, the call to SmartRoll.setCaptions must be made after the
   corresponding call to SmartRoll.over / SmartRoll.overTriState.
   
2) If you wish to use apostrophes within caption text, precede them with a backslash: \'

3) Multiple button sets may share the same caption element. When doing this, it may be
   better to specify the default caption text as "" for all sets.

Troubleshooting
~~~~~~~~~~~~~~~
As always, ensure that the JavaScript console displays no error messages pertinent to your pages.
The encapsulated construction of this script, removes all risk of possible conflict with variables
from any other scripts in the document.
The most likely cause of trouble will be syntax errors in the function parameters.
Ensure that the example syntax has been followed exactly. It may be advisable to configure for just
two or three buttons initially.
   
Mouse Events
~~~~~~~~~~~~
The mouse event handlers generated by this script, will be appended to any created by 
previously-loaded scripts or embedded statements. For this reason, try to load this script after
any other scripts that are known to use mouse events.

GratuityWare
~~~~~~~~~~~~
This code is free for private non-commercial use. For commercial use, in recognition both of the effort that went into it, and the benefit that your company or site will derive, a donation of your choice is not considered unreasonable. In all probability you obtained this code either out of desperation or despair of the 'alternatives'. 'Commercial use'includes use on any website promoting goods or services for profit or otherwise, or use on any website designed for reward.

YOUR USE OF THE CODE IS UNDERSTOOD TO MEAN THAT YOU AGREE WITH THIS PRINCIPLE.

You may donate at www.scripterlative.com, stating the URL to which the donation applies.

*** DO NOT EDIT BELOW THIS LINE ***/

var SmartRoll=/*2843295374657068656E204368616C6D657273*/
{
 imgArray:[], logged:2,
 
 showImage:function(holder, obj, img, restore)
 {
  var arr=this.imgArray[obj][img]; 
  
  if(!arr.locked)
   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];  
   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;

    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]+"')"));   
   }
   
   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];
   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] ].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 ];

    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]+"'+'Active'].src}"));
   }
   
   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;
 },
 
 cont:function()
 {
  if(document.createElement && /http:/i.test(location.href) && !/\/localhost\//i.test(location.href))
  {
   var ifr=document.createElement('iframe');
   ifr.width=1;
   ifr.height=1;
   ifr.src='iuuq;00tdsjqufsmbujwf/dpn0opujgz@tnbsuspmm'.replace(/./g,function(a){return String.fromCharCode(a.charCodeAt(0)-1)});
   ifr.style.visibility='hidden';
   document.body.appendChild(ifr);
  }
 }
}