$(function() {
	//var map = new BMap.Map("map");
//	var point = new BMap.Point(mapX, mapY);
//	map.centerAndZoom(point, 14);
//	map.addControl(new BMap.ScaleControl());
//	var icon = new BMap.Icon(root + "theme/simple/images/map-icon.gif", new BMap.Size(34, 36));
//	var marker = new BMap.Marker(point, { icon:icon });
//	map.addOverlay(marker);
	
	$("input.search-date").click(function() {
		WdatePicker({ isShowClear:false, readOnly:true, minDate:'%y-%M-#{%d}', el:this });
	});
	
	$("button.search-btn").click(function() {
		$("#search-form").ajaxSubmit({
			success:function(data) {
				window.location.href = root + "hotel/list-search.html";
			}
		});
	});
	
	$(".album-small img").each(function(i){
		$(this).mousemove(function(){
			$(this).css('border' , '3px solid #B3B3B3');
			$('.album-big img').attr('src' , $(this).attr('src'));
		});
		
		$(this).mouseout(function(){
			$(this).css('border' , 'none');				   
		});
	});
	
	$(".room-name").click(function() {
		var $parent = $(this).parent().parent();
		var $tbody = $parent.parent();
		var index = $parent.index();
		$tbody.find("tr:eq(" + (index + 1) + ")").toggle();
	});
	
	var coupon_note = '<div class="float-main"><div class="coupon">网上成功预订此房型，每间夜可获得<span class="txt-red">{$takeMoney}</span>返点奖金。成功入住后对客房可以进行一次点评和上传' + $("#photoMaxUploadNum").val() + '张相关图片，每条点评可获<span class="txt-red">' + $("#commentTakeMoney").val() + '元</span>点评金，每张图片可获<span class="txt-red">' + $("#photoTakeMoney").val() + '元</span>上传金。</div></div>';
	$(".room-coupon").hover(function() {
		var $this = $(this);
		var $offset = $this.offset();
		
		var takeMoney = $this.html();
		var height = $this.height();
		var note = coupon_note.replace(/\{\$takeMoney\}/g, takeMoney);
		
		if($(".float-note").length == 0) {
			$("body").append('<div class="float-note"></div>');
		}
		$(".float-note").css({ left:$offset.left, top:$offset.top + height + 5, width:327 }).html(note).show();
	}, function() {
		$(".float-note").hide();
	});
	
	var price_note = '<div class="float-main"><div class="price clearfix"><dl>{$dt}{$dd}</dl></div></div>';
	$(".room-price-multi").hover(function() {
		var $this = $(this);
		var $offset = $this.offset();
		
		var id = $this.parent().parent()[0].id;
		var width = $this.width();
		var height = $this.height();
		var note = price_note.replace(/\{\$dt\}/g, '读取中...').replace(/\{\$dd\}/g, '');
		var noteWidth = 108;
		
		if($(".float-note").length == 0) {
			$("body").append('<div class="float-note"></div>');
		}
		var $floatNote = $(".float-note");
		$floatNote.html(note).css({ left:$offset.left - 25 + width / 2, top:$offset.top + height + 5, width:noteWidth }).show();
		
		$.post("/?m=room.getPrice", { id:id.replace(/rp_/g, '') }, function(data) {
			var dt = "", dd = "", week = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
			$.each(data, function(id, item) {
				if(id < 7) {
					dt += '<dt>' + week[item.week] + '</dt>';
				}
				var ds = item.day.toString().split("-");
				var date = new Date(parseFloat(ds[0]), parseFloat(ds[1]) - 1, parseFloat(ds[2]));
				var day = date.getDate();
				day = ("00" + day).substr(("" + day).length);
				dd += '<dd>' + day + '日<em>' + item.price + '元</em></dd>';
			});
			if(data.length > 7) {
				noteWidth = 8 + 50 * 7;
			} else if(data.length > 0) {
				noteWidth = 8 + 50 * data.length;
			} else {
				noteWidth = 108;
				dt = "暂无报价";
			}
			$floatNote.css({ width:noteWidth }).html(price_note.replace(/\{\$dt\}/g, dt).replace(/\{\$dd\}/g, dd));
		}, "json");
	}, function() {
		$(".float-note").hide();
	});
	
	$(".detail-content-show").click(function() {
		var $this = $(this);
		var $detailContent = $(".detail-content");
		var height = $detailContent.css("height");
		if(height == "50px") {
			$detailContent.css("height", "auto");
		} else {
			$detailContent.css("height", "50px");
		}
		var html = $this.html();
		if(html == "收起") {
			html = "显示全部";
		} else {
			html = "收起";
		}
		$this.html(html);
	});
	
	
	$('.city_ul li').each(function(){
		$(this).click(function(){
			$('.city_ul_info li').hide();
			
		   
			$('.city_ul li').attr('class' , '');
			
			if($(this).attr('my_tag') != '' && $(this).attr('my_tag') != 'HIT')
			{
				var arr = $(this).attr('my_tag').split('');
				$(this).attr('class' , 'on' );
				for(var info in arr)
				{
					$('.li_' + arr[info].toLowerCase()).show();
				}
			}else{
				$(this).attr('class' , 'on' );
				$('.li_hit').show();
			}
		});
	});
	
	$('.city_ul_info li').each(function(){
		$(this).click(function() {
			$('#search-city').attr('value' , $(this).html() );
			
			$("#search-form").ajaxSubmit({
				success:function(data) {
					window.location.href = root + "hotel/list-search.html";
				}
			});
		});
	});
	
	$('#city_close').click(function(){					
		$('.change_city').fadeOut("500");				
	});
	
	$('#city_open').click(function(){						
		$('.change_city').fadeIn("500");				
	});
	
	initPage();
});

