 //  北京单场注数数组 

// 页面初始加载
$(document).ready(function(){
    // 增加投注插件，实现点击效果
//    $(".vs").voteColor();
    
    // initialCommon
    // 公共初始化
    initialCommon();
    
    // Change tabs
    // 初始化过关类型
    initialTabs();
    
    // Cancel select option
    // 加载场次胜平负选择事件
//    cancelOption();
    
    // checkView
    // 让球与非让球
    checkView1();
    checkView2();
    
    // ShowHideVSButton
    // 显示截止比赛
    showHideVSButton();
    
    //selectVS
    // 显示对阵信息
    selectVS();
    
    //setDanma();
    // 设置胆码 
    // modified by nightcat 2009-02-24 
    // 改为通过点击激发
//    setDanma();
    
    // countGGType
    // 设置过关方式 
    // modified by nightcat 2009-02-24 
    // 改为点击激活
//    countGGType();
    
    // 加载去重复事件
//    setCutMulitCHK();
    
    // 加载倍数事件
    countBeiShu();
    
    // Hide ChangCi
    // 隐藏场次 分解为点击更新
    hideChangCi();
    
    // 点击删除事件
//    deleteRecord();

    // 手工更新SP值
    updateSPByClick();
    
    // 自动每5分钟更新
    updateSPAuto();
    
    // 清空所有投注
    cleanAllVote();
    
    // 提交事件
    doSubmit();
    

    
    // 提交判断
    checkSubmit();  
    
    // 初始SP
//    updateSP();
});

// 屏蔽键盘效果
$(window).keydown(function(event){
    switch (event.keyCode) {
    // ...
    // different keys do different things
    // Different browsers provide different codes
    // see here for details: http://unixpapa.com/js/key.html    
    // ...
    case 116:
         event.keyCode=0; 
         event.returnValue=false; 
         window.location.reload(); 
        break;
    }
});
function KeyDown(){   
  if ((window.event.altKey)&& 
      ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ← 
       (window.event.keyCode==39))){  //屏蔽 Alt+ 方向键 → 
     alert("不准你使用ALT+方向键前进或后退网页！"); 
     event.returnValue=false; 
     } 
  if ((event.keyCode==8)||            //屏蔽退格删除键 
      (event.keyCode==116)){          //屏蔽 F5 刷新键 
     event.keyCode=0; 
     event.returnValue=false; 
     } 
  if ((event.ctrlKey)&&(event.keyCode==78)){   //屏蔽 Ctrl+n 
     event.returnValue=false; 
     } 
  if ((event.shiftKey)&&(event.keyCode==121)){ //屏蔽 shift+F10 
     event.returnValue=false; 
     } 
  if (event.keyCode==122){ //屏蔽 F11 
     event.returnValue=false; 
  } 
} 

// 提交判断
function checkSubmit(){
    $("#complexform").submit(function(){
        // 判断是否登陆
        if ( $("#loginflag").val() == "notlogin"){
            openloginwindow();
            return false;
        }
        
        // 是否选择场次
        if ( !$("#strvote").val() ) {
            alert("你还没有选择场次,不能提交!");
            return false;
        }
        
        // 是否选择过关方式
        if ( !$("#ggtype").val() ) {
            alert("你还没有选择过关方式,不能提交!");
            return false;
        }
        
        // 是否符合倍数
        if ( $("#beishu").val() < 1 ||  $("#beishu").val() > 99999 ) {
            alert("你填写的倍数不能小于或者大于9999");
            $("#beishu").focus();
            return false;            
        }
        if ( $("#beishu").val() <= 0 || parseInt($("#beishu").val()) !=$("#beishu").val()  ) {
            alert("你填写的倍数必须是大于1的整数");
            $("#beishu").focus();
            return false;            
        }        
        
        // 单倍金额不能超过20000元, 最小金额为2元
        if ( $("#zhushui").val()*2 > 20000 ) {
            alert("单倍购买金额不能超过20000元");
            return false;
        }
        if ( $("#zhushui").val()*2 < 2 ) {
            alert("单倍购买金额不能低于过2元");
            return false;
        }        
        
        if ($("#yt").val() == 1) {
            var ytmin = parseInt($("#ytmin").val());
            var ytmax = parseInt($("#ytmax").val());
            var allmoney = parseInt($("#allmoney").text());
            if(allmoney<ytmin || allmoney>ytmax){
                alert("您方案的预投金额在 "+ytmin+" 和 "+ytmax+" 之间");
                return false;
            }
        }
    });
}




