﻿
function RolloversByClassNameAndType( className, type )
{
    var itemSrc = '';
    
    var els = document.getElementsByTagName(type); //Not supported by safari
    
    for ( var i = 0, j = els.length; i < j ; i++ )
    {
        if ( els.item( i ).className.indexOf( className ) != -1 )
        {
            itemSrc = els.item( i ).src;
            //These expando properties are used to store values against the element as the loop runs
            
            els[ i ].srcOn = itemSrc.substring( 0,itemSrc.length - 4 ) + '_on' + itemSrc.substring( itemSrc.length, itemSrc.length - 4 );
            els[ i ].srcOff = itemSrc
            
            els.item( i ).onmouseover = function()
            {
                this.src = this.srcOn;
            }
            
            els.item( i ).onmouseout = function()
            {
                this.src = this.srcOff;
            }
        }
    }
     
}


function imageOn() 
{	
    this.src = this.src.replace( ".gif", "_on.gif" );
}


function imageOff() 
{
    this.src = this.src.replace( "_on.gif", ".gif" );
}


function attachEventHandlers()
{    
	RolloversByClassNameAndType( "swapbutton", "img" );	
	RolloversByClassNameAndType( "swapbutton", "input" );
	
	/* ID Specific rollovers */
    
    var submitbutton = document.getElementById( "submitbtn" );
    if ( submitbutton )
    {
		addEvent( submitbutton, "mouseover", imageOn );
		addEvent( submitbutton, "mouseout", imageOff );
    }
    
    var searchbtn = document.getElementById( "searchbtn" );
    if ( searchbtn )
    {
		addEvent( searchbtn, "mouseover", imageOn );
		addEvent( searchbtn, "mouseout", imageOff );
    }    
}


function addEvent( obj, type, fn )
{
	if ( obj )
	{
		if ( obj.addEventListener )
			obj.addEventListener( type, fn, false );
		else if ( obj.attachEvent )
		{
			obj[ "e" + type + fn ] = fn;
			obj[ type + fn ] = function() { obj[ "e" + type + fn ] ( window.event ); }
			obj.attachEvent( "on" + type, obj[ type + fn ] );
		}
	}
}


function removeEvent( obj, type, fn )
{
	if ( obj.removeEventListener )
		obj.removeEventListener( type, fn, false );
	else if ( obj.detachEvent )
	{
		obj.detachEvent( "on"+type, obj[ type + fn ] );
		obj[ type + fn ] = null;
		obj[ "e" + type + fn ] = null;
	}
}


// Load in all the event on body load
addEvent( window, "load", attachEventHandlers );
