//系统配置参数
var wdt={
	width:0,//当前屏幕的宽度
	height:0,//当前屏幕的高度
	store_code:'',//店铺编号
	shopcode:'',//店铺编号
	room:'',//餐台号
	servecode:'',//供应商
	openid:0,//当前微信来的会员
	act:'menu_list',//当前微信来的会员
	peisong:'N',//当前微信来的会员
	msg:function(mess,autoclose){
		var msg_o = $('#wdt_msg');
 		$('#wdt_cover').show();
		msg_o.show();
		msg_o.html(mess);
 		l = (wdt.width-msg_o.width()-30)/2;
		t = (wdt.height-msg_o.height()-30)/2;
 		msg_o.css({'left':l,'top':t});
		f = autoclose==false?false:true;
 		if(f)
			$('#wdt_cover').bind('click',function(){wdt.msgClose();})
   	},
	msgClose:function(){
		$('#wdt_msg').hide();
		$('#wdt_cover').hide();
	},
	tip:function(msg){
		var msg_o = $('#wdt_msg');
 		$('#wdt_cover').show();
		msg_o.show();
		msg_o.html(msg);
 		l = (wdt.width-msg_o.width()-30)/2;
		t = (wdt.height-msg_o.height()-30)/2;
 		msg_o.css({'left':l,'top':t});
	}
};

//手机上的模拟页面 
var _page = {
	page:[],
	cur:null,//当前页的对象
	//在手机上模拟一个一页
	open:function(callback,success){
 		var win = $("<div class='page'><table width=100% height=100% border=0><tr><td align=center><div class='page_loading'>正在载入中...</div></td></tr></table></div>");
		$(win).css({'width':0,'height':wdt.height,left:wdt.width});
		$("body").append(win);
		//保存当前的页
		this.page.push(win);
		//动画执行之前执行的
		if(typeof(callback)=='function'){
			callback(win);//测试
		}
		//动画效果打开窗口
		$(win).animate({left:0,'width':wdt.width},function(){
			if(typeof(success)=='function')success(win);
 		});
		_page.cur = win;
	},
	msg:function(msg){

	},
	close:function(callback){
		var ot = _page.page.pop();
		l = _page.page.length
		if(l>0){
			_page.cur = _page.page[l-1];		
		}
		$(ot).animate({'left':-1*wdt.width},function(){
 			$(ot).remove();
			if(typeof(callback)=='function'){
				callback();
			}
		});

		for(k in _cart.goods[_cart.code])
		{
			_cart.showGoodsNum(k);
			//_cart.goods[_cart.code][k]=[];
		}
		_cart.showTip();
	},
	gofirst:function(){
		_shop.go_class(_shop.class[0].i);
		_cart.showTip();
	}
	
};