/*
提交事件
*/
function doSubmit(){
    $("#btnHemai, #btnDaigou, #evaluate ,#btnYtsc").mouseover(function(){
    	if($("#IsCutMulit").get(0).checked){
    		$("#IsCutMulit2").val("1"); 
    	}else{
    		$("#IsCutMulit2").val("0");
    	}
        var url = $(this).attr("url");
        $("#complexform").attr("action", url);
    });  
    $("#btnHemai, #btnDaigou, #evaluate ,#btnYtsc").mouseout(function(){
        $("#complexform").attr("action", "");
    });  
}

/*
清空所有投注
*/
function cleanAllVote(){
    $("#cleanAll").click(function(){
        window.location.reload();              
    });
}

/*
更新SP值
*/
function updateSP(){
    var obj      = $("#updatesp") ; 
    var url      = obj.attr("url");
    var expect   = $("#period").val();
    var playtype = $("#playtype").val();
    var totalVoteShu = $("#totalVoteShu").val();
    $.ajax({
        type: "POST",
        url: url + "?expect="+expect+"&type="+playtype,
        success: function(msg){
            if ( msg != 0 ) {
                var msg = $.evalJSON(msg);
                
                $(".form_tr4").each(function(){
                    var sId = $(this).attr("id");
                    var iIsdisable = $(this).attr("isdisable");
                    var sChangCi = sId.replace("tr_vs_", "");
                    sChangCi = parseInt(sChangCi);
                    if ( parseInt(iIsdisable) == 0 && sChangCi > 0 ) {
                        if ( msg[sChangCi] ) {
                           for(var i=1; i<=Number(totalVoteShu); i++){
                               var sp = $("#span_vs_"+sChangCi+"_"+i);
                               var o = sp.attr("rate");
                               var n = msg[sChangCi][i];
                               if ( n > o ) {
                                  sp.attr("rate", n); 
                                  sp.html(n + "<font color=red>↑</font>"); 
                               } 
                               
                               if ( n < o )  {
                                  sp.html(n + "<font color=green>↓</font>"); 
                               }    
                           }                             
                        }
                        
                    }
                });
            }
        }
    });           
}

function updateSPAuto(){
    setInterval ("updateSP()", 5*60*10000);
}

function updateSPByClick(){
    $("#updatesp").click(function(){
        updateSP();
    });
}

// 点击删除事件
function deleteRecord(obj){
        // 隐藏此行
        $(obj).parent().css("display", "none");      
        
        // 清除选择投注项
        var iChangCi = $(obj).attr("changci");
        $("#tr_vote_"+iChangCi+"_5").attr("num", 0);
        
        // 清除选择颜色
        // added by nightcat 2009-1-14
        var totalVoteShu = $("#totalVoteShu").val();
        for ( var i=1; i<=totalVoteShu; i++ ) {
            var oVote = $("#span_vote_" + iChangCi + "_" + i);  
            $(oVote).css("display", "none");
            
            var oSelect = $("#span_vs_" + iChangCi + "_" + i);   
            if(getSelectClass(oSelect)){
                // 计算场数
                countChangShu(oSelect);
                // 投注字符串
                countStrVote(oSelect);
                // 去颜色
                $(oSelect).css("color", "#2C94F4");
                $(oSelect).parent().removeClass(); 
            }
        }
        // 删除事件，只有胆码被选才去除胆码
        deleteDanma(obj);
        // 胆码取消选择
        $("#danma_"+iChangCi).attr("checked", "");
        // 计算场数
        countChangShu(oSelect);
        // 过关方式
        passMethod(oSelect);
        // 算金额
        addCountMoney(); 
}

