var search_info = "";

var current_loc = null;
var longitude = ''; //经度
var latitude = ''; //纬度

var street = getCookie('street') ? getCookie('street') : '';
var point = getCookie('point') ? getCookie('point') : '';
$(document).ready(function() {

	ifLogin(function(flag) {
		$("#show_html").show();

		var search_h = $("#search").height();
		var all_h = $(document).height() - 44;
		var list_h = all_h - search_h;
		$("#prod_list").height(list_h);
		$("#navbar-example").height(list_h);
		if(street == '') {
			get_list();
			getLocation();
		} else {
			$('#dingwei').text(street);
			$('#dingwei').show();
			get_list();
			//			getLocation();

		}

		//		get_list();
		//		getLocation();

		$('#search_input').on('input', function(e) {
			var input_text = $("#search_input").val();
			if(input_text != "") {
				$(".search_r").show();
				$("#go_search").show();
				delay(function() {
					get_list();
				}, 1500);
			} else {
				$(".search_r").hide();
				$("#go_search").hide();
				get_list();
			}
		});
		$('#cancel').on('click', function() {
			$("#search_input").val("");
			$(".search_r").hide();
			$("#go_search").hide();
			get_list();
		});

		$('#go_search').on('click', function() {
			get_list();
		});
	});
	//滚动监听
	//滚动监听
	// $("#prod_list").scroll(function(){
	//var arr = [];
	//$(".prod_scroll").each(function(index,dom){
	// var top_h=$("#search").height();
	// var div_h = $(dom).offset().top;
	// 
	// var t=top_h-div_h;
	// arr.push($(dom).attr('id'));
	// if(t>-1 && t<=30){
	//  var this_id_name=$(dom).attr('id');
	//  var t = $('a[href="#'+this_id_name+'"]');
	//  
	//  console.log(this_id_name);
	//  console.log('%ccoding..........................................','font-size:20px;color:red;');
	//  $('#left_menu li').removeClass('active');
	//  $('a[href="#'+this_id_name+'"]').parent().addClass('active');
	// }
	// if(t>=-30 && t<1){
	//  console.log('top_h',top_h,'-----------div_h',div_h,'tttttttttt=',t);
	//  var i = index;
	//  if(i>0){
	//   i = i-1;
	//  }else{
	//   i=0;
	//  }
	//  $('#left_menu li').removeClass('active');
	//  $('a[href="#'+arr[i]+'"]').parent().addClass('active');
	//  
	// }
	// 
	//});
	//});
});

function get_list() {
	search_info = $("#search_input").val();
	ZZLog(point);
	//	point = '121.321586' + ',' + '31.203112';
	$.ajax({
		url: base_api + "prod/prod/get-prod-list", //请求地址
		data: { where: search_info, point: point },
		type: "post",
		dataType: "json",
		async: false,
		success: function(res_data) {
			console.log(res_data)
			if(res_data.flag) {
				list_html(res_data.data);
				$('#left_menu li:first').addClass('active');
				$('#prod_list').animate({
						scrollTop: 0
					},
					200);
			} else {
				if(codeNotLogin == res_data['code']) {
					location.href = res_data['url'];
				} else if(codeNotRegister == res_data['code']) {
					location.href = 'register.html';
				} else {
					alert(res_data['msg']);
				}
			}
		},
		error: function(error) {
			console.log(error);
		}
	});
}

