/* 
* @Author: anchen
* @Date:   2017-07-05 10:02:51
* @Last Modified by:   anchen
* @Last Modified time: 2017-07-07 15:41:43
*/

$(document).ready(function(){
    //关闭选择楼宇弹窗

    function showBuild(){
        $(".buildingmask").show().find(".selectbuilding").animate({
            bottom: 0
        }, 500);
    }
    $(".uu").click(function(){
        showBuild();
    })

    $(".closebuild").click(function(){
        closeBuild();
    })

    $(".searchbuild").click(function(){
        var _this = $(this);
        _this.parents("#opSearchbar").hide();
        $("#opSearchbarentry").show();
        $("#opName").focus();
    })

    // 搜索框有值时显示 ‘x’图标 -- 无值时隐藏 ‘x’图标    
    $("#opName").bind('input propertychange', function() {
		var value = $(this).val();
		if(value){
            $("#opIconclear").show();
        } else {
            $("#opIconclear").hide();
        }
		loadProvincecityByCity();
	});

    // 点击搜索框的'x'图标时清楚搜索框里的字
    $("#opIconclear").click(function(){
        var _this = $(this);
        $("#opName").val("");
        _this.hide();
        loadProvincecityByCity();
    });

    $(".buildinglist").on("click", ".buildingcity", function(){
    	var _this = $(this).find(".arrowcity");
    	var cityCode=_this.attr('id');
    	var params=$("#opName").val();
    	if(opName!=""){
    		loadOrganizationLists(cityCode);
    	}else{
    	    loadOrganizationList(cityCode,_this);
    	}
    });
});

/**
 * 加载城市
 */
function loadProvincecityByCity(){
	var contextPath=$("#contextPath").val();
	var params=$("#opName").val();
	var developId=$("#developId").val();
	$.ajax({
		type:'POST',
		url:contextPath+'/wechat/appUser/getProvincecityByCityList',
		data:{params:params,developId:developId},
		dataType: 'json',
		success: function (data){
			$(".buildinglist").empty();
			if(data!=null && data!="" && data.length>0){
				var str="";
				$.each(data,function(index,item){
					if(item.cityCode==$("#cityCode").val()){
						str="<li><div class='buildingcity' id='buildingcity_"+item.cityCode+"'><span>"+item.name+"</span><strong class='arrowcity' id='"+item.cityCode+"'><i></i></strong></div></li>";
					}else{
						var html="<li><div class='buildingcity' id='buildingcity_"+item.cityCode+"'><span>"+item.name+"</span><strong class='arrowcity' id='"+item.cityCode+"'><i></i></strong></div></li>";
						$(".buildinglist").append(html);
						if(params!=""){
							loadOrganizationLists(item.cityCode);
						}
					}
				});
				if(str!="" && params==""){
					$(".buildinglist").prepend(str);
				}
				if(str!="" && params!=""){
					$(".buildinglist").prepend(str);
					loadOrganizationLists($("#cityCode").val());
				}
			}
		}
	});
}

/*
 * 注入微信
 */
function wexConfig() {
	$.ajax({
		url : $("#contextPath").val() + "/wechat/visitor/fenxiang",
		type : "POST",
		async : true,
		cache : false,
		dataType : "json",
		data : {
			url : location.href.split('#')[0],
			developId : $("#developId").val()
		},
		success : function(data) {
			wx.config({
				debug : false, // 开启调试模式
				appId : data.appid, // 必填，公众号的唯一标识
				timestamp : data.timestamp, // 必填，生成签名的时间戳
				nonceStr : data.nonceStr, // 必填，生成签名的随机串
				signature : data.signature,// 必填，签名，见附录1
				jsApiList : [ 'getLocation', 'hideOptionMenu' ]
			// 必填，需要使用的JS接口列表，所有JS接口列表见附录2
			});
			//隐藏右上角菜单
			wx.hideOptionMenu();
			wx.ready(function(){
					wx.checkJsApi({  
		 	             jsApiList: [   
		 	               'getLocation',
		 	               'hideOptionMenu'
		 	             ],  
		 	             success: function (res) {  
		 	               //alert(res.errMsg);  
		 	             }
					});
				wx.getLocation({
						success : function(res) {
							var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
							var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
							var speed = res.speed; // 速度，以米/每秒计
							var accuracy = res.accuracy; // 位置精度
							//定位项目
							initOrgSelect(latitude,longitude);
						},
						cancel : function(res) {
							initOrgSelect(0.00,0.00);
						}
					});
			});

		}
			
	});
}

/**
 * 加载楼宇
 */