/*
隐藏场次
*/
function hideChangCiEach(obj){
    $(obj).parent().css("display", "none");
    var hidecc = $("#hidecc").html();
    if ( !hidecc ) {
        $("#hidecc").html(1);
    } else {
        $("#hidecc").html(parseInt(hidecc)+1);
    }
}

function hideChangCi(){
    $("#showcc").click(function(){
        $(".form_tr4").each(function(){
            // 只操作可见的场次
            if ( $(this).attr("isdisable") !=1 ) {
                if ( $(this).css("display") == 'none') {
                    $(this).css("display", '');
                }
            }
            $("#hidecc").html('0');
        });
    });
    
    $(".split").toggle(function(){
        var key = $(this).attr("key");
        var i = 0 ;
        // 隐藏对阵信息
        $(".form_tr4").each(function(){
            if ( $(this).attr("key") == key && $(this).attr("isdisable") == 0 ) {
                if ( $(this).css("display") != "none" ) {
                    $(this).css("display", "none"); 
                    i++;
                }
            }
        });
        
        // 增加隐藏数
        var hidecc = $("#hidecc").html();
        $("#hidecc").html(parseInt(hidecc)+i);
        
        // 更改显示图片
        var src = $(this).children().attr('src');
        var newsrc = src.replace('btn_hide', 'btn_show');
        $(this).children().attr('src', newsrc);
    },function(){
        var key = $(this).attr("key");
        var i = 0 ;
        // 显示对阵信息
        $(".form_tr4").each(function(){
            if ( $(this).attr("key") == key && $(this).attr("isdisable") == 0 ) {
                if ( $(this).css("display") != "" ) {
                    $(this).css("display", ""); 
                    i++;
                }
            }
        });    
        
        // 减少隐藏数
        var hidecc = $("#hidecc").html();
        if ( parseInt(hidecc)-i == 0 ) {
            $("#hidecc").html('0');
        } else {
            $("#hidecc").html(parseInt(hidecc)-i);
        }
                    
        // 更改隐藏图片
        var src = $(this).children().attr('src');
        var newsrc = src.replace('btn_show', 'btn_hide');
        $(this).children().attr('src', newsrc);     
           
    });
}

/*
初始化数值
包括场数，过关方式，过关类型，倍数，胆码
*/
function initialCommon(){
    // Init changshu
    // 场数-给用户显示
    $("#changshu").html('0');
    
    // Init changshui
    // 场数-记录选择场数
    $("#changshui").val(0);
    
    // Init gggroup
    // 过关类型
    $("#gggroup").val(1);
    
    // Init peishu
    // 倍数
    $("#beishu").val(1);    
    
    // 胆码
    $("#danmanum").val(0);    
    $("#strdanma").val('');    
    
     // 投注内容
    $("#strvote").val('');  
    
    // 投注内容
    $("#showstrvote").val('');  
    
    // 投注场数
    $("#totalChangShu").val('');     
    
    // 投注时间
    $("#totalChangShuTimeStamp").val('');     
}

