


//最新画像表示タグ書き出し

function writeRecentPic() {

	numr = hotpic.length - 1 ;
	makeTag(numr) ;
	document.write(html) ;	//HTML出力

}


//画像切り替え

function changePic(number) {
	var number;
	html="";
//	makefilename(number);
	makeTag(number) ;

	br = navigator.appName.toUpperCase();
	os = navigator.userAgent.toUpperCase();

//IEのみプリロードさせる
//	if (br.indexOf("EXPLORER") >= 0){
//		preload(file);
//	} else {
//		hpdatahtml();
//	}

//オペラとMac版IEはプリロードさせない
//alert(br + ',' + os);
	if (br.indexOf("EXPLORER") >= 0 && os.indexOf("WIN") >= 0){
		preload(file);
	} else if (br.indexOf("EXPLORER") >= 0 && os.indexOf("MAC") >= 0){
		hpdatahtml();
	} else if (br.indexOf("OPERA") >= 0){
		hpdatahtml();
	} else {
		preload(file);
	}


//	preload(file);
//	document.getElementById('hp_data').innerHTML = html ;

//alert(html);

}


//WinIE用に、画像ファイルをプリロードしてキャッシュさせる

function preload(file) {
	var imgObj = new Image();
	imgObj.src = file;
	var viewStatus = function(){
		var count = 0;
		var str = "";
		if(imgObj.complete) count++;
		if(count == 1){
			imgObj.src = file;
			str = '<img src="' + file + '" onload="hpdatahtml();">';
		}

		var viewElem = document.getElementById("view");
		if(viewElem){
			viewElem.innerHTML = str;
		}
		if(count < 1){
			setTimeout(viewStatus, 100);
		}
	};
	viewStatus();
}



function preload0(file) {
	var img = "";
	var imgObj = new Image();
	imgObj.src = file;

	var waitImageLoading = function(){
		if(imgObj.complete){
			img.src = imgObj.src;
			clearInterval();
		}
	}

	setInterval(waitImageLoading, 100);
	str = '<img src="' + file + '" onload="hpdatahtml();">';
	document.getElementById("view") = str;

//	hpdatahtml();
}



//onloadで起動
function hpdatahtml() {
	document.getElementById('hp_data').innerHTML = html ;
}


//画像表示部分タグ書き出し

function makeTag(numr) {

	var numr;
	makefilename(numr);

	var date = hotpic[numr][0];
	var title = hotpic[numr][1];
	var description = hotpic[numr][2];




	if ( location.href.indexOf("/j/") > 0 ) {
		var path = 'http://www.jamstec.go.jp/j/hot_pictures/';
	} else {
		var path = 'http://www.jamstec.go.jp/e/hot_pictures/';
	}

	var directURL = path + '?' + numr;


//	html += '<!-- 日付 --><p class="date">' + date + '<\/p>\n';
//	html += '<!-- 日付 --><p class="date">' + date + ' [' + numr + ']' + '<\/p>\n';
	html += '<!-- 日付 --><p class="date">' + date + ' [' + numr + ']' + '<br \/>\n';
	html += '<!-- URL -->Direct link to this image : <a href="' + directURL + '">' + directURL + '</a></p>' + '<\/p>\n';
	html += '<!-- 画像 --><img class="photo" alt="' + title + '" src="' + file + '">\n';
//	html += '<!-- 説明文 --><p class="description">' + description + '<\/p>\n';
//	html += '<!-- 説明文 -->' + description;

	html += '<!-- 説明文 -->' + description;

//	html += '<!-- URL --><p style="margin-top:10px; padding-top:5px; text-align:center; border-top: 1px dotted #999999;">Direct URL of this image : <input type="text" style="width:25em;" value="' + directURL + '"></p>' ;
//	html += '<!-- URL --><p style="margin-top:10px; padding-top:5px; text-align:center; border-top: 1px dotted #999999;">Direct URL to this image : <a href="' + directURL + '">' + directURL + '</a></p>' ;
//	html += '<!-- URL --><p style="margin-bottom:10px; text-align:right;">Direct URL to this image : <a href="' + directURL + '">' + directURL + '</a></p>' + '\n' ;




}


//サムネール部分タグ書き出し

function writeThumbnail() {

	number = hotpic.length - 1 ;
	thumbhtml="";

	br = navigator.appName.toUpperCase();
	os = navigator.userAgent.toUpperCase();
if ((br.indexOf("EXPLORER") >= 0 && os.indexOf("MAC") >= 0) == false){
//横1行
	segment = 139; //1つあたり139px
	areawidth = number * segment + 'px';
	document.getElementById('thumbnail_list').style.width = areawidth;
	document.getElementById('thumbnail_list').style.left = '0px';
}



	for (i = number-1; i >= 0; i--) {

		numt = i + 1;
		if ( numt < 10 ) { zeronumt = '00' + numt ; }
		else if ( numt <100 ) { zeronumt = '0' + numt ; }
		else { zeronumt = numt ; } 
	
		var thumbfile = '/j/hot_pictures/img/thumb_' + zeronumt + '.jpg';
		var date = hotpic[numt][0];
		var title = hotpic[numt][1];
	
		thumbhtml += '<div class="thumb">\n';
		thumbhtml += '<div class="thumb_bg"><img src="/j/hot_pictures/img/thumb_bg_top.gif" width="134" height="6" alt="" /></div>\n';
		thumbhtml += '<div class="thumb_data">\n';
		thumbhtml += '<!-- 写真 -->\n';
//		thumbhtml += '' + numt + '<br />\n';
		thumbhtml += '<a href="javascript:void(0);" onclick="javascript:changePic(\'' + numt + '\');">\n';
		thumbhtml += '<img src="/j/hot_pictures/img/thumb_' +zeronumt + '.jpg" width="120" height="90" alt="' + title + '" /></a><br />\n';
		thumbhtml += '<!-- 日付 -->\n';
		thumbhtml += date + '</div>\n';
//		thumbhtml += date + ' [' + numt + ']' + '</div>\n';
//		thumbhtml += '[' + numt + '] ' + date + '</div>\n';
		thumbhtml += '<div class="thumb_bg"><img src="/j/hot_pictures/img/thumb_bg_bottom.gif" width="134" height="8" alt="" /></div>\n';
		thumbhtml += '</div>\n';

	}

	document.write(thumbhtml) ;	//HTML出力

	page = 0 ;
	start = number;
	end = start - 4;
	counter();


}


