/*
	Νσ(︶く︶f
*/

$(document).ready(function() {
	/* 检测url加载是否正确 */
	var petshowselfurl = "";
	try {
		petshowselfurl = parent.document.getElementById("mainIframe").src.substr(parent.document.getElementById("mainIframe").src.lastIndexOf("petshowselfurl="));
	} catch (execption) {
		petshowselfurl = false;
	}

	if (petshowselfurl == false) {
		window.location.href="index.php?mainUrl=main"
	}

	/* 点击产品将产品信息以JSON形式返回给Flash */
	$("div[handle=lookproductinfo], div[handle=lookproductinfo2], div[handle=lookproductinfo3]").click(function() {
		if ($(this).children("div[class=res]").attr("val") != undefined) {
			
			$(window.parent.document).find("#loading").css("display", "");
			$(window.parent.document).find("#mainIframe").css("display", "none");

			$.ajax({
				type: "POST",
				url: "returnFlv.php",
				data: "pid=" + $(this).children("div[class=res]").attr("val"),
				dataType: "json",
				async: false,
				success: function(json) {
					
					$(window.parent.document).find("#loading").css("display", "none");
					$(window.parent.document).find("#mainIframe").css("display", "");

					if (typeof(json) == "object") {
						//Flash function do something
						parent.document.getElementById("VirtualBodyTest").sendToActionScript(json);
					} else {
						return false;
					}
				}
			});
		} else {
			return false;
		}
	});

	/* 套装搭配 */
	$("div[name=suitinfo]").click(function() {
		if ($(this).attr("val") != undefined) {
			$.ajax({
				type: "POST",
				url: "getSuit.php",
				data: "psid=" + $(this).attr("val") + "&activities=" + $(this).attr("activities"),
				async: false,
				success: function(resXmlStr) {
					if (resXmlStr == 0) {
						return false;
					} else if (typeof(resXmlStr) == "string") {
						parent.document.getElementById("VirtualBodyTest").sendToActionScript(resXmlStr);
					} else {
						return false;
					}
				}
			});
		} else {
			return false;
		}
	});

	/* 页面多物品 索要 */
	$.moreWant = function(param) {
		if (param.lastIndexOf(",") != -1) {
			param = param.replace(/,/g, '|');
			param = param.substr(0, param.length-1);
			window.location.href = "want.php?handle=flashmoreproduct&action=cartcomein&pid=" + param;
		} else if (isNaN(parseInt(param)) != true) {
			window.location.href = "want.php?handle=flashmoreproduct&action=cartcomein&pid=" + param;
		} else {
			alert("操作错误,请重新操作!");
			return false;
		}
	};

	/* 最热, 最新, 推荐 */
	$("div[handle=lookproductinfo], div[handle=lookproductinfo2], div[handle=lookproductinfo3]").mouseover(function() {
		var pid = $(this).children("div[class=res]").attr("val");
		$(this).find("#haspet_show_" + pid + "").attr("style", "display:block");

		$(this).find("div[class=button]").css("z-index", "99");
	});

	$("div[handle=lookproductinfo], div[handle=lookproductinfo2], div[handle=lookproductinfo3]").mouseout(function() {
		var pid = $(this).children("div[class=res]").attr("val");
		$(this).find("#haspet_show_" + pid + "").attr("style", "display:none");

		$(this).find("div[class=button]").css("z-index", "0");
	});

	/* 购买单个产品 */
	$("a[handle=petshow_buy]").click(function() {
		pid = $(this).attr("href").substr($(this).attr("href").lastIndexOf("#") + 1);

		if ($(this).attr("pic-link") == "to_thickbox") {
			url = "?mainUrl=cart";
		} else {
			url = "cart.php";
		}

		if (pid.lastIndexOf(",") == -1) {
			if (isNaN(parseInt(pid))) {
				return 0;
			} else {
				$.ajax({
					type: "GET",
					url: "update.php",
					data: "action=addcart&pid=" + pid,
					async: false,
					success: function(res) {
						if (res == 1) {
							if (window.confirm("添加成功,是否跳转到购物车?")) {
								window.location.href = url;
							} else {
								return false;
							}
						} else if (res == 0) {
							alert("失败,请重新操作!");
							return false;
						} else {
							return false;
						}
					}
				});
			}
		} else {
			$.ajax({
				type: "GET",
				url: "update.php",
				data: "action=addcart&pid=" + pid,
				async: false,
				success: function(res) {
					if (res == 1) {
						if (window.confirm("添加成功,是否跳转到购物车?")) {
							window.location.href = url;
						} else {
							return false;
						}
					} else if (res == 0) {
						alert("失败,请重新操作!");
						return false;
					} else {
						return false;
					}
				}
			});
		}
	});

	$("#cpmp").click(function() {
		cpdonee = $("input[name=cpdonee]").val();

		/* 验证用户名是否存在 */
		$.ajax({
			type: "POST",
			url: "checkuser.php",
			data: "username=" + cpdonee + "&listprices=" + parseInt($("span[name=salespromotion2]").html()) + "&handle=agree",
			async: false,
			success: function(data) {
				if (data == 0) {
					alert("请正确输入受赠人帐号!");
					return false;
				} else if (data == -1) {
					alert("啵币数不够!");
					return false;
				} else {
					if (window.confirm("确定同意?")) {
						$("form[name=cpmpForm]").submit();
					}
				}
				return false;
			}
		});
	});
});

/* 处理iframe外层的Browser Title */
parent.document.title = document.title;

/* 容错 */
window.onerror = function() {
	return true;
};