/*
过关类型转换事件
普通过关，组合过关，自由过关
*/
function initialTabs(){
    // 普通过关
    $("#sgtype_1").click(function(){
        $("#gggroup").val(1);
        // Set class 
        for(i=1;i<=3;i++){
            $("#sgtype_" + i).removeClass();
        }
        $(this).addClass("active");
                
        // Disable sgtype_2 and sgtype_3
        $("#tab0_1").css("display", "");
        $("#tab0_2").css("display", "none");
        $("#tab0_3").css("display", "none");
        
        // 转换过关类型清除其他类型所选
        cleanAllSelectedOption();
        // 清除胆码
        setDanmaDisabled(); 
        // 普通过关
        commonMethod(1);
    });    
    
    // 组合过关
    $("#sgtype_2").click(function(){
        $("#gggroup").val(2);
        for(i=1;i<=3;i++){
            $("#sgtype_" + i).removeClass();
        }
        $(this).addClass("active");

        $("#tab0_1").css("display", "none");
        $("#tab0_2").css("display", "");
        $("#tab0_3").css("display", "none");
        
        // 清除已选项
        cleanAllSelectedOption();  
        // 清除胆码
        setDanmaDisabled();         
        
        // 开启去重复
//		$("#IsCutMulit").attr("checked", "checked");
		$("#IsCutMulit").attr("disabled", "");     
    
		// 组合过关
		composeMethod(1);
		 
    });
    
    // 自由过关
    $("#sgtype_3").click(function(){
        $("#gggroup").val(3);
        for(i=1;i<=3;i++){
            $("#sgtype_" + i).removeClass();
        }
        $(this).addClass("active");

         // Disable sgtype_2 and sgtype_3
        $("#tab0_1").css("display", "none");
        $("#tab0_2").css("display", "none");
        $("#tab0_3").css("display", "");
        
        // 清除已选项
        cleanAllSelectedOption();
         // 清除胆码
        setDanmaDisabled();   
        
        $("#IsCutMulit").attr("disabled", "disabled");
        
        // 自由过关
        customMethod(1);
    });
}

/*
加载场次胜平负选择事件
改为通过点击激活事件 
modified by nightcat 2009-03-01
*/
function cancelOption(obj){
//    $(".sel_chk").click(function(){
        
        $(obj).parent().parent().css("display", "none");
        var changci = $(obj).attr('changci');

        var num = $("#tr_vote_" + changci +"_5").attr("num");
        var index = $(obj).attr("index");

        // Cancel choice
        // 取消选择
        var oSelect = $("#span_vs_" + changci + "_" + index);
        setColor(oSelect);
        
        // Cancel ratio
        // 取消赔率
        var oRatio = $("#span_sp_" + changci + "_" + index);
        oRatio.css("display", "none");
        
        // handle chhangshu 
        // 处理投注
        countVote(oSelect);
        
        // 处理场数
        countChangShu(oSelect);

        // 处理胆码
        changeDanma(obj);
        
        // handle passMethod
        // 处理过关方式
        passMethod(oSelect);    
        
        // selected again
        // 重新选上取消的选择
        $(obj).attr("checked", "checked");
        
        // 处理投注结果
        countStrVote(oSelect);
        
        // 重新计算Money
        addCountMoney();
//    });    
}

/*
让球
*/
function checkView1(){
    $("#chk1").click(function(){
        var checked = $(this).attr("checked");
        if ( checked ) {
            $(".sg_td_01").each(function(){
                var value = $(this).html();
                var isdisable = $(this).parent().attr("isdisable");
                if ( isdisable==0 && parseInt(value) !=0 ) {
                    $(this).parent().css("display", "");
                }
            });
        } else {
            $(".sg_td_01").each(function(){
                var value = $(this).html();
                var isdisable = $(this).parent().attr("isdisable");
                if ( isdisable==0 && parseInt(value) !=0 ) {
                    $(this).parent().css("display", "none");
                }
            });            
        }
    });    
}

/*
非让球
*/
function checkView2(){
    $("#chk2").click(function(){
        var checked = $(this).attr("checked");
        if ( checked ) {
            $(".sg_td_01").each(function(){
                var value = $(this).html();
                var isdisable = $(this).parent().attr("isdisable");
                if ( isdisable==0 && parseInt(value) ==0 ) {
                    $(this).parent().css("display", "");
                }
            });
        } else {
            $(".sg_td_01").each(function(){
                var value = $(this).html();
                var isdisable = $(this).parent().attr("isdisable");
                if ( isdisable==0 && parseInt(value) ==0 ) {
                    $(this).parent().css("display", "none");
                }
            });            
        }
    });    
}

