/*
 */
define("component/loadData", ["component/slider", "component/tab", "component/map"], function (require, exports, module) {
    /*==============地图=====================*/
    var MapO = require("./map");
    var map = new MapO({
        con: "#j_map"
    });
    map.selectArea("haikou");
    // tab
    var tab = require("./tab");
    var pTab = new tab({
        menu: "#j_aj_menu",
        content: "#p_box",
        activeClass: "on",
        effect: {
            "name": "fade",
            "speed": 300
        }

    });
    pTab.bind("clickMenu", function (event, target) {
        switch (target) {
            case "all":
                if (SliderData[target] == null) {
                    var obj = {
                        url: "/home/home/GetSourcePersion",
                        param: { Id: 0, CreateTime: "2014-02-28 14:21" },
                        con: "#personItem",
                        template: "j_person_template"
                    };
                    renderPanel(obj, target);

                } else {
                    SliderData[target].play();

                }
                SliderData["air"] && SliderData["air"].stop();
                SliderData["train"] && SliderData["train"].stop();
                SliderData["dock"] && SliderData["dock"].stop();
                break;

            case "air":
                if (SliderData[target] == null) {
                    var obj = {
                        url: "/home/home/GetDataSourceJSON",
                        param: { SourceName: "JC", Id: 0, CreateTime: "2014-02-28 14:21" },
                        con: "#airPersonItem",
                        template: "j_person_template"
                    };
                    renderPanel(obj, target);
                } else {
                    SliderData[target].play();

                }
                SliderData["all"] && SliderData["all"].stop();
                SliderData["train"] && SliderData["train"].stop();
                SliderData["dock"] && SliderData["dock"].stop();
                break;

            case "train":
                if (SliderData[target] == null) {
                    var obj = {
                        url: "/home/home/GetDataSourceJSON",
                        param: { SourceName: "HCZ", Id: 0, CreateTime: "2014-02-28 14:21" },
                        con: "#trainPersonItem",
                        template: "j_person_template"
                    };
                    renderPanel(obj, target);
                } else {
                    SliderData[target].play();

                }
                SliderData["air"] && SliderData["air"].stop();
                SliderData["all"] && SliderData["all"].stop();
                SliderData["dock"] && SliderData["dock"].stop();
                break;

            case "dock":
                if (SliderData[target] == null) {
                    var obj = {
                        url: "/home/home/GetDataSourceJSON",
                        param: { SourceName: "GK", Id: 0, CreateTime: "2014-02-28 14:21" },
                        con: "#dockPersonItem",
                        template: "j_person_template"
                    };
                    renderPanel(obj, target);
                } else {
                    SliderData[target].play();

                }
                SliderData["air"] && SliderData["air"].stop();
                SliderData["train"] && SliderData["train"].stop();
                SliderData["all"] && SliderData["all"].stop();
                break;

        }

    });
    // 要访问的数据链接及参数
    var dataUrl = {
        GetDataSourceJSON: {   // 获取各个点的数据//  'GK'  'JC' 'HCZ'
            name: "/home/home/GetDataSourceJSON",
            param: {
                SourceName: "JC", Id: 0, CreateTime: "2014-02-28 14:21"
            }
        },
        GetSourcePersion: {    // 获取所有人的数据
            name: "/home/home/GetSourcePersion",
            param: {
                Id: 0, CreateTime: "2014-02-28 14:21"
            }
        },
        GetAlarmReport: {   // 获取预警报表


        },
        GetProjectCount: {

        },
        GetAlarmJSON: {

        },
        GetAlermPerson: {

        }

    };
    var Slider = require("./slider");
    var SliderData = {
        "all": null,
        "air": null,
        "train": null,
        "dock": null,
        "yujing": null

    };
    /*
    *   {
    url : "",
    param : {},
    con : "",
    template : "",


    }
    */
    function renderPanel(obj, target) {
        $.post(obj.url, obj.param, function (result) {
            if (+result.status == 1 && result.data.length > 0) {
                var con = new Slider({
                    con: obj.con,
                    template: obj.template,
                    data: {
                        list: result.data,
                        isFirst: true
                    },
                    auto: true
                });
                SliderData[target] = con;
                var count = 0;  // 记录没有数据的情况次数，如果连续5次请求不到数据 则间隔3分钟再发请求
                con.bind("loadData", function (event, $index) {
                    var id = $index.data("id");
                    con.isLoadData = true;
                    if (count < 5) {
                        $.post(obj.url, { Id: +id, CreateTime: "2014-03-01 00:00" }, function (result) {
                            if (+result.status == 1 && result.data.length > 0) {
                                count = 0;
                                con.genericList({
                                    list: result.data,
                                    type: "default",
                                    isFirst: false
                                }, obj.template);

                            } else {
                                count++;
                            }
                            con.isLoadData = false;

                        }, "json");
                    } else {
                        setTimeout(function () {
                            var param = {
                                Id: +id
                            };
                            switch (target) {
                                case "all":
                                    param.CreateTime = "2014-03-01 00:00";
                                    break;
                                case "air":
                                    param.CreateTime = "2014-03-01 00:00";
                                    param.SourceName = "JC";
                                    break;
                                case "train":
                                    param.CreateTime = "2014-03-01 00:00";
                                    param.SourceName = "HCZ";
                                    break;
                                case "dock":
                                    param.CreateTime = "2014-03-01 00:00";
                                    param.SourceName = "GK";
                                    break;

                            }
                            $.post(obj.url, param, function (result) {
                                if (+result.status == 1 && result.data.length > 0) {
                                    count = 0;
                                    con.genericList({
                                        list: result.data,
                                        isFirst: false
                                    }, obj.template);

                                } else {
                                    count++;
                                }
                                con.isLoadData = false;

                            }, "json");

                        }, 1000 * 60 * 3);

                    }

                });
            } else {
                $(obj.con).html('<div style="padding:20px 10px 20px 50px;font-size:14px;font-weight:bold;">暂无数据</div>');

            }
        }, "json");
    }
    // 页面初始化即执行
    renderPanel({
        url: "/home/home/GetSourcePersion",
        param: { Id: 0, CreateTime: "2014-02-28 14:21" },
        con: "#personItem",
        template: "j_person_template"
    }, "all");
    $("#p_box").delegate(".scroll_down", "click", function () {
        var type = $(this).data("scrollbtn");
        SliderData[type] && SliderData[type].prev();

    });
    $("#p_box").delegate(".scroll_down", "mouseenter", function () {
        var type = $(this).data("scrollbtn");
        SliderData[type] && SliderData[type].stop();

    });
    $("#p_box").delegate(".scroll_down", "mouseleave", function () {
        var type = $(this).data("scrollbtn");
        SliderData[type] && SliderData[type].play();

    });
    $("#scroll_pre").click(function () {
        SliderData["yujing"] && SliderData["yujing"].prev();
    });
    $("#scroll_pre").mouseleave(function () {
        SliderData["yujing"] && SliderData["yujing"].play();
    });
    $("#scroll_pre").mouseenter(function () {
        SliderData["yujing"] && SliderData["yujing"].stop();
    });
    $("#scroll_next").click(function () {
        SliderData["yujing"] && SliderData["yujing"].next();
    });
    $("#scroll_next").mouseleave(function () {
        SliderData["yujing"] && SliderData["yujing"].play();
    });
    $("#scroll_next").mouseenter(function () {
        SliderData["yujing"] && SliderData["yujing"].stop();
    });
    $.post("/home/home/GetAlarmJSON", {}, function (result) {
        if (+result.status == 1) {
            var con = new Slider({
                con: "#b_box",
                template: "j_person_template",
                data: {
                    list: (result.data || []),
                    type: "yujing",
                    isFirst: true
                },
                slide: {
                    direction: "hor"
                },
                auto: true
            });
            SliderData["yujing"] = con;
        } else {
            var con = new Slider({
                con: "#b_box",
                template: "j_person_template",
                data: {
                    list: result.data,
                    type: "yujing",
                    isFirst: true
                },
                slide: {
                    direction: "hor"
                },
                auto: true
            });
            SliderData["yujing"] = con;
        }
    }, "json");
    //{"Name":"杨圣云","Idcard":"460006198205302716","ToolType":"火车站","Id":"88","City":"海口","HeadPhoto":"http://10.21.54.15:9795/2014/02/28/14/21/GangKou/6e6be26f-6f18-49ab-aad5-7f39034125ae.jpg"}
    var areaMap = {
        "海口": "haikou",
        "三亚": "sanya",
        "琼海": "qionghai",
        "东方": "dongfang"
    };
    // 给右侧的人员增加事件，与地图进行响应

    function addTip(that) {
        var param = {
            city: "",
            name: "",
            tooltype: "",
            idcard: "",
            headphoto: ""
        };
        // var that = $(this);
        param.city = that.data("city").replace(/'/g, "");
        map.options.showType = "person";
        param.name = that.data("name").replace(/'/g, "");
        param.tooltype = that.data("tooltype").replace(/'/g, "");
        param.idcard = that.data("idcard").replace(/'/g, "");
        param.headphoto = that.data("headphoto").replace(/'/g, "");
        var _html = "";
        _html += '<ul><li style="line-height:20px;"><span>姓名:</span><span style="margin-left:0px;">' + param.name + '</span></li>';
        _html += '<li style="line-height:20px;"><span style="margin-left:0px;">' + param.idcard + '</span></li>';
        _html += '<li style="line-height:20px;"><span>城市:</span><span style="margin-left:0px;">' + param.city + '</span></li>';
        _html += '<li style="line-height:20px;"><span>来源:</span><span style="margin-left:0px;">' + param.tooltype + '</span></li>';
        _html += '</ul>';
        map.setPersonToolTip(_html);
        if (areaMap[param.city]) {


            var isSelect = map.$con.mapster('get', areaMap[param.city]);

            map.selectArea(areaMap[param.city]);

        } else {

            map.deSelect(map.$con.mapster('get'));
            map.toolTip();
        }
    }
    $("#p_box").mouseleave(function () {
        map.options.showType = "map";

    });
    $("#p_box").delegate('[data-type="listItem"]', "click", function () {
        addTip($(this));

    });
    $("#b_box").mouseleave(function () {
        map.options.showType = "map";

    });
    $("#b_box").delegate('[data-type="listItem"]', "click", function () {
        addTip($(this));

    });


});