function _JLJS_styleSwitcher_() {
	this.styleDir = "/en/common_rn/css";
	this.styleFile = "UDfont_";
	this.id = "styleSwitcher_";
	this.idSuffixes = [ "S", "M", "L" ];
	this.cookieKey = "UDfontSize";
	this.size = null;
	this.linkNode = null;
	this.fileSuffixPtn   = /\.(jpe?g|gif|png)$/i;
	this.statusSuffixPtn = /_[nso]$/;
	this.statusSuffix1 = "_n";
	this.statusSuffix2 = "_s";
	this.isNN = ( JLJS.env.isGecko && JLJS.env.ua.match( /Netscape/ ));
	this.isNN_isMacIE = ( this.isNN || ( JLJS.env.isMac && JLJS.env.isIE )) ? 1 : 0;
	this.isSafari = JLJS.env.isSafari;
}

_JLJS_styleSwitcher_.prototype  =  {
	setStyle : function() {
		if( this.isNN_isMacIE ) {
			this.size = this.getValueFromCookie( this.cookieKey ) ? this.getValueFromCookie( this.cookieKey ) : "M";
			document.write( '<link rel="stylesheet" type="text/css" href="' + this.styleDir + '/' + this.styleFile + this.size + '.css">' );
		}

		else {
			this.size = ( typeof size != "undefined" ) ? size :
				this.getValueFromCookie( this.cookieKey ) ? this.getValueFromCookie( this.cookieKey ) : "M";

			if( this.linkNode ) this.linkNode.parentNode.removeChild( this.linkNode );

			this.linkNode = document.createElement( "link" );
			this.linkNode.rel = "stylesheet";
			this.linkNode.type = "text/css";
			this.linkNode.href = this.styleDir + "/" + this.styleFile + this.size + ".css";
			document.getElementsByTagName( "head" )[0].appendChild( this.linkNode );
		}
	},

	buttonEventSetup : function() {
		JLJS.addEvent( document.getElementById( this.id + "S" ), "click", function( e ) { e.preventDefault(); JLJS_styleSwitcher.click( "S" ); } );
		JLJS.addEvent( document.getElementById( this.id + "M" ), "click", function( e ) { e.preventDefault(); JLJS_styleSwitcher.click( "M" ); } );
		JLJS.addEvent( document.getElementById( this.id + "L" ), "click", function( e ) { e.preventDefault(); JLJS_styleSwitcher.click( "L" ); } );
	},

	switchButtonImage : function() {
		for ( var i = 0; i < this.idSuffixes.length; i ++ ) {
			var statusSuffix = ( this.size == this.idSuffixes[i] ) ? this.statusSuffix2 : this.statusSuffix1;

			var imgNode = document.getElementById( this.id + this.idSuffixes[i] ).childNodes[0];
			var src = imgNode.src;
			var fSuffix = src ? src.match( this.fileSuffixPtn )[0] : null;
			var fStatus = fSuffix ? src.replace( this.fileSuffixPtn, '' ).match( this.statusSuffixPtn ) : null;
			var fRemain = fStatus ? src.replace( this.fileSuffixPtn, '' ).replace( this.statusSuffixPtn, '' ) : null;
			if( fRemain && fStatus && fSuffix ) imgNode.src = fRemain + statusSuffix + fSuffix;
		}
	},

	click : function( size ) {
		this.setCookie( size );
		if( this.isNN_isMacIE )
			location.reload();
		else {
			this.setStyle();
			this.switchButtonImage();
		}
	},

	setCookie : function( size ) {
		var domain = document.domain;
//		var domain = "jal.co.jp";
		var path = "/";
//		var expires = "";

		var value = this.cookieKey + "=" + size + ";";
		value += !this.isSafari ? "domain=" + domain + ";" : "";
		value += !this.isSafari ? "path=" + path + ";" : "";
//		value += "expires=" + expires + ";";
		document.cookie = value;
	},

	getValueFromCookie : function( cookieKey ) {
		var data = ( document.cookie ) ? document.cookie.split( ';' ) : [];
		var value;
		for( var i in data )
			if( data[i].split( '=' )[0].replace( /\s/g, '' ) == cookieKey )
				if( value = data[i].split( '=' )[1] )
					return unescape( value ).replace( /\s/g, '' );
	}
};

JLJS_styleSwitcher = new _JLJS_styleSwitcher_;
JLJS_styleSwitcher.setStyle();

JLJS.addOnload( function() {
	JLJS_styleSwitcher.buttonEventSetup();
	JLJS_styleSwitcher.switchButtonImage();
} );

