/**
 * 登录输入Model的控制
 */
function showLoginModel() {
	if (window != window.top) {
		window.top.location.reload();
	}
	// e.preventDefault();
	$("#denglu").modal({
		appendTo : 'body',
		focus : false,
		opacity : 30,
		overlayId : 'overlay',
		overlayCss : {},
		overlayCssClass : '',
		containerId : '',
		containerCss : {},
		containerCssClass : '',
		dataId : '',
		dataCss : {},
		dataCssClass : '',
		minHeight : 200,
		minWidth : 300,
		maxHeight : null,
		maxWidth : null,
		autoResize : false,
		autoPosition : true,
		zIndex : 1000,
		close : true,
		closeHTML : '<a title="Close"></a>',
		closeClass : 'close',
		escClose : true,
		overlayClose : false,
		position : null,
		persist : false,
		onOpen : onModelOpen,
		onShow : null,
		onClose : onModelClose
	});
};

function onModelOpen(dialog) {// 弹出窗口回调函数
	dialog.overlay.fadeIn(100, function() {
		dialog.container.fadeIn(200, function() {
			dialog.data.fadeIn(100);
		});
	});
}

function onModelClose(dialog) {// 关闭窗口回调函数
	dialog.data.fadeOut(100, function() {
		dialog.container.fadeOut(200, function() {
			dialog.overlay.fadeOut(100, function() {
				$.modal.close();
			});
		});
	});
}

function cleanLoginInformationDiv() {
	$("#loginInformation").empty();
}

function closeModal() {
	$.modal.close();
}

/**
 * 导航栏菜单的显示效果
 */
function prepareTopMenu() {
	/* 1st example */
	// / wrap inner content of each anchor with first layer and append
	// background layer
	$("#menu li a").wrapInner('<span class="out"></span>')
			.append('<span class="bg"></span>');

	// loop each anchor and add copy of text content
	$("#menu li a").each(function() {
		$('<span class="over">' + $(this).text() + '</span>')
				.appendTo(this);
	});

	$("#menu li a").hover(function() {
		// this function is fired when the mouse is moved over
		$(".out", this).animate({
			'top' : '45px'
		}, 250); // move down - hide
		$(".over", this).animate({
			'top' : '0px'
		}, 250); // move down - show
		$(".bg", this).animate({
			'top' : '0px'
		}, 120); // move down - show

	}, function() {
		// this function is fired when the mouse is moved off
		$(".out", this).animate({
			'top' : '0px'
		}, 250); // move up - show
		$(".over", this).animate({
			'top' : '-45px'
		}, 250); // move up - hide
		$(".bg", this).animate({
			'top' : '-45px'
		}, 120); // move up - hide
	});

	/* 2nd example */
	$("#menu2 li a").wrapInner('<span class="out"></span>');

	$("#menu2 li a").each(function() {
		$('<span class="over">' + $(this).text() + '</span>')
				.appendTo(this);
	});

	$("#menu2 li a").hover(function() {
		$(".out", this).animate({
			'top' : '45px'
		}, 200); // move down - hide
		$(".over", this).animate({
			'top' : '0px'
		}, 200); // move down - show

	}, function() {
		$(".out", this).animate({
			'top' : '0px'
		}, 200); // move up - show
		$(".over", this).animate({
			'top' : '-45px'
		}, 200); // move up - hide
	});
}

$(document).ready(function() {
	$.ajax({
		type : "POST",
		url : _app_ctx + "/passenger/passenger_checkLogin.htm",
		dataType : "json",
		success : function(json) {
			if (json.result == '2') {
				$("#notLoginDiv").hide();
				$("#userName").empty();
				$("#userName").append(json.userName);
				$("#loginedDiv").show();
			} else {
				$("#loginedDiv").hide();
				$("#notLoginDiv").show();
			}
		}
	});

	prepareTopMenu();

	$("#loginOutButton").click(function(e) {
		e.preventDefault();
		$.ajax({
			type : "POST",
			url : _app_ctx + "/passenger/passenger_loginOut.htm",
			success : function() {
				$("#loginedDiv").hide();
				$("#notLoginDiv").show();
			}
		});
	});

	$("#title").blur(function() {
		if ($(this).val() == "") {
			$(this).css("color", "#949494");
			$(this).val("请输入要查找的关键字");
		}

	});
	$("#title").focus(function() {
		if ($(this).val() == "请输入要查找的关键字") {
			$(this).val("");
			$(this).css("color", "black");
		}
	});
	$("div#backToTop").click(function() {// 返回顶部
		window.scrollTo(0, 0);
	});
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop(), oGoTop = $("div#backToTop");
		if (scrollTop > 240) {
			oGoTop.show();
			// oGoTop.css("top", scrollTop + $(window).height() - 120);
		} else {
			oGoTop.hide();
		}
	});
});