function loadOrganizationLists(cityCode){
	var contextPath=$("#contextPath").val();
	var params=$("#opName").val();
	var developId=$("#developId").val();
	if($("#projectlist_"+cityCode).length>0){
		$("#buildingcity_"+cityCode).parents("li").toggleClass("buildchioce");
		if($("#buildingcity_"+cityCode).parents("li").hasClass("buildchioce") ){
			$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideDown(200);
		} else {
			$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideUp(200);
		}
		//$("#buildingcity_"+cityCode).parents("li").siblings().removeClass("buildchioce").find("projectlist").slideUp(200);
	}else{
		$.ajax({
			type:'POST',
			url:contextPath+'/wechat/appUser/getOrganizationListByName',
			data:{params:params,cityCode:cityCode,developId:developId},
			dataType: 'json',
			success: function (data){
				if(data!=null && data!="" && data.length>0){
					$("#projectlist_"+cityCode).empty();
					var html="<ul class='projectlist' id='projectlist_"+cityCode+"'>";
					$.each(data,function(index,item){
						html+="<li value='"+item.ID+"' onclick='getValue(this)'>"+item.name+"</li>";
					});
					html+="</ul>";
					$("#buildingcity_"+cityCode).after(html);
					$("#buildingcity_"+cityCode).parents("li").toggleClass("buildchioce");
					$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideDown(800);
				}
			}
		});
	}
}


/**
 * 加载楼宇
 */
function loadOrganizationList(cityCode,obj){
	var contextPath=$("#contextPath").val();
	var developId=$("#developId").val();
	var params=$("#opName").val();
	if($("#projectlist_"+cityCode).length>0){
		obj.parents("li").toggleClass("buildchioce");
		if(obj.parents("li").hasClass("buildchioce") ){
			obj.parent(".buildingcity").siblings(".projectlist").slideDown(200);
		} else {
			obj.parent(".buildingcity").siblings(".projectlist").slideUp(200);
		}
		obj.parents("li").siblings().removeClass("buildchioce").find(".projectlist").slideUp(200);
	}else{
		$.ajax({
			type:'POST',
			url:contextPath+'/wechat/appUser/getOrganizationListByName',
			data:{params:params,cityCode:cityCode,developId:developId},
			dataType: 'json',
			success: function (data){
				if(data!=null && data!="" && data.length>0){
					$("#projectlist_"+cityCode);
					var html="<ul class='projectlist' id='projectlist_"+cityCode+"'>";
					$.each(data,function(index,item){
						html+="<li value='"+item.ID+"' onclick='getValue(this)'>"+item.name+"</li>"
					});
					html+="</ul>"
				}
				$("#buildingcity_"+cityCode).after(html);
				obj.parents("li").toggleClass("buildchioce");
				if(obj.parents("li").hasClass("buildchioce") ){
					obj.parent(".buildingcity").siblings(".projectlist").slideDown(200);
				} else {
					obj.parent(".buildingcity").siblings(".projectlist").slideUp(200);
					
				}
				obj.parents("li").siblings().removeClass("buildchioce").find(".projectlist").slideUp(200);
				
				
			}
		});
	}
}

function getValue(obj){
	 var _this = $(obj);
     var _html = _this.html();
     if(_html){
    	 var openId=$("#openId").val();
    	 var developId=$("#developId").val();
    	 var userId=$("#userId").val();
    	 var redirectUrl=$("#redirectUrl").val();
         //location.href=$("#contextPath").val()+"/wechat/index/homePageView?openId="+openId+"&developId="+developId+"&organizationId="+_this.val()+"&userId="+userId;
    	 if(redirectUrl!=null && (redirectUrl.indexOf("ChannelInterop")!=-1 || redirectUrl.indexOf("neigouFirstJump")!=-1)){//内购网
 			//window.location.href=contextPath+redirectUrl+"?userId="+userId+"&developId="+developId+"&openId="+openId;
 			window.location.href=$("#contextPath").val()+"/wechat/neigou/neigouFirstJump?userId="+userId+"&developId="+developId+"&openId="+openId+"&organizationId="+_this.val();
 			
 		}
     }
}

function getLocationiconValue(){
	if($("#locationiconId").val()==""){
		return;
	}
 	  var openId=$("#openId").val();
 	  var developId=$("#developId").val();
 	  var userId=$("#userId").val();
     location.href=$("#contextPath").val()+"/wechat/index/homePageView?openId="+openId+"&developId="+developId+"&organizationId="+$("#locationiconId").val()+"&userId="+userId;
}

function closeBuild(){
	var height = $(window).height();
    $(".selectbuilding").animate({
        bottom: -height + "px"
    }, 500, function(){
        $("#opSearchbar").show();
        $("#opSearchbarentry").hide();
        $(this).parents(".buildingmask").hide();
    });
}

function addressOp(_this){
        _this.parents("#opSearchbar").hide();
        $("#opSearchbarentry").show();
        $("#opName").focus();
        if($("#opName").val()){
            $("#opIconclear").show();
        } else {
            $("#opIconclear").hide();
        }
}