$(function() {
	$("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";
			}
		});
	});
	
	$(".search-condition a").click(function() {
											
		var $this = $(this);
		var $parent = $this.parent().parent();
		
		var classname = $parent[0].className;
		var field = classname.replace(/condition\-/g, '');
		var key = $this.attr("key");
		


		var obj = eval("({" + field + ":'" + key + "'})");

		$.post(root + "?m=hotel.setSearchSession", obj, function(data) {
							
			$parent.find("a").removeClass("selected");
			$this.addClass("selected");
			$selectedField = $("#selected-" + field);
			$selectedField.remove();
			$(".selected-3").append('<a id="selected-' + field + '" title="" href="javascript:void(0);">' + $this.html() + '<i></i></a>');
			$selectedField.click(function() {
				$selectedField.remove();
				$parent.find("a").removeClass("selected").eq(0).addClass("selected");
				var obj = eval("({" + field + ":''})");
				$.post(root + "?m=hotel.setSearchSession", obj);
				
			});
			
			window.location.href = root + "hotel/list-search.html";
		});
	});
	
	$(".selected-3 a").click(function() {
		$(this).remove();
		var id = this.id;
		var field = id.replace(/selected\-/g, '');
		$(".condition-" + field + " a").removeClass("selected").eq(0).addClass("selected");
		var obj = eval("({" + field + ":''})");
		$.post(root + "?m=hotel.setSearchSession", obj);
	});
	
	$(".filter-order li a").click(function() {
		var $this = $(this);
		var classname = $this.parent()[0].className;
		var field = $this.attr("field");
		var asc = classname.indexOf('asc') != -1 ? true : false;
		$.post(root + "?m=hotel.setOrderSession", { field:field, asc:asc }, function(data) {
			window.location.reload();
		});
	});
	
	$(".room-name").click(function() {
		var $parent = $(this).parent().parent();
		var $tbody = $parent.parent();
		var index = $parent.index();
		$tbody.find("tr:eq(" + (index + 1) + ")").toggle();
	});
	
	$(".item-bar-show").click(function() {
		var $this = $(this);
		var $roomList = $this.parent().parent().find(".room-list");
		$roomList.find(".room-item.hide").toggle();
		var html = $(this).html();
		if(html == "收起房型") {
			html = "展开所有房型(" + $roomList.find(".room-item").length + ")";
		} else {
			html = "收起房型";
		}
		$this.html(html);
	});
	
	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(root + "?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();
	});
	
	
	$('.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");				
	});
	
	
	$('#search_area').click(function(){	
		if($(this).html() == '更多')
		{
			$('#search_area_frame').attr('class' , 'condition-more');
			$(this).html('收起');
		}else{
			$('#search_area_frame').attr('class' , 'condition-2');
			$(this).html('更多');	
		}
		
	});
	
	$('#search_landmark').click(function(){	
		if($(this).html() == '更多')
		{
			$('#search_landmark_frame').attr('class' , 'condition-more');
			$(this).html('收起');
		}else{
			$('#search_landmark_frame').attr('class' , 'condition-2');
			$(this).html('更多');	
		}
		
	});
	
});