/* class : UaRollover */
function UaRollover(){}

UaRollover.prototype =
{
	/* method : initialize */
	initialize : function( clses )
	{
		this.storedImg = new Array();
		var elems      = new Array();
		var targetImgLength   = this.getElementsByClassName( "img", clses ).length;
		var targetInputLength = this.getElementsByClassName( "input", clses ).length;

		for( var k = 0; k < targetImgLength; k++ )
		{
			elems = elems.concat( this.getElementsByClassName( "img", clses )[k] );
		}

		for( var j = 0; j < targetInputLength; j++ )
		{
			elems = elems.concat( this.getElementsByClassName( "input", clses )[j] );
		}

		var targetElementLength = elems.length;

		for( var i = 0; i < targetElementLength; i++ )
		{
			var elem   = elems[i];
			var imgSrc = elem.src;

			if( imgSrc.indexOf( "_cur." ) > -1 )
			{
				continue;
			}

			var ovrStr                 = this.imgOvrReplace( imgSrc, true );
			this.storedImg[imgSrc]     = new Image();
			this.storedImg[imgSrc].src = ovrStr;
			this.targetElement = elem;
			this.defaultImgSrc = imgSrc;
			this.storedImgSrc  = ovrStr;
			this.setMouseEvent();
		}
	},


	/* method : getElementsByClassName */
	getElementsByClassName : function( pTagName, pClassName )
	{
		var res  = new Array();
		var tags = document.getElementsByTagName( pTagName );
		var len  = tags.length;
		for( var i = 0; i < len; i++ )
		{
			var classes = tags[i].className;


			if( classes.indexOf( pClassName ) > -1 )
			{
				res[res.length] = tags[i];
			}
		}
		return res;
	},


	/* method : imgOvrReplace */

	imgOvrReplace:function( pSrc, pIsOvr )
	{
		var srcStr = pSrc;
		if( pIsOvr )
		{
			if( srcStr.indexOf( "_ovr" ) > -1 )
			{
				return srcStr;
			}


			srcStr = this.strReplace( srcStr, ".gif", "_ovr.gif" );
			srcStr = this.strReplace( srcStr, ".jpg", "_ovr.jpg" );
			srcStr = this.strReplace( srcStr, ".png", "_ovr.png" );
		}
		else
		{
			srcStr = this.strReplace( srcStr, "_ovr.gif", ".gif" );
			srcStr = this.strReplace( srcStr, "_ovr.jpg", ".jpg" );
			srcStr = this.strReplace( srcStr, "_ovr.png", ".png" );
		}
		return srcStr;
	},


	/* method : strReplace */
	strReplace:function( pStr, pKey, pAlt )
	{
		return pStr.split( pKey ).join( pAlt );
	},


	/* method : setMouseEvent */
	setMouseEvent : function()
	{
		var _targetElement = this.targetElement;
		var _defaultImgSrc = this.defaultImgSrc;
		var _storedImgSrc  = this.storedImgSrc;
		var _swapImage     = this.swapImage;

		function focusImage()
		{
			_swapImage( _targetElement, _storedImgSrc  )
		}

		function blurImage()
		{
			_swapImage( _targetElement, _defaultImgSrc  )
		}


		if( _targetElement.nodeName.toUpperCase() == 'INPUT' )
		{
			_targetElement.onmouseover = function(){ _swapImage( _targetElement, _storedImgSrc  ) };
			_targetElement.onmouseout  = function(){ _swapImage( _targetElement, _defaultImgSrc  ) };
		}
		else if( _targetElement.nodeName.toUpperCase() == 'IMG' )
		{
			this.parentAnchorElement = '';
			this.getParentAnchorElement( _targetElement );

			if( this.parentAnchorElement != '' )
			{
				if( this.parentAnchorElement.addEventListener )
				{
					this.parentAnchorElement.addEventListener( 'mouseover', focusImage, false );
					this.parentAnchorElement.addEventListener( 'mouseout', blurImage, false );
				}
				else if( this.parentAnchorElement.attachEvent )
				{
					this.parentAnchorElement.attachEvent( 'on'+'mouseover', focusImage );
					this.parentAnchorElement.attachEvent( 'on'+'mouseout', blurImage );
				}
				else
				{
					return false;
				}

			}
		}
	},


	/* method : swapImage */
	swapImage : function( targetElement, src )
	{
		targetElement.src = src;
	},

	getParentAnchorElement : function( element )
	{
		var tempElement = element;
		if( tempElement.parentNode.nodeName.toUpperCase() != 'BODY' )
		{
			if( tempElement.parentNode.nodeName.toUpperCase() == 'A' )
			{
				this.parentAnchorElement = tempElement.parentNode;
			}
			else
			{
				this.getParentAnchorElement( tempElement.parentNode );
			}
		}
	}
}


/* uaRollover : instance of UaRollover */

var uaRollover = new UaRollover();
uaRollover.initialize( 'rollover' );
