// =============================================================================
// ページ読み込み完了後
// =============================================================================

var JUMP_PROGRAM = "/system/jump/?u=";

function doCommonProc() {

	// カウンタ呼び出し
	var conter = "/cgi-local/acc/acclog.cgi" +
				 "?referrer=" + document.referrer +
				 "&width="    + screen.width      +
				 "&height="   + screen.height     +
				 "&color="    + screen.colorDepth;
	
	// イメージプレロード
	preloadImages(conter,
				  "/images/header_btn_bg_u.gif",
				  "/images/header_btn_bg_d.gif");
	
	// ツールチップ用の領域を用意する。（IE5.5+,Netscape6+,Opera7用？互換性要チェック）
	var e = document.createElement("div");
	e.setAttribute("id", "TOOL_CHIP");
	e.style.position   = "absolute";
	e.style.visibility = "hidden";
	document.body.appendChild(e);
	
	processLinkUrl();
	
	return true;
}


var chip;

function preloadImages() { //v3.0
	var d = document;
	if (!d.images) return;
	if (!d.MM_p) d.MM_p=new Array();
	var i;
	var j = d.MM_p.length;
	var a = preloadImages.arguments;
	for (i = 0; i < a.length; i++)
		if (a[i].indexOf("#") != 0) {
			d.MM_p[j] = new Image;
			d.MM_p[j++].src = a[i];
		}
}

function showOuterLinkChip() {

//	if (!document.all) return;		// 互換性が心配なのでチェックする。
	
	// Aタグでなければ、終了する。
	if (this.tagName != 'A') return;
	
	// URL を取得する。
	var url = this.href;
	
	switch (url) {
		// ぐるなびの場合。
		case "http://ck.jp.ap.valuecommerce.com/servlet/referral?sid=2184330&pid=872583943":
			url = "http://www.gnavi.co.jp/";
			break;
		// 以降追加された場合はここに追加する。
		
		// その他の場合。
		default:
			// 何もしない。
	}
	
	// ツールチップ内の URL 部を定義して、ステータスバー内に表示する。
	if (url.indexOf(JUMP_PROGRAM) == 0) {
		url = url.substring(JUMP_PROGRAM.length, url.length);
		url = unescape(url);
	}
	window.status = url;
	if (url.length > 48) url = url.substring(0, 48) + " ...";
	
	// ツールチップ内のHTMLを定義する。
	var html =
		'	<table>									' +
		'		<tr><td style="vertical-align:top; padding-right:2px">	' +
		'				<img src="/images/information.gif" alt=""		' +
		'					 style="width:16px;height:16px"></td>		' +
		'			<td style="white-space:nowrap;line-height:1em">		' +
		'				<strong>JunSoftサイト外へのリンクです</strong>	' +
		'				<br>' + url + '</td></tr>						' +
		'	</table>													';
	showToolChip(this, html);
	
	return true;
}

function showToolChip(sender, msg) {
	
	sender['onmouseout'] = hideOuterLinkChip;
	
	chip = document.all["TOOL_CHIP"];
	
	// すでに表示されていたら、終了する。
	if (chip.style.visibility.charAt(0) == "v") return;
	
	msg =
		'<div style="												' +
		'	filter:alpha(opacity=90) shadow(color=#999999,direction=135,strength=3);	' +		// このスタイルはＩＥ専用。
		'	padding:0px 3px 3px 0px;								' +
		'	position:absolute;										' +
		'	">														' +
		'	<div 													' +
		'		id="CHIP_TABLE"										' +
		'		style="												' +
		'			color:InfoText;									' +
		'			font-size:9pt;									' +
		'			border:1px solid #000000;						' +
		'			padding:2px;									' +
		'			background:InfoBackground;						' +
		'			line-height:1em;								' +
		'		"													' +
		'	>														' +
		msg															  +
		'	</div>													' +
		'</div>														';
	
	chip = document.all["TOOL_CHIP"];
	chip.innerHTML = msg;
	
	// ツールチップの幅・高さを取得する。
	var w = document.getElementById("CHIP_TABLE").offsetWidth;
	var h = document.getElementById("CHIP_TABLE").offsetHeight;
	
	// ツールチップのX・Y座標を計算する
	var x = document.body.scrollLeft + event.x -  2;		// 左記 "-2" や "+19" は「こんな感じ」って値
	var y = document.body.scrollTop  + event.y + 19;
	
	// 表示しようとする x 座標がブラウザの幅をはみ出すようなら、はみ出さないように再設定する。
	if (x > document.body.clientWidth  - w - 3) x = document.body.clientWidth  - w - 3;	// 左記 "3" は影や線を考慮して加算した値
//	if (y > document.body.clientHeight - h    ) y = document.body.scrollTop; //y = document.body.clientHeight - h - 10;
	
//	y = document.body.clientHeight+document.body.scrollTop - h - 3;
	
	// ツールチップを画面に表示する。
	chip.style.left = x;
	chip.style.top  = y;


	chip.style.visibility = "visible";
	
	return true;
}

