var menu_id = "";
var list_id = "";


/**
 *	質問箱メニュー表示
 */
function show_menu( pos_type ) {
	menu_id = "qaSearch" + pos_type + "Menu";
	list_id = "qaSearch" + pos_type + "MenuList";

	//背景透明度
	opacity_popup_base( 0 );
	bgcolor_popup_base( "#FFFFFF" );

	//メニュー非表示トリガーの設定
	func_popup_base( "onfocus", "hide_menu" );
	func_popup_base( "onclick", "hide_menu" );
	func_popup_box( "onmouseout", "chk_hide_menu" );

	//表示位置計算
	var top = 0;
	var left = 0;

	//
	var base = document.getElementById( menu_id );
	if( base ) {
		top = getOffsetTop( base )
		//高さをプラス
		top += base.offsetHeight;

		//左位置は同じ
		left = getOffsetLeft( base );

		if( pos_type == "Footer" ) {
			top --;
			left += 2;
		}
	}

	var menu = document.getElementById( list_id );
	if( menu ) {
		//高さ取得の為、透明表示
		menu.style.filter = "Alpha(opacity=" + (0) + ")";
		menu.style.opacity = 0;
		menu.style.display = "block";

		//メニューの高さ取得
		var menu_height = menu.offsetHeight;

		//はみ出るようなら上側に出す
		if( top + menu_height - 1 > ( getScrollTop() + getWindowHeight() ) ) {
			top -= base.offsetHeight;

			top -= menu_height;
		}
	}

	display_popup( list_id, "block", top, left );
}

/**
 *
 */
function chk_hide_menu( e ) {
	var box = document.getElementById( list_id );

	var x = 0;
	var y = 0;
	if( window.event ) {
		x = window.event.clientX + getScrollLeft();
		y = window.event.clientY + getScrollTop();
	}
	else {
		x = e.pageX;
		y = e.pageY;
	}

	var box_top = getOffsetTop( box );
	var box_left = getOffsetLeft( box );
	var box_bottom = box_top + box.offsetHeight;
	var box_right = box_left + box.offsetWidth;

	box_top += 2;
	box_left += 2;
	box_bottom -= 2;
	box_right -= 2;

	//alert( "prof----\ntop:" + box_top + "\nleft:" + box_left + "\nbottom:" + box_bottom + "\nright:" + box_right + "\n\nx:" + x + "\ny:" + y );
	if( y < box_top || box_bottom < y || x < box_left || box_right < x ) {
		hide_menu();
	}
}

/**
 *	質問箱メニュー非表示
 */
function hide_menu() {
	display_popup( list_id, "none" );
}

/**
 *	初期化関数
 *		body.onloadに設定
 *		各ページに必要な初期化を行う
 */
function body_onload( page ) {
	switch( page ) {
	case "top":
		//++++++++++++++++++++++++++++++++++++++
		//TOPページ

		//表示質問の設定
		if( window.show_question ) {
			show_question();
		}

		break;
	case "result":
		//検索タイプの切り替え、検索キーワードの再設定
		restore_search_items();
		
		break;
	case "questionlist":
		//表示質問の設定
		var obj = document.getElementById("selecttab");
		if( window.show_question ) {
			if(obj != null) {
				var select_tab = obj.value;
				show_question(select_tab);
			}
		}
		break;
	case "member":
		//
		if( window.show_member ) {
			var obj = document.getElementById("selecttab");
			if(obj != null) {
				var select_tab = obj.value;
				show_member(select_tab);
			}
		}

		break;
	case "qq":
		//
		if( window.show_enquete ) {
			var obj = document.getElementById("selecttab");
			if(obj != null) {
				var select_tab = obj.value;
				show_enquete(select_tab);
			}
		}

		break;
	case "member2":
	case "qa":
	case "ranking":
		//
		if( window.show_ranking ) {
			var obj = document.getElementById("selectTab");
			if(obj != null) {
				var select_tab = obj.value;
				show_ranking(select_tab);
			}
		}

		break;
	default:
		break;
	}

	//++++++++++++++++++++++++++++++++++++++
	//全ページ共通

	//文字サイズ
	if( window.font_change ) {
		font_change( "" );
	}

	//透明背景作成
	if( window.init_popup_base ) {
		init_popup_base();
	}

	//検索タイプの設定
	if( window.set_search_type ) {
		if( document.getElementById('searchType')) {
			set_search_type("", document.getElementById('searchType').value);
			set_search_type("Footer", document.getElementById('searchType').value);
		} else {
			set_search_type("", "");
			set_search_type("Footer", "");
		}
	}
}

/**
 *	文字サイズ変更用初期設定
 */
//初期サイズ
var defaultSize = 90;
//クッキーの名前
var cookie_name = "qa_fontchange";
//クッキー有効期限(日)
var cookie_limit = 2;
//クッキーパス
var cookie_path = "/";

/**
 *	ヘッダー内にて出力する為、onloadではない
 */
//クッキー読み出し
var currentSize = GetCookie( cookie_name );
if( !currentSize ) {
	currentSize = defaultSize;
}
else {
	currentSize = eval( currentSize );
}

/* ========== ::: head内にstyle要素を出力 ::: ========== */
document.writeln( '<style type="text/css">' );
document.write( 'body{font-size:' + currentSize + '%}' );
document.writeln( '</style>' );

/**
 *	クッキー書込み
 */