function list_html(data) {
	var left_temp = "";
	var right_temp = "";
	for(var i = 0, j = data.prod_list.length; i < j; i++) {
		if(data.prod_list[i].cms_category_id == 2) {
			left_temp += '<li><a class="ui_p_a10 ulev1 text_c_80 hot lightgreen_color" href="#prod' + i + data.prod_list[i].cms_category_id + '">' + data.prod_list[i].category_name + '</a></li>'
		} else {
			left_temp += '<li><a class="ui_p_a10 ulev1 text_c_80 lightgreen_color" href="#prod' + i + data.prod_list[i].cms_category_id + '">' + data.prod_list[i].category_name + '</a></li>'
		}

		var length = data.prod_list[i].recom_arr.length;

		if(length > 0) {

			for(var h = 0; h < length; h++) {
				//				right_temp = '<div class="info">'
				if(h == 0) {
					right_temp += '<div style="font-weight: bold;padding-top:0.02rem;padding-bottom:0.02rem;" class="text_c_2b ui_p_l12 prod_scroll" name="prod' + i + data.prod_list[i].cms_category_id + '" id="prod' + i + data.prod_list[i].cms_category_id + '">' + data.prod_list[i].category_name + '（' + data.prod_list[i].recom_arr.length + '）' + '</div>' +
						'<div category_id="' + data.prod_list[i].prod_arr[h].category_id + '" pro_cate_id="' + data.prod_list[i].prod_arr[h].pro_cate_id + '" class="click_prod ub ui_p_a12 lightgreen_color list_sha">' +
						'<div style="background-image: url(' + data.prod_list[i].prod_arr[h].show_img + ');" class="zhanwei_pic ui_m_r10"></div>' +
						'<div class="ub ub-f1 ub-ver ui_p_r02"> ' +
						'<div class="text_c_2b" style="font-size:0.13rem">' + data.prod_list[i].prod_arr[h].pro_cate_name + '</div>' +
						'<div class="font_gray">' + data.prod_list[i].prod_arr[h].prod_des + '</div>' +
						'<div class="ub ub-f1 ub-pe">' +
						'<div class="ub ub-f1 font_sale ub-ae" style="display:none;">已售 ' + data.prod_list[i].prod_arr[h].sales_count + '</div>' +
						'<div class="ub ub-pe ub-ae text_c_669" style="line-height:0.9">￥<span class="text_c_669 ulev3" style="line-height:1">' + data.prod_list[i].prod_arr[h].show_price + '</span></div>' +
						'</div>' +
						'</div>' +
						'</div>'
				} else {
					right_temp += '<div category_id="' + data.prod_list[i].recom_arr[h].category_id + '" pro_cate_id="' + data.prod_list[i].recom_arr[h].pro_cate_id + '" class="click_prod ub ui_p_a12 lightgreen_color list_sha">' +
						'<div style="background-image: url(' + data.prod_list[i].recom_arr[h].show_img + ');" class="zhanwei_pic ui_m_r10"></div>' +
						'<div class="ub ub-f1 ub-ver ui_p_r02"> ' +
						'<div class="text_c_2b" style="font-size:0.13rem">' + data.prod_list[i].recom_arr[h].pro_cate_name + '</div>' +
						'<div class="font_gray">' + data.prod_list[i].recom_arr[h].prod_des + '</div>' +
						'<div class="ub ub-f1 ub-pe">' +
						'<div class="ub ub-f1 font_sale ub-ae" style="display:none;">已售 ' + data.prod_list[i].recom_arr[h].sales_count + '</div>' +
						'<div class="ub ub-pe ub-ae text_c_669" style="line-height:0.9">￥<span class="text_c_669 ulev3" style="line-height:1">' + data.prod_list[i].recom_arr[h].show_price + '</span></div>' +
						'</div>' +
						'</div>' +
						'</div>'
				}
			}
			right_temp += '<div class="ub ub-ac ub-pc ulev0" style="color:#2b3857;padding-top:0.07rem;padding-bottom:0.07rem;border-bottom:1px solid #e5e5e5;background-color:white" name=' + data.prod_list[i].category_name + ' onclick="seeMoreList(this,' + data.prod_list[i].prod_arr.length + ',' + data.prod_list[i].recom_arr.length + ',' + i + ',' + data.prod_list[i].cms_category_id + ')">点击查看全部<img src="images/xiala.png" style="width: 0.13rem;height: 0.08rem;margin-left:0.05rem;" /></div>'
			right_temp += '<div class="more" style="display:none">';
			for(var h = 0, k = data.prod_list[i].prod_arr.length; h < k; h++) {
				right_temp += '<div category_id="' + data.prod_list[i].prod_arr[h].category_id + '" pro_cate_id="' + data.prod_list[i].prod_arr[h].pro_cate_id + '" class="click_prod ub ui_p_a12 lightgreen_color list_sha">' +
					'<div style="background-image: url(' + data.prod_list[i].prod_arr[h].show_img + ');" class="zhanwei_pic ui_m_r10"></div>' +
					'<div class="ub ub-f1 ub-ver ui_p_r02"> ' +
					'<div class="text_c_2b" style="font-size:0.13rem">' + data.prod_list[i].prod_arr[h].pro_cate_name + '</div>' +
					'<div class="font_gray">' + data.prod_list[i].prod_arr[h].prod_des + '</div>' +
					'<div class="ub ub-f1 ub-pe">' +
					'<div class="ub ub-f1 font_sale ub-ae" style="display:none;">已售 ' + data.prod_list[i].prod_arr[h].sales_count + '</div>' +
					'<div class="ub ub-pe ub-ae text_c_669" style="line-height:0.9">￥<span class="text_c_669 ulev3" style="line-height:1">' + data.prod_list[i].prod_arr[h].show_price + '</span></div>' +
					'</div>' +
					'</div>' +
					'</div>'

			}
			right_temp += '</div>';
			//			right_temp += '</div>';

		} else {
			for(var h = 0, k = data.prod_list[i].prod_arr.length; h < k; h++) {
				if(h == 0) {
					right_temp += '<div style="font-weight: bold;padding-top:0.02rem;padding-bottom:0.02rem;" class="text_c_2b ui_p_l12 prod_scroll" name="prod' + i + data.prod_list[i].cms_category_id + '" id="prod' + i + data.prod_list[i].cms_category_id + '">' + data.prod_list[i].category_name + '（' + data.prod_list[i].prod_arr.length + '）' + '</div>' +
						'<div category_id="' + data.prod_list[i].prod_arr[h].category_id + '" pro_cate_id="' + data.prod_list[i].prod_arr[h].pro_cate_id + '" class="click_prod ub ui_p_a12 lightgreen_color list_sha">' +
						'<div style="background-image: url(' + data.prod_list[i].prod_arr[h].show_img + ');" class="zhanwei_pic ui_m_r10"></div>' +
						'<div class="ub ub-f1 ub-ver ui_p_r02"> ' +
						'<div class="text_c_2b" style="font-size:0.13rem">' + data.prod_list[i].prod_arr[h].pro_cate_name + '</div>' +
						'<div class="font_gray">' + data.prod_list[i].prod_arr[h].prod_des + '</div>' +
						'<div class="ub ub-f1 ub-pe">' +
						'<div class="ub ub-f1 font_sale ub-ae" style="display:none;">已售 ' + data.prod_list[i].prod_arr[h].sales_count + '</div>' +
						'<div class="ub ub-pe ub-ae text_c_669" style="line-height:0.9">￥<span class="text_c_669 ulev3" style="line-height:1">' + data.prod_list[i].prod_arr[h].show_price + '</span></div>' +
						'</div>' +
						'</div>' +
						'</div>'
				} else {
					right_temp += '<div category_id="' + data.prod_list[i].prod_arr[h].category_id + '" pro_cate_id="' + data.prod_list[i].prod_arr[h].pro_cate_id + '" class="click_prod ub ui_p_a12 lightgreen_color list_sha">' +
						'<div style="background-image: url(' + data.prod_list[i].prod_arr[h].show_img + ');" class="zhanwei_pic ui_m_r10"></div>' +
						'<div class="ub ub-f1 ub-ver ui_p_r02"> ' +
						'<div class="text_c_2b" style="font-size:0.13rem">' + data.prod_list[i].prod_arr[h].pro_cate_name + '</div>' +
						'<div class="font_gray">' + data.prod_list[i].prod_arr[h].prod_des + '</div>' +
						'<div class="ub ub-f1 ub-pe">' +
						'<div class="ub ub-f1 font_sale ub-ae" style="display:none;">已售 ' + data.prod_list[i].prod_arr[h].sales_count + '</div>' +
						'<div class="ub ub-pe ub-ae text_c_669" style="line-height:0.9">￥<span class="text_c_669 ulev3" style="line-height:1">' + data.prod_list[i].prod_arr[h].show_price + '</span></div>' +
						'</div>' +
						'</div>' +
						'</div>'
				}
			}
		}

	}
	//				right_temp += '</div>';
	$("#left_menu").html(left_temp);
	$("#prod_list").html(right_temp);

	if(data.prod_list.length == 0) {
		$('#no_info').show();
		$('#prod_list').hide();
	} else {
		$('#no_info').hide();
		$('#prod_list').show();
	}

	$("#left_menu li").on("click", function() {
		$("#left_menu li").removeClass("active");
		$(this).addClass("active");
	});
	$(".click_prod").on("click", function() {
		var category_id = $(this).attr("category_id");
		var pro_cate_id = $(this).attr("pro_cate_id");
		setStorage("category_id", category_id);
		setStorage("pro_cate_id", pro_cate_id);
		if(category_id == "1") {
			window.location.href = "landmark_ticket.html?"+version;
		} else if(category_id == '1000') {
			window.location.href = 'station_ticket.html?'+version;
		} else {
			window.location.href = "scene_ticket.html?"+version;
		}
	});
}