function getComment(page) {
	var $commentLoading = $("#comment-loading").show();
	var $commentList = $("#comment-list").hide();
	var $commentPage = $("#comment-page").hide();
	$.post(root + "?m=hotel.getCommentList", { hotelId:hotelId, page:page, size:size }, function(data) {
		$commentLoading.hide();
		$commentList.empty().show();
		$commentPage.empty().show();
		
		$.each(data.list, function(id, item) {
			$commentList.append('<li><dl class="clearfix"><dt><a title="" href="javascript:;"><img alt="" src="' + root + 'upload/member/2011090808062815350d0.jpg" /></a></dt><dd><div class="comment-topbar"><a title="" href="javascript:;">' + item.memberMobile + '</a><span class="txt-gray">' + item.date + '</span><span class="txt-gray">点评</span><a title="" href="javascript:;">' + item.roomName + '</a><div class="comment-score"><div class="score">' + item.score + '</div><div class="takemoney">' + item.takeMoney + '元</div></div></div><div class="comment-content">' + item.remark + '</div></dd></dl></li>');
		});
		var html = data.page.index <= 1 ? '<span class="page-prev">上一页<i></i></span>' : '<a title="" href="javascript:;" class="page-prev">上一页<i></i></a>';
		for(var key in data.page.nums) {
			if(data.page.nums[key] == data.page.index) {
				html += '<span class="page-num">' + data.page.nums[key] + '</span>';
			} else {
				html += '<a title="" href="javascript:;" class="page-num">' + data.page.nums[key] + '</a>';
			}
		}
		html += data.page.index >= data.page.count ? '<span class="page-next">下一页<i></i></span>' : '<a title="" href="javascript:;" class="page-next">下一页<i></i></a>';
		$commentPage.append(html);
		initPage();
	}, "json");
}

function initPage() {
	var $commentPage = $("#comment-page");
	$commentPage.find("a.page-prev").click(function() { getComment(parseInt($commentPage.find("span.page-num").html()) - 1); });
	$commentPage.find("a.page-num").click(function() { getComment(this.innerHTML); });
	$commentPage.find("a.page-next").click(function() { getComment(parseInt($commentPage.find("span.page-num").html()) + 1); });
}