//サムネエールエリア移動

function moveThumbnail(dir) {

//移動距離
	var movewidth = 690 ;
	var position = document.getElementById('thumbnail_list').style.left;
	position = parseInt( position.replace('px','') );
//	page = parseInt( Math.abs( position ) / movewidth ) + 1 ;

	width = document.getElementById('thumbnail_list').style.width;
	width = parseInt( width.replace('px','') );
//	maxpage = parseInt( width / movewidth );
//	maxpage = parseInt( width / (segment * 5) -1 );

if ( width % (segment * 5) == 0 ) {
	maxpage = parseInt( width / (segment * 5) -1 );
} else {
	maxpage = parseInt( width / (segment * 5) );
}

	if (dir == '+' || dir == '-' ) {
		if (dir == '+' ) { position = position - movewidth ;}
		if (dir == '-' ) { position = position + movewidth ;}
	} else if (dir == '++' || dir == '--' ) {
		if (dir == '++' ) { position = maxpage * movewidth * -1 ;}
		if (dir == '--' ) { position = 0 ;}
	}

	if (position + width <= 0) { position = position +movewidth ;}
	if (position > 0) { position = position -movewidth ; }
	
	number = hotpic.length - 1 ;
	page = parseInt( Math.abs( position ) / movewidth ) ;
//	page = parseInt( parseInt( position.replace('px','') ) / movewidth * -1 ) ;

	position = position + 'px';
	document.getElementById('thumbnail_list').style.left = position;

	start = number - 5 * page;
	end = start - 4;
	counter();
}



function endLeft() {
	document.getElementById('moveleft').innerHTML = '<a class="movethumbleft_end" href="javascript:void(0);">-</a>';
}

function endRight() {
	document.getElementById('moveright').innerHTML = '<a class="movethumbright_end" href="javascript:void(0);">+</a>';
}

function noendLeft() {
	document.getElementById('moveleft').innerHTML = '<a class="movethumbleft" href="javascript:void(0);" onclick="javascript:moveThumbnail(\'-\');">-</a>';
}

function noendRight() {
	document.getElementById('moveright').innerHTML = '<a class="movethumbright" href="javascript:void(0);" onclick="javascript:moveThumbnail(\'+\');">+</a>';
}



function endLeftEnd() {
	document.getElementById('moveleftend').innerHTML = '<a class="movethumbleftend_end" href="javascript:void(0);">--</a>';
}

function endRightEnd() {
	document.getElementById('moverightend').innerHTML = '<a class="movethumbrightend_end" href="javascript:void(0);">++</a>';
}

function noendLeftEnd() {
	document.getElementById('moveleftend').innerHTML = '<a class="movethumbleftend" href="javascript:void(0);" onclick="javascript:moveThumbnail(\'--\');">--</a>';
}

function noendRightEnd() {
	document.getElementById('moverightend').innerHTML = '<a class="movethumbrightend" href="javascript:void(0);" onclick="javascript:moveThumbnail(\'++\');">++</a>';
}


function counter() {
	var counter ;
	if ( end <= 0 ){ end = 1 ;}
	if ( start == end ) {
		counter = '[ ' + start + ' / ' + number + ' ]' ;
	} else {
		counter = '[ ' + start + ' - ' + end + ' / ' + number + ' ]' ;
	}
	document.getElementById('position').innerHTML = counter;

	if ( start == number ){	//1枚目の時
		endLeft();			//左ボタンを不能
		endLeftEnd();		//左端ボタンを不能
	} else {				//1枚目ではない時
		noendLeft();		//左ボタンを可能
		noendLeftEnd();	//左端ボタンを可能
	}

	if ( end == 1 ){			//最終の時
		endRight();		//右ボタンを不能
		endRightEnd();		//右端ボタンを不能
	} else {				//最終ではない時
		noendRight();		//右ボタンを可能
		noendRightEnd();	//右端ボタンを可能
	}


}


//パス付きファイル名を作る

function makefilename(number){
	if ( number < 10 ) { zeronumr = '00' + number ; }
	else if ( number <100 ) { zeronumr = '0' + number ; }
	else { zeronumr = number ; }
	file = '/j/hot_pictures/img/hp_' + zeronumr + '.jpg';
}




