$(function(){
	window.setInterval("daojishi()",1000);//倒计时
	
	$("#n1 > div > label > a").click(function(){
		var index = $(this).index();
		$("#n1 > div > label > a").removeClass("cur");
		$("#n1 > div > label > a > i").removeClass("cur");
		$(this).addClass("cur");
		$(this).find("i").addClass("cur");
		 CheckHM(index,$(this).attr("lotid"),$(this).attr("lotname"),"load");
	})
	//刷新合玩列表
	$("#update").click(function(){
		var div = $("#n1 > div > label > a.cur");
		 CheckHM($(div).index(),$(div).attr("lotid"),$(div).attr("lotname"),"refresh");
	})
	
	$("#dj_list_tabs > a").mouseover(function(){
		$("#dj_list_tabs > a").removeClass("tab_cur")
		$(this).addClass("tab_cur")
		$("#dj_list_tabs").next().children().hide();
		$("#dj_list_tabs").next().children().eq($(this).index()).show();
	})
	
	
	$("#n1 > div > label > a").eq(0).click()
})

//选择合玩列表
function CheckHM(num,lotid,lotname,ac)
{
	num = num/2
	$("#n1 > div ").each(function(index){
		if(index>1)
		{
			$(this).hide();
		}
	})
	if($("#case1_"+num).length > 0 ) 
	{
		$("#case1_"+num).show()
		if(ac=="refresh")
		{
			$("#case1_"+num).text("正在载入...")
			LoadHM("#case1_"+num,lotid,lotname)
		}
	}
	else
	{
		$("#n1").append("<div id='case1_"+num+"' style='display: block;'>正在载入...</div>")
		LoadHM("#case1_"+num,lotid,lotname)
	}
	
	LoadHM("#case1_"+num,lotid,lotname)
	
	/*返回继续投注*/
	$("#aContinue").click(function(){
		alertdiv.close('_ajax_common_div5');
			LoadHM("#case1_"+num,lotid,lotname)
	})
	
	/*关闭投注成功提示*/
	$("#_ajax_common_btn5").click(function(){
		alertdiv.close('_ajax_common_div5');
			LoadHM("#case1_"+num,lotid,lotname)
	})
}
//载入合玩列表
function LoadHM(id,lotid,lotname)
{
	$.ajax({
		type: "POST",
		url: "/Trade/Ajax_Trade.html",
		data: {
			action:"hmlist",
			lotid:lotid,
			pagesize:10
		},
		dataType:"xml",
		success: function(data){
			var err = $(data).find("err");
			if($(err).attr("type")=="1")
			{
				var str = "<table class='rec_table index' width='100%' cellspacing='0' cellpadding='0' border='0'>"
				str += "<colgroup><col width='15%'><col width='15%'><col width='10%'><col width='10%'><col width='16%'><col width='11%'><col width='11%'><col width='11%'></colgroup>"
				str += "<thead><tr class=''><th>发起人</th><th class='fa_money'>方案"+coinType+"</th><th>提成</th><th>详细</th><th>方案进度</th><th>剩余份数</th><th>投注份数</th><th>合玩</th></tr></thead>"
				str += "<tbody>"
					
				$(data).find("row").each(function(index){
					if(index > 9)
					{
						return false
					}
					str += "<tr>"
					//str += "<td class=splittd>"+$(this).attr("num")+"</td>"
                    //str += "<td><a href='/trade/viewpath.asp?pid="+$(this).attr("pid")+"'>"+$(this).attr("pid")+"</a></td>"
					str += "<td class=splittd>"+unescape($(this).attr("user"))+"</td>"
					//str += "<td class=splittd>"+$(this).attr("images")+"&nbsp;</td>"
                    str += "<td class='fa_money'>"+$(this).attr("allmoney")+coinUnit+"</td>"
					str += "<td>"+$(this).attr("tc")+"%</td>"
					//str += "<td>￥"+$(this).attr("mfje")+"彩豆</td>"
                    str += "<td><a href='"+lotname+"/Project-Info-"+$(this).attr("cid")+"'>查看</a></td></td>"
					if($(this).attr("state")=="1")
					{
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>撤单</td>"
						}
						else if($(this).attr("state")=="2")
						{
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>满员</td>"
						}
						else if($(this).attr("state")=="3")
						{
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>--</td>"
							str += "<td>已截止</td>"
					}
					else
					{
						if($(this).attr("bd")!="0.00")
						{
							str += "<td style='text-align:left;'><span style='margin-left:0px;;color:000'>"+$(this).attr("jd")+"%</span> + <span style='margin-left:0px;color:#fff;background:#b90000;'>"+$(this).attr("bd")+"%保</span><BR><div style='width:100px;height:6px; font-size:0px; line-height:0px;border:1px solid #b90000;background:#fff;'><img src='/images/bar_space.gif' width='"+$(this).attr("jd")+"' height='6'></div></td>"
						}
						else
						{
							str += "<td style='text-align:left;'><span style='margin-left:0px;color:000'>"+$(this).attr("jd")+"%</span><BR><div style='width:100px;height:6px; font-size:0px; line-height:0px;border:1px solid #b90000;'><img src='/images/bar_space.gif' width='"+$(this).attr("jd")+"' height='6'></div></td>"
						}
						str += "<td>"+$(this).attr("sy")+"份</td>"
						str += "<td>"
						str += "<input type='text' class='rec_text' value='1' style='width: 38px;' name='buynum'  onkeyup='FormatNum(this)' />"
						str += "<input type='hidden' name='pid' value='"+$(this).attr("pid")+"' />"
						str += "<input type='hidden' name='senumber' value='"+$(this).attr("sy")+"' />"
						str += "<input type='hidden' name='onemoney' value='"+$(this).attr("mfje")+"' />"
						str += "</td>"
						str += "<td><a class='public_Dora' title='' href='javascript:;' onclick='AddProject(this)'><b>投注</b></a></td>"
					}
					str += "</tr>"
				})
				var num = $(data).find("row").length;
				if(num<10)
				{
					num = 10 - num;
					for(var i=0; i<num; i++ )
					{
						str += "<tr>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "<td>&nbsp;</td>"
						str += "</tr>"
					}
				}
				str = str + "</tbody>"
				str = str + "</table>"
				str = str + "<div class='ft clearfix'>"
				str = str + "<span class='l gray'></span>"
				str = str + "<span class='r'><a target='_blank' title='' href='"+lotname+"/Project_List.html'>查看更多合玩&gt;&gt;</a></span>"
				str = str + "</div>"
				$(id).html(str)
			}
			else
			{
				if($(err).attr("msg").length>0)
				{
					alertdiv.alert($(err).attr("msg"));
				}
				if($(err).attr("url").length>0)
				{
					location.href = $(err).attr("url");
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
		},
		complete: function (jqXHR, textStatus) {
		}
	});
}


//投注按钮事件
function AddProject(obj)
{
	var td = $(obj).parent().prev();
	var buynum = $(td).find("input[name=buynum]").val();
	var pid = $(td).find("input[name=pid]").val();
	var senumber = $(td).find("input[name=senumber]").val();
	var 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'>￥"+formatCurrency2(Number(buynum)*onemoney)+coinUnit+"</font></p>";
	alertdiv.confirm(msg,function(){add_project(buynum,pid,onemoney)});
}


//参加合玩
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")=="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,LoadHM)
			}
			else
			{
				if($(err).attr("msg").length>0)
				{
					alertdiv.alert($(err).attr("msg"));
				}
				if($(err).attr("url").length>0)
				{
					location.href = $(err).attr("url");
				}
			}
		},
		error: function (jqXHR, textStatus, errorThrown) {
			alertdiv.alert("交易失败,请重新提交！");
		},
		complete: function (jqXHR, textStatus) {
		}
	});	
}
//倒计时
function daojishi(){
	servertime = servertime + 1000
	var arr = $("#countDownData").val().split(",");
	for(var i=0; i<arr.length;i++)
	{
		var classarr = arr[i].split("#")
		var endtime = Date.parse(classarr[0].replace(/-/g,"/"))
		var surplussecond = Math.round((endtime-servertime)/1000);
		if(surplussecond>0)
		{
			var h = Math.floor(surplussecond/3600);
			var d = parseInt(h/24);
			h = h%24;
			if (h < 10)
			{
				h = "0"+h;
			}
			var syms = Math.floor(surplussecond%3600);
			var m = Math.floor(syms/60);
			if (m<10)
			{
				m = "0"+m;
			}
			var s = Math.floor(syms%60);
			if(s<10)
			{
				s = "0"+s;
			}
			if (d>0)
			{
				$("#"+classarr[1]).text(d+"天"+h+"时"+m+"分")
			}
			else
			{
				$("#"+classarr[1]).text(h+":"+m+":"+s+"")
			}
		}
		else
		{
			$("#"+classarr[1]).html("<span class='red'>已截止</span>")
		}
	}
}
