/// <reference path="../lib/jquery-1.4.1-vsdoc.js" />
/// <reference path="../lib/highcharts.js" />

function appendNode(data, range) {
    try {
        for (var i = 0; i < data.length; i++) {
            if (data[i].isleaf == "false") {
                var $node = $('<li><div id="' + range + "_" + data[i].id + '" class="parent"><div style="float:left;width:' + (data[i].level * 10 + 20) + 'px;"></div> <span>' + (data[i].money * 1).toFixed(window.parent.strMoneyPlace) + '</span>  ' + data[i].txt + '</div><ul></ul></li>');
            }
            else {
                var $node = $('<li><div id="' + range + "_" + data[i].id + '"><div style="float:left;width:' + (data[i].level * 10 + 20) + 'px"></div><span>' + (data[i].money * 1).toFixed(window.parent.strMoneyPlace) + '</span>' + data[i].txt + '</div></li>');
            }
            $("#" + range + "_" + data[i].parent).next().append($node);
        }
    } catch (e) { }
}

function getdata() {
    try {
        var edate = $("#sjdate").val();
        //        if (edate) {
        var records = ParseData(window.external.DoOptions("Tp0512", "GetList", '{"edate":"' + edate + '"}'));
        if (records.ReturnCode == 0 && records.RecordsFZ instanceof Array && records.RecordsJZC instanceof Array && records.RecordsZC instanceof Array) {
            $("#left_tree_0,#center_tree_0,#right_tree_0").next("ul").empty();

            var zclist = records.RecordsZC;
            var fzlist = records.RecordsFZ;
            var jzclist = records.RecordsJZC;

            var zcdata = getTotalMoney(zclist);
            var fzdata = getTotalMoney(fzlist);
            var jzcdata = getTotalMoney(jzclist);

            $("#left_tree_0 label").text(zcdata.total.toFixed(window.parent.strMoneyPlace));
            $("#center_tree_0 label").text(fzdata.total.toFixed(window.parent.strMoneyPlace));
            $("#right_tree_0 label").text(jzcdata.total.toFixed(window.parent.strMoneyPlace));

            appendNode(zclist, "left_tree");
            appendNode(fzlist, "center_tree");
            appendNode(jzclist, "right_tree");

            createPieChart("left_pie", zcdata.list, "", " 元");
            createPieChart("center_pie", fzdata.list, "", " 元");
            createPieChart("right_pie", jzcdata.list, "", " 元");
        }
        //        } else {
        //            window.parent.cihoAlert("日期不能为空！", msgBox.dlgBtn.Ok);
        //        }

    } catch (e) { }
}

function bindEvent() {
    try {
        $("div.overflo").delegate(".parent", "click", function () {
            $(this).next().slideToggle("slow").end().children("div").toggleClass("expand");
        });
        $("#expand").toggle(function () {
            $(".parent+ul").slideDown("slow");
            $(".parent>div").addClass("expand");
            $(this).text("全部合闭<<");
        }, function () {
            $(".parent+ul").slideUp("slow");
            $(".parent>div").removeClass("expand");
            $(this).text("全部展开>>");
        });
    } catch (e) { }
}

function getTotalMoney(arr) {
    try {
        var total = 0;
        var pielist = [];
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].level == 0) {
                total += arr[i].money * 1;
                var item = {};
                item.name = arr[i].txt;
                item.y = Math.abs(arr[i].money * 1);
                item.isMinus = arr[i].money * 1 < 0 ? "-" : "";
                //item.push(arr[i].txt, Math.abs(arr[i].money * 1))
                pielist.push(item);
            }
        }
        return { total: total, list: pielist };
    } catch (e) { }
}


function createPieChart(bindId, data, title, unit) {
    try {
        chart = new Highcharts.Chart({
            chart: { renderTo: bindId, plotBackgroundColor: null, plotBorderWidth: null, plotShadow: false, backgroundColor: "none" },
            title: { text: title },
            tooltip: { formatter: function () {
                return this.point.name + ' : ' + this.point.isMinus + (this.y * 1).toFixed(window.parent.strMoneyPlace);
            }
            },
            plotOptions: {
                pie: {
                    showInLegend: false,
                    allowPointSelect: true,
                    cursor: 'pointer',
                    size: "80%",
                    slicedOffset: 15,
                    dataLabels: {
                        enabled: false,
                        distance: 10,
                        color: '#000000',
                        connectorColor: '#000000',
                        rotation: 0,
                        formatter: function () { return this.point.name + ':' + this.y + unit; }
                    }
                },
                series: { animation: { duration: 2000} }
            },
            series: [{
                type: 'pie',
                data: data
            }]
        });
    } catch (e) { }
}


//调整窗口层的大小。
function resizeMyDiv() {
    //设定饼状图所在层的高度：过滤条件47，全部展开30
    $(".overflo").css("height", window.parent.document.documentElement.clientHeight - _iHtMenu - _iHtTab - _iHtCopyright - 47 - 26);
}