$(function() {
	var b = $("#marquee"), b_, scrtime;
	b_ = b.find("dl");
 	b.hover(function(){
		clearInterval(scrtime);
	},function(){
		scrtime = setInterval(function(){
			var l = b.find("dl:last"),h = l.height();
			b.prepend("<dl style=\"height:91.3876px; opacity:0.961584; overflow:hidden;\">" + l.html() + "</dl>");
			b.find("dl:first").css({ height:0, opacity:0 }).animate({ height:h, opacity:1 }, 1000);
			l.remove();
		}, 5000);
	}).trigger("mouseleave");
	
	$("#marquee1").Marquee({ isEqual:false });
	
	$(".slide-box").find("li img").click(function(e) {
		e.stopPropagation();
		if($(this).attr("class") == "current") return;
		var slideBox = $(this).parent().parent().parent();
		//active状态的图片恢复原样
		slideBox.find("li img.current").fadeTo(200, 0.6).removeClass("current").animate({ top:5, width:52, height:35, left:10 }, 300);
		//获取数据
		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); //更改图片
			$(this).fadeTo(500, 1);
		});
	});
	$(".slide-box").find("li img").eq(0).click();
	var slidetimer;
	$(".slide-box").hover(function() {
		clearInterval(slidetimer);
	}, function() {
		var slideBox = $(this);
		slidetimer = setInterval(function() {
			var size = slideBox.find("li img").size();
			slideBox.find("li img").each(function(i) {
				if($(this).attr("class") == "current") {
					slideBox.find("li img").eq(i + 1 < size ? i + 1 : 0).click();
					return false;
				}
			});
		}, 5000);
	}).trigger("mouseleave");
	
	var page = 1;
	var i = 3; 
	$('.index_box:eq(4) .box_tt .scrollnav .next').click(function(){
		var $pictureShow = $('.index_box:eq(4) .index_tuan');
		var downwidth = $pictureShow.width();
		var len = $pictureShow.find('.tuan_box').length;
		var page_number = Math.ceil(len / i);
		var s = $pictureShow.find("#s");
		if(!s.is(":animated")) {
			if(page == page_number){
				s.animate({ left:'0px' }, "fast");
				page = 1;
			} else {
				s.animate({ left:'-=' + downwidth }, "fast");
				page++;
			}
		}
		$('.scrollnav a').eq((page-1)).addClass("current").siblings().removeClass("current");	
	});
	$('.index_box:eq(4) .box_tt .scrollnav .prev').click(function(){
		var $pictureShow = $('.index_box:eq(4) .index_tuan');
		var downwidth = $pictureShow.width();
		var len = $pictureShow.find('.tuan_box').length;
		var page_number = Math.ceil(len/i);
		var s = $pictureShow.find("#s");
		if(!s.is(":animated")) {
			if(page == 1) {
				s.animate({ left:'-=' + downwidth * (page_number - 1) }, "fast");
				page = page_number;
			} else {
				s.animate({ left:'+=' + downwidth }, "fast");
				page--;
			}
		}
		$('.scrollnav a').eq((page - 1)).addClass("current").siblings().removeClass("current");	
	});
	
	$("#search-btn").click(function() {
		$("#search-form").ajaxSubmit({
			success:function(data) {
				window.location.href = "/hotel/list-search.html";
			}
		});
	});
	
	var highLightIndex = -1;
	$("input.search-city").click(function(e) {
		e.stopPropagation();
		$("#citySelect .select-item").show();
		$("#citySelect .select-list").hide();
		var height = $(this).height();
		var left = $(this).offset().left;
		var top = $(this).offset().top;
		$("#citySelect").css({ left:left, top:top + height + 4 }).show().click(function(e) {
			e.stopPropagation();
		});
		$("#citySelect .select-item li").click(function() {
			$("input.search-city").val($(this).html());
			$("#citySelect").hide();
		});
		$("#citySelect .select-list dl").click(function() {
			$("input.search-city").val($(this).find("dt").eq(0).html());
			$("#citySelect").hide();
		});
	}).keyup(function(event) {
		var myEvent = event || window.event;
		var keyCode = myEvent.keyCode;//alert(keyCode);
		var key = $(this).val().replace(/ /g, '').replace(/'/g, '');
		$("#citySelect .select-item").hide();
		$("#citySelect .select-list").empty().show();
		if(key.length > 0) {
			if(keyCode >= 48 && keyCode <= 57 || keyCode >= 65 && keyCode <= 90 || keyCode == 8 || keyCode == 46 || keyCode == 32) {//alert(keyCode);
				highLightIndex = -1;
				$.post("/?m=city.getSearch", { key:key }, function(data) {
					if(data.length > 0) {
						$("#citySelect .select-tips").html("<strong>热门城市</strong>(可直接选择城市或输入城市拼音)");
						$("#citySelect .select-list").empty();
						$.each(data, function(id, item) {
							$("#citySelect .select-list").append("<dl><dt>" + item.name + "</dt><dd>" + item.englishName + "</dd></dl>");
						});
						$("#citySelect .select-list dl").click(function() {
							$("input.search-city").val($(this).find("dt").eq(0).html());
							$("#citySelect").hide();
						});
					} else {
						$("#citySelect .select-tips").html("对不起, 找不到: " + key);
					}
				}, "json");
			} else if(keyCode == 38 || keyCode == 40) {//alert(keyCode);
//				//向上/向下箭头
//				var nodes = O_Auto.children("div");
//				//使未向上移时的当前项底色去掉
//				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) {
//					O_SearchKey.val(O_Auto.children("div").eq(highLightIndex).html());
//					highLightIndex = -1;
//					O_Auto.html("");
//					O_AutoF.hide();
//					O_Auto.hide();
//				} else {
//					O_SearchBtn.trigger("click");
//				}
			} else {}
		} else {
			highLightIndex = -1;
			$.post("/?m=city.getSearch", { key:key }, function(data) {
				$("#citySelect .select-tips").html("<strong>热门城市</strong>(可直接选择城市或输入城市拼音)");
				$("#citySelect .select-list").empty();
				$.each(data, function(id, item) {
					$("#citySelect .select-list").append("<dl><dt>" + item.name + "</dt><dd>" + item.englishName + "</dd></dl>");
				});
				$("#citySelect .select-list dl").click(function() {
					$("input.search-city").val($(this).find("dt").eq(0).html());
					$("#citySelect").hide();
				});
			}, "json");
		}
	});
	
	$(document).click(function() {
		$("#citySelect").hide();
	});
	
	$("input.search-date").attr("readonly", "true").click(function() {
		WdatePicker({ minDate:'%y-%M-#{%d}', el:this });
	});
	
	$("#categoryBtn li a").click(function() {
		var category = $(this).attr("title");
		$.post("/?m=hotel.setSearchSession", { category:category }, function() {
			window.location.href = "/hotel/list-search.html";
		});
	});
});