function SetCookie( name , value ){
	var now = new Date();
	now.setTime( now.getTime() + 24 * 60 * 60 * cookie_limit * 1000 );
	var expiryDate = now.toGMTString();
	document.cookie = name + '=' + escape( value ) + ';expires=' + expiryDate + ';path=' + cookie_path;
}

/**
 *	クッキー読込
 */
function GetCookie (name){
	var arg  = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var pos = 0;

	while( pos < clen ) {
		var offset = pos + alen;
		if( document.cookie.substring( pos, offset ) == arg ) {
		    return getCookieVal( offset );
		}

		pos = document.cookie.indexOf( " ", pos ) + 1;
		if( pos == 0 ) {
			break;
		}
	}
	return( null );
}

/**
 *	クッキーの値抽出
 */
function getCookieVal( offset ) {
	var endstr = document.cookie.indexOf( ";", offset );
	if( endstr == -1 ) {
		endstr = document.cookie.length;
	}
	return unescape( document.cookie.substring( offset, endstr ) );
}

/**
 *	クッキー削除
 */
function DeleteCookie( name ){
	if( GetCookie( name ) ) {
		document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT;path=' + cookie_path;
	}
}

/**
 *	フォントサイズ変更
 */
var font_list = new Array( "S", "M", "L" );
var size_list = new Array( 80, 90, 100 );

function font_change( size ) {
	//表示設定
	var idx;
	var key;
	var name;
	var e;

	var curret = "qaHeaderCharSizeSelected";

	for( idx=0; idx<font_list.length; idx++ ) {
		key = font_list[ idx ];

		//表示アイコンの設定
		name = "qaHeaderCharSize" + key;
		e = document.getElementById( name );
		if( e ) {
			if( size_list[ idx ] == currentSize ) {
				e.firstChild.className = curret;
			}
			else {
				e.firstChild.className = "";
			}
		}
	}

	//サイズ設定
	switch( size ) {
	case "S":
		SetCookie( cookie_name , 80 );
		break;
	case "M":
		DeleteCookie( cookie_name );
		break;
	case "L":
		SetCookie( cookie_name , 100 );
		break;
	}

	if( size ) {
		//ページの再読み込み
		//再読み込みをすることで変更後の値を反映したstyle要素が出力される
		location.reload();
	}
}

/**
 *
 */
function getOffsetTop( e ) {
	if (e.id != undefined) {
		var tag = e.offsetParent.tagName.toLowerCase();	
		if( tag == "body" || tag == "html" ) {
			return( e.offsetTop );
		}
	} else {
		return;
	}

	return( e.offsetTop + getOffsetTop( e.offsetParent ) );
}

/**
 *
 */
function getOffsetLeft( e ) {
	if (e.id != undefined) {
		var tag = e.offsetParent.tagName.toLowerCase();
		if( tag == "body" || tag == "html" ) {
			return( e.offsetLeft );
		}
	} else {
		return;
	}

	return( e.offsetLeft + getOffsetLeft( e.offsetParent ) );
}

/**
 *
 */
function getScrollTop() {
	var scroll_top = 0;

	if( window.scrollY ) {
		// Mozilla
		scroll_top = window.scrollY;
	}
	else if( window.pageYOffset ) {
		// Opera, NN4
		scroll_top = window.pageYOffset;
	}
	else if( document.documentElement && document.documentElement.scrollTop ) {
		// 以下 IE
		scroll_top = document.documentElement.scrollTop;
	}
	else if( document.body && document.body.scrollTop ) {
		scroll_top = document.body.scrollTop;
	}

	//scroll_top = document.body.scrollTop  || document.documentElement.scrollTop;

	return( scroll_top );
}

/**
 *
 */
function getScrollLeft() {
	var scroll_left = 0;

	if( window.scrollX ) {
		// Mozilla
		scroll_left = window.scrollX;
	}
	else if( window.pageXOffset ) {
		// Opera, NN4
		scroll_left = window.pageXOffset;
	}
	else if( document.documentElement && document.documentElement.scrollLeft ) {
		// 以下 IE
		scroll_left = document.documentElement.scrollLeft;
	}
	else if( document.body && document.body.scrollLeft ) {
		scroll_left = document.body.scrollLeft;
	}

	//scroll_left = document.body.scrollLeft || document.documentElement.scrollLeft;

	return( scroll_left );
}

/**
 *
 */
function getWindowHeight() {
	var window_height = 0;

	if( window.innerHeight ) {
		// Mozilla, Opera, NN4
		window_height = window.innerHeight;
	}
	else if( document.documentElement && document.documentElement.clientHeight ) {
		// 以下 IE
		window_height = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight ) {
		window_height = document.body.clientHeight;
	}

	return( window_height );
}

/**
 *
 */
function getWindowWidth() {
	var window_width = 0;

	if( window.innerWidth ) {
		// Mozilla, Opera, NN4
		window_width = window.innerWidth;
	}
	else if( document.documentElement && document.documentElement.clientWidth ) {
		// 以下 IE
		window_width = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		window_width = document.body.clientWidth;
	}

	return( window_width );
}

function changeCategoryOrCompany(elementId) {

	var obj = document.getElementById(elementId);
	var changeObj;
	if (obj) {
		obj.style.display = 'none';
		if (elementId == 'qaFooterareaBlock') {
			changeObj = document.getElementById('qaFootercategoryBlock');
		} else if (elementId == 'qaFootercategoryBlock') {
			changeObj = document.getElementById('qaFooterareaBlock');
		}
		if (changeObj) {
			changeObj.style.display = 'block';
		}
	}
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17668098-19']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

