/* 
	鉄道研究室 openwin.js	(C)K.Oka
	ver.2.20 : Dec.23,2007 : 関数名変更
					OpenPictureF → OpenPicture
					OpenPictureE → OpenPicture2
				OpenPicture2の引数でinnerHTMLを利用
	(update)Jan.18,2008 : OpenPicture2で08年を00年と表示するバグを修正
		Feb.07,2008 : OpenPictureを修正
		Mar.10,2008 : OpenPicture2にaltとtitleからのタイトル文字列抽出を追加
		Feb.11,2009 : dir_arrayのkeiseiをnkthに変更
*/

// 車両写真用文字列配列
mFcaption = new Array(
	'',
	'',
	'デハ',
	'モハ',
	'クモハ',
	'クハ',
	'サハ',
	'デキ',
	'ホキ',
	'タキ',
	'チキ',
	'チ',
	'コキ',
	'キハ'
);

// タイトル文字列配列
mMcaption = new Array(
	'',
	'クハニ29+デハ107',
	'第2ターミナルサテライト連絡シャトル'
);

// ディレクトリ文字列
dir_array = new Array(
	'',
	'http://homepage1.nifty.com/OkaLab/images/',
	'http://okalab.cocolog-nifty.com/photos/uncategorized/',
	'',
	'images/',
	'tokyu/',
	'odakyu/',
	'lions/',
	'nkth/',
	'subway/',
	'orange/',
	'jreast/',
	'freight/',
	'album/',
	'memorial/'
);

function OpenPicture(ymd, FileName, ii) {
// 編成写真 or 車両写真
	var ttl = FileName.replace(/_[0-9]+/,'');	// '_'以降はカット
	var w = 480;
	var h = 360;	/* default value */
	var imgsrc = '';

	switch (ymd) {
		case '19841100':
			if (FileName == '151') {
				w = 240;	// 玉電デハ151
			}
			break;
		case '19900800':
			if (FileName == 'EF64 64') {
				w = 360;	// EF64 64
			}
			break;
		case '20050815':
		// 秩父鉄道の一部は備忘録の写真を流用
			switch (FileName) {
				case '201':
					FileName = 'deki201';
					break;
				case 'C58363':
					FileName = 'c58363b';
					break;
				default:
			}
			break;
	}

	ttl = ttl.replace(/c/,'運転台')	// 'c'を'運転台'に置換;
	ttl = ttl.replace(/r/,'車内')	// 'r'を'車内'に置換;
	ttl = ttl.replace(/k/,'系')	// 'k'を'系'に置換;

	if ((ii & 0xff) == 1) ttl += 'F';	// iiの下位8bit=1のときは編成番号

	if (ymd.length != 0) {
		ttl = mFcaption[ii & 0xff] + ttl + '（' + ymd.substr(0,4) + '年' + parseInt(ymd.substr(4,2), 10) + '月';
		if (ymd.substr(6,2) != '00') {
			ttl = ttl + parseInt(ymd.substr(6,2), 10)+'日';
		}
		ttl += '撮影）';
		imgsrc = ymd.substr(2,6) + '_' + FileName;
	} else {
		imgsrc = FileName;
	}

	OpenWindow(ttl, Directory(ii) + imgsrc, w, h);
}

