// 
! function($) {
    var dataUrl = "http://lolbox.duowan.com/new/api/index.php?_do=dealconfig/getchampiontalent&champion_id=";
    var errHtml = "天赋数据获取异常，请<a href=\"http://fankui.duowan.com/\" target=\"_blank\">反馈客服</a>！"

    function pageInit() {
        var championsId =  getQueryString("championsId");
        if(!championsId){
            $("#errTips").show().html(errHtml);
            return ;
        }
        $.ajax({
            url: dataUrl + championsId,
            dataType: "jsonp",
            success: function(res) {
                if(res.status == 10402) {
                    $("#errTips").show().html(errHtml);                    
                }else if(res.status == 10401) {
                    $("#errTips").show().html(errHtml);         
                }else {
                    var data = res.talent_data,
                        $con = $con = $("<div>");

                    for( var i = 0; i < data.length; i++ ) {
                        var temp = $("#contentTemp").html(), 
                            talent_con = drawCalculator(),
                            talent_summaries = drawSummaries(),
                            sHtml = temp.replace(/\{(.*)\}/g, function(arg1, arg2) {
                                return data[i][$.trim(arg2)]
                            }),
                            $temp = $(sHtml);                    
                        importMasteries(data[i], talent_con, talent_summaries, $temp, championsId);
                        $temp
                        .find(".tallent__head")
                            .append(talent_summaries).end()
                        .find(".tallent__con")
                            .append(talent_con);
                        $con.append($temp);
                    }

                    $("#talentListCon").append($con);
                    scrollApi.reinitialise();
                }
            }
        })
    }

    function getQueryString(name) { 
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i"),
            r = window.location.search.substr(1).match(reg); 
        if (r!=null) {
            return (r[2]);
        }
        return null; 
    }
  
    var scrollDom, scrollApi;
    $(function() {
        $(document).on("click", ".btn_check", function() {
            var $this = $(this),
                lists = $this.parent().next();
            $this.toggleClass("btn_check_up");
            lists.toggleClass("show");
            $this.parents(".talentItem").toggleClass("active");
            scrollApi.reinitialise();
            if($(".talentItem.active").length > 0) {
                scrollApi.scrollToElement($this.parents(".talentItem"), true, 300);
            } else {
                $(".jspPane").css({
                    top: 0
                })
            }
        }).on("click", ".btn_apply", function() {
            var timer, $this = $(this);
            $this.on("click", function(event) {
                event.stopPropagation()
            }), $("#submitTips").show(), timer = setTimeout(function() {
                $("#submitTips").hide(), $this.off("click")
            }, 4e3)
        });
        
        pageInit();

        scrollDom = $("#talentList").jScrollPane({
            mouseWheelSpeed: 20
        }), scrollApi = scrollDom.data("jsp")
    })
}(jQuery, this);
