/* cunstructor */
function FontSize( configObject )
{
	this.configObject = configObject;
	this.init();
}

/* class value */
FontSize.initialized = false;

/* method : init */
FontSize.prototype.init = function()
{
	if( this.isCookieEnable() )
	{
		if( this.getCookieValue( this.configObject.cookieKey ) == '' )
		{
			this.setCookie( this.configObject.cookieKey, this.configObject.defaultSize, this.configObject.cookieExdays );
		}

		var value = this.getCookieValue( this.configObject.cookieKey );

		this.setHTMLCode( this.configObject.code );
		this.setFontClass( value );
		this.setCurrentSizeImage( value );

		FontSize.initialized = true;
	}
}



/* method : setHTMLCode */
FontSize.prototype.setHTMLCode = function( HTMLCode )
{
	document.write( HTMLCode );
}

/* method : setFontClass */
FontSize.prototype.setFontClass = function( value )
{
	this.configObject.targetElement.className = this.configObject.fontClass[value];
}

/* method : setCurrentSizeImage */
FontSize.prototype.setCurrentSizeImage = function( value )
{
	for( var i = 0; i < this.configObject.fontSize.length; i++ )
	{
		if( this.configObject.fontSize[i] == value )
		{
			this.setImage( this.configObject.imgStatus.strCurrent, this.configObject.fontSize[i] );
		}
		else
		{
			this.setImage( this.configObject.imgStatus.strInitialize, this.configObject.fontSize[i] );
		}

		if( FontSize.initialized == false )
		{
			this.setImage( this.configObject.imgStatus.strPreLoad, this.configObject.fontSize[i] );
		}
	}
}



/* method : setImage */
FontSize.prototype.setImage = function( status, value )
{
	var targetImg       = document.getElementById( this.configObject.imgId[value] );
	var path            = targetImg.src.substring( 0, targetImg.src.lastIndexOf( '/' ) + 1 );
	var fileName        = targetImg.src.substring( targetImg.src.lastIndexOf( '/' ) + 1 );

	var replaceImg      = new Image();
	var fileStr         = '';
	var extension       = fileName.substring( fileName.lastIndexOf( '.' ) );
	var replaceFileName = ''

	var isCurrent       = fileName.indexOf( this.configObject.suffixCurrent ) != -1;
	var isFocus         = fileName.indexOf( this.configObject.suffixFocus ) != -1;

	if( status == this.configObject.imgStatus.strInitialize )
	{
		if( isCurrent || isFocus )
		{
			if( isCurrent )
			{
				fileStr = fileName.substring( 0, fileName.indexOf( this.configObject.suffixCurrent ) );
			}
			else if( isFocus )
			{
				fileStr = fileName.substring( 0, fileName.indexOf( this.configObject.suffixFocus ) );
			}

			replaceFileName = fileStr + extension;
			replaceImg.src  = path + replaceFileName;
			targetImg.src   = replaceImg.src;
		}
	}
	else if( status == this.configObject.imgStatus.strCurrent )
	{
		if( !isCurrent )
		{
			if( isFocus )
			{
				fileStr = fileName.substring( 0, fileName.indexOf( this.configObject.suffixFocus ) );
			}
			else
			{
				fileStr = fileName.substring( 0, fileName.lastIndexOf( '.' ) );
			}

			replaceFileName = fileStr + this.configObject.suffixCurrent + extension;
			replaceImg.src = path + replaceFileName;
			targetImg.src  = replaceImg.src;
		}
	}
	else if( status == this.configObject.imgStatus.strFocus )
	{
		if( !isCurrent && !isFocus )
		{
			fileStr         = fileName.substring( 0, fileName.lastIndexOf( '.' ) );
			replaceFileName = fileStr + this.configObject.suffixFocus + extension;

			replaceImg.src = path + replaceFileName;
			targetImg.src  = replaceImg.src;
		}
	}
	else if( status == this.configObject.imgStatus.strDefault )
	{
		if( !isCurrent )
		{
			if( isFocus )
			{
				fileStr = fileName.substring( 0, fileName.indexOf( this.configObject.suffixFocus ) );
			}
			else
			{
				fileStr = fileName.substring( 0, fileName.lastIndexOf( '.' ) );
			}

			replaceFileName = fileStr + extension;
			replaceImg.src = path + replaceFileName;
			targetImg.src  = replaceImg.src;
		}
	}
	else if( status == this.configObject.imgStatus.strPreLoad )
	{
		if( isCurrent )
		{
			fileStr         = fileName.substring( 0, fileName.indexOf( this.configObject.suffixCurrent ) );
			replaceFileName = fileStr + this.configObject.suffixFocus + extension;
			replaceImg.src  = path + replaceFileName;
		}
		else if( isFocus )
		{
			fileStr         = fileName.substring( 0, fileName.indexOf( this.configObject.suffixFocus ) );
			replaceFileName = fileStr + this.configObject.suffixCurrent + extension;
			replaceImg.src  = path + replaceFileName;
		}
		else
		{
			fileStr         = fileName.substring( 0, fileName.lastIndexOf( '.' ) );
			replaceFileName = fileStr + this.configObject.suffixFocus + extension;
			replaceImg.src  = path + replaceFileName;

			replaceFileName = fileStr + this.configObject.suffixCurrent + extension;
			replaceImg.src  = path + replaceFileName;
		}
	}
}