//购物车类
var _cart = {
	//添加菜品
	code:'',
	goods:[],
	glide:[],
	setCode:function(c){
		_cart.code = c;
	},
	add:function(gid,num){
		//获取当前菜品
		var co = _shop.get_goods(gid);
		if(co){
			//判断地前门店是否有点单
			if(typeof(_cart.goods[_cart.code])=='undefined'){
				_cart.goods[_cart.code] = [];
			}
			//判断当前菜品是否已经有点单 
			if(typeof(_cart.goods[_cart.code][gid])=='undefined'){
 				_cart.goods[_cart.code][gid] = null;
				//保存当前菜品的详细点单信息 
				_cart.goods[_cart.code][gid] = {'id':gid,'code':co.c,'num':num,'price':co.p,'name':co.n,'unit':co.u,'taste':'','make':'','memo':'','img':co.m};
 			}else{
				_cart.goods[_cart.code][gid].num+=num;
			}
			//
			var od = $(_page.cur).find('#detail_num');
			num = parseFloat(od.text());
			od.text(num+1);
			_cart.showGoodsNum(gid);
			_cart.showTip();
			_cart.save();
			//设置一个流水号
			if(typeof(_cart.glide[_cart.code])=='undefined'){
				_cart.glide[_cart.code] = new Date().getTime();
			}
 		}
 	},
	//购物车减
	sub:function(gid,num){
		//判断是否存在
		if(typeof(_cart.goods[_cart.code][gid])!='undefined'){
			if(_cart.goods[_cart.code][gid].num-num>0){
				_cart.goods[_cart.code][gid].num-= num;
 			}else{
 				_cart.del(gid);
  			}
			var od = $(_page.cur).find('#detail_num');
			num = parseFloat(od.text());
			od.text(num-1);
 		}
		_cart.showGoodsNum(gid);
		_cart.showTip();
	},
	//删除ID指定的菜品
	del:function(gid){
 		var ng = [];
		for(j in _cart.goods[_cart.code]){
			ji = _cart.goods[_cart.code][j].id;
			if(ji!=gid){
				ng[ji] = _cart.goods[_cart.code][j];
			}
		}
		_cart.goods[_cart.code] = ng;
	},
	//得到指定的菜品
	get:function(gid){
 		if(_cart.goods[_cart.code] && typeof(_cart.goods[_cart.code][gid])!='undefined'){
			return _cart.goods[_cart.code][gid];
		}
		return null;
 	},
	//得到账单列表
	getMenuList:function(){
		var str = "";
		for(k in _cart.goods[_cart.code])
		{
			str+= k+"[数量："+_cart.goods[_cart.code][k].num+"]"+"\n";
		}
		alert(str);
	},
	//得到菜单的格式化
	getMenuString:function(){
		var str = '';
		for(k in _cart.goods[_cart.code]){
			var ko = _cart.goods[_cart.code][k];
			if(str==''){
				str = ko.id+"$"+ko.num+"$"+ko.price+"$"+ko.taste+"$"+ko.make;
			}else{
				str+= '|'+ko.id+"$"+ko.num+"$"+ko.price+"$"+ko.taste+"$"+ko.make;;
			}
		}
		return str;
	},
	//显示菜品的点单数量
	showGoodsNum:function(gid){
		n = 0;
		if(typeof(_cart.goods[_cart.code][gid])!='undefined'){
			n = _cart.goods[_cart.code][gid].num;
 		}
		if(n>0){
			$(_page.cur).find("#goods_"+gid).text(n);			
		}else{
			$(_page.cur).find("#goods_"+gid).text('');			
		}
	},
	//得到点单的明细
	getMenu:function(){
		var c = 0;
		var sum_m = 0;//点单金额
		var sum_n = 0;//点单数量
		for(j in _cart.goods[_cart.code]){
			n = _cart.goods[_cart.code][j].num;
			sum_n+= n;
			sum_m+= n*_cart.goods[_cart.code][j].price;
 			c++;
		}
		return {'num':sum_n,'money':sum_m};
	},
	//显示点单的tip
	showTip:function(){
 		var oM = _cart.getMenu();
		$(_page.cur).find("#cart_info").html('已点 '+oM.num+' / ￥'+oM.money);
	},
	//获取账单列表
	menuHtml:function(iswai){
		if(_shop.peisong=='Y'){
			var htmls = '<table border=0 width=100% cellpadding=3 cellspacing=1 class="table_menu">';
				htmls += '<tr>';
				htmls += '<td>送货时间</td>';
				htmls += '<td><input name="delivery_time" type="text" id="delivery_time" value="" class="search_input"></td>';
				htmls += '</tr>';
				htmls += '<tr>';
				htmls += '<td>送货地址</td>';
				htmls += '<td><input name="delivery_addr" type="text" id="delivery_addr" value="" class="search_input"></td>';
				htmls += '</tr>';
				htmls += '<tr>';
				htmls += '<td>联系人</td>';
				htmls += '<td><input name="delivery_user" type="text" id="delivery_user" value="" class="search_input"></td>';
				htmls += '</tr>';
				htmls += '<tr>';
				htmls += '<td>联系电话</td>';
				htmls += '<td><input name="delivery_tel" type="text" id="delivery_tel" value="" class="search_input"></td>';
				htmls += '</tr>';
				htmls += '<tr>';
				htmls += '<td>留言</td>';
				htmls += '<td><input name="delivery_memo" type="text" id="delivery_memo" value="" class="search_input"></td>';
				htmls += '</tr>';
			htmls += '</table>';
			htmls += '<table border=0 width=100% cellpadding=3 cellspacing=1 class="table_menu">';
			htmls += '<tr class="">';
			htmls += '<td> </td>';
			htmls += '</tr>';
			htmls += '</table>';
			htmls += '<table border=0 width=100% cellpadding=3 cellspacing=1 class="table_menu">';
		}else{
			var htmls = '<table border=0 width=100% cellpadding=3 cellspacing=1 class="table_menu">';
		}
		
		var sum_m = 0;
		var sum_n = 0;
		for(j in _cart.goods[_cart.code]){
			mo = _cart.goods[_cart.code][j];
			sum_n += mo.num;//数量
			sum_m += mo.num*mo.price;//金额
			htmls += '<tr>';
			htmls += '<td >'+mo.name+'</td>';
			htmls += '<td class="font_red">￥'+(mo.num*mo.price)+'</td>';
			htmls += '<td class="font_red">'+mo.unit+'</td>';
			htmls += '<td align=center><button class="menu_btn" onclick="_cart.list_sub('+mo.id+');">-</button></td>';
			htmls += '<td align=center>'+mo.num+'</td>';
			htmls += '<td align=center><button class="menu_btn" onclick="_cart.list_add('+mo.id+');">+</button></td>';
			htmls += '<td align=center><button class="menu_btn" onclick="goods_detail('+mo.id+');">详</button></td>';
			htmls += '</tr>';
		}
		htmls += '<tr>';
		htmls += '<th align=right>合计：</th>';
 		htmls += '<th colspan=6 align=center>数量:<font class="font_red">'+sum_n+'</font>&nbsp;&nbsp;&nbsp;金额：￥ <font class="font_red">'+sum_m+'</font>&nbsp;&nbsp;&nbsp;</th>';
		htmls += '</tr>';
		htmls += '</table>';
		return htmls;
	},
	//我的账单
	list:function(){
		_page.open(function(o){
			$(o).loadTemplate('menu_list',function(t){
 			});
		},function(){
			var uo = $(_page.cur).find("#page_menu_list");
			uo.html(_cart.menuHtml());
		});
	},
	//刷新当前账单
	listFlash:function(iswai){
		$(_page.cur).find("#page_menu_list").html(_cart.menuHtml(iswai));	
	},
	//账单加
	list_add:function(gid){
		if(typeof(_cart.goods[_cart.code][gid])!='undefined'){
			_cart.goods[_cart.code][gid].num+= 1;
			$(_page.cur).find("#page_menu_list").html(_cart.menuHtml());
 		}
	},
	//账单减
	list_sub:function(gid){
		//判断是否存在
		if(typeof(_cart.goods[_cart.code][gid])!='undefined'){
			if(_cart.goods[_cart.code][gid].num-1>0){
				_cart.goods[_cart.code][gid].num-= 1;
 			}else{
 				_cart.del(gid);
  			}
			$(_page.cur).find("#page_menu_list").html(_cart.menuHtml());
 		}
	},
	//保存账单
	save111:function(){
		var list = _cart.getMenuString();
		if(list){
			wdt.msg("正在提交，请稍后...");
			$.get("wx_index.php?act=save_menu",{'menulist':list,'code':_cart.code},function(ret){
				var ro = ret.toJson();
				if(ro.ret){
					wdt.msg("保存成功");
				}else{
					wdt.msg(ro.msg);				
				}
			});
		}
	},
	//结算中心
	checkout:function(){
		_cart.postOrder();
	},
	//外卖单买
	waimai:function(){
		_cart.listFlash('Y');
	},
	//将菜单格式化
	toString:function(){
 		var ng = [];
		//{'id':gid,'num':num,'price':co.p,'name':co.n,'taste':'','make':'','img':co.m}
		var str = "";
		for(gi in _cart.goods[_cart.code]){
			ji = _cart.goods[_cart.code][gi];
			if(str!=""){
				str += ",{'id':"+ji.id+",'num':"+ji.num+",'price':"+ji.price+",'name':'"+ji.name+"','unit':'"+ji.unit+"','taste':'"+ji.taste+"','make':'"+ji.make+"','img':'"+ji.img+"','code':'"+ji.code+"'}";			
			}else{
				str = "{'id':"+ji.id+",'num':"+ji.num+",'price':"+ji.price+",'name':'"+ji.name+"','unit':'"+ji.unit+"','taste':'"+ji.taste+"','make':'"+ji.make+"','img':'"+ji.img+"','code':'"+ji.code+"'}";			
			}
		}
		return "["+str+"]";
	},
	//保存当前购物车到cookie
	save:function(){
		setCookie("["+_cart.code+"]_cart",_cart.toString());
	},
	//将当前cookie里面内容回调到购物车
	unSave:function(){
		var sCookie = getCookie("["+_cart.code+"]_cart");
		if(sCookie){
			//判断地前门店是否有点单
			if(typeof(_cart.goods[_cart.code])=='undefined'){
				_cart.goods[_cart.code] = [];
			}
			var oL = eval("("+sCookie+")");
			for(g in oL){
 				_cart.goods[_cart.code][oL[g].id] = oL[g];
			}

			//设置一个流水号
			if(typeof(_cart.glide[_cart.code])=='undefined'){
				_cart.glide[_cart.code] = new Date().getTime();
			}
		}
	},
	//清空当前的点单
	empty:function(){
		if(confirm("确定清空吗?")){
			for(k in _cart.goods[_cart.code])
			{
				_cart.goods[_cart.code][k]=[];
			}
			_cart.goods[_cart.code] = [];
			_cart.listFlash();
			_cart.save();
			window.location.reload();
		}
	},
	//提交已点订单
	postOrder:function(){
		//if(confirm("确定下单吗?")){
			var para		= {};
			para.code		= _cart.code;
			para.glide		= _cart.getGlide();
			para.room		= wdt.room;
			para.man		= "";
			
			var goodStr = '';
			for(p in _cart.goods[_cart.code]){
				po = _cart.goods[_cart.code][p];
				var tastes = po.taste?po.taste:"";
				var makes = po.make?po.make:"";
				var memo = po.memo?po.memo:"";
				str = '{';
				str+= '"id":"'+po.code+'"';
				str+= ',"count":'+po.num;//数量
				//str+= ',"method":'+po.make.split(",");//制作方法编号
				str+= ',"method":"'+makes+'"';//制作方法编号
				str+= ',"state":""';//起菜方式
				str+= ',"memo":"'+memo+'"';//备注
				//str+= ',"tasteList":'+po.taste.split(",");//口味列表
				str+= ',"tasteList":"'+tastes+'"';//口味列表
				str+= '}';
				if(goodStr!=""){
					goodStr+= ','+str;
				}else{
					goodStr+= str;
				}
			}
			$('input').each(function(i,o){
				if($(o).attr('id')=='delivery_time'){
					delivery_time = $(o).val();
				}
				if($(o).attr('id')=='delivery_addr'){
					delivery_addr = $(o).val();
				}
				if($(o).attr('id')=='delivery_user'){
					delivery_user = $(o).val();
				}
				if($(o).attr('id')=='delivery_tel'){
					delivery_tel = $(o).val();
				}
				if($(o).attr('id')=='delivery_memo'){
					delivery_memo = $(o).val();
				}
			});
			
			para.orderTime	= delivery_time?delivery_time:"";
			para.sendAddress= delivery_addr?delivery_addr:"";
			para.phoneNum	= delivery_tel?delivery_tel:"";
			para.user		= delivery_user?delivery_user:"";
			para.memo		= delivery_memo?delivery_memo:"";
			para.eat		= _shop.peisong?_shop.peisong:"N";

			para.good = goodStr;
			$.get("store.php?do=ajax_postOrder",para,function(ret){
				//alert(ret);
				try{
					var ro = eval("("+ret+")");
					if(ro && ro.ret=='OK'){
						setCookie("["+_cart.code+"]_cart",'');
						location.href='travel.php?act=repayment&id='+ro.billCode;
					}else{
						alert(ro.MSG);
					}
				}catch(e){alert(ret);}
			});
		//}
	},
	getGlide:function(){
		return _cart.glide[_cart.code];
	}
};

//当前门店里面的数据
var _shop = {
	'goods':null,	//菜品数据
	'class':null,	//类别数据
	'gtaste':null,	//口味
	'gmake':null,	//制作方法
	'peisong':'N',	//制作方法
	//开始展示类别数据
	'dis_class':function(){
 		var uo = $(_page.cur).find("#ul_goods_class");
		//清空类别
		uo.html('');
		//清空菜品
		$(_page.cur).find("#ul_goods_list").html('');
		for(j in _shop.class){
			//alert('id='+_shop.class[j].i+' name='+_shop.class[j].n);
 			uo.append("<li onclick='_shop.go_class("+_shop.class[j].i+");' val='"+_shop.class[j].i+"'><span>"+_shop.class[j].n+"</span></li>");
 		}
 	},
	//跳转到指定的类别
	'go_class':function(cid){		
  		var co = eval("_shop.goods.c"+cid);
 		var ct = $(null).loadTemplate('goods_list_cell',null,1);//获取菜品单元的模板
 		var uo = $(_page.cur).find("#ul_goods_list");//父容器用来显示菜品列表的
 		//遍历分类下面的菜品
		uo.html('');
		for(j in co){
			s = ct;
			s = s.replaceAll('{name}',co[j].n.substring(0,5));//替换名称 get
			s = s.replaceAll('{unit}',co[j].u);//替换单位
			s = s.replaceAll('{price}',co[j].p);//替换价格
			s = s.replaceAll('{id}',co[j].i);//替换菜品ID
			
			if(co[j].m){
				s = s.replaceAll('{img}',co[j].m);//替换图片
			}else{
				s = s.replaceAll('{img}','./img/restaurant/logo.gif');//替换图片			
			}
			mo = _cart.get(co[j].i);
			//如果该菜品有点单信息
			if(mo){
				s = s.replaceAll('{num}',mo.num);//替换菜品ID
			}else{
				s = s.replaceAll('{num}','');//替换菜品ID
			}
   			uo.append(s);
		}
		//标记地前分类
		//$(_page.cur).find("#ul_goods_class>li").each(function(i,o){
		//	var to = $(o);
 		//	if(to.attr('val')==cid){
		//		to.addClass('cur_class');
		//	}else{
		//		to.removeClass('cur_class');			
		//	}
		//});
	},
	//获取门店里面指定菜品的信息
	'get_goods':function(gid){
		//遍历菜品
		for(c in _shop.goods){
			var go = eval("_shop.goods."+c);
			for(g in go){
 				if(go[g].i==gid){
 					return go[g];
				}
			}
 		}
		return null;
	}
};

//初始化
function on_load()
{
	//初始化系统参数
	wdt.width = $(window).width();
	wdt.height = $(window).height();
	//动态添加css
	css = ".scroll_body{height:"+(wdt.height-50)+"px;overflow:hidden;-webkit-overflow-scrolling : touch;background-color:#ffffff}";
	css+= ".scroll{height:"+(wdt.height)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	css+= ".scroll_body_header{height:"+(wdt.height-50)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	css+= ".scroll_body1{height:"+(wdt.height-50)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	css+= ".scroll_detail{height:"+(wdt.height-281)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	css+= ".scroll_goods{height:"+(wdt.height-110)+"px;overflow:hidden;-webkit-overflow-scrolling : touch;}";
	css+= ".goods_class{height:"+(wdt.height-110)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	css+= ".goods_list{height:"+(wdt.height-110)+"px;overflow:auto;-webkit-overflow-scrolling : touch;}";
	add_css(css);
	//var to = toUrlObj();
	wdt.store_code = $("#store_code").val();
	wdt.openid = $("#mem_id").val();
	wdt.peisong = $("#peisong").val();
	wdt.room = $("#room").val();
	wdt.act='menu_list'
	//载入指定店铺的菜品列表
	if(wdt.act=='menu_list'){
		shop_info(wdt.store_code,'');
	}
	//会员列表页面
	else if(wdt.act=='member_info'){
		_page.open(function(o){
 			$(o).loadTemplate('member_info',function(t){});
		});	
	}
}

function on_unload()
{
}

function loadShopListByPage(page,callback)
{
	$.get("wx_index.php?act=json_shop_list",{page:page},function(ret){
		 callback(ret);
	});
}

function add_css(str_css)
{
	try {
		var style = document.createStyleSheet();
		style.cssText = str_css;
	}
	catch (e){//Firefox,Opera,Safari,Chrome下可行
		var style = document.createElement("style");
		style.type = "text/css";
		style.textContent = str_css;
		document.getElementsByTagName("HEAD").item(0).appendChild(style);
	}
}

//菜品详情
function goods_detail(gid)
{
	
	_page.open(function(o){
		//var ct = $(null).loadTemplate('goods_detail',null,1);//
		$(o).loadTemplate('goods_detail',function(t){});
		//缓存当前页的信息
		$(o).data('gid',gid);
	},function(){
		//商品的标题部分
		mo = _cart.get(gid);
		if(mo){
			var ct = $(null).loadTemplate('goods_detail_info',null,1);//
			ct = ct.replaceAll('{name}',mo.name);//
			ct = ct.replaceAll('{price}',mo.price);//
			ct = ct.replaceAll('{num}',mo.num);//
			ct = ct.replaceAll('{id}',mo.id);
			if(mo.img){
				$(_page.cur).find("#detail_img").css({'background':'url("'+mo.img+'") no-repeat center center'});
			}else{
				$(_page.cur).find("#detail_img").css({'background':'url("./img/restaurant/logo.gif") no-repeat center center'});
			}
			$(_page.cur).find("#detail_info").html(ct);
			//载入口味
			htmls = "";
			for(j in _shop.gtaste){
				htmls += '<li val="'+_shop.gtaste[j].i+'">'+_shop.gtaste[j].c+':'+_shop.gtaste[j].n+'<button class="fr" onclick="detail_select(this,1);">'+(mo.taste.inStr(_shop.gtaste[j].i)?'√':'')+'</button></li>';
			}
			$(_page.cur).find("#ul_detail_taste").html(htmls);
			//载入制作方法
			htmls = "";
			for(j in _shop.gmake){
				htmls += '<li val="'+_shop.gmake[j].i+'">'+_shop.gmake[j].c+':'+_shop.gmake[j].n+'<button class="fr" onclick="detail_select(this,1);">'+(mo.make.inStr(_shop.gmake[j].i)?'√':'')+'</button></li>';
			}
			$(_page.cur).find("#ul_detail_make").html(htmls);

			$(_page.cur).find("#goods_memo").html(mo.memo);
		}else{
			var co = _shop.get_goods(gid);			
			if(co){
				var ct = $(null).loadTemplate('goods_detail_info2',null,1);
				ct = ct.replaceAll('{name}',co.n);
				ct = ct.replaceAll('{price}',co.p);
				ct = ct.replaceAll('{num}',0);
				ct = ct.replaceAll('{id}',gid);
				if(co.m){
					//$(_page.cur).find("#detail_img").css({'background':'url("'+co.m+'") no-repeat center center'});
					var imgurl = '<img src="'+co.m+'" style="width:100%;">';
					$(_page.cur).find("#detail_img").html(imgurl);
				}else{
					//$(_page.cur).find("#detail_img").css({'background':'url("./img/restaurant/logo.gif") no-repeat center center'});
					var imgurl = '<img src="./img/restaurant/logo.gif" style="width:100%;">';
					$(_page.cur).find("#detail_img").html(imgurl);
				}
				$(_page.cur).find("#detail_info").html(ct);
				//载入口味
				htmls = "";
				for(j in _shop.gtaste){
					htmls += '<li val="'+_shop.gtaste[j].i+'">'+_shop.gtaste[j].c+':'+_shop.gtaste[j].n+'<button class="fr" onclick="detail_select(this,1);">'+''+'</button></li>';
				}
				$(_page.cur).find("#ul_detail_taste").html(htmls);

				get_explain(gid);

				//载入制作方法
				htmls = "";
				for(j in _shop.gmake){
					htmls += '<li val="'+_shop.gmake[j].i+'">'+_shop.gmake[j].c+':'+_shop.gmake[j].n+'<button class="fr" onclick="detail_select(this,1);">'+''+'</button></li>';
				}
				$(_page.cur).find("#ul_detail_make").html(htmls);

				$(_page.cur).find("#goods_memo").html('');
			}
		}
	});
	$("#cart_info").hide();
}

//获取商品的说明
function get_explain(gid){
	var para = {
		'do':'getExplain',
		'gid':gid
	};
	//开始提交
	$.get('restaurant.php',para,function(ret){
		//alert(ret);
		var ot = eval("("+ret+")");
		var explain = ot.explain;
		var i=1;
		var m=1;
		while(i>0){
			m++;
			if(explain.indexOf('\\"')>0){
				explain = explain.replace('\\"','"');
				if(m>50000){
					i = 0;
				}
			}else{
				i = 0;
			}
		}
		$(_page.cur).find("#ul_detail_explain").html(explain);
	});
}
//打开菜品详情时保存
function save_detail()
{
	var od = $(_page.cur);
	var gid = od.data('gid');
	if(gid>0){
		var num = parseFloat(od.find('#detail_num').text());//点单数量
		//获取口味
		var taste = "";
		od.find('#ul_detail_taste>li').each(function(i,o){
			if($(o).children('button').text()){
				taste+= taste==""?$(o).attr('val'):","+$(o).attr('val');
			}
		});

		//alert('taste='+taste);
 		//获取制作方法
		var make = "";
		od.find('#ul_detail_make>li').each(function(i,o){
			if($(o).children('button').text()){
				make = $(o).attr('val');
			}
		});
		var memo = od.find('#goods_memo').val();//点单数量

		if(_cart.get(gid)){
			_cart.get(gid).taste	= taste;
			_cart.get(gid).make		= make;
			_cart.get(gid).num		= num;
			_cart.get(gid).memo		= memo;
		}
		_page.close(function(){
			_cart.listFlash();
		});
  	}
}

