$(function(){
	/**
	 * 点击表格添加按钮
	 */
	$('#add_btn').click(function(){
		var data = {};
		doDataPost(data,false,ctx+"/adm/build/add/form_ui",function(htmlText){
			$("div.popup_window").remove();
			addForm(htmlText);
			openWin('build_add_div');
			//初始化地图
            var msgJson = isJsonObject(htmlText);
            if(msgJson == false){
                fshowMap("build_mapContainer",0,"build_address","build_latitude","build_longitude");
            }
		});
	});

	/**
	 * 点击表格删除按钮
	 */
	$('#remove_btn').click(function(){
		var x = ($(window).width() - 260 + $('div.viewFramework-sidebar').width())/2;
		var y = ($(window).height() / 2 - 73);
		var flag=false;
		$('table > tbody > tr > td > input[type=checkbox]').each(function(){
			var input = $(this);
			if(input.is(':checked')){
				flag=true;
			}
		});
		if(!flag){
			layer.alert('请选择要删除的楼栋。', {
				  icon: 0,
				  title:'提示',
				  area: ['260px', '146px'],
				  offset: [y+'px', x+'px']
				});
			return;
		};
		layer.confirm('确定要删除？', {
			icon: 3,
			title:'提示' ,
			btn: ['确定','取消'],
		  area: ['260px', '146px'],
		  offset: [y+'px', x+'px']
		}, function(index){
			// remove(ctx+"/adm/build/del");
            removeAndRefresh(ctx + "/adm/build/del",function () {
                refreshList();
            });
		    layer.close(index);
		});
	});

	/**
	 * 双击表格弹出修改窗口
	 */
	 $('#buildListTableDiv > div.scroll-pane > table > tbody > tr').click(function () {		 
		 mobileClickTimes++;
		 var this_index=$(this).index();
		 if(this_index==tr_index){
	        setTimeout(function () {
	            mobileClickTimes = 0;
	            tr_index=-1;
	        }, 800);
	        
	        if (mobileClickTimes > 1) {
	        	dblclickF(this);
	            mobileClickTimes = 0;
	            tr_index=-1;
	        } 
		 }else{
			 tr_index=this_index;
		 }
    });

	// $('table > tbody > tr').dblclick(dblclickF);
	function dblclickF(obj){
		var id = $(obj).find(':checkbox:eq(0)').val();
		var data = {};
		data["id"] = id;
		doDataPost(data,false,ctx+"/adm/build/edit/form_ui",function(htmlText){
			$("div.popup_window").remove();
			addForm(htmlText);
		    openWin('build_edit_div');
		    //初始化地图
			fshowMap("build_mapContainer",1,"build_address","build_latitude","build_longitude");
		});
	}

	/**
	 * 实现分页条件查询
	 */
	$('#buildListTableDiv div.my-pagination > div > a').click(function(){
		var inputs = $("form[name=build_searcg_from] :input");
		var data = {};
		$(inputs).each(function(){
			var input = $(this);
			data[input.attr('name')]=input.val();
		});
		var href = $(this).attr('href');
		//截取href中#之后的值
		var str = href.substring(1,href.Length);
		data.page=parseInt(str);
		getPageData(data,ctx+"/adm/build/list");
	});

	$('#buildListTableDiv div.my-pagination .page-current').change(function(){
		var inputs = $("form[name=build_searcg_from] :input");
		var data = {};
		$(inputs).each(function(){
			var input = $(this);
			data[input.attr('name')]=input.val();
		});
		var page = $(this).val();
		data.page=parseInt(page);
		getPageData(data,ctx+"/adm/build/list");
	});
	adjustTableCellWidth('buildListTableDiv');
});
/**
 * 刷新列表页面（附搜索条件）
 */
function refreshList(){
	doFormPost("build_searcg_from",ctx+"/adm/build/list",function(msg){
		addPage(msg);
	});
}