/*
 * common.js各種共通JavaScript
 */

// IEのチェック
var isIE6 = false;
if (navigator.userAgent.match(/MSIE (\d\.\d+)/)) {
	if (RegExp.$1 == "6.0") isIE6 = true;
}

// 定数
var SCREEN_ID = "overlayscreen";
var FORM_ID = "overlaylogin";
var CLOSEBUTTON_ID = "overlayclosebtn";

var obScreen = null;
var obForm = null;

var fadeSec = 25;	// 1ステップの間隔（ミリ秒）
var fadeOpacity = 10;	// 1ステップあたりの透過度（%）


/*
 * ログインフォームの表示処理
 */
function openLoginForm() {
	// IE6 の場合はスクロールを無効にする
	if (isIE6) {
		$("body","html").css({
			"height": "100%", 
			"width": "100%"
		});
		$("html").css("overflow","hidden");

		// 親HTMLにSELECTがある場合は、表示を隠す（IEのバグ対応）
		$("select").css("visibility","hidden");
	}

	if (!obScreen) {
		obScreen = document.createElement("div");
		obScreen.id = SCREEN_ID;
		$("body").append(obScreen);
	}

	if (!obForm) {
		obForm = document.createElement("div");
		obForm.id = FORM_ID;
		$("body").append(obForm);
		$(obForm).attr("class", "overlayform");
	}

	var html = "<div class='header'></div>";
	html += "<div class='body'>";
	html += "<h2>ログイン</h2>";
	html += "<p class='name'>";
	html += "<label for='loginname'>Emailアドレス</label>";
	html += "<input type='text' id='loginname' name='loginname' value='' />";
	html += "</p>";
	html += "<p class='password'>";
	html += "<label for='loginpass'>パスワード</label>";
	html += "<input type='password' id='loginpass' name='loginpass' value='' />";
	html += "</p>";
	html += "<p class='auto'>";
	html += "<input type='checkbox' id='chkAuto' name='chkAuto' value='' /><label for='chkAuto'>次回から自動的にログインする</label>";
	html += "</p>";
	html += "<p class='button'>";
	html += "<a href='javascript:alert(0);' class='loginBtn'>ログインする</a>";
	html += "<a href='aaa.html'>&raquo;&nbsp;パスワードを忘れた方はこちら</a>";
	html += "</p>";
	html += "</div>";
	html += "<div class='footer'></div>";
	html += "<a id='overlayclosebtn' href='javascript:closeLoginForm()'>閉じる</a>";

	$(obForm).html(html);

	var formHeight = $(obForm)[0].offsetHeight;
	var setHeight = "";
	if (isIE6) {
		var srl = getScrollPosition();
		setHeight = (srl.y - Math.floor(formHeight / 2)) + "px";
	} else {
		setHeight = "-" + Math.floor(formHeight / 2) + "px";
	}
	$(obForm).css({"margin-top": setHeight, "visibility": "visible"});

	// 背景部にクリックイベントを付加する
	$(obScreen).click(function() {
		closeLoginForm();
	});

	$("#loginname")[0].focus();
}

/*
 * ログインフォームの非表示処理
 */
function closeLoginForm() {
	$(obScreen).unbind("click");
	$(obScreen).remove();
	obScreen = null;

	$(obForm).remove();
	obForm = null;

	// IE6 の場合はスクロールを有効にする
	if (isIE6) {
		$("body","html").css({
			"height": "auto",
			"width": "auto"
		});
		$("html").css("overflow","");

		// 親HTMLにSELECTがある場合は、非表示を解除する
		$("select").css("visibility", "visible");
	}
}





/*
 * 記事ランキングのタブ切り替え処理
 */
function changeRankingInfo(obj, focusId) {
	$("ul", $("#newsranking")).each(function() {
		var getId = $(this).attr("id");
		if (getId == "tabwrap") {
			// タブを一旦初期化する
			$("li", $(this)).attr("class", "");
		} else {
			if (getId == focusId) {
				$(this).attr("class", "visible");
			} else {
				$(this).attr("class", "hidden");
			}
		}
	});

	// クリックしたタブをフォーカス状態にする
	$(obj).parent().attr("class", "focus");

}


/*
 * スクロール量を取得する
 */
function getScrollPosition() { 
	var obj = new Object(); 
	obj.x = document.documentElement.scrollLeft || document.body.scrollLeft; 
	obj.y = document.documentElement.scrollTop || document.body.scrollTop; 
	return obj; 
}
 