/* method : isCookieEnable */
FontSize.prototype.isCookieEnable = function()
{
	var tKey   = 'tKey';
	var tValue = 'tValue';
	var exDate = 1;
	var result;

	document.cookie = this.setCookie( tKey, tValue, exDate );
	result          = ( document.cookie.indexOf( tKey + '=' + tValue ) != -1 ) ? true : false;
	document.cookie = this.setCookie( tKey, tValue, 0 );
	return result;
}

/* method : setCookie */
FontSize.prototype.setCookie = function( key, value, days )
{
	var cookieString;
	var exTime = new Date();
	exTime.setTime( exTime.getTime() + ( days * 1000 * 60 * 60 * 24 ) );
	var strExTime = exTime.toGMTString();
	cookieString = key + '=' + value + ';' + 'expires=' + strExTime + '; path=/';
	document.cookie = cookieString;
}

/* method : getCookieValue */
FontSize.prototype.getCookieValue = function( key )
{
	var result ='';
	var cookies = document.cookie.split( ';' );	

	for( var i = 0; i < cookies.length; i++ )
	{
		if( cookies[i].indexOf( key ) != -1 )
		{
			result = cookies[i].split( '=' )[1];
			break;
		}
	}

	return result;
}



/* method : changeAction */
FontSize.prototype.changeAction = function( value )
{
	this.setCookie( this.configObject.cookieKey, value, this.configObject.cookieExdays );
	this.setFontClass( value );
	this.setCurrentSizeImage( value );
}



/* excute */
var configObject01 =
{
	cookieKey     : 'fontSize',
	cookieExdays  : 365,
	fontSize      : [ 's', 'm', 'l' ],
	defaultSize   : 'm',
	fontClass     : { s : 'text11', m : 'text13', l : 'text15'  },
	imgId         : { s : 'btn-s', m : 'btn-m', l : 'btn-l' },
	imgStatus     : { strInitialize : 'initialize', strCurrent : 'current', strFocus : 'focus', strDefault : 'default', strPreLoad : 'preLoad' },
	suffixFocus   : '_ovr',
	suffixCurrent : '_cur',
	targetElement : document.getElementById( 'all' )
}

configObject01.code =
'<dl id="fontsize01" class="clearfix">' +
'<dt><img src="/images/label-fontsize01.gif" width="61" height="12" alt="文字サイズ" /></dt><!--' +
'--><dd><a onmouseover="fontSize01.setImage(\'focus\', \'s\')" onmouseout="fontSize01.setImage(\'default\', \'s\')" onclick="fontSize01.changeAction(\'s\');"><img src="/images/btn-fontsize-s01.gif" width="19" height="20" alt="小" id="btn-s" /></a></dd><!--' +
'--><dd><a onmouseover="fontSize01.setImage(\'focus\', \'m\')" onmouseout="fontSize01.setImage(\'default\', \'m\')" onclick="fontSize01.changeAction(\'m\');"><img src="/images/btn-fontsize-m01.gif" width="19" height="20" alt="中" id="btn-m" /></a></dd><!--' +
'--><dd><a onmouseover="fontSize01.setImage(\'focus\', \'l\')" onmouseout="fontSize01.setImage(\'default\', \'l\')" onclick="fontSize01.changeAction(\'l\');"><img src="/images/btn-fontsize-l01.gif" width="19" height="20" alt="大" id="btn-l" /></a></dd>' +
'</dl>';

var fontSize01 = new FontSize( configObject01 );

