$(function() {
	if (document.domain != 'act.51.com') {	// cross domain
		document.domain = '51.com';
		if (!document.getElementById("proxyiframe")) {
			alert("Bad Config!");
			return false;
		}
		var proxy = document.getElementById("proxyiframe").contentWindow;
		var act = $("#act_dir").val();
		if (!act) {
			var thisPath = window.location.pathname;
			var pathArr = new Array();
			pathArr = thisPath.split('/');
			var act = pathArr[2];
		}
		// 预览模版
		$("a[@name='preview'],img[@name='preview'],area[@name='preview'],:button[@name='preview']").click(function() {
			var isAnchor = $(this).is("a")|| $(this).is("area") ? 1 : 0;
			var thisID = this.id;
			if (1 == isAnchor) {
				$("#"+thisID).attr("href", "###");
			}
			var tplID = thisID.substring(7);
			var checkFlg = 0;
			var msgArr = new Array();
			proxy.preview(act, tplID);
		})
		// 领取模版
		$("a[@name='obtain'],img[@name='obtain'],area[@name='obtain'],:button[@name='obtain']").click(function() {
			var isAnchor = $(this).is("a")|| $(this).is("area") ? 1 : 0;
			var thisID = this.id;
			if (1 == isAnchor) {
				$("#"+thisID).attr("href", "###");
			}
			var tplID = thisID.substring(6);
			proxy.obtain(act, tplID);
		})
	}
	else {
		var act = $("#act_dir").val();
		if (!act) {
			var thisPath = window.location.pathname;
			var pathArr = new Array();
			pathArr = thisPath.split('/');
			var act = pathArr[2];
		}
		// 预览模版
		$("a[@name='preview'],img[@name='preview'],area[@name='preview'],:button[@name='preview']").click(function() {
			var isAnchor = $(this).is("a") || $(this).is("area") ? 1 : 0;
			var thisID = this.id;
			var tplID = thisID.substring(7);
			var checkFlg = 0;
			var msgArr = new Array();
			$.ajax({
				type  : "POST",
				url   : "http://act.51.com/2008/templet/templet.ajax.php",
				data  : "act=" + act + "&type=1&tplid=" + tplID,
				async : false,
				success: function(msg) {
					msgArr = msg.split('|');
					var para = $.trim(msgArr[0]);
					switch (para) {
						case 's1' :                                            break;
						case 'e1' : alert("对不起，参数错误！");               break;
						case 'e2' : alert("对不起，请您先登录网站！");         break;
						case 'e3' : alert("对不起，该活动已结束！");           break;
						default   : alert("网络通讯错误！请您稍候再试！");     break;
					}
					if ('s1' == para) {
						$("#preview_amount").html(msgArr[3]);
						if (1 == isAnchor) {
							$("#"+thisID).attr("href", "###");
						}
						checkFlg = 1;
					}
					if ('e2' == para) {
						if (1 == isAnchor) {
							$("#"+thisID).attr("href", "###");
						}
						location.href = 'http://passport.51.com/passport.5p?gourl=' + location.href;
						return false;
					}
				},
				error: function() {
					alert("网络通讯错误！请您稍候再试！");
					return false;
				}
			});
			if (1 == checkFlg) {
				var rs = window.open("http://home.51.com/?u=" + msgArr[2] + "&tplid=-" + msgArr[1] + "&cache=disable",'newwindow','top=0,left=0,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no');
				rs.focus();
			}
		})
		// 领取模版
		$("a[@name='obtain'],img[@name='obtain'],area[@name='obtain'],:button[@name='obtain']").click(function() {
			var isAnchor = $(this).is("a")|| $(this).is("area") ? 1 : 0;
			var thisID = this.id;
			var tplID = thisID.substring(6);
			$.ajax({
				type: "POST",
				url : "/2008/templet/templet.ajax.php",
				data: "act=" + act + "&type=2&tplid=" + tplID,
				success: function(msg) {
					var msgArr = new Array();
					msgArr = msg.split('|');
					var para = $.trim(msgArr[0]);
					switch (para) {
						case 's2' : alert("恭喜您领取成功！");                 break;
						case 'e1' : alert("对不起，参数错误！");               break;
						case 'e2' :                                            break;
						case 'e4' : alert("对不起，您不能重复领取！");         break;
						case 'e3' : alert("对不起，该活动已结束！");           break;
						case 'e5' : alert("对不起，您已经领取过了！");         break;
						case 'e6' : alert("对不起，已经领取结束！");           break;
						case 'e10': alert("对不起，领取失败，请您稍候再试！"); break;
						default   : alert("网络通讯错误！请您稍候再试！");     break;
					}
					if ('s2' == para) {
						$("#obtain_amount").html(msgArr[1]);
					}
					if ('e2' == para) {
						if (1 == isAnchor) {
							$("#"+thisID).attr("href", "###");
						}
						alert('对不起，请您先登录网站！');
						location.href = 'http://passport.51.com/passport.5p?gourl=' + location.href;
						return false;
					}
				},
				error: function() {
					alert("网络通讯错误！请您稍候再试！");
					return false;
				}
			});
		})
	}
})