/* 
	Annex openwin.js	(C)K.Oka
	ver.1.00 : Mar.01,2008
		京王研究室 openwin.js から改造
	(update)Mar.10,2008 ： OpenPicture2にaltとtitleからのタイトル文字列抽出を追加
		Aug.24,2008 ： dir_arrayにmanholeを追加
		Sep.28,2008 ： dir_arrayにsewerage、waterworks、firefight、etceteraを追加、manholeを削除
*/

// ディレクトリ文字列
dir_array = new Array(
	'',
	'http://homepage1.nifty.com/OkaLab/images/',
	'http://okalab.cocolog-nifty.com/photos/uncategorized/',
	'',
	'images/',
	'flower/',
	'sewerage/',
	'waterworks/',
	'firefight/',
	'etcetera/'
);

function OpenPicture2(FileName, ii) {
// 写真用、innerHTMLに対応
	var w = 480;
	var h = 360;
	var ttl = '';
	var imgsrc = '';
	var person = '';
	var flag = (ii & 0x300) >> 8;

	if (flag == 0) {
	// ファイル名をタイトルとする
		imgsrc = FileName;
		ttl = FileName.replace(/_[0-9]+/,'');	// '_'以降はカット
		if (person != '') {
			ttl += '（提供：' + person + 'さん）';
		}
	} else {
	// ファイル名をimgタグのsrc属性から取得、innerHTML使用
		var ymd = '';
		var p1 = FileName.indexOf('src=') + 5;
		var p2 = FileName.indexOf('s.jpg', p1);
		if (FileName.substr(p2 - 1,1) == "_") {p2--;}	// _s.jpgのときの処理
		var imgsrc = FileName.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 (flag) {
			case 1:
			// aタグで囲まれているテキストを抽出
				p1 = FileName.search(/<br>/i);
				ttl = FileName.substring(p1 + 4).replace(/^\s+/, '');
				break;
			case 2:
			// imgタグのalt属性を抽出（Mar.10,2008追加）
				p1 = FileName.indexOf('alt=') + 4;
				p2 = FileName.indexOf(' ', p1) ;
				ttl = FileName.substring(p1, p2).replace(/"/g,'');
				break;
			case 3:
			// imgタグのtitle属性を抽出（Mar.10,2008追加）
				p1 = FileName.indexOf('title=') + 6;
				p2 = FileName.indexOf(' ', p1) ;
				ttl = FileName.substring(p1, p2).replace(/"/g,'');
				break;
		}
	// 画像高さの算出
		p1 = FileName.indexOf('height=') + 7;
		p2 = FileName.indexOf(' ', p1) ;
	// 撮影日の処理
		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 += person + '撮影）';
		}
	}
	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) {
/*
   ディレクトリ文字列の作成
	bit0-4：ディレクトリ番号
	bit5：ルートディレクトリ＝0、子ディレクトリ＝1
	現バージョンではルートもしくは1階層下のディレクトリのみに対応
*/
	var s = '';
	var i = 0;
	if (((ii & 0x20) >> 5) == 1) {
	// 親ディレクトリ
		s = '../';
	}
	s += dir_array[ii & 0x1f];
	return(s);
}