// http://webos-goodies.jp/archives/50548720.html

httpRequest = false;

var imgcount = 4;
$('#randomimg img').load(function(){
	// ページの読み込みが完了した後に実行するコード
	if (imgcount-- == 1) {
		$('#randomimg').fadeIn('slow');
		imgcount = 4;
	}
});


if(window.XMLHttpRequest && (navigator.userAgent.indexOf('MSIE') == -1)) {
	// Firefox, Opera など
	httpRequest = new XMLHttpRequest();
	httpRequest.overrideMimeType('text/html');
}
else if(window.ActiveXObject) {
	// IE
	try {
		httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch (e) {
		httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
	}
}

function renew(num) {
	httpRequest.abort();
	httpRequest.open('GET', 'rand_img.cgi?renew_' + num, true);
	// IEのキャッシュを強制的に更新させる
	httpRequest.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
	httpRequest.onreadystatechange = processResponse;
	httpRequest.send(null);
}

function processResponse() {
	if(httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			// リクエストの処理
			/*
			var linktext = '<script type="text/JavaScript"><!--';
			linktext = linktext + 'document.write(\'<a href="#" onClick="JavaScript:renew(); return false;">次</a>\');';
			linktext = linktext + '// --></script>';
			*/
			//var linktext = '<img src="./img/right.png" alt="次へ" style="margin:54px 0px 0px 4px;">';
			//linktext = '<a href="#" onClick="JavaScript:renew(); return false;">' + linktext + '</a>';
			
			$('#randomimg').hide();
			document.getElementById('randomimg').innerHTML = httpRequest.responseText; //+ linktext;
			$('#randomimg').fadeIn('slow');
		}
		else {
			// エラー処理
			var errmsg = '<span style="margin:10px 0px;">エラー： 読み込みに失敗しました</span>';
			document.getElementById('randomimg').innerHTML = errmsg;
		}
	}
}

