//グローバル変数の定義
var httpObj = null;		// HTTP通信用オブジェクト
var timerId;			// HTTP通信用タイマーオブジェクト
var timeout_sec = 10;	// HTTP通信タイムアウト秒数
var xml_url= "./data/dl-button-info.xml";

function httpXmlRequest()
{
	try {
		httpObj = new XMLHttpRequest();
	} catch(e) {
		try {
			httpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				httpObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				httpObj = null;
			}
		}
	}
	if (! httpObj) {
		httpObjGenerateFail();
	}

	timerId = setInterval('timeoutCheck()', 1000);
    httpObj.open("GET", xml_url, true);
	httpObj.onreadystatechange = function() {
			if (httpObj.readyState == 4) {
			clearInterval(timerId);
				if (httpObj.status == 200) {
					dispXmlData(httpObj.responseXML);
				} else {
					httpError(httpObj.status + ' : ' + httpObj.statusText);
					return false;
				}
			}
		}
	httpObj.send(null);
}

function dispXmlData(xmlData)
{
	var pIdx, idx;
	var arrButInfo = new Array();
	var ul_obj = document.getElementById('sublinks');

	//タグ情報取得
	butInfoListTags = xmlData.getElementsByTagName("DLButInfo");

	// XML読み込み
	for (pIdx = 0; pIdx < butInfoListTags.length; pIdx++) {
		var cIdx;

		for (cIdx = 0; cIdx < butInfoListTags[pIdx].childNodes.length; cIdx++) {
			var child = butInfoListTags[pIdx].childNodes[cIdx];
			var refTags;
			var titleTags;

			if (child.tagName == 'title') {
				if (child.firstChild) {
					titleTags = child.firstChild.nodeValue;
				}
			}
			if (child.tagName == 'ref') {
				if (child.firstChild) {
					refTags = child.firstChild.nodeValue;
				}
			}
		}
		arrButInfo[pIdx] = "<li class=\"sublink\"><a href=\"javascript://\" onclick=\"jBox.open('iframe-jBoxID','iframe','" + refTags + "','pop','width=540,height=400,center=true,draggable=true');\"><span>" + titleTags + "</span></a></li>\n";
		}

	// ランダム順作成
	for (idx = 0; idx < arrButInfo.length; idx++) {
		var rnd;
		var CVal, RVal;

		rnd = Math.floor(Math.random() * arrButInfo.length);

		CVal = arrButInfo[idx];
		RVal = arrButInfo[rnd];

		arrButInfo[idx] = RVal;
		arrButInfo[rnd] = CVal;
	}

	// 表示文字列作成
	var putHtml = "<ul id=\"sublinks\" class=\"sublinks\">";
	for (idx = 0; idx < arrButInfo.length; idx++) {
		putHtml = putHtml + arrButInfo[idx];
	}
	putHtml = putHtml + "</ul>";

	ul_obj.innerHTML = putHtml;
}

function httpError(error)
{
	alert(error);
}

function timeoutCheck()
{
	timeout_sec --;
	if (timeout_sec <= 0) {
		// タイマーをストップする
		clearInterval(timerId);
		// HTTPリクエストを中断する
		httpObj.abort();
		return false;
	}
}

function OpenWin(pop_url)
{
	var width=600;
	var height=450;
	var url = pop_url;
	var windowname = "new";
	var features="location=no, directories=no, menubar=no, status=no, scrollbars=no, resizable=no, toolbar=no";

	if (width) {
		if (window.screen.width > width) {
			features += ", left=" + (window.screen.width-width) / 2;
		} else {
			width = window.screen.width;
		}
		features += ", width=" + width;
	}
	if (height) {
		if (window.screen.height > height) {
			features += ", top=" + (window.screen.height-height) / 2;
		} else { 
			height = window.screen.height;
		}
		features += ", height=" + height;
	}
	var win= window.open(url,windowname,features);

//	x = (screen.width -600 ) / 2;
//	y = (screen.height -500) / 2;

//	win=window.open(pop_url,"new","width=600,height=450");
//	win.moveTo(x,y);
	win.focus(); 
}

