/* -----------------------------------------------------
	ロールオーバー画像変更
----------------------------------------------------- */
imgOn = function() {
	this.setAttribute( 'src', this.getAttribute( 'src' ).replace( '_off.', '_on.' ) );
}
imgOff = function() {
	this.setAttribute( 'src', this.getAttribute( 'src' ).replace( '_on.', '_off.' ) );
}
/* -----------------------------------------------------
	フォントサイズ変更ボタンの変更
----------------------------------------------------- */
function changeFontSizeBtn( name ) {
	var images = document.getElementById( 'menuFont' ).getElementsByTagName( 'img' );
	var len = images.length;
	for ( var i=0; i < len; i++ ) {
		var img = images[i];
		if ( img.getAttribute( 'src' ).split( '/font_' )[1].split( '_' )[0] == name ) {
			img.btnOnMouseOut = imgOn;
			img.setAttribute( 'src', img.getAttribute( 'src' ).replace( '_off.', '_on.' ) );
		} else {
			img.btnOnMouseOut = imgOff;
			img.setAttribute( 'src', img.getAttribute( 'src' ).replace( '_on.', '_off.' ) );
		}
	}
}
/* -----------------------------------------------------
	初期設定: ロールオーバー画像変更
----------------------------------------------------- */
function setRollOver() {
	var images = document.getElementsByTagName( 'img' );
	var len = images.length;
	for ( var i=0; i < len; i++ ) {
		var img = images[i];
		img.btnOnMouseOver = imgOn;
		if ( img.getAttribute( 'src' ).match( '_off.' ) ) {
		addEvent( img, 'mouseover', 'btnOnMouseOver' );
		addEvent( img, 'mouseout', 'btnOnMouseOut' );
			img.btnOnMouseOut = imgOff;
		} else if ( img.getAttribute( 'src' ).match( '_on.' ) ) {
		addEvent( img, 'mouseover', 'btnOnMouseOver' );
		addEvent( img, 'mouseout', 'btnOnMouseOut' );
			img.btnOnMouseOut = imgOn;
		}
	}
}
addEvent( window, 'load', 'setRollOver' );
/* -----------------------------------------------------
	初期設定: フォントサイズ変更
----------------------------------------------------- */
function setFontSizeSelector() {
	var images = document.getElementById( 'menuFont' ).getElementsByTagName( 'img' );
	var len = images.length;
	for ( var i=0; i < len; i++ ) {
		var img = images[i];
		img.changeFont = function() {
			var src = this.getAttribute( 'src' );
			var sheet = src.split( '/font_' )[1].split( '_' )[0];
			setActiveStyleSheet('font_' + sheet );
			changeFontSizeBtn( sheet );
		}
		addEvent( img, 'click', 'changeFont' );
	}
}
addEvent( window, 'load', 'setFontSizeSelector' );
/* -----------------------------------------------------
	初期設定: フォントサイズをクッキーの値に変更
----------------------------------------------------- */
function initFontSize() {
	var cookie = readCookie("style");
	var title = cookie ? cookie : 'font_normal';
	setActiveStyleSheet(title);
	var s = title.replace( 'font_', '' );
	changeFontSizeBtn( s );
}
addEvent( window, 'load', 'initFontSize' );
/* -----------------------------------------------------
	クッキーの値を読み込む
----------------------------------------------------- */
function getCookie( key ) {
	var i, index, arr;
	arr = document.cookie.split( ';' );
	for ( i = 0; i < arr.length; i++ ) {
		index = arr[i].indexOf( '=' );
		if ( arr[i].substring( 0, index ) == key || arr[i].substring( 0, index ) == ' ' + key )
			return arr[i].substring( index + 1 );
	}
	return '';
}
/* -----------------------------------------------------
	イベント追加
----------------------------------------------------- */
function addEvent( target, eventName, handlerName ) {
	if ( target.attachEvent ) {
		target.attachEvent( 'on' + eventName, function(e){ target[handlerName](e); } );
	} else if ( target.addEventListener ) {
		target.addEventListener(eventName, function(e){ target[handlerName](e); }, false );
	} else {
		var originalHandler = target['on' + eventName];
		if ( originalHandler ) {
			target['on' + eventName] = function(e){ originalHandler(e); target[handlerName](e); };
		} else {
			target['on' + eventName] = target[handlerName];
		}
	}
}
/* -----------------------------------------------------
	戻るボタン遷移先セット
----------------------------------------------------- */
function setReturnURL() {
	var url = location.href;
	if (url.match(/[^;\/?:@&=+\$,A-Za-z0-9\-_.!~*\'\(\)%#]/)) return false;
	var r = new RegExp('^https?:\/\/' + window.location.hostname + '\/');
	if (! url.match(r)) return false;
	var d = new Date();
	d.setTime(d.getTime() + 1000*60*60*12);
	var e = d.toGMTString();
	document.cookie = 'return_url=' + encodeURI(url) + '; expires=' + e + '; path=/';
}
