$(function() {
	// 图表框1
	$("ul.chart-box").find("li.hotel-item").hover(function() {
		var $this = $(this);
		var width = $this.width() - 6;
		var height = $this.height() - 6;
		
		$this.find("a.hover-border").css({ "width":width, "height":height }).show();
	}, function() {
		$(this).find("a.hover-border").hide();
	});
	// 图表框2
	$("ul.chart-box2").find("li.hotel-item").hover(function() {
		var $this = $(this);
		var $offset = $this.offset();
		var $parent = $this.parent();
		var $parentOffset = $parent.offset();
		var $hoverBorder = $this.find("a.hover-border");
		var $picInfo = $this.find("figcaption.pic-info");
		
		var boxLeft = $parentOffset.left;
		var boxTop = $parentOffset.top;
		var boxWidth = $parent.width();
		var boxHeight = $parent.height();
		var itemLeft = $offset.left;
		var itemTop = $offset.top;
		var itemWidth = $this.width();
		var itemHeight = $this.height();
		
		$this.find("img.chart-pic").css({ "position":"absolute", "left":itemLeft, "top":itemTop, "opacity":1 }).show();
		if(itemLeft + itemWidth * 2.1 > boxLeft + boxWidth) {
			$picInfo.css({ "width":itemWidth * 2 - 13, "height":itemHeight - 10, "left":itemLeft - itemWidth * 2 + 3, "top":itemTop }).hide();
			$hoverBorder.css({ "width":itemWidth, "left":itemLeft - 3, "top":itemTop - 3 }).show().animate({ "width":itemWidth * 3, "left":itemLeft - itemWidth * 2 - 3 }, 200, function() {
				$picInfo.show();
			});
		} else {
			$picInfo.css({ "width":itemWidth * 2 - 13, "height":itemHeight - 10, "left":itemLeft + itemWidth + 3, "top":itemTop }).hide();
			$hoverBorder.css({ "width":itemWidth, "left":itemLeft - 3, "top":itemTop - 3 }).show().animate({ "width":itemWidth * 3 }, 200, function() {
				$picInfo.show();
			});
		}
	}, function() {
		var $this = $(this);
		$this.find("img.chart-pic").css({ "position":"static" });
		$this.find("figcaption.pic-info").hide();
		$this.find("a.hover-border").stop().hide();
	});
	// 看看别人在做什么
	var $doingUl = $("div.doing-box").children(), scrtime;
	
	$doingUl.hover(function() {
		clearInterval(scrtime);
	}, function() {
		scrtime = setInterval(function() {
			var $doingLi = $doingUl.find("li:last");
			var height = $doingLi.height();
			
			$doingUl.prepend('<li class="doing-list">' + $doingLi.html() + "</li>");
			$doingUl.find("li:first").css({ "height":0, "opacity":0 }).animate({ "height":height, "opacity":1 }, 1000);
			$doingLi.remove();
		}, 5000);
	}).trigger("mouseleave");
	// 幻灯片
	var $slideBox = $("div.slide-box");
	var $slideImg = $slideBox.find("li img"), slidetimer;
	
	$slideImg.click(function(event) {
		event.stopPropagation();
		if(this.className == "current") return;
		// active状态的图片恢复原样
		$slideBox.find("li img.current").fadeTo(200, 0.6).removeClass("current").animate({ top:5, width:52, height:35, left:10 }, 300);
		// 获取数据
		var $this = $(this);
		var alt = $this.attr("alt");
		var src = $this.attr("src");
		// 当前
		$this.animate({ top:-5, width:70, height:40, left:1 }, 100).addClass("current").fadeTo(200, 1).parent();
		$slideBox.find(".slide-pic img").fadeTo(500, 0.1, function() {
			$(this).attr("alt", alt).attr("src", src).fadeTo(500, 1); //更改图片
		});
	}).eq(0).click();
	$slideBox.hover(function() {
		clearInterval(slidetimer);
	}, function() {
		slidetimer = setInterval(function() {
			var size = $slideImg.size();
			$slideImg.each(function(i) {
				if(this.className == "current") {
					$slideImg.eq(i + 1 < size ? i + 1 : 0).click();
					return false;
				}
			});
		}, 5000);
	}).trigger("mouseleave");
	// 酒店列表翻页
	$("h1.title ul li").click(function(event) {
		event.stopPropagation();
		if(this.className == "current") return;
		
		var $this = $(this);
		var $thisUl = $this.parent();
		var $thisBox = $thisUl.parent().parent();
		var $thisSpan = $thisUl.parent().find("span");
		
		$thisUl.find("li").removeClass("current");
		$this.addClass("current");
		$thisSpan.html($this.html());
		$thisBox.find(".change-list").children().hide().eq($this.index()).fadeIn(300);
	});
	// 搜索框
	var highLightIndex = -1;
	var $searchCity = $("#search-city");
	var $citySelect = $("#city-select");
	var $selectTips = $citySelect.find("div.select-tips");
	var $selectItem = $citySelect.find("div.select-item");
	var $selectList = $citySelect.find("div.select-list");
	
	$searchCity.click(function(event) {
		event.stopPropagation();
		
		$selectTips.html("<strong>热门城市</strong>(可直接选择城市或输入城市拼音)");
		$selectItem.show();
		$selectList.hide();
		
		var height = $searchCity.height();
		var left = $searchCity.offset().left;
		var top = $searchCity.offset().top;
		
		$citySelect.css({ "left":left - 65, "top":top + height + 3 }).show().click(function(event) {
			event.stopPropagation();
		});
		$selectItem.find("li").click(function() {
			$searchCity.val($(this).html());
			$citySelect.hide();
		});
	}).keyup(function(event) {
		var myEvent = event || window.event;
		var keyCode = myEvent.keyCode;
		var key = $searchCity.val().replace(/ /g, '').replace(/'/g, '');
		
		if(key.length > 0) {
			if(keyCode >= 48 && keyCode <= 57 || keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46 || keyCode == 32) {
				highLightIndex = -1;
				$selectItem.hide();
				$selectList.empty().show();
				$.post(root + "?m=city.getSearch", { key:key }, function(data) {
					if(data.length > 0) {
						$selectTips.html("<strong>城市列表</strong>(可直接选择城市或输入城市拼音)");
						$selectList.empty();
						$.each(data, function(id, item) {
							$selectList.append("<dl><dt>" + item.name + "</dt><dd>" + item.englishName + "</dd></dl>");
						});
						$selectList.find("dl").click(function() {
							$searchCity.val($(this).find("dt").eq(0).html());
							$citySelect.hide();
						});
					} else {
						$selectTips.html('对不起, 找不到: <strong class="txt-red">' + key + "</strong>");
					}
				}, "json");
			} else if(keyCode == 38 || keyCode == 40) {
				// 向上/向下箭头
				var nodes = $selectList.find("dl");
				//使未向上移时的当前项底色去掉
//				if(highLightIndex != -1) { nodes.eq(highLightIndex).css("background-color", "#fff"); }
//				if(keyCode == 38) { //向上
//					nodes.css("background-color", "#fff").css("border", "1px solid #fff");
//					highLightIndex--;
//					if(highLightIndex == -1) { highLightIndex = nodes.length - 1; }
//				}
//				if(keyCode == 40){ //向下
//					nodes.css("background-color", "#fff").css("border", "1px solid #fff");
//					highLightIndex++;
//					if(highLightIndex == nodes.length) { highLightIndex = 0; }
//				}
//				//使用当前项高亮
//				nodes.eq(highLightIndex).css("background-color", "#eee").css("border", "1px solid #ccc");
			} else if(keyCode == 13) {
				// 如果输入的是回车
				// 如果下拉框有高亮内容
				if(highLightIndex != -1) {
					$searchCity.val($selectList.find("dt").eq(highLightIndex).html());
					highLightIndex = -1;
					$citySelect.hide();
				}
			}
		} else {
			highLightIndex = -1;
			$.post(root + "?m=city.getSearch", { key:key }, function(data) {
				$selectTips.html("<strong>热门城市</strong>(可直接选择城市或输入城市拼音)");
				$selectItem.show();
				$selectList.hide();
			}, "json");
		}
	});
	$(document).click(function() {
		$citySelect.hide();
	});
	$("input.search-date").click(function() {
		WdatePicker({ isShowClear:false, readOnly:true, minDate:'%y-%M-#{%d}', el:this });
	});
	$("button.search-btn").click(function() {
		

		
		if($("#search-city") && $("#search-city").attr('value') == '中文/拼音')
		{
			$("#search-city").attr('value' , '');
		}
		
		
		if($("#search-position") && $("#search-position").attr('value') == '中文/拼音')
		{
			$("#search-position").attr('value' , '');
		}
		
		
		
		if($("#search-hotelName") && $("#search-hotelName").attr('value') == '中文/拼音')
		{
			$("#search-hotelName").attr('value' , '');
		}
	
		$("#index-search").ajaxSubmit({
			success:function(data) {
				window.location.href = root + "hotel/list-search.html";
			}
		});
	});
});