﻿$(document).ready(function() {
	show_res();
});

function add_res(tid){
		$.ajax({
		async: false,
			url: '/add_res/add_res_list.php',
			cache: false,
			type: 'POST',
			data: {
				tid: tid,
				action: "add"
			},
			error: function(xhr) {
				alert('Ajax request error');
			},
			success: function(response) {
				myResult = eval('(' + response + ')');
				if(myResult.uid <= 0){
				var link;
					link = location.pathname;
					subs = link.substring(link.length-4,link.length);
					if (subs != '.php'){
						link += 'index.php';
					}
					alert("立即登入/註冊成為會員，可以收藏您經常會叫外賣的餐單，方便你每次使用，仲可以去討論區分享下您既心水外賣。");
					window.open('http://takeaway.happymacao.com/forum/logging.php?action=login&referer='+link, '');
				}
				show_res();
				
			}
		});
}

function del_res(tid){
		$.ajax({
		async: false,
			url: '/add_res/add_res_list.php',
			cache: false,
			type: 'POST',
			data: {
				tid: tid,
				action: "delete"
			},
			error: function(xhr) {
				alert('Ajax request error');
			},
			success: function(response) {
				myResult = eval('(' + response + ')');
				if(myResult.uid <= 0){
					var link;
					link = location.pathname;
					subs = link.substring(link.length-4,link.length);
					if (subs != '.php'){
						link += 'index.php';
					}
					alert("立即登入/註冊成為會員，可以收藏您經常會叫外賣的餐單，方便你每次使用，仲可以去討論區分享下您既心水外賣。");
					window.open('http://takeaway.happymacao.com/forum/logging.php?action=login&referer='+link, '');
				}
				show_res();
			}
		});
}

function show_res(){
	limit = 10;	
		$.ajax({
		async: false,
			url: '/add_res/add_res_list.php',
			type: 'POST',
			data: {
				action: "show"
			},
			error: function(xhr) {
				alert('Ajax request error');
			},
			success: function(response) {
				//alert(response);
				var link;
				link = location.pathname;
				subs = link.substring(link.length-4,link.length);
				if (subs != '.php'){
					link += 'index.php';
				}
				
				myResult = eval('(' + response + ')');
				str = "";
				str = '<ul id="mymenu"><li id="mymenutitle"></li>';
				if(myResult[0].form != 1){
					
					if(myResult[0].rec == 1){
						for(var i = 0; i < myResult.length && i < limit; i++) {
							title1 = '';
							title1 = myResult[i].title;
							if (title1.length>6){
								title1 = title1.substr(0,6)+' ...';
							}
							
							tele1 = '';
							tele1 = myResult[i].tele1;
							if (tele1.length>8){
								tele1 = tele1.substr(0,8);
							}
							
							tele2 = '';
							tele2 = myResult[i].tele2;
							if (tele2.length>8){
								tele2 = tele2.substr(0,8);
							}
							
							numa = i+1;
							str += '<li><div style="float: left;">';
							str += '<a href = "'+ myResult[i].link + '" alt = "' + myResult[i].title + '">' + title1 + '</a>';
							str += '<p>' +  tele1 + '&nbsp;&nbsp;&nbsp;' + tele2 + '</p></div>';
							str += ' <a href = "#" onclick = del_res(' + myResult[i]['id'] + ') id="remove"></a></li>';
							
						}
					}else{
						str += '<li><div style="float: left;">尚未加入任何餐單。</li>';
					}
				
					//str += '<li><a href="/forum/logging.php?action=logout&formhash='+myResult[0].form+'&referer='+link+'">按此登出</a></li>';
				}else{
					str += '<li>請先 <a href="/forum/logging.php?action=login&referer='+link+'">登入</a> 或 <a href="/forum/register.php">註冊</a>。</li>';
				}
				str += '</ul>';
				//alert(str);
				$('#tabs_menub').html(str);
			}
		});
}