var _img_reload_count = 0;
var nextUrl = "";
function reloadImg(img) {
	img.src = _app_ctx + "/servlet/validatorImage.png" + "?count=" + _img_reload_count;
	_img_reload_count++;
}

function firstLoadValidateImg() {
	var _src = $("#validateImgDomLogin").attr("src");
	if (-1 < _src.indexOf("/include/images/yanzm.gif")) {
		reloadImg(document.getElementById("validateImgDomLogin"));
	}
}

function login() {
	if ($("#passengerNameLogin").val() == ''
			|| $("#passengerPasswordLogin").val() == '') {
		alert("请输入登录名和密码");
		return;
	}
	if ($("#validateImgLogin").val() == '') {
		alert("请输入验证码");
		return;
	}
	// alert(calcMD5($('#passengerPasswordLogin').val()));
	var md5String = calcMD5($('#passengerPasswordLogin').val());
	$.ajax({
		type : "POST",
		url : _app_ctx + "/passenger/passenger_login.htm",
		data : "passenger.userName=" + $("#passengerNameLogin").val()
				+ "&passenger.password=" + md5String + "&validateImg="
				+ $("#validateImgLogin").val(),
		dataType : "json",
		success : function(json) {
			$("#loginInformation").empty();
			if (json.loginStatus == '1') {
				$("#loginInformation")
						.append("<font color=\"#426464\">"
								+ json.information + "</font>");
				$("#notLoginDiv").hide();
				$("#userName").empty();
				$("#userName").append(json.userName);
				$("#loginedDiv").show();
				setTimeout('closeModal()', 200);
				if (nextUrl != "") {
					window.location.href = _app_ctx + nextUrl;
				}
			} else {
				reloadImg(document
						.getElementById("validateImgDomLogin"));
				$("#loginInformation")
						.append("<font color=\"#993300\">"
								+ json.information + "</font>");
				setTimeout('cleanLoginInformationDiv()', 500);
			}
		}
	});
}

/**
 * 站内搜索
 * @return {Boolean}
 */
function search() {
	if ($("#title").val() == '请输入要查找的关键字') {
		alert("请输入要查找的关键字");
		return false;
	}
	var title = encodeURIComponent($("#title").val());
	window.location.href = _app_ctx + "/cms/cms_list.htm?cmsContent.title=" + title;
}

function intoEcommerce(type, product_id, item_id) {
	var _add = "";
	if (null != type && undefined != type) {
		_add += "productType=" + type + "&";
	}
	if (null != product_id && undefined != product_id) {
		_add += "productId=" + product_id + "&";
	}
	if (null != item_id && undefined != item_id) {
		_add += "itemId=" + item_id;
	}
	if (1 < _add.length) {
		_add = "?" + _add;
	}
	$.ajax({
		type : "POST",
		url : _app_ctx + "/passenger/passenger_checkLogin.htm",
		dataType : "json",
		success : function(data) {
			if ("2" == data.result) {
				window.location.href = _app_ctx
								+ "/ecommerce/order/order_order.htm" + _add;
			} else if ("1" == data.result) {
				nextUrl = _app_ctx + "/ecommerce/order/order_order.htm" + _add;
				showLoginModel();
				// $("#loginButton").trigger("click");
			}
		}
	});
}
function intoEcommerce(type, product_id, item_id,hoteltype) {
	var _add = "";
	if (null != type && undefined != type) {
		_add += "productType=" + type + "&";
	}
	if (null != product_id && undefined != product_id) {
		_add += "productId=" + product_id + "&";
	}
	if (null != item_id && undefined != item_id) {
		_add += "itemId=" + item_id + "&";
	}
	if (null != hoteltype && undefined != hoteltype) {
		_add += "hoteltype=" + hoteltype;
	}
	if (1 < _add.length) {
		_add = "?" + _add;
	}
	$.ajax({
		type : "POST",
		url : _app_ctx + "/passenger/passenger_checkLogin.htm",
		dataType : "json",
		success : function(data) {
			if ("2" == data.result) {
				window.location.href = _app_ctx
								+ "/ecommerce/order/order_order.htm" + _add;
			} else if ("1" == data.result) {
				nextUrl = _app_ctx + "/ecommerce/order/order_order.htm" + _add;
				showLoginModel();
				// $("#loginButton").trigger("click");
			}
		}
	});
}

