$(function(){
	$("ul#hmcase > li").click(function(){
		$("ul#hmcase > li").removeClass("active")
		$(this).addClass("active")
		var index = $(this).index();
		CheckHM(index, $(this).attr("lotid"), $(this).attr("lotname"),"load")
	})
	/*$("ul#hmcase > li").eq(2).click()*/
		var index = $(this).index();
		CheckHM(index,"", '',"load")
})

//选择合玩列表
function CheckHM(num,lotid,lotname,ac)
{
	$("ul#hmcase").parent().find("div.tabs-cnt").hide();
	var div = $("#groupa tbody");
	
	$(div).show();
	
	if($(div).html()=="" ) 
	{
		if(ac=="refresh")
		{
			$(div).text("正在载入...");
			LoadHM(div,lotid,lotname)
		}
	}
	else
	{
		LoadHM(div,lotid,lotname)
	/*返回继续投注*/
	$("#aContinue").click(function(){
		alertdiv.close('_ajax_common_div5');
		LoadHM(div,lotid,lotname)
	})
	
	/*关闭投注成功提示*/
	$("#_ajax_common_btn5").click(function(){
		alertdiv.close('_ajax_common_div5');
		LoadHM(div,lotid,lotname)
	})
	}
	
}
function Record_(record)
{
		var am = record,ar = Math.floor(am / 10000000),val_="";
		if (ar > 0 )
		{
			val_ += "<img src='../../Images/y4.gif' />";
			val_ += "<img src='../../Images/s" + ar + ".png' />";
			am = am - 10000000 * ar
		}
		var ar = Math.floor(am / 1000000)
		if (ar > 0) {
			val_ += "<img src='../../Images/y3.gif' />";
			val_ += "<img src='../../Images/s" + ar + ".png' />";
			am = am - 1000000 * ar
		}
		var ar = Math.floor(am / 100000)
		if (ar > 0) {
			val_ += "<img src='../../Images/y2.gif' />";
			val_ += "<img src='../../Images/s" + ar + ".png' />";
			am = am - 100000 * ar
		}
		var ar = Math.floor(am / 10000)
		if (ar > 0) {
			val_ += "<img src='../../Images/y1.gif' />";
			val_ += "<img src='../../Images/s" + ar + ".png' />";
			am = am - 10000 * ar
		}
		return val_;		
}
//载入合玩列表
function LoadHM(id,lotid,lotname)
{
		$.post("/Trade/Include/Ajax_Trade.html",{
			action:"hmlist",
			lotid:escape(lotid),
			Pageno:page.pageindex
		},function(data){
		data = eval(data);
		if(data.length<0 || data =="[]" || data ==""){
			$("table.rec_table > tbody").html("<tr><td colspan='9'>抱歉！没有找到符合条件的结果！</td></tr>");	
			page.pagesize = page.pageindex = page.countrs =0;
			createpage();return;	
		}
		var context = data[0],page_ = data[1],tr = "";
		$.each(context,function(key,value){
                  if( key >12 ) return ; 
			var key_1 = (page_.pageindex>1?(Number(page_.pageindex)-1)*20+Number(key):Number(key))+1,val = "",key_2 = (key_1%2)==0?"th_on":"th_even"; 
			if(key_1>15) return;
		if(value.lotteryname == '竞彩足球')
			 value.url = '/Trade/Jczq/view.html?id='+value.id;
		else
		{
			if(value.lotteryid != ""){
			var lottName = value.lotteryid.match(/[a-zA-Z]/g).join("").toLowerCase().replace(/(\w)/,function(v){return v.toUpperCase()});
			if(lottName.indexOf("ssc")!=-1){ lottName = lottName.replace("ssc","Ssc");}
			else if(lottName.indexOf("syxw")>-1){ lottName = lottName.replace("syxw","Syxw");}
			else if(lottName.indexOf("ks")>-1){ lottName = lottName.replace("ks","Ks");}
			lottName = lottName == "CqSsc"?"Ssc":lottName;
			lottName = lottName == "JxSyxw"?"Syxw":lottName;}else lottName = "";
			
			value.url = '/Trade/'+lottName+'/Project-Info-'+value.id+'.html';
		}		
		if(value.state > 0){
			val = "<td><b class='red'>已中奖</b></td><td>"
		}else if(value.state == "0"){
			val = "<td><b class='blue'>未中奖</b></td><td>"
		}else if(value.state == '-1' || value.ssy <1){
			val = "<td> <b class='blue'>进行中</b></td><td>"
		}else{
			val = "<td><input type='text' class='rec_text' value='1' style='width: 38px;' name='buynum'  onkeyup='FormatNum(this)' />"
				+"<input type='hidden' name='pid' value='"+value.id+"' />"
				+"<input type='hidden' name='senumber' value='"+value.ssy+"' />"
				+"<input type='hidden' name='onemoney' value='1' /></td>"
				+"<td><a class='btn_cy' title='' href='javascript:;' onclick='AddProject(this)'>投注</a>";
		}	
		tr += '<tr class="'+key_2+'" onmouseover="this.className=\'listmouseover\'" onmouseout="this.className=\''+key_2+'\'">'
					+'<td>'+key_1+'</td>'
					+'<td>'+value.lotteryname+'</td>'
					+'<td>'+value.username+'</td>'
					+'<td>'+ Record_(value.record)+'</td>'//战绩'+value.record+'
					+'<td class="tr">'+value.money+'&nbsp;|&nbsp;</td>'
					+'<td class="tl">'+value.ssy+'份</td>'
					+'<td style="text-align:left;">'+value.jdbfb+'%+'+value.bdbfb+'%<span style="color:#fff;background:#F60;">保</span><br><div style="width:98px;height:4px; border:1px solid #cccccc;"><img style="margin-left:-1px;margin-top:-1px;" src="/images/bar_space.gif" width="'+value.jdbfb+'" height="6"></div></td>'
					+val+'<a href="'+value.url+'" class="btn_xx">详情</a></td>'
				+'</tr>';		
		})
		$("table.rec_table > tbody").html(tr);
		page.pagesize = Number(page_.pagesize);
		page.pageindex = Number(page_.pageindex);
		page.countpage = Number(page_.countpage);
		page.countrs = Number(page_.countrs);
		createpage();	
		}			 
	);
}
//投注按钮事件
function AddProject(obj)
{
	var td = $(obj).parent().prev(),buynum = $(td).find("input[name=buynum]").val(),pid = $(td).find("input[name=pid]").val(),senumber = $(td).find("input[name=senumber]").val(),
	onemoney = $(td).find("input[name=onemoney]").val();
	if (buynum=="")
	{
		alertdiv.alert("投注份数不能为空！");
		$(td).find("buynum").focus();
		return;
	}
	else if (Number(buynum) <= 0)
	{
		alertdiv.alert("投注份数不能为空！");
		$(td).find("buynum").focus();
		return;
	}
	else if (Number(buynum) > Number(senumber))
	{
		alertdiv.alert("您投注份数不能大于剩余份数！");
		$("#buynum").focus();
		return;
	}
	var msg = "<p style='text-align:left;text-indent:2em;'>您好，请您确认</p>";
	msg = msg + "<p style='text-align:left;text-indent:2em;'>投注份数：<font color='red' style='font-weight:bold'>"+buynum+"</font>份</p>";
	msg = msg + "<p style='text-align:left;text-indent:2em;'>投注"+coinType+"：<font color='red' style='font-weight:bold'>￥"+buynum*formatCurrency2(onemoney)+coinUnit+"</font></p>";
	if($(obj).parents("tr").find("td").eq(1).text()=="竞彩足球"){
		alertdiv.confirm(msg,function(){
			AJAX_HM({
                id:pid,
                money:buynum,
                action:"buy"
            });
		});
	}else{
		alertdiv.confirm(msg,function(){ add_project(buynum,pid,onemoney)});
	}
}

    var AJAX_HM = function(postdata){//竞彩主球 合
        $.ajax({
            type: 'POST',
            url: "/Trade/jczq/HM_BUY.asp",
            data: postdata,
            success: function(data){
                var d = eval("("+data+")");
                if(d.err == 1){
                    alertdiv.login();
                }else{
                    if(d.err == 2){
                        alert("参数错误！");
                    }else if(d.err == 3){
                        alert("投注错误(参数非法)！");
                    }else if(d.err == 4){
                        alert("您所投注的订单不存在！");
                    }else if(d.err == 5){
                        alert("投注错误(您的【"+coinType+"】不足或【投注份额】超出最大投注数)！");
                    }else if(d.err == 6){
                        alert("投注失败，投注已截止！");
                    }else if(d.err == 0){
                    	//$("#aViewPro").attr("href","/Trade/Project-Info.html?id="+pid)
						alertdiv.showBox("_ajax_common_div5", "温馨提示", null, 400, null,null) 
						ColseCountdownWin('_ajax_common_div5','sytime2','sytime2_second',null,LoadHM)
                    }else{
                        alert("内部错误！");
                    }
                    //$(".KR_window_close").click();
                    //window.location.reload();
                }
            }
        });
    }