function OpenPicture2(st, ii) {
/*
	その他の写真
		innerHTMLで得られたimgタグの文字列から該当画像を別窓で開く
		aタグのonclickイベントで使用
		引数stは必ず this.innnerHTML
*/

// default value
	var ttl = '';
	var ymd = '';
	var w = 480;
	var h = 360;

// ファイル名
// imgタグのsrc属性から取得
	var p1 = st.indexOf('src=') + 5;
	var p2 = st.indexOf('s.jpg', p1);
	if (st.substr(p2 - 1,1) == "_") {p2--;}	// _s.jpgのときの処理
	var imgsrc = st.substring(p1, p2).replace(/"/g, '');
	p1 = imgsrc.lastIndexOf("/",imgsrc.length - 1) + 1;	// ディレクトリ名の削除（Mar.10,2008追加）
	if (p1 != 0) {
		imgsrc = imgsrc.substring(p1);
	}
	p1 = (imgsrc.match(/^[0-9]{6}_/));	// ファイル名から年月日の取出し
	if (p1 != null) {
		ymd = p1[0];
	}

// タイトル文字列
	switch (ii & 0xff) {
		case 0:
		// iiの下位8bitが0のときはimgsrcをタイトル文字列として設定
			if (ymd != null) {
				ttl = imgsrc.substring(7);
			} else {
				ttl = imgsrc;
			}
			ttl = ttl.replace(/_[0-9]+/,'');	// '_'以降はカット
			break;
		case 1:
		// aタグで囲まれているテキストを抽出
			p1 = st.search(/<br>/i);
			ttl = st.substring(p1 + 4).replace(/^\s+/, '');
			break;
		case 2:
		// imgタグのalt属性を抽出（Mar.10,2008追加）
			p1 = st.indexOf('alt=') + 4;
			p2 = st.indexOf(' ', p1);
			ttl = st.substring(p1, p2).replace(/"/g,'');
			break;
		case 3:
		// imgタグのtitle属性を抽出（Mar.10,2008追加）
			p1 = st.indexOf('title=') + 6;
			p2 = st.indexOf(' ', p1) ;
			ttl = st.substring(p1, p2).replace(/"/g,'');
			break;
		default:
			ttl = mMcaption[ii - 3];
	}

	if (ymd.length != 0) {
		p1 = parseInt(ymd.substr(0,2), 10);
		if (p1 >= 75) {
			p1 += 1900;
		} else {
			p1 += 2000;
		}
		ttl += '（' + p1 + '年' + parseInt(ymd.substr(2,2), 10) + '月';
		if (ymd.substr(4,2) != '00') {
			ttl += parseInt(ymd.substr(4,2), 10) + '日';
		}
		ttl += '撮影）';
	}

	OpenWindow(ttl, Directory(ii) + imgsrc, w, h);
}

function OpenWindow(ttl,imgsrc, w, h) {
// ウィンドウを開く
	var lflag = 0;
	if (navigator.userAgent.indexOf("SV1") >= 0) {
		lflag = 1;	// IE6.0(SP2)ならばアドレスバーを表示
	} else if ((navigator.userAgent.indexOf("Firefox") >= 0) && (navigator.userAgent.indexOf("Windows NT 5.1") >= 0)) {
		lflag = 1;	// Windows XPでかつFirefoxならばアドレスバーを表示
	}
	var win=window.open('','picture_window','width=480,height=360,location='+lflag);
	var topmrg = Math.floor((360 - h) / 2);		// 上下マージンの計算
	var leftmrg = Math.floor((480 - w) / 2);	// 左右マージンの計算
	with (win.document) {
		open();
		write('<html>');
		write('<head>');
		write('<title>');
		write(ttl);
		write('<','/title>');
		write('<link rel=\"stylesheet\" type=\"text/css\" href=\"',document.styleSheets[0].href,'\">');
		write('<meta http-equiv="Pragma" content="no-cache">'); 
		write('<meta http-equiv="Cache-Control" content="no-cache">'); 
		write('<meta http-equiv="Expires" content="0">');
		write('<meta http-equiv="imagetoolbar" content="no">');
		write('<','/head>');
		write('<body topmargin=',topmrg,' leftmargin=',leftmrg,' marginheight=',topmrg,' marginwidth=',leftmrg,'>');
		write('<img src=\"',imgsrc,'.jpg\" oncontextmenu=\"return false;\" onclick=\"window.close();\" border=0 width=',w,' height=',h,'>');
		write('<','/body>');
		write('<head>');
		write('<meta http-equiv="Pragma" content="no-cache">'); 
		write('<','/head>');
		write('<','/html>');
 		close();
	}
	win.focus();
}

function Directory(ii) {
/*
   ディレクトリ文字列の作成
	bit8-12：ディレクトリ番号
	bit13：ルートディレクトリ＝0、子ディレクトリ＝1
	現バージョンではルートもしくは1階層下のディレクトリのみに対応
*/
	var s = '';
	if ((ii >> 13) == 1) {
	// 親ディレクトリ
		s = '../';
	}
	s += dir_array[(ii >> 8) & 0x1f];
	return(s);
}