$('#refresh').on('click', function() {
	getLocation();
})

//定位
function getLocation() {

	$('#dingwei').text('正在定位……');
	current_loc = new BMap.Geolocation();
	var geoc = new BMap.Geocoder();
	current_loc.getCurrentPosition(function(r) {
		if(this.getStatus() == BMAP_STATUS_SUCCESS) {
			var temp_street = '';
			if(r.accuracy == null) {
				//用户决绝地理位置授权  
				alert('用户拒绝地理位置授权');
				$('#dingwei').text('无法获取定位');
				$('.dingwei').hide();
			} else {
				longitude = r.longitude;
				latitude = r.latitude;
				point = longitude + ',' + latitude;
				geoc.getLocation(r.point, function(rs) {
					var addComp = rs.addressComponents;
					$('#dingwei').text(addComp.district + ' ' + addComp.street);
					temp_street = addComp.district + ' ' + addComp.street;
					$('.dingwei').show();
					setCookie('point', point, 'h4');
					setCookie('street', temp_street, 'd30');
					if(street != temp_street) {
						get_list();
					}
				});
			}
		}
	})
}

//查看更多
function seeMoreList(obj, count1, count2, index, id) {
	$(obj).hide();
	$(obj).next().show();
	var name = $(obj).attr('name');
	var total = count1 + count2;
	$('#prod' + index + '' + id).text(name + '（' + total + '）');
}