//明细的添加和修改
function detail_addsub(ty){
	var od = $(_page.cur).find('#detail_num');
	num = parseFloat(od.text());
	if(ty=='+'){
		od.text(num+1);
	}else{
		od.text(num-1);
	}
}
//口味的多重选择
function detail_select(ot,muliti)
{
	var op = $(ot).parent().parent();
	op.children('li').each(function(i,o){
		to = $(o).children('button')[0];
   		if(ot==to){
			if($(to).text()){
				$(to).text('');			
			}else{
				$(to).text('√');			
			}
 		}else{
			if(muliti!=1){
				$(to).text('');
			}
		}
	});
}

//标签选择
function other_select(ot,ty)
{
	$(ot).parent().find('li').each(function(i,o){
		if(ot==o){
			$(o).addClass('cur_other');
  		}else{
			$(o).removeClass('cur_other');
 		}
	});
	$(_page.cur).find(".detail_child").each(function(i,o){
		if(o.id.indexOf(ty)>0){
			$(o).show();
		}else{
			$(o).hide();		
		}
	});
}

//个人中心
function my_center()
{
	//如果已经登录
	if(getCookie('wdt_loged')=='1'){
		_page.open(function(o){
			//获取当前的cookie
			if(getCookie('wdt_key')){
				//载入个人中心的页面
				$(o).loadTemplate('my_center',function(t){
				});
			}else{
				//载入指定的页
				//$(o).loadPage({'act':'my_center'});
			}
		});	
	}else{
		my_login();
	}
}

//跳转到登陆页面
function my_login(){
	//载入指定的页
	//$(o).loadPage({'act':'my_center'});
	_page.open(function(o){
		//载入个人登陆中心
		$(o).loadTemplate('my_center_login',function(t){
		
		});
	});
}

//我的结算中心
function my_checkout()
{
	$.get("wx_index.php?act=json_member_info",{},function(ret){
 		var co = eval("("+ret+")");
		var oM = _cart.getMenu();
 		_page.open(function(o){
			//载入个人登陆中心
 			var ct = $(null).loadTemplate('my_checkout',null,1);//获取菜品单元的模板
 			ct = ct.replaceAll('{num}',oM.num);//数量
 			ct = ct.replaceAll('{money}',oM.money);//金额
 			ct = ct.replaceAll('{save}',co.save);//余额
			$(o).html(ct);
		});		
	});
}

//开始结算
function confirm_checkout()
{
	//total_money
	var m_save  = parseFloat($(_page.cur).find('#member_save').val());
	var m_money = parseFloat($(_page.cur).find('#total_money').val());
 	//余额大于消费金额
	if(m_save>=m_money)
	{
		if(confirm("确认付款吗？"))
		{
			wdt.msg("请稍后...",false);
			var para = {
				'act':'save_bill',
				'code':_cart.code,
				'menu':_cart.getMenuString(),
				'money':m_money
			};
			//开始提交
			$.get('wx_index.php',para,function(ret){
				wdt.msgClose();
			});
		}
 	}
	else
	{
		wdt.msg("余额不足！");
	}
}

//会员登录开始
function my_sign()
{
	var op = $(_page.cur);
	var uname = $(op).find('#user_name').val();
	var upass = $(op).find('#user_pass').val();
	if(uname==""){
		alert('用户名不能为空！');
		return false;
	}
	wdt.msg('登陆中，请稍后...',false);
	$.get("wx_index.php?act=sign",{'name':uname,'pass':upass},function(ret){
 		wdt.msgClose();
		var o = ret.toJson();
		//登陆成功
		if(o.ret){
 			_page.close();
			//设置cookie
 			setCookie('wdt_key',o.key);
			my_center();
		}else{
			wdt.msg(o.msg);
			$(_page.cur).find('#msg_text').html(ret.msg);		
		}
	});
}