//参加合玩
function add_project(buynum,pid,onemoney){
	$.ajax({
		type: "POST",
		url: "/Trade/Include/KR.HeMai.Buy.html",
		data: {
			action:"add",
			pid:pid,
			buynum:buynum,
			onemoney:onemoney
		},
		dataType:"xml",
		success: function(data){
			var err = $(data).find("err");
			if($(err).attr("type")=="-1") //未登录
			{
				alertdiv.login(function(){add_project(buynum,pid,onemoney)})
			}
			else if($(err).attr("type")=="0")
			{
			alertdiv.alert("对不起您的"+coinType+"不足！");
			return ;
			}
			else if($(err).attr("type")=="2")
			{
			alertdiv.alert("对不起当前期已经截止投注！");
			return ;
			}
			else if($(err).attr("type")=="-3")
			{
			alertdiv.confirm2("系统繁忙请重试!",default_login)
			return ;
			}
			else if($(err).attr("type")=="3")
			{
			alertdiv.confirm2("对不起，该方案已撤单!",default_login)
			return ;
			}
			else if($(err).attr("type")=="4")
			{
			alertdiv.confirm2("对不起，该方案已停止追号!",default_login)
			}
			else if($(err).attr("type")=="6")
			{
			alertdiv.confirm2("对不起，该方案已满员!",default_login)
			}
			else if($(err).attr("type")=="5")
			{
			alertdiv.confirm2("参与"+coinType+"已经超过最大限额！",default_login)
			}
			else if($(err).attr("type")=="-2")
			{
			alertdiv.alert("对不起您投注的份大于当前份数！");
			return ;
			}
			else if($(err).attr("type")=="1")
			{
				/*查看方案*/
				$("#aViewPro").attr("href","/Trade/Project-Info.html?id="+pid)
				alertdiv.showBox("_ajax_common_div5", "温馨提示", null, 400, null,null) 
				//ColseCountdownWin('_ajax_common_div5','sytime2','sytime2_second',null,do_search)
			}
			else
			{
				if($(err).attr("msg")!=null)
				{
					if($(err).attr("msg").length>0)
					{
						alertdiv.alert($(err).attr("msg"));
					}
				}
				if($(err).attr("url")!=null)
				{
					if($(err).attr("url").length>0)
					{
						location.href = $(err).attr("url");
					}
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			alertdiv.alert("交易失败,请重新提交！");
		},
		complete: function (jqXHR, textStatus) {
		}
	});
}

$("#hongbao").click(function()
{
	$.ajaxSetup({ cache: false });
	$.get("/User/HongBao.html",function(data){
			var err = $(data).find("err");
			switch($(err).attr("type")){
					case "5":{alertdiv.login();break;}
					case "2":{alert("今日抽取红包次数已完，请明天再来！");break;}
					case "3":{alert("消费额不足，不能参与抽取红包！");break;}
					case "4":{alert("消费额不足以再次抽红包！");break;}
					case "1":{alert("恭喜！抽取红包 "+$(err).attr("money")+" "+coinUnit+"，已返现于您的账户"+coinType+"！\n今日还剩 "+$(err).attr("cisu")+" 次抽取红包机会！");break;}
				} 
		});
});
function $a(id,tag){var re=(id&&typeof id!="string")?id:document.getElementById(id);if(!tag){return re;}else{return re.getElementsByTagName(tag);}}
if ($("#js_ads_banner_top_slide").length){
	var $slidebannertop = $("#js_ads_banner_top_slide"),$bannertop = $("#js_ads_banner_top");
	setTimeout(function(){$bannertop.slideUp(1000);$slidebannertop.slideDown(1000);},2500);
	setTimeout(function(){$slidebannertop.slideUp(1000,function (){$bannertop.slideDown(1000);});},8500);
}