/**
Rollover is a static class providing a function to automatically install rollover capabilities on 
arbitrary images in the page. The function is automatically run onload. It scans the page for images 
with a src which ends in _ro. it then installs the necessary mouseovers/mousouts to do
rollovers on the image. if you have an image with name ending _ro, you must have a corresponding image 
with name _omo. for example nice_ro.gif needs nice_omo.gif.

@usage: just include rollover.js in the header. functions.js is also required . if you had to make a fake functions.js
in an emergency to suit this you would at least need the event_install and ctad_error functions.
you would also need the settings.js file setting functions_debug. this script also requires
getElementsByTagName which the normal functions.js 'repairs' for older IE browsers

@author: jo frudd
@author: jon halle

$Id: rollover.js,v 1.3 2005/03/16 16:18:43 jonh Exp $
*/


function Rollover()
{
}


// Setting for time to wait before checking if preload worked (in ms)
Rollover.wait_for_preload=2000;


IMPLEMENT_GARBAGE(Rollover); 

/** 
  * function called by garbage collector to release all its
  * references. 
  */
Rollover.dispose = function() 
{ 
	Rollover.wait_for_preload = null;
}


	
/**
  * Installs rollovers on images which fit the naming scheme. 
  * Images have to have _ro in their file names. 
  */
Rollover.install= function(){

   var current_loc = location.href; 
	var imgs = document.getElementsByTagName('img');

	for (var i=0; i < imgs.length; i++)
	{
		// check if image fallows the naming scheme 
		if (imgs[i].src.match(/_ro\..*$/i))
		{
		
			var img=imgs[i];
         
         // make sure we do not install 
         // rollover state on selected tabs 
         
         var anchor = img.parentNode; 
        
         if (current_loc.match(anchor.getAttribute("href")) 
             && !anchor.getAttribute("href").match(/#/))  
          {
           img.src=img.src.replace(/_ro\./i,"_over.");
         } else { 
           // initialise rollover and normal states
           img.i_src=img.src;
           img.r_src=img.i_src.replace(/_ro\./i,"_over.");
           
           event_install(img,"onmouseover",Rollover.change_to_over);
           event_install(img,"onmouseout",Rollover.change_to_normal);
           
           Rollover.preload(img.i_src);
           Rollover.preload(img.r_src);
         }
		}
	}

	imgs = null; 
}

/** 
  * Changes image state to over state. 
  */
Rollover.change_to_over  = function() 
{ 
	this.src=this.r_src;
}

/** 
  * Changes image state to normal state. 
  * Restores image.
  */
Rollover.change_to_normal = function() 
{ 
	this.src=this.i_src; 
}

/**
  * Preload an arbitrary src
  */
Rollover.preload=function(src){
	
	var img = new Image();
	
	//test for success
	//img.onerror would be easiest but mozilla does not support it for local files
	event_install(img,"onload",Rollover.set_loaded);
	
	// not sure how much timeout leaks 
	window.setTimeout(function() { Rollover.check_loaded(img); },Rollover.wait_for_preload)	;
	
	img.src=src;
}


/** 
  * Sets image status to loaded 
  */
Rollover.set_loaded = function()
{ 
	this.loaded=true;
}


/**
  * Show developer error for any image which doesnt preload
  * @argument DOM element reference to image 
  */
Rollover.check_loaded=function(img){

	if (!img.loaded){
		ctad_error("Image "+img.src.split("/").pop()+" does not exist but rollover wants to preload it. Full path is:\n"+img.src);
	}

}

//install this onload
event_install(window,"onload",Rollover.install);

