


// functions

function controlPanel() {
	// hide contents
	if ( itemListFilterVisible == "block" ) { hideContents(); }
	if (currentFileName=='new' || currentFileName=='supportCGI' || currentFileName=='supportCom' || currentFileName=='supportCreator' || currentFileName=='supportDomain' || currentFileName=='supportGame' || currentFileName=='supportIllust' || currentFileName=='supportServer' || currentFileName=='linkGame' || currentFileName=='linkKouryaku' || currentFileName=='linkRank') { return; }
	document.write('<div id="control-filter"></div>');
	requestFile('','GET', '/includes/control.php',false,'printControlPanel');
}

function printControlPanel(s) {
	$("control-filter").innerHTML = decodeURIComponent(s);
}

function setListViewType() {
	$('item-list-block').className = sListViewType;
	// button refresh
	var obj = $('list-view-type-btn-default');
	if ( sListViewType == 'view-default' ) {
		obj.style.color = "#f60";
		obj.style.fontWeight = "bold";
		obj.style.textDecoration = "none";
		obj.style.backgroundImage = "url(/images/arrow2.gif)";
	} else {
		obj.style.color = "#00f";
		obj.style.fontWeight = "normal";
		obj.style.textDecoration = "underline";
		obj.style.backgroundImage = "url(/images/arrow.gif)";
	}
	var obj = $('list-view-type-btn-simple');
	if ( sListViewType == 'view-simple' ) {
		obj.style.color = "#f60";
		obj.style.fontWeight = "bold";
		obj.style.textDecoration = "none";
		obj.style.backgroundImage = "url(/images/arrow2.gif)";
	} else {
		obj.style.color = "#00f";
		obj.style.fontWeight = "normal";
		obj.style.textDecoration = "underline";
		obj.style.backgroundImage = "url(/images/arrow.gif)";
	}
	var obj = $('list-view-type-btn-text');
	if ( sListViewType == 'view-text' ) {
		obj.style.color = "#f60";
		obj.style.fontWeight = "bold";
		obj.style.textDecoration = "none";
		obj.style.backgroundImage = "url(/images/arrow2.gif)";
	} else {
		obj.style.color = "#00f";
		obj.style.fontWeight = "normal";
		obj.style.textDecoration = "underline";
		obj.style.backgroundImage = "url(/images/arrow.gif)";
	}
}

function hideContents() {
	var winHeight = document.all ? document.body.clientHeight : window.innerHeight;
	document.getElementById('contents').style.marginTop = winHeight + "1000px";
	document.getElementById('container').style.backgroundImage = "url(./images/loading.gif)";
}

function showContents() {
	document.getElementById('contents').style.marginTop = "0";
	document.getElementById('container').style.backgroundImage = "none";
}

function refreshItemList() {
	
	var lp = location.pathname;
	if (!$('control-filter')) {
		//$('control-filter').style.display = "none";
		showContents(); return;
	}
	
	// filter
	var s = '';
	var itemCount = 0;
	var itemCountTotal = 0;
	var itemtypes = ",";
	var nodes = document.getElementById('item-list-block').childNodes;
	for (i=0;i<nodes.length;i++) {
 		if (!nodes[i].id) continue;
 		
 		if (nodes[i].id.substring(0,4) == "item") { itemCountTotal ++; }
		
		
		if (((","+aSelectedType.join(",")+",").indexOf(","+nodes[i].getAttribute('itemType')+",")) > -1 || aSelectedType.length == 0 || itemListFilterVisible != "block") {
			nodes[i].style.display = 'block';
 			if (nodes[i].id.substring(0,4) == "item") {
 				itemCount ++;
 			}
		} else {
			nodes[i].style.display = 'none';
		}
		
		itemtypes += nodes[i].getAttribute('itemType') + ",";
		
		//s = s + nodes[i].id + "<br />"; // debug
		//s = s + nodes[i].itemType + "<br />"; // debug
	}
	//s += 'count='+itemCount;
	
	// not found
	if ( itemCount == 0 ) {
		$('debug').innerHTML = '<div class="message-block"><h3>'+itemCountTotal+'サイトのうち、該当するサイトはありませんでした。</h3><p>条件を変更するか、検索パネルを閉じてください。</p></div></div>';
	} else {
		$('debug').innerHTML = '';
	}
	
	// sort
	
	if (!bCheckSorted && sSelectedOrder == "+itemInsert") {
	} else {
		sortItem('itemId');
		if (itemListFilterVisible != "block") {
			sortItem('+itemInsert');
		} else {
			sortItem(sSelectedOrder);
		}
	}
	
	// debug
	/*
	for (i=0;i<nodes.length-1;i++) {
 		if (!nodes[i].id) continue;
		s = s + nodes[i].id + "*<br />";
		s = s + nodes[i].getAttribute('itemType') + "<br />";
		s = s + nodes[i].getAttribute('itemInsert') + "<br />";
	}
	*/
	//document.getElementById("debug").innerHTML = "test=<br />" + s;
	
	showContents();
	
	// refresh filter buttons
	/*for (n=0;n<15;n++) {
		if (!document.getElementById("control-filter-item-" + n)) continue;
		if ((","+aSelectedType.join(",")+",").indexOf(","+n+",") > -1 || aSelectedType.length == 0) {
			document.getElementById("control-filter-item-" + n).style.color = "#f60";
			document.getElementById("control-filter-item-" + n).style.textDecoration = "underline";
		} else {
			document.getElementById("control-filter-item-" + n).style.color = "#00f";
			document.getElementById("control-filter-item-" + n).style.textDecoration = "none";
		}
	}*/
	
	// refresh filter buttons visible
	for (i=0;i<nodes.length;i++) {
 		if (!nodes[i].id) continue;
		itemtypes += nodes[i].getAttribute('itemType') + ",";
	}
	for (n=0;n<15;n++) {
		var obj = $("control-filter-item-" + n);
		if (!obj) continue;
		if ((","+aSelectedType.join(",")+",").indexOf(","+n+",") > -1 || aSelectedType.length == 0) {
			obj.style.color = "#f60";
			obj.style.textDecoration = "underline";
			obj.parentNode.style.backgroundImage = "url(/images/arrow2.gif)";
		} else {
			obj.style.color = "#00f";
			obj.style.textDecoration = "none";
			obj.parentNode.style.backgroundImage = "url(/images/arrow.gif)";
		}
		if (itemtypes.indexOf(","+n+",") == -1) {
			obj.href = "#";
			obj.style.color = "#ccc";
			obj.style.textDecoration = "none";
			obj.parentNode.style.backgroundImage = "url(/images/arrow.gif)";
		}
	}
	for (var n in aSelectedType) {
		if ($("control-filter-item-" + aSelectedType[n]).href.substr(-1,1)== "#") {
			$("control-filter-item-" + aSelectedType[n]).style.color = "#999";
		}
	}
	
	// refresh order buttons
	for (n=0;n<12;n++) {
		if (!document.getElementById("control-order-item-" + n)) continue;
		var o = document.getElementById("control-order-item-" + n);
		if (o.getAttribute('orderId') == sSelectedOrder) {
			o.style.color = "#f60";
			o.textDecoration = "underline";
		} else {
			o.style.color = "#00f";
			o.style.textDecoration = "none";
		}
	}
	
	// refresh counter
	document.getElementById("display-item-count").innerHTML = itemCount + ' / ' + itemCountTotal;
	
	// refresh control-filter panel
	var obj = document.getElementById("control-filter-inner");
	obj.style.display = itemListFilterVisible;
	if (itemListFilterVisible == "block") {
		document.getElementById("control-filter").style.paddingBottom = "10px";
	} else {
		document.getElementById("control-filter").style.paddingBottom = "0";
	}
}


function switchOrder(s) {
	bCheckSorted = true;
	sSelectedOrder = s;
	setCookie();
	hideContents();
	setTimeout('refreshItemList()',30);
}


function switchFilterType(i) {

	var vDel = false;
	for (var n in aSelectedType) {
		if (aSelectedType[n] == i) {
			aSelectedType.splice(n,1);
			vDel = true;
		}
	}
	if (!vDel) {
		aSelectedType.push(i);
	}
	setCookie();
	hideContents();
	setTimeout('refreshItemList()',1);
}


function switchFilterTypeReset() {
	aSelectedType = [];
	setCookie();
	hideContents();
	setTimeout('refreshItemList()',1);
}

function switchListViewType(s) {
	sListViewType = 'view-'+s;
	setListViewType();
	setCookie();
}

function controlFilterVisible() {
	if (itemListFilterVisible == "block") {
		itemListFilterVisible = "none";
	} else {
		itemListFilterVisible = "block";
	}
	setCookie();
	hideContents();
	setTimeout('refreshItemList()',1);
}


function swapItem(a,b) {
	a = $(a);
	b = $(b);
	var pa = a.parentNode;
	var pb = b.parentNode;
	var c = document.createElement('div');
	pa.replaceChild(c, a);
	pb.replaceChild(a, b);
	pa.replaceChild(b, c);
}


function sortItem(s) {
	var nodes = document.getElementById('item-list-block').childNodes;
	if (s) {
		if (s.substr(0,1) == '+') {
			for (i=0;i<nodes.length-1;i++) {
		 		if (!nodes[i].id) continue;
				for (j=0;j<nodes.length;j++) {
		 			if (!nodes[j].id) continue;
					a = nodes[j].getAttribute(s.substr(1,s.length-1));
					b = nodes[i].getAttribute(s.substr(1,s.length-1));
					if (Number(a) < Number(b)) {
						swapItem(nodes[i].id,nodes[j].id);
					}
				}
			}
		} else {
			for (i=0;i<nodes.length-1;i++) {
		 		if (!nodes[i].id) continue;
				for (j=0;j<nodes.length;j++) {
		 			if (!nodes[j].id) continue;
					a = nodes[j].getAttribute(s);
					b = nodes[i].getAttribute(s);
					if (Number(a) > Number(b)) {
						swapItem(nodes[i].id,nodes[j].id);
					}
				}
			}
		}
	}
}