function hideOuterLinkChip() {
	
	hideToolChip();
	return true;
}

function hideToolChip() {
	
	// ツールチップを消す。
	chip.style.left = 0;	// 左記がないと、非表示にした後に余計な横スクロールバーが残ることがある。
	chip.style.top  = 0;
	chip.style.visibility = "hidden";
	
	// ステータスバー内の文字を消去する。
	window.status = "";
	
	return true;
}

function showOuterLinkStatusMessage() {
	window.status = this.href.substring(JUMP_PROGRAM.length, this.href.length);
	return true;
}

function hideOuterLinkStatusMessage() {
	window.status = "";
	return true;
}



// =============================================================================
// ヘッダ部リンクボタンの表示
// =============================================================================
function putLinkBtn() {
	a_tag_prefix = "<a class=\"flat\" " +
				   "onMouseOver=\"className='up'\"   " +
				   "onMouseOut =\"className='flat'\" " +
				   "onMouseDown=\"className='down'\" " +
				   "onMouseUp  =\"className='flat'\" " +
				   "href=";
	document.write("<span class=\"link-btn\">"								+
				   a_tag_prefix + "\"/\"          >トップ</a>"				+
				   a_tag_prefix + "\"/interface/\">インターフェース</a>"	+
				   a_tag_prefix + "\"/gourmet/\"  >プチグルメ</a>"			+
				   "</span>");
}



// =============================================================================
// リンクを加工
// =============================================================================
function processLinkUrl() {
	
//		// マピオンの中心アイコン。指定可能な値は下記のいずれか。
//		//     map_icon_33,,,,,
//		//     map_icon_01,,,,,
//		//     map_icon_06,,,,,
//		//     yubi2,,,,,
//		//     map_icon_04,,,,,
//		//     house,,,,,
//		// 
//		// アイコンの一覧は下記を参照のこと
//		// http://www.mapion.co.jp/c/f?uc=27&nl=35/39/38.070&el=139/42/04.724&grp=all&pg=A&coco=35/39/38.070,139/42/04.724&icon=map_icon_33,,,,,
//		
//		
//		var MAPION_CENTER_MARK = "yubi2,,,,,";
//		
//		// リンクすべてに対して処理を行う。
//		for (var i = 0; i < document.links.length; i++) {
//			
//			var link = document.links[i];
//			
//			
//			
//	//		// ページ内のアンカーなら、クリック時にスムーズスクロールさせる。
//	//		// 動作検証が面倒なので、とりあえず IE のみとする。
//	//		if (IE)
//	//			if (link.hash.charAt(0) == "#")
//	//				link['onclick'] = function() { return scrollToAnchor(this); }
//			
//			
//			
//			// サイト内のリンクか JavaScript なら、以降の処理を行わずに次のループを実行する。
//			if (link.hostname == location.hostname ||
//				link.href.toLowerCase().indexOf("javascript:") == 0) continue;
//			
//			// バリューコマースへのリンクなら、以降の処理を行わずに次のループを実行する。
//			if (link.href.toLowerCase().indexOf("valuecommerce.com/") != -1) continue;
//	
//			// プチグルメ内のマピオンへのリンクなら、地図の中心地点を強調する。
//			var needProcess = true;
//			if (location.href.indexOf("junsoft.jp/gourmet/") != -1 &&
//			    link.href.indexOf("www.mapion.co.jp/c/f?") != -1) {
//			    
//				params = link.search.substring(1, link.search.length).split("&");
//				var nl = "";
//				var el = "";
//				for (j = 0; j < params.length; j++) {
//					tmp = params[j].split("=");
//					var key = tmp[0];
//					var val = tmp[1];
//					if (key == "nl") nl = val;
//					if (key == "el") el = val;
//					if (key == "coco") needProcess = false;
//				}
//				
//				if (needProcess && nl != "" && el != "") {
//					link.href = link.href + "&icon=" + MAPION_CENTER_MARK + "&coco=" + nl + "," + el;
//				}
//			}
//			
//	//		// 外部リンクにアイコンを付ける。
//	//		if (location.href.indexOf("/other/links/") == -1)
//	//			link.innerHTML = link.innerHTML + "<img src=\"/images/exit.gif\" class=\"exit-link\" alt=\"\">";
//			
//			// イベントを割り当てる。
//			link['onmouseover'] = showOuterLinkChip;
//			link['onmouseout' ] = hideOuterLinkChip;
//			link['onfocus'    ] = showOuterLinkStatusMessage;
//			link['onblur'     ] = hideOuterLinkStatusMessage;
//			
//			// target属性を変える。
//			link.target = "_blank";
//			
//			// 解析用ページを経由するようリンク先を変更する。
//			link.href = JUMP_PROGRAM + escape(link.href);
//		}
	return true;
}



