// scriptタグを生成する
// (生成されるとJSON形式の関連情報ファイルが読み込まれ、以下のkijiinfo関数が実行される)
function recommend(id){
	if (id.match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{2}\//)){
		var script = document.createElement('script');
		script.charset = 'euc-jp';
		script.src = "http://rec.asahi.com/asahicom/" + id + ".json"; // 関連情報ファイルURL
		document.body.appendChild(script);
	}
}

// レコメンド記事タイトルに情報を付加する
function titleconv(ur,tle){
	var url = ur;
	var title = tle; 
	var kijiid = "";
	var mat = url.match(/([A-Z0-9]{3})[0-9]{12}\.html$/);
	if(mat) {
		kijiid = mat[1];    // URLから記事IDを取り出す
	}
	
	if(title.match(/^〈ＣＮＮ〉/)){
		// 元々タイトルにCNN表記されている場合は処理しない
		return title;
	}

	if(title.match(/^<みんなの知恵蔵>/)){
		// 元々タイトルに みんなの知恵蔵 表記されている場合は処理しない
		return title;
	}

	if(kijiid.match(/CNN/)){
		// CNN記事の場合は先頭に〈ＣＮＮ〉を付与
		title = "〈ＣＮＮ〉" + title;
		return title;
	}
	if(kijiid.match(/RTR/)){
		// ロイター記事の場合は先頭に〈ロイター〉を付与
		title = "〈ロイター〉" + title;
		return title;
	}
	if(kijiid.match(/NIK/)){
		// 日刊スポーツ記事の場合は先頭に〈日刊スポーツ〉を付与
		title = "〈日刊スポーツ〉" + title;
		return title;
	}
	if(kijiid.match(/TKZ/)){
		// 東洋経済記事の場合は先頭に〈東洋経済〉を付与
		title = "〈東洋経済〉" + title;
		return title;
	}
	if(kijiid.match(/NKK/)){
		// 日刊工業記事の場合は先頭に〈日刊工業〉を付与
		title = "〈日刊工業〉" + title;
		return title;
	}
	if(kijiid.match(/JSN/)){
		// 住宅新報記事の場合は先頭に〈住宅新報〉を付与
		title = "〈住宅新報〉" + title;
		return title;
	}
	if(kijiid.match(/BCN/)){
		// ＢＣＮ記事の場合は先頭に〈ＢＣＮ〉を付与
		title = "〈ＢＣＮ〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/mytown\.asahi\.com\//)){
		// マイタウン記事の場合は先頭に〈マイタウン〉を付与
		title = "〈マイタウン〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/chiezou\.jp\//)){
		// みんなの知恵蔵記事の場合は先頭に〈kotobank〉を付与
		title = "〈kotobank・用語解説〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/kotobank\.jp\//)){
		// kotobank記事の場合は先頭に〈kotobank〉を付与
		title = "〈kotobank・用語解説〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/www\.asahi\.com\/showbiz\/korea\//)){
		// 東亜日報記事の場合は先頭に〈東亜日報〉を付与
		title = "〈東亜日報〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/www\.asahi\.com\/kansai\//)){
		// 関西記事の場合は先頭に〈関西〉を付与
		title = "〈関西〉" + title;
		return title;
	}
	if(url.match(/^http:\/\/www\.asahi\.com\/video\//)){
		// 動画記事の場合は先頭に〈動画〉を付与
		title = "〈動画〉" + title;
		return title;
	}

	// 処理対象外なのでそのままのタイトルを返す
	return title;
}

// リンクリスト生成
function makeAnchor(url,title,photourl,photocaption,body,kijidatetime,first,last) {
	this.url = url;                     // リンク先URL
	this.title = title;                 // タイトル
	this.photourl = photourl;           // 写真URL
	this.photocaption = photocaption;   // 写真エトキ
	this.body = body;                   // 商品説明
	this.kijidatetime = kijidatetime;   // 記事日時(yyyymmddhhmmss)
	this.first = first;                 // 0以外のときリスト最初に表示
	this.last = last;                   // 0以外のときリスト最後に表示
}
// asahi.comの内と外で違うターゲットを返す
makeAnchor.prototype.target = function () {
	if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//)) {
		return "_self";
	}
	return "_blank\" class=\"Blank\" title=\"別ウインドウで開きます";
}
// asahi.comの内と外で違うターゲットを返す
makeAnchor.prototype.targetosusume = function () {
	if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//)) {
		return "_self";
	}
	return "_blank";
}
// (mm/dd) 形式で日付を返す
makeAnchor.prototype.kijidate = function () {
	var arr = this.kijidatetime.match(/[0-9]{4}([0-9]{2})([0-9]{2})[0-9]{6}/);
	if(arr) {
		var mm = arr[1].replace(/^0/,"");
		var dd = arr[2].replace(/^0/,"");
		return "(" + mm + "/" + dd + ")";
	} else {
		return "";
	}
}
// レコメンドエンジンからの「こんな記事も」リンク生成
makeAnchor.prototype.kiji_reco = function () {
	var anc = "";
	var urlc = "";
	var c_title = "";
	c_title = titleconv(this.url,this.title);  // タイトルに情報を付加

	if(this.url.match(/^http:\/\/premium\.asahi\.com\//) && !this.url.match(/\?/)) {
		// アサヒコムプレミアム内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?iref=reca";
	} else if(this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//) && !this.url.match(/\?/)) {
		// asahicom内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=reca";
	} else if(this.url.match(/^http:\/\/www\.cnn\.co\.jp\//) && !this.url.match(/\?/)) {
		// CNNへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=reca";
	} else if(this.url.match(/^http:\/\/chiezou\.jp\//) && !this.url.match(/\?/)) {
		// 知恵蔵へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=reca";
	} else if(this.url.match(/^http:\/\/kotobank\.jp\//) && !this.url.match(/\?/)) {
		// kotobankへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=reca";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

        if (this.url.match(/^http:\/\/www\.asahi\.com\/senkyo2009\/localnews\//)) {
                // www.asahi.com 選挙ページlocalnewsは www2.asahi.com に置換
                urlc = this.url.replace(/http:\/\/www\.asahi\.com\/senkyo2009\/localnews\//,"http:\/\/www2\.asahi\.com\/senkyo2009\/localnews\/") + "?ref=reca";
	} 

	anc += "<li><a href=\"" + urlc + "\" target=\"" + this.target() + "\">" + c_title;
	if(this.photourl) {
		if (this.photourl.match(/^http:\/\/www\.asahi\.com\/video\//)){
			// 動画付き記事はビデオカメラマーク付加
			anc += "<img src=\"/images08/common/icn_movie.gif\" alt=\"動画付き記事\" width=\"14\" height=\"14\" class=\"Icn\" />";
		} else {
			// 写真付き記事はカメラマーク付加
			anc += "<img src=\"/images08/common/icn_photo.gif\" alt=\"写真付き記事\" width=\"13\" height=\"14\" class=\"Icn\" />";
		}
	}
	anc += "</a><span class=\"Update\">" + this.kijidate() + "</span></li>";

	return anc;
}
// レコメンドエンジンからの「こんな記事も」リンク生成(写真表示部分)
makeAnchor.prototype.kiji_reco_photo = function () {
	var anc = "";
	var urlc = "";

	if (this.url.match(/^http:\/\/premium\.asahi\.com\//) && !this.url.match(/\?/)) {
		// アサヒコムプレミアム内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?iref=recb";
	} else if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//) && !this.url.match(/\?/)) {
		// asahicom内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recb";
	} else if(this.url.match(/^http:\/\/www\.cnn\.co\.jp\//) && !this.url.match(/\?/)) {
		// CNNへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recb";
	} else if(this.url.match(/^http:\/\/chiezou\.jp\//) && !this.url.match(/\?/)) {
		// 知恵蔵へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recb";
	} else if(this.url.match(/^http:\/\/kotobank\.jp\//) && !this.url.match(/\?/)) {
		// kotobankへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recb";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

	if (this.photourl.match(/^http:\/\/www\.asahi\.com\//)) {
		// www.asahi.com の写真URLは www2.asahi.com に置換
//		var kijiphotourl = this.photourl.replace(/http:\/\/www\.asahi\.com\//,"http:\/\/www2\.asahi\.com\/");
		var kijiphotourl = this.photourl;
	} else if(this.photourl.match(/^http:\/\/mytown\.asahi\.com\//)) {
		// mytown.asahi.com の写真URLは mytown.asahi.com.edgesuite.net に置換
		var kijiphotourl = this.photourl.replace(/http:\/\/mytown\.asahi\.com\//,"http:\/\/mytown\.asahi\.com\.edgesuite\.net\/");
//		var kijiphotourl = this.photourl;
	} else {
		var kijiphotourl = this.photourl;
	}

	if(this.first) {
		// 最初の表示のとき
		anc += "<li class=\"Fst\">";
	} else {
		// 2番目以降
		anc += "<li>";
	}
	anc += "<a href=\"" + urlc + "\" target=\"" + this.target() + "\">";
	anc += "<span class=\"Thmb\">";
	anc += "<img src=\"" + kijiphotourl + "\" alt=\"" + this.photocaption + "\" height=\"54\" /></span></a></li>";

	return anc;
}
// アドミンからの「こんな記事も」リンク生成
makeAnchor.prototype.kiji_link = function () {
	var anc = "";
	var urlc = "";

	if (this.url.match(/^http:\/\/premium\.asahi\.com\//) && !this.url.match(/\?/)) {
		// アサヒコムプレミアム内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?iref=recc";
	} else if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//) && !this.url.match(/\?/)) {
		// asahicom内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recc";
	} else if(this.url.match(/^http:\/\/www\.cnn\.co\.jp\//) && !this.url.match(/\?/)) {
		// CNNへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recc";
	} else if(this.url.match(/^http:\/\/chiezou\.jp\//) && !this.url.match(/\?/)) {
		// 知恵蔵へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recc";
	} else if(this.url.match(/^http:\/\/kotobank\.jp\//) && !this.url.match(/\?/)) {
		// kotobankへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recc";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

	anc += "<li><a href=\"" + urlc + "\" target=\"" + this.target() + "\">";
	anc += this.title + "</a></li>";

	return anc;
}
// レコメンドエンジンからの「おすすめリンク」生成
makeAnchor.prototype.shop_reco = function () {
	var anc = "";
	var urlc = "";

	if (this.url.match(/^http:\/\/premium\.asahi\.com\//) && !this.url.match(/\?/)) {
		// アサヒコムプレミアム内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?iref=recd";
	} else if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//) && !this.url.match(/\?/)) {
		// asahicom内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recd";
	} else if(this.url.match(/^http:\/\/www\.cnn\.co\.jp\//) && !this.url.match(/\?/)) {
		// CNNへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recd";
	} else if(this.url.match(/^http:\/\/chiezou\.jp\//) && !this.url.match(/\?/)) {
		// 知恵蔵へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recd";
	} else if(this.url.match(/^http:\/\/kotobank\.jp\//) && !this.url.match(/\?/)) {
		// kotobankへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=recd";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

	if(this.last) {
		// 最後の表示のとき
		anc += "<div class=\"Col13 Lst\">";
	} else {
		// まだ最後でないとき
		anc += "<div class=\"Col13\">";
	}
	anc += "<div class=\"ThmbSetXw60 FstMod\">";
	anc += "<p class=\"ThmbCol\">";
	anc += "<a href=\"" + urlc + "\" target=\"" + this.targetosusume() + "\">";
	anc += "<span>";
	anc += "<img src=\"" + this.photourl + "\" alt=\"\" width=\"60\" height=\"60\" />";
	anc += "</span></a></p>";
	anc += "<div class=\"ThmbTxt\">";
	anc += "<p><a href=\"" + urlc + "\" target=\"" + this.targetosusume() + "\">";
	anc += "<em>" + this.title + "</em></a></p>";
	anc += "</div>";
	anc += "<div class=\"ThmbTxt2\"><p>" + this.body + "</p></div>";
	anc += "</div></div>";

	return anc;
}
// アドミンからの「おすすめリンク」生成
makeAnchor.prototype.shop_link = function () {
	var anc = "";
	var urlc = "";

	if (this.url.match(/^http:\/\/premium\.asahi\.com\//) && !this.url.match(/\?/)) {
		// アサヒコムプレミアム内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?iref=rece";
	} else if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//) && !this.url.match(/\?/)) {
		// asahicom内へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=rece";
	} else if(this.url.match(/^http:\/\/www\.cnn\.co\.jp\//) && !this.url.match(/\?/)) {
		// CNNへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=rece";
	} else if(this.url.match(/^http:\/\/chiezou\.jp\//) && !this.url.match(/\?/)) {
		// 知恵蔵へかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=rece";
	} else if(this.url.match(/^http:\/\/kotobank\.jp\//) && !this.url.match(/\?/)) {
		// kotobankへかつパラメータ無しのリンクの場合はURLの後にマークを付与
		urlc = this.url + "?ref=rece";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

	anc += "<li><a href=\"" + urlc + "\" target=\"" + this.target() + "\">";
	anc += "<em>" + this.title + "</em></a></li>";

	return anc;
}
// キーワード&検索URLを生成
makeAnchor.prototype.keyword = function () {
	var anc = "";
	var urlc = "";

	if (this.url.match(/^http:\/\/[^\/]*?\.asahi\.com\//)) {
		// asahicom内へのリンクの場合はURLの後にマークを付与
		urlc = this.url + "&ref=rec";
	} else {
		// asahicom外へのリンクの場合のURLはそのまま
		urlc = this.url;
	}

	if(this.first) {
		// 最初の表示のとき
		anc += "<dd class=\"FstItm\">";
	} else {
		// 2番目以降
		anc += "<dd>";
	}
	anc += "<a href=\"" + urlc + "\" target=\"" + this.target() + "\">" + this.title;
	anc += "</a></dd>";

	return anc;
}


// 読み込んだ関連情報(JSONファイル)からデータを抜き出して表示させる
function kijiinfo(data){

	var asahikeywords="";
	var asahinews="";
	var asahishop="";
	var kijidata="";
	var i=0;
	var linktag="";
	var photocount=0;
	var photoflag=0;
	var firstflag=0;
	var lastflag=0;

	// キーワード表示
	if(data["keyword"].length > 0){
		try{
			asahikeywords = document.getElementById("asahikeywords");
			kijidata = "";	

			kijidata += "<dl><dt>キーワード：</dt>";

			// キーワード&検索URLをセット
			for(i=0; i<data["keyword"].length; i++){
				if(i == 0){
					// 最初のとき
					firstflag = 1;
				} else {
					// 2番目以降のとき
					firstflag = 0;
				}

				linktag = new makeAnchor(
					data["keyword"][i]["url"],
					data["keyword"][i]["title"],
					"",
					"",
					"",
					"",
					firstflag,
					0
				);

				kijidata += linktag.keyword();

			}

			kijidata += "</dl>";

			// キーワードを表示
			asahikeywords.innerHTML = kijidata;

		}
		catch(e){
			// キーワードは表示しない
		}
	}

	// 関連記事表示
	if(data["kiji_reco"].length > 0 || data["kiji_link"].length > 0){
		try{
			asahinews = document.getElementById("asahinews");
			kijidata = "";	

			kijidata += "<div class=\"Box2\"><div class=\"H2Box\"><h2>こんな記事も</h2><ul class=\"FollowLnk\"><li class=\"BdrNon\"><img src=\"/images08/common/notice_recom.gif\" alt=\"レコメンドシステムによる自動選択\" width=\"162\" height=\"18\" /></li></ul></div>"

			// レコメンドエンジンからの関連記事表示
			if(data["kiji_reco"].length > 0){

				kijidata += "<ul class=\"Lnk\">";

				// 関連記事のタイトル&リンクをセット
				photoflag = 0;
				for(i=0; i<data["kiji_reco"].length; i++){
					linktag = new makeAnchor(
						data["kiji_reco"][i]["url"],
						data["kiji_reco"][i]["title"],
						data["kiji_reco"][i]["photourl"],
						data["kiji_reco"][i]["photocaption"],
						"",
						data["kiji_reco"][i]["date"],
						0,
						0
					);

					kijidata += linktag.kiji_reco();

					if(data["kiji_reco"][i]["photourl"]) {
						// 写真付き記事があった
						photoflag++;
					}
				}

				kijidata += "</ul>";

				// 関連記事の写真をセット
				if( photoflag > 0 ){
					kijidata += "<ul class=\"ThmbLst\">";

					firstflag = 1;
					for(i=0; i<data["kiji_reco"].length; i++){
						if( data["kiji_reco"][i]["photourl"] ){
							linktag = new makeAnchor(
								data["kiji_reco"][i]["url"],
								data["kiji_reco"][i]["title"],
								data["kiji_reco"][i]["photourl"],
								data["kiji_reco"][i]["photocaption"],
								"",
								data["kiji_reco"][i]["date"],
								firstflag,
								0
							);

							kijidata += linktag.kiji_reco_photo();

							firstflag = 0;
							photocount++;
						}
						// 写真は3枚まで
						if( photocount >= 3 ){
							break;
						}
					}

					kijidata += "</ul>";
				}
			}

			// アドミンからの関連記事表示
			if(data["kiji_link"].length > 0){
				kijidata += "<ul class=\"Flat1\">";
				for(i=0; i<data["kiji_link"].length; i++){
					linktag = new makeAnchor(
						data["kiji_link"][i]["url"],
						data["kiji_link"][i]["title"],
						data["kiji_link"][i]["photourl"],
						data["kiji_link"][i]["photocaption"],
						"",
						"",
						0,
						0
					);

					kijidata += linktag.kiji_link();

				}
				kijidata += "</ul>";
			}

			kijidata += "</div>";

			// 関連情報を表示
			asahinews.innerHTML = kijidata;

		}
		catch(e){
			// 関連情報は表示しない
		}
	}


	// 関連(商品)リンク表示
	if(data["shop_reco"].length > 0 || data["shop_link"].length > 0){
		try{
			asahishop = document.getElementById("asahishop");
			kijidata = "";

			kijidata += "<div class=\"Box2\"><h2>おすすめリンク</h2>";

			// レコメンドエンジンからの関連(商品)リンク表示
			if(data["shop_reco"].length > 0){
				kijidata += "<div class=\"ColSet\">";
				for(i=0; i<data["shop_reco"].length; i++){
					if(i == (data["shop_reco"].length - 1)){
						// 最後の商品のとき
						lastflag = 1;
					} else {
						lastflag = 0;
					}

					linktag = new makeAnchor(
						data["shop_reco"][i]["url"],
						data["shop_reco"][i]["title"],
						data["shop_reco"][i]["photourl"],
						data["shop_reco"][i]["photocaption"],
						data["shop_reco"][i]["body"],
						"",
						0,
						lastflag	
					);

					kijidata += linktag.shop_reco();

				}
				kijidata += "</div>";
			}

			// アドミンからの関連(商品)リンク表示
			if(data["shop_link"].length > 0){
				kijidata += "<ul class=\"Flat2\">";
				for(i=0; i<data["shop_link"].length; i++){
					linktag = new makeAnchor(
						data["shop_link"][i]["url"],
						data["shop_link"][i]["title"],
						data["shop_link"][i]["photourl"],
						data["shop_link"][i]["photocaption"],
						"",
						"",
						0,
						0
					);

					kijidata += linktag.shop_link();

				}
				kijidata += "</ul>";
			}

			kijidata += "</div>";

			// 関連(商品)リンク表示
			asahishop.innerHTML = kijidata;

		}
		catch(e){
			// 関連(商品)リンクは表示しない
		}
	}
}