/*
显示或隐藏过期对阵信息
*/
function showHideVSButton(){
    $("#ShowHideVSButton").click(function(){
        var checked = $(this).attr("checked") ;
        if ( checked ) {
            $(".sg_td_01").each(function(){
                var p         = $(this).parent();
                var isdisable = p.attr("isdisable");
                if ( isdisable == 1 ) {
                    p.css("display", "");
                }
            });
        } else {
            $(".sg_td_01").each(function(){
                var p         = $(this).parent();
                var isdisable = p.attr("isdisable");
                if ( isdisable == 1 ) {
                    p.css("display", "none");
                }            
            });             
        }   
    });
}

/*
选择对阵信息
*/
function selectVS(){
    $("#openVS").click(function(){
        $("#openSelectVS").css("display", "");
    });
    
    $(".closeSelectVS").click(function(){
        $("#openSelectVS").css("display", "none");
    });
    
    $(".selectTeam").click(function(){
        var checked = $(this).attr("checked");
        var wcchecked = $("#ShowHideVSButton").attr("checked");
        var tmpcss = checked ? "" : "none";
        var teamname1 = $(this).attr("teamname");
        if(wcchecked){
            $(".sg_td_01").each(function(){
                var p = $(this).parent();
                var teamname2 = p.attr("teamname");
                if ( teamname2 == teamname1 ) {
                    p.css("display", tmpcss);
                }
            });
        }else{
            $(".sg_td_01").each(function(){
                var p = $(this).parent();
                if (p.attr("isdisable") == 0 ) {
                    var teamname2 = p.attr("teamname");
                    if ( teamname2 == teamname1 ) {
                        p.css("display", tmpcss);
                    }
                }
            });
        }
//        if (checked) {
//            var teamname1 = $(this).attr("teamname");
//            $(".sg_td_01").each(function(){
//                var p = $(this).parent();
//                if (p.attr("isdisable") == 0 ) {
//                    var teamname2 = p.attr("teamname");
//                    if ( teamname2 == teamname1 ) {
//                        p.css("display", "");
//                    }
//                }
//            });
//        } else {
//            var teamname1 = $(this).attr("teamname");
//            $(".sg_td_01").each(function(){
//                var p = $(this).parent();
//                if (p.attr("isdisable") == 0 ) {
//                    var teamname2 = p.attr("teamname");
//                    if ( teamname2 == teamname1 ) {
//                        p.css("display", "none");
//                    }
//                }
//            });            
//        }
    });
    
    $(".selectAllTeam").click(function(){
        $(".selectTeam").each(function(){
            $(this).attr("checked", "checked");
        });
        
        $(".sg_td_01").each(function(){
            p = $(this).parent();
            if (p.attr("isdisable") == 0 ) {
                p.css("display", "");
            }
        });     
    });
    
    $(".selectNoneTeam").click(function(){
         $(".selectTeam").each(function(){
            if ($(this).attr("checked")) {
                $(this).attr("checked", "");
            } else {
                $(this).attr("checked", "checked");
            }
        });
        
        $(".sg_td_01").each(function(){
            var p = $(this).parent();
            var display = p.css("display");
            if (p.attr("isdisable") == 0 ) { 
                if ( display == "none") {
                    p.css("display", "");
                } else {
                    p.css("display", "none");
                }
            }
        });         
    });
}


//拖慢速度暂时去掉
//modified by nightcat 2009-02-24
$(document).ready(function(){
	if ( $("#playid").val() != "104" ) {
		//实现表格斑马效果
		$('.tables1 tr:even').addClass('zebra');
		
		//高光显示当前行
		$('.tables1 tr').hover(function(){
			$(this).addClass('uronhere');
		}, function(){
			$('.tables1 tr').removeClass('uronhere');
		});
	}
});