// =============================================================================
// 更新履歴の表示切替
// =============================================================================
function displayUpdateNotice(updateStr) {
	// 初期設定
	now = new Date();
	newMode = false;
	
	// 前回閲覧日時以降にサイトが更新されている場合、新着表示モードとする。
	lastBrowseDT = getCookie("LastBrowse");
	if (lastBrowseDT != null) {
		if ((new Date(lastBrowseDT)) < (new Date(updateStr))) {
			newMode = true;
		}
	}
	
	// 前回の新着情報モードから6時間以上経過してなければ継続する。
	lastNoticeDT = getCookie("LastNotice");
	if (lastNoticeDT != null) {
		if (now - (new Date(lastNoticeDT)) < 21600000) {	// 21600000 = 6h * 60m * 60s * 1000ms
			newMode = true;
		}
	}
	
	// 更新履歴の表示方法を切り替える。
	if (newMode == true) {
		setCookie("LastNotice", now, 180);
		document.write('<div style="white-space:nowrap">')
		document.write('<span class="new">新着があります</span>')
		document.write('<img src="images/new.gif" style="width:60px height:20px" align="top" alt="">')
		document.write('</div>')
	} else {
		document.write('<div class="bold">更新履歴</div>')
	}
	
	// 今回の閲覧時刻を Cookie に保存する。
	setCookie("LastBrowse", now, 180);
}