//进入店铺信息
function shop_info(store_code,search_name)
{
 	//打开店铺展示，当前默认是进入店铺的点单列表页
	_page.open(function(o){
  		$(o).loadTemplate('goods_list',function(t){
			$.get("store.php?do=getGoodsListByCode",{'store_code':store_code,'searchname':search_name},function(ret){
				var ot = eval("("+ret+")");
				//设置购物车的店铺编号
				_cart.setCode(store_code);
				//开始展示店铺的点单列表
				_shop.goods = ot.goods;
				_shop.class = ot.classe;
				_shop.gtaste = ot.gtaste;
				_shop.gmake = ot.gmake;
				//_shop.peisong = isps;
				//还原cookie的点单
				_cart.unSave();
				_cart.showTip();
				//_shop.dis_class();
				//跳转到第一个分类
				_shop.go_class(1);	
			});
		});
 	});
}

//检索菜品
function goods_search()
{
	var to = toUrlObj();
	var search_name = '';
	wdt.openid = wdt.openid;
	//$('input').each(function(i,o){
	//	alert($(o).attr('name')+'__'+$(o).attr('id')+"==>"+$(o).val());
	//});
	//alert($("#search_goods").parent().parent().html());
	$('input').each(function(i,o){
		if($(o).attr('id')=='search_goods'){
			search_name = $(o).val();
		}
	});
	chageGoodsList(wdt.store_code,search_name);
}

function chageGoodsList(store_code,search_name)
{
	$.get("store.php?do=getGoodsListByCode",{'store_code':store_code,'searchname':search_name},function(ret){
		var ot = eval("("+ret+")");
		//开始展示店铺的点单列表
		_shop.goods = ot.goods;
		_shop.class = ot.classe;
		_shop.gtaste = ot.gtaste;
		_shop.gmake = ot.gmake;
		//_shop.dis_class();
		//跳转到第一个分类
		_shop.go_class(1);
	});
}

//扩展方法
$.fn.extend({
	//请求一个页
	loadPage:function(para){
		var ot = this;
 		$.ajax({
			type: "POST",
			url: "restaurant.php",
			data: para,
			success: function(msg){
				$(ot).html(msg);
			}
		});
 	},
	//载入制定的静态模板
	loadTemplate:function(temp,callback,is_return){
		temp = 'template_'+temp;
		//tp = $('#'+temp).html();
		//alert(tp);
		if(is_return){
 			return $('#'+temp).html();
		}
		$(this).html($('#'+temp).html());
		if(typeof(callback)=='function'){callback();}
	},
	//载入动态的
	loadDynamicTemplate:function(temp,arr){
		temp = 'template_'+temp;
		tp = $('#'+temp).html();
		for(j in arr){
			tp = tp.replaceAll(arr[j][0],arr[j][1]);
		}
		return tp;
	},
	//载入数据
	loadData:function(para){
	}
});

//将当前的url转换成对象
function toUrlObj()
{
	para = location.href.indexOf('?')==-1?'':location.href.substring(location.href.indexOf('?')+1);
	if(para=="")return false;
	p = para.split('&');
	str = "";
	for(i in p){
		t = p[i].split('=');
		if(str==""){
			str = t[0]+":'"+t[1]+"'";
		}else{
			str+= ","+t[0]+":'"+t[1]+"'";
		}
	}
	return eval("({"+str+"})");
}

//扩展内置方法
String.prototype.replaceAll=function(node,str){
	var re=new RegExp(node,"g"); 
	return this.replace(re,str);
}

//判断字符是在这个字符串中
String.prototype.inStr=function(_s){
	var ar = this.split(',');
	for(_i in ar){
		if(ar[_i]==_s){
			return true;
		}
	}
	return false;
}

//字符串转换成JSON
String.prototype.toJson=function(){
	try{
	var _o = eval("("+this+")");
	return _o?_o:null;
	}catch(e){alert(e.message+"\n"+this);}
}

getCookie = function(sName)
{
  //alert(document.cookie);
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++){
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return decodeURIComponent(aCrumb[1]);
  }
  return null;
}

setCookie = function(sName, sValue, sExpires)
{
  var sCookie = sName + "=" + encodeURIComponent(sValue);
  if (sExpires != null)
  {
    sCookie += "; expires=" + sExpires;
  }
  document.cookie = sCookie;
}
