var marketList;

function loadData(){
	loadNMData();
	// now we load region 0
	var region_0 = document.getElementById('region_0');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 0) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
	// now we load region 1
	var region_0 = document.getElementById('region_1');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 1) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
	// now we load region 2
	var region_0 = document.getElementById('region_2');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 2) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
	// now we load region 3
	var region_0 = document.getElementById('region_3');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 3) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
	// now we load region 4
	var region_0 = document.getElementById('region_4');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 4) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
	// now we load region 5
	var region_0 = document.getElementById('region_5');
	for(var i=0; i<marketList.length; i++){
		var tmpMarket = marketList[i];
		if(tmpMarket.region != 5) continue;
		//alert(marketList[i].name);		
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('title', tmpMarket.description);
		a.setAttribute('href', 'javascript:nmSelect("'+ i +'")');
		var link = document.createTextNode(tmpMarket.name);
		// now contruct the link
		a.appendChild(link);
		region_0.appendChild(a);
		var br = document.createElement('br');
		region_0.appendChild(br);
	}
	
}

function loadVender(){
	// vender name
	var vender_name = document.getElementById('vender_name');
	vender_name.innerHTML = vender.name;
	
	// vender contact
	var vender_contact = document.getElementById('vender_contact');
	vender_contact.innerHTML = '地址：' + vender.address + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;電話：' + vender.tel_1;
	
	// vender shop pic
	var vender_picture = document.getElementById('vender_picture');
	var img = document.createElement('img');
	img.setAttribute('src', vender.shop);
	img.setAttribute('width', "320");
	img.setAttribute('height', "240");
	vender_picture.appendChild(img);
	
	// vender google map
	var map_url = "http://maps.google.com.tw/maps/api/staticmap?center="+vender.latitude+","+vender.longitude+"&zoom=18&size=570x240&maptype=roadmap&markers=color:blue|label:G|"+vender.latitude+","+vender.longitude+"&sensor=false";
	var vender_map = document.getElementById('vender_map');
	var img = document.createElement('img');
	img.setAttribute('src', map_url);
	vender_map.appendChild(img);
}

function loadMenu(){
	var menu_table = document.getElementById('menu');
	for(var i=0; i<menu_array.length; i++){
		var tmpMenu = menu_array[i];
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		//td.setAttribute('height', '25');
		var a = document.createElement('a');
		a.setAttribute('class', 'itemTitle_template');
		a.setAttribute('href', 'javascript:menuSelect("'+ i +'")');
		var link = document.createTextNode(tmpMenu.name);

		// now contruct the link
		a.appendChild(link);
		td.appendChild(a);
		tr.appendChild(td);
		menu_table.appendChild(tr);
	}
}

function menuSelect(m_id){
	// check if the vender has the menu[0]
	if (menu_array.length == 0) return;
	
	var dish = menu_array[m_id];	
	// load dish picture
	var dish_picture = document.getElementById('dish_picture');	
	while(dish_picture.childNodes[0]){	
		dish_picture.removeChild(dish_picture.childNodes[0])
	}
	var img = document.createElement('img');
	img.setAttribute('src', dish.picture);
	img.setAttribute('width', "320");
	img.setAttribute('height', "239");
	dish_picture.appendChild(img);
	
	// now it is the name term
	var dish_name = document.getElementById('dish_name');
	dish_name.innerHTML=dish.name;
	
	// price
	var dish_price = document.getElementById('dish_price');
	dish_price.innerHTML=dish.price;
	
	// description
	var dish_description = document.getElementById('dish_description');
	dish_description.innerHTML=dish.description;
}

function readFile(url){
	var xmlhttp;
	if(window.ActiveXObject){
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {		
		try {
	      xmlhttp = new XMLHttpRequest();
	    } catch (e) {
	      xmlhttp = false;
	      alert('XHR error: ' + e);
	    }
	}  
	url += '?timestamp=' + new Date().getTime();  
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	return xmlhttp;
}

function loadNMData(){
	// now we retrive the err msg file
	var d = new Date();
	var msg_file = 'venderData/market.xml?ts=' + d.getTime();
	temp = readFile(msg_file);	
	var msgDoc = temp.responseXML.documentElement;
	var nmlist = msgDoc.getElementsByTagName("night_market");
	
	//alert(nmlist.length);
	marketList = new Array();
	for(var i=0; i<nmlist.length; i++){
		temp_nm = nmlist[i];
		var nm_obj = new Object();
		nm_obj.id = temp_nm.getElementsByTagName("id")[0].firstChild.nodeValue;
		nm_obj.name = temp_nm.getElementsByTagName("name")[0].firstChild.nodeValue;
		nm_obj.description = temp_nm.getElementsByTagName("description")[0].firstChild.nodeValue;
		nm_obj.latitude = temp_nm.getElementsByTagName("latitude")[0].firstChild.nodeValue;
		nm_obj.longitude = temp_nm.getElementsByTagName("longitude")[0].firstChild.nodeValue;
		nm_obj.delta = temp_nm.getElementsByTagName("delta")[0].firstChild.nodeValue;
		nm_obj.region = temp_nm.getElementsByTagName("region")[0].firstChild.nodeValue;
		marketList.push(nm_obj);
	}
	//alert('nm list: ' + marketList.length);
}