// =============================================================================
// 良い／悪いの投票
// =============================================================================
var rank = "%%unselected%%";
function dispalyPollArea(showText) {
	document.write('<form name="feedback" method="post" accept-charset="utf-8" style="width:468px; margin:auto;">');
	document.write('<fieldset style="padding:10px;">');
	document.write('	<legend style="color:#003366"><strong>このページの評価をお寄せください</strong></legend>');
	document.write('<input type="hidden" name="from_url" value="' + document.location + '">');
	document.write(showText ? 
				   '良い <img src="/images/rank_good.gif" alt=""     style="vertical-align:middle; width:40px; height:20px;">' :
				   '     <img src="/images/rank_good.gif" alt="良い" style="vertical-align:middle; width:40px; height:20px;">');
	document.write('     <input type="hidden" value="0.2.0" name="version"');
	document.write('    ><input type="radio"  value="1"     name="rating" onmouseover="showToolChip(this, \'とても良い\')"     onclick="setRank(this)" style="vertical-align:middle; height:20px;" ');
	document.write('    ><input type="radio"  value="2"     name="rating" onmouseover="showToolChip(this, \'まあまあ良い\')"   onclick="setRank(this)" style="vertical-align:middle; height:20px;" ');
	document.write('    ><input type="radio"  value="3"     name="rating" onmouseover="showToolChip(this, \'普通\')"           onclick="setRank(this)" style="vertical-align:middle; height:20px;" ');
	document.write('    ><input type="radio"  value="4"     name="rating" onmouseover="showToolChip(this, \'あまり良くない\')" onclick="setRank(this)" style="vertical-align:middle; height:20px;" ');
	document.write('    ><input type="radio"  value="5"     name="rating" onmouseover="showToolChip(this, \'とても悪い\')"     onclick="setRank(this)" style="vertical-align:middle; height:20px;">');
	document.write(showText ? 
				   '     <img src="/images/rank_bad.gif" alt=""     style="vertical-align:middle; width:40px; height:20px;"> 悪い' :
				   '     <img src="/images/rank_bad.gif" alt="悪い" style="vertical-align:middle; width:40px; height:20px;">     ');
	document.write('    <input type="hidden" name="next_url" value="/system/goback.htm">');
	document.write('    <input type="button" value="送信" onclick="submitData()" style="width:4em; height:2em; vertical-align:middle;"><br/>');
	document.write('    <textarea name="comment" style="width:436px; margin-top:0.5em;"></textarea>');
	document.write('</fieldset>');
	document.write('</form>');
}

function setRank(radio) {
	rank = radio.value;
}

function submitData() {
	if (rank == "%%unselected%%") {
		alert("このページの評価を選択してください。");
	} else {
		document.feedback.action="http://junsoft.jp/cgi-local/formdecode/formdecode.cgi"
		document.feedback.submit();
	} 
}



// =============================================================================
// Cookieの書き込み（内部関数）
// =============================================================================
function setCookie(key, value, days) {
	var exp = new Date();
	exp.setTime(exp.getTime() + days * 86400000);	// 86400000 = 24h * 60m * 60s * 1000ms
	document.cookie = key + '=' + escape(value) + ";expires=" + exp.toGMTString();
}



// =============================================================================
// Cookieの読み出し（内部関数）
// =============================================================================
function getCookie(key) {
	var search = key + '=';
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end     = document.cookie.indexOf(';',offset);
			if(end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset,end));
		}
	}
	return null;
}









// ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■


//ブラウザ判別
var IE = navigator.appName.indexOf("Microsoft Internet Explorer",0) != -1;

flag = false;

function getAnchorPos(elementID) {

	var result = new Object();
	if (document.getElementById) {
		var obj = document.getElementById(elementID);
		result.x = obj.offsetLeft;
		result.y = obj.offsetTop;
		while ((obj = obj.offsetParent) != null) {
			result.x += obj.offsetLeft;
			result.y += obj.offsetTop;
		}
	} else if(document.all) {
		var obj = document.all(elementID);
		result.x = obj.offsetLeft;
		result.y = obj.offsetTop;
		while ((obj = obj.setParent) != null) {
			result.x += obj.offsetLeft;
			result.y += obj.offsetTop;
		}
	} else if (domument.layers) {
		result = document.anchors[elementID].x;
		result = document.anchors[elementID].y;
	} else {
		result.x = 0;
		result.y = 0;
	}
	return result;
}


/**
 * スムーズスクロール
 */
function scrollToAnchor(sender) {
	timer(sender.hash.substr(1));
	return false;
}




var timeID;				// これ省略しても動きそう…。
var prevOffsetY = 0;	// スクロール不可時の判定に必要

function timer(elementID) {


	var offsetY = Math.floor((getAnchorPos(elementID).y - getScrollTop()) / 2);
	
	if (offsetY == 0 || offsetY == prevOffsetY) {
		clearTimeout(timeID);
	} else {
		scrollBy(0, offsetY);
		timeID = setTimeout("timer('" + elementID + "')", 10);	// 現実的には 10ms での再タイマーは実現不可？
	}
	prevOffsetY = offsetY;

}



// 現在の縦位置を取得
function getScrollTop() {
	if (IE) {
		return document.body.scrollTop;
	} else if(window.pageYOffset) {
		return window.pageYOffset;
	} else {
		return 0;
	}
}
