﻿var currentCst;
var prizes;
var GuaData;
var eMsg = ["",
"您还未登录，请先登录。",
"汉字抽奖码应为4个汉字，请重试。",
"您输入的汉字抽奖码有误，请重试。",
"您的抽奖码已经用过，不能再次抽奖！",
"对不起，目前系统繁忙，请稍后再试！",
"由于商家奖品不同，重新设定了转盘奖项，请再次按抽奖按钮，重新抽奖！"];

function GetCode(){
	return $("#txtCode").val().trim();
}
function msgFlash(msg){
	new Boxy("<p>"+msg+"</p>",{title:'提示',closeable:true,afterShow:function(){setTimeout("Boxy.closeAll()",4000)}});
}
function GetPrizes(type){
	var code=GetCode();
	if (code.length!=4){msgFlash("抽奖码应为4个汉字");return false;}
	$.getJSON("Code.ashx", { c: code, t: type, cstId: currentCst, r: Math.random() },
	function(data) {
		var fls = s$("flashGua");
		GuaData = data;
		if (data.rst == 0) {
			if (type == 0) {
				fls.SetShop(data);
				SetTopBarImg(data.cstId);
			}
			else { fls.PointerRoll(data); }
		}
		else {
			if (data.rst == -1) {
				msgFlash("只有白不拍注册拍友才能参加抽奖，请您先登录。");
				setTimeout("tbLoginShow('../')", 4000);
			}
			else if (data.rst == -6) {
				SetTopBarImg(data.cstId, 1);
				fls.SetShop(data);
			}
			fls.ResetStartBtn();
			if (data.rst != -1) {
				msgFlash(eMsg[0 - data.rst]);
			}
		}
	});
	return true;
}
function msgCount(sender) {
	var msg = $(sender).val();
	if (msg.length > 80) {
		$(sender).val(msg.substring(0, 80));
		msgFlash("留言不能超过 80 个字!");
	}
}

function ShowInfo(v) {
	if ($(".PrzIntro"+v).length>0){
		$("#PrizeInfo").html($(".PrzIntro"+v).html());
	}
	else {
		var url = "../shop/" + currentCst + "/gua/" + v + ".";
		jQuery.ajax({
			url: url + 'jpg',
			complete: function(XMLHttpRequest, textStatus) {
				if (textStatus == 'error') {
					url += 'gif'
				}
				else { url += 'jpg' }
				$("#PrizeInfo").html("<img src='" + url + "' />");
			}
		})
		
	}
}

function UpdateWinHis(przName){
	if ($("#HisTable tr").length==19){
		$("#HisTable tr:last").remove();
	}
	var winner='striker';
	$("#HisTable tr:first").after(
		"<tr><td>"+decodeURI($.cookie("userName"))+"</td><td>"+new Date().format()+"</td><td>"+przName+"</td></tr>"
	);
}

function goShop(){
	location.href="/fastfood/Orders.aspx?cst="+getCst();
}

function SetTopBarImg(cstId,notHttpGet){
	if(cstId){
		currentCst = cstId;
		$.cookie('guaCst', cstId);
	}
	else {
		cstId=$.cookie('guaCst');
	}
	$("#top").css("background-image","url(../Shop/"+cstId+"/gua/T.jpg)");
	if (typeof(notHttpGet)!='undefined'){return;}
	$.getJSON("Code.ashx", {cstId:cstId, r:Math.random()},
	function(data){
		var fls = s$("flashGua");
		if (data.rst == 0){
			if (typeof(fls.SetShop)=='undefined')
			{
				setTimeout(SetTopBarImg,100);
				return;
			}
			fls.SetShop(data);
		}
	});
}

var csts=[2232,2231,2230,2229,2224,2223,2221,2219,2218,2214,2213,2208,2206,2205,
2204,2200,2197,2195,2194,2193,2192,2191,2189,2188,2187,2186,2111,1555,1396,1353,
1280,1263,1226,1166,446,442,440]
function getCst(){
	var cst=getUrlParam("cstId");
	if (cst=="") cst=$.cookie('guaCst');
	if(isNaN(parseInt(cst))){cst=csts[Math.floor(Math.random()*csts.length)];}
	$.cookie('guaCst',cst);
	return cst;
}

$(function(){
	$("#txtCode").val(decodeURI(getUrlParam("c")));
	SetTopBarImg(getCst());
	ShowInfo(1);
});

function PopWinCodeHint(){
	switch (GuaData.result) {
		case 0: if (GuaData.code) {
			Boxy.alert("白不拍已经向您的手机发送了奖品领取密码，您的领取密码是“<em>" + GuaData.code
			+ "</em>”，请注意查收。请您在领取有效期内回复本密码，并商家预约领取。如果您10分钟之内仍然没有收到领取密码，请拨打白不拍客服电话：0755-86110256。");
			};
			break;
		case 1: alert("您的手机号尚未通过验证，不能获取领取密码，请先设置并验证手机号。");
			Boxy.iframeLoad("../security/CheckMobile.aspx", { title: "验证手机号", closeText: "[关闭]" });
			break;
		case 2: Boxy.alert("拍品不存在。"); break;
		case 3: Boxy.alert("您的帐户余额不足以支付服务费，请先充值。");
			location = "../security/account.aspx";
			break;
		case 4: Boxy.alert("拍品已过期。"); break;
		case 6: Boxy.alert("您已经获取过领取密码，不能再次获取。"); break;
		default: Boxy.alert("无法发送领取密码，错误编号：" + GuaData.result); break;
	}
}