(function () {
    var fun = {
        init : function () {
            time_left_init_active:{
                $('#time_ul_l').prev().addClass('time_line_active');
                $('#time_ul_l li .time_date').addClass('time_date_all_active');
                $('#time_ul_l li:last .time_date').addClass('time_date_active');
                $('#time_ul_l li:last .time_circle').addClass('time_circle_l_active');
            };
            left_seven_time:{
                //设置日期，当前日期的前七天
                var myDate = new Date(); //获取今天日期
                myDate.setDate(myDate.getDate() - 7);
                var dateArray = [];
                var dateTemp;
                var flag = 1;
                for (var i = 0; i < 7; i++) {
                    dateTemp = (myDate.getMonth()+1)+"月"+myDate.getDate()+'日';
                    dateArray.push(dateTemp);
                    myDate.setDate(myDate.getDate() + flag);

                };
                $.each($('#time_ul_l .time_date'),function (i,item) {
                    //console.log(dateArray[i])
                    $(this).text(dateArray[i])
                })
            };
        },
        time_left_init:function () {
            $('#time_ul_l').prev().removeClass('time_line_active');
            $('#time_ul_l li .time_date').removeClass('time_date_all_active');
            $('#time_ul_l li .time_date').removeClass('time_date_active');
            $('#time_ul_l li .time_circle').removeClass('time_circle_l_active');
        },
        time_left_active:function (obj) {
            $('#time_ul_l').prev().addClass('time_line_active');
            $('#time_ul_l').find('.time_date').addClass('time_date_all_active');
            $(obj).find('.time_date').addClass('time_date_active');
            $(obj).find('.time_circle').addClass('time_circle_l_active');
        },
        time_right_init:function () {
            $('#time_ul_r').prev().removeClass('time_line_active');
            $('#time_ul_r li .time_date_r').removeClass('time_date_all_active');
            $('#time_ul_r li .time_date_r').removeClass('time_date_active');
            $('#time_ul_r li .time_circle_r').removeClass('time_circle_r_active');
        },
        time_right_active:function (obj) {
            $('#time_ul_r').prev().addClass('time_line_active');
            $('#time_ul_r').find('.time_date_r').addClass('time_date_all_active');
            $(obj).find('.time_date_r').addClass('time_date_active');
            $(obj).find('.time_circle_r').addClass('time_circle_r_active');
        },

    };
    fun.init();
    //时间轴右侧点击事件
    $('#time_ul_r li').on('click',function () {
        var obj = $(this);
        fun.time_left_init();
        fun.time_right_init();
        fun.time_right_active(obj);
        var o=obj.find('.time_date_r').text().indexOf(':');
        var o2=obj.find('.time_date_r').text().substr(o-2,o);
        var hourTime='2017-07-12 '+o2;
        console.log(hourTime);
        
        //ajax
        var req= {
                sysCode:sysComm.sysCode,
                cityId:sysComm.cityId,
                provId:sysComm.provId,
                queryDate:new Date(hourTime.replace(new RegExp(/-/gm) ,"/")),
            };
        var queryParkHeatmapByHour= {
            method: "post",
            url: dataUrl.util.queryParkHeatmapByHour(),
            data: JSON.stringify(req),
            contentType: 'application/json; charset=utf-8',
            dataType:'json',
            success: function(res){
                if(res.code == '8888'){
                    var result = res.data;
                    console.log(JSON.stringify(result));
                    var points =[];
                    for(var i=0; i<result.length;i++){
                        points.push({"lng":result[i].lon,"lat":result[i].lat,"count":result[i].currentParkCount});

                    }
                    heatmapOverlay.setDataSet({data:points,max:10});
                    setGradient()
                }
        	}
        };
        sysAjax(queryParkHeatmapByHour);

    });
    //时间轴左侧点击事件
    $('#time_ul_l li').on('click',function () {
        var obj = $(this);
        fun.time_right_init();
        fun.time_left_init();
        fun.time_left_active(obj);
       // console.log(obj.find('.time_date').text())
        var monthAddress=obj.find('.time_date').text().indexOf('月');
        var month=obj.find('.time_date').text().substr(monthAddress-1,monthAddress);
       // console.log(month)
        var dayAddress=obj.find('.time_date').text().indexOf('日');
        var day=obj.find('.time_date').text().substr(monthAddress+1,dayAddress-2);
        //console.log(day);
        var date=new Date();
        var s=date.getFullYear();
        //console.log("year"+s)
        var time=s+'-'+month+'-'+day+' 00:00:00';
       	console.log(time);
        //ajax
        var req= {
                sysCode:sysComm.sysCode,
                cityId:sysComm.cityId,
                provId:sysComm.provId,
                queryDate:new Date(time.replace(new RegExp(/-/gm) ,"/")),
            };
        var queryParkHeatmapByDay= {
            method: "post",
            url: dataUrl.util.queryParkHeatmapByDay(),
            data: JSON.stringify(req),
            contentType: 'application/json; charset=utf-8',
            dataType:'json',
            success: function(res){
                if(res.code == '8888'){
                    var result = res.data;
                   var points =[];
                    for(var i=0; i<result.length;i++){
                    	 points.push({"lng":result[i].lon,"lat":result[i].lat,"count":result[i].currentParkCount});
                    }

                    console.log(points);
                    heatmapOverlay.setDataSet({data:points,max:10});
                    setGradient()
                }
        	}
        };
        sysAjax(queryParkHeatmapByDay);

    });


})();


var map = new BMap.Map('allmap',{enableMapClick:false});// 创建地图实例
var point = new BMap.Point(sysComm.point_lng,sysComm.point_lat);

//第5步：设置地图风格
map.setMapStyle({
    styleJson:[
        {
            "featureType": "background",
            "elementType": "geometry",
            "stylers": {
                "color": "#0a0906"
            }
        },
        {
            "featureType": "building",
            "elementType": "geometry",
            "stylers": {
                "color": "#444444"
            }
        },
        {
            "featureType": "local",
            "elementType": "geometry",
            "stylers": {
                "color": "#ff9900",
                "weight": "0.1"
            }
        },
        {
            "featureType": "road",
            "elementType": "all",
            "stylers": {
                "color": "#9b780e",
                "weight": "0.1",
                "saturation": -21,
                "visibility": "on"
            }
        },
        {
            "featureType": "green",
            "elementType": "geometry",
            "stylers": {
                "color": "#344222",
                "saturation": -20
            }
        },
        {
            "featureType": "water",
            "elementType": "geometry",
            "stylers": {
                "color": "#163e5a"
            }
        },
        {
            "featureType": "subway",
            "elementType": "all",
            "stylers": {
                "color": "#ff9900",
                "saturation": -39
            }
        },
        {
            "featureType": "railway",
            "elementType": "geometry",
            "stylers": {
                "color": "#ff9900"
            }
        },
        {
            "featureType": "all",
            "elementType": "labels.text.stroke",
            "stylers": {
                "color": "#313131"
            }
        },
        {
            "featureType": "all",
            "elementType": "labels.text.fill",
            "stylers": {
                "color": "#8b8787"
            }
        },
        {
            "featureType": "manmade",
            "elementType": "geometry",
            "stylers": {
                "color": "#1b1b1b"
            }
        },
        {
            "featureType": "highway",
            "elementType": "geometry",
            "stylers": {
                "lightness": -75,
                "saturation": -91
            }
        },
        {
            "featureType": "subway",
            "elementType": "geometry",
            "stylers": {
                "lightness": -65
            }
        },
        {
            "featureType": "railway",
            "elementType": "geometry",
            "stylers": {
                "color": "#000000",
                "weight": "1",
                "lightness": -40,
                "saturation": 1
            }
        }
    ]
});
map.centerAndZoom(point, 11);             // 初始化地图，设置中心点坐标和地图级别
map.enableScrollWheelZoom(); // 允许滚轮缩放

var points =[];
//默认请求昨天的热力图点
function defaultHeat() {

var date = new Date();
var seperator1 = "-";
var month = date.getMonth() + 1;
var strDate = date.getDate()-1;
var yesterday = date.getFullYear() + seperator1 + month + seperator1 + strDate;
console.log(yesterday);
var req= {
    sysCode:sysComm.sysCode,
    cityId:sysComm.cityId,
    provId:sysComm.provId,
    queryDate:new Date(yesterday.replace(new RegExp(/-/gm) ,"/")),
};
var queryParkHeatmapByDay= {
    method: "post",
    url: dataUrl.util.queryParkHeatmapByDay(),
    data: JSON.stringify(req),
    contentType: 'application/json; charset=utf-8',
    dataType:'json',
    success: function(res){
        if(res.code == '8888'){
            var result = res.data;
            console.log(result);
            for(var i=0; i<result.length;i++){
                points.push({"lng":result[i].lon,"lat":result[i].lat,"count":result[i].currentParkCount});
            }
        }
        heatmapOverlay.setDataSet({data:points,max:10});
        setGradient()
    }
};
sysAjax(queryParkHeatmapByDay);

}
 defaultHeat();

if(!isSupportCanvas()){
    alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
}

heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":50});
map.addOverlay(heatmapOverlay);
heatmapOverlay.setDataSet({data:points,max:10});

setGradient();
function setGradient(){
    //*格式如下所示:
    var gradient = {
        .2:'rgb(0,255,0)',
     .5:'rgb(255,255,0)',
     1:'rgb(255,0,0)'
     };

    heatmapOverlay.setOptions({"gradient":gradient});
}
//判断浏览区是否支持canvas
function isSupportCanvas(){
    var elem = document.createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
}
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
   var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}