var httpObj = null;
var htmlTimer = null;
var xmlTimer = null;
var timeout = 10;

var list_url = 'api/ver1/search_list.php';
var shop_url = 'http://eat-point.jp/index.php?cmd=shop&id=';

function showShopList(){
	// Show the requested shop list block
	document.getElementById('shoplist').style.display = 'block';
	
	// Get the shop list
	requestShopData();
	
	// Set the timer to update the shop list every 5 seconds
	xmlTimer = setInterval("requestShopData()", 5000);
}

function requestShopData(){
	httpXmlRequest(list_url, 'GET', '', showSearchedShopInfo);
}

function showSearchedShopInfo(xml){
	var date = new Date();
	var now = '　（' + date.getHours() + '時' + date.getMinutes() + '分' + date.getSeconds() + '秒現在）';
	document.getElementById('search_time').innerHTML = now;
	
	var shops = xml.getElementsByTagName('shop');
	for(var i = 0; i < shops.length; i++){
		var shop_id = '';
		var photo_url = '';
		var shop_name = '';
		var category = '';
		var address = '';
		
		for(var j = 0; j < shops[i].childNodes.length; j++){
			var child = shops[i].childNodes[j];
			if(child.tagName == 'shop_id'){
				if(child.firstChild != null){
					shop_id = child.firstChild.nodeValue;
				}
			}else if(child.tagName == 'photo_url'){
				if(child.firstChild != null){
					photo_url = child.firstChild.nodeValue;
				}
			}else if(child.tagName == 'name'){
				if(child.firstChild != null){
					shop_name = child.firstChild.nodeValue;
				}
			}else if(child.tagName == 'category'){
				if(child.firstChild != null){
					category = child.firstChild.nodeValue;
				}
			}else if(child.tagName == 'address'){
				if(child.firstChild != null){
					address = child.firstChild.nodeValue;
				}
			}
		}
		
		var image_tag = '<img src="' + photo_url + '" width="60" height="60" border="0" src="" alt="" />';
		document.getElementById('photo' + i).innerHTML = '<a href="' + shop_url + shop_id + '">' + image_tag + '</a>';
		document.getElementById('name' + i).innerHTML = '<a href="' + shop_url + shop_id + '">' + shop_name + '</a>';
		document.getElementById('category' + i).innerHTML = category;
		document.getElementById('address' + i).innerHTML = address;
	}
}

function checkTimeout(){
	timeout --;
	if(timeout <= 0){
		if(htmlTimer != null){
			clearInterval(htmlTimer);
		}
		if(xmlTimer != null){
			clearInterval(xmlTimer);
		}
		// Abort HTTP request
		httpObj.abort();
	}
}

function httpXmlRequest(url, method, data, callback){
	try{
		if(window.XMLHttpRequest){
			httpObj = new XMLHttpRequest();
		}else if(window.ActiveXObject){
			httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}catch(e){
		httpObj = null;
	}
	
	htmlTimer = setInterval('checkTimeout()', 1000);
	
	if(httpObj != null){
		httpObj.open(method, url, true);
		httpObj.onreadystatechange = function(){
			if (httpObj.readyState == 4){
				clearInterval(htmlTimer);
				if (httpObj.status == 200){
					callback(httpObj.responseXML);
				}else{
					alert(httpObj.status + ' : ' + httpObj.statusText);
				}
			}
		}
		httpObj.setRequestHeader("If-Modified-Since", "Thu, 01 Jun 1970 00:00:00 GMT");
		httpObj.send(data);
	}
}
