//------------------------init begin------------------------//
document.domain = "joy.cn";
document.onmouseover = MJActiononmouseover;

var MJActionObject;

var MJActionTypeCollect = "Collect";
var MJActionTypeSupport = "Support";
var MJActionTypeAgainst = "Against";
var MJActionTypePlay = "Play";

var MJActionInitObject;
var MJActionDomain = "http://action.app.joy.cn";
var MJActionActionURL = MJActionDomain + "/DOMJAction.aspx";
var MJActionLoginDomain = "http://usercenter.joy.cn";
var MJActionLoginURL = MJActionLoginDomain + "/userInfo.do";
var MJActionRegisterURL = MJActionLoginDomain + "/userInfo.do?m=UserCenter&type=register";

var MJActionAppID = "";
var MJActionActionID = "";
var MJActionContentTypeID = "";
var MJActionContentID = "";
var MJActionUpdID = "";

var MJActionColAuthor = "";
var MJActionColCategoryId = "";
var MJActionColDescription = "";
var MJActionColImageUrl = "";
var MJActionColPubDate = "";
var MJActionColTitle = "";
var MJActionColUrl = "";

var MJActionCallJS = null;

$(document).ready(function(){ MJActionInit(); });

//------------------------init end------------------------//

//------------------------Main function begin------------------------//
function MJActiongetScroll()
{		
	var left = 0;
	var top  = 0;
	
	if (document.documentElement.scrollLeft == 0 &&
		document.documentElement.scrollTop == 0)
	{
		left = document.body.scrollLeft;
		top = document.body.scrollTop;
	}
	else
	{
		left = document.documentElement.scrollLeft;
		top = document.documentElement.scrollTop;
	}

	return {x:left, y:top};
}

function MJActiononmouseover(ev)
{ 
	ev = ev || window.event; 
	var o = ev.target || ev.srcElement;
	MJActionObject = MJActiongetPosition(o); 
} 

function MJActiongetPosition(e)
{		
	var left = 0;
	var top  = 0;
	while (e.offsetParent)
	{
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;
	return {x:left, y:top};
}

function MJActionmouseCoords(ev)
{		
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function MJActiongetMouseOffset(target, ev){		
	ev = ev || window.event;
	var elementPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - elementPos.x, y:mousePos.y - elementPos.y};
}

function MJActionInit()
{
	try
	{
		if (document.body != null)
		{
			MJActionLoadBasic();
			clearTimeout(MJActionInitObject);
		}
		else
		{
			var self = this;
			MJActionInitObject = setTimeout(MJActionInit, 500);
		}
	}
	catch (e)
	{
		var self = this;
		MJActionInitObject = setTimeout(MJActionInit, 500);
	}
}

function MJActionLoadBasic()
{
	var CurNode = document.createElement("SPAN");
	CurNode.id = "MJActionPanel";
	document.body.appendChild(CurNode);
	CurNode.innerHTML   = "<div id=\"MJActionCover\" class=\"MJActionCover\" style=\"display:none\"></div> \r\n"
						+ "<iframe src=\"" + MJActionDomain + "/SubDomainProxy.htm\" width=0 height=0 id=\"MJActioniframeProxy\"></iframe> \r\n"
						+ "<iframe src=\"" + MJActionLoginDomain + "/userInfo.do?m=UserCenter&type=subdomainprox\" width=0 height=0 id=\"MJActionLoginiframeProxy\"></iframe> \r\n"
					 	+ "<div id=\"MJActionLoading\" class=\"MJActionLoading\" style=\"display:none;\">处理中...</div> \r\n"
						+ "<div id=\"MJActionLogin\" class=\"MJActionLogin\" style=\"display:none\"> \r\n"
						+ "	<h2><img src=\"http://css.megajoy.com/common/MJAction/images/ico_close.gif\" alt=\"关闭\" width=\"12\" height=\"12\" border=\"0\" onclick=\"MJActionHideAll();\" style=\"CURSOR:pointer\"/></h2> \r\n"
						+ "	<ul> \r\n"
						+ "    	<li> \r\n"
						+ "        	<dl> \r\n"
						+ "            	<dt>用户名：</dt> \r\n"
						+ "              	<dd><input type=\"text\" id=\"MJActionLoginUserName\" class=\"MJActionLoginInput\" size=\"20\" style=\"width:120px\"/> \r\n"
						+ "              </dd> \r\n"
						+ "            </dl> \r\n"
						+ "        </li> \r\n"
						+ "        <li> \r\n"
						+ "        	<dl> \r\n"
						+ "            	<dt>密码：</dt> \r\n"
						+ "              	<dd><input type=\"password\" id=\"MJActionLoginUserPass\" class=\"MJActionLoginInput\" size=\"20\" style=\"width:120px\"/> \r\n"
						+ "              </dd> \r\n"
						+ "            </dl> \r\n"
						+ "        </li> \r\n"
						+ "    </ul> \r\n"
						+ "    <h5><input type=\"button\" class=\"MJActionLoginBt\" value=\"提交\" onclick=\"MJActionLoginDO();\"/></h5> \r\n"
						+ "    <h5><br><a href=\"" + MJActionRegisterURL + "\" target=\"_blank\"><font color=red><u>注册成为新用户</u></font></a></h5> \r\n"
						+ "</div> \r\n";
						
	var o = document.getElementById("MJActionCover");
	o.style.height = document.body.scrollHeight + "px";
	o.style.width = document.body.scrollWidth + "px";
}

function MJActionHideAll()
{
	 document.getElementById('MJActionLogin').style.display = 'none';
}

function MJActionShowLogin(Type, CallJS)
{
	MJActionCallJS = null;
	
	if (CallJS != null)
	{
		MJActionCallJS = CallJS;
	}
	
	var top;
	var left;
	if (Type == 1)
	{
		top = parseInt(document.getElementById("MJActionLoading").style.top);
		left = parseInt(document.getElementById("MJActionLoading").style.left);
	}
	else
	{
		top = MJActionObject.y;
		left = MJActionObject.x;
	}
	
	if (document.body.clientWidth < left + 200)
	{
		left = left - 176;
	}
	
	document.getElementById("MJActionLogin").style.top = top + "px";
	document.getElementById("MJActionLogin").style.left = left + "px";
	document.getElementById("MJActionLogin").style.display = '';
	//document.getElementById('MJActionCover').style.display = '';
}

function MJActionClearParm()
{
	MJActionColAuthor = "";
	MJActionColCategoryId = "";
	MJActionColDescription = "";
	MJActionColImageUrl = "";
	MJActionColPubDate = "";
	MJActionColTitle = "";
	MJActionColUrl = "";
}

function MJActionDO(MJActionType, Count)
{
	if (MJActionType != MJActionTypePlay)
	{
		document.getElementById("MJActionLoading").style.top = MJActionObject.y + 20 + "px";
		document.getElementById("MJActionLoading").style.left = MJActionObject.x + "px";
		document.getElementById("MJActionLoading").style.display = '';
	}
	
	try
	{
		var Parm = "AppID=" + MJActionAppID + 
			"&ActionID=" + MJActionActionID +
			"&ContentTypeID=" + MJActionContentTypeID + 
			"&ContentID=" + MJActionContentID +
			"&Count=" + Count + 
			"&ColAuthor=" + encodeURI(MJActionColAuthor) +
			"&ColCategoryId=" + MJActionColCategoryId +
			"&ColDescription=" + encodeURI(MJActionColDescription) +
			"&ColImageUrl=" + encodeURI(MJActionColImageUrl) +
			"&ColPubDate=" + MJActionColPubDate +
			"&ColTitle=" + encodeURI(MJActionColTitle) +
			"&ColUrl=" + encodeURI(MJActionColUrl);

		var proxy = document.getElementById("MJActioniframeProxy").contentWindow;
		proxy.sendRequest(MJActionType, Count, 'POST', MJActionActionURL, Parm);
	}
	catch (e)
	{
		document.getElementById("MJActionLoading").style.display = 'none';
		
		if (MJActionType != MJActionTypePlay)
		{
			alert("请求出错" + e);
		}
	}
}

function MJActionReturn(MJActionType, Count, ReturnCode)
{
	if (ReturnCode != "000000")
	{
		if (ReturnCode.substring(0, 6) == "000001")
		{
			alert("您没有登录，请登录后使用该功能");
			MJActionShowLogin(1);
		}
		else
		{
			if (MJActionType != MJActionTypePlay)
			{
				alert(ReturnCode);
			}
		}
		
		document.getElementById("MJActionLoading").style.display = 'none';
		
		return;
	}
	
	if (MJActionType == MJActionTypeCollect) 
	{
		alert("收藏成功");
	}
	
	document.getElementById("MJActionLoading").style.display = 'none';
	
	if (MJActionUpdID == null)
	{
		return;
	}

	if (MJActionType == MJActionTypeCollect) 
	{
		document.getElementById(MJActionUpdID).innerHTML = 
			parseInt(document.getElementById(MJActionUpdID).innerHTML) + parseInt(Count);
	}
	else if (MJActionType == MJActionTypeSupport) 
	{
		document.getElementById(MJActionUpdID).innerHTML = 
			parseInt(document.getElementById(MJActionUpdID).innerHTML) + parseInt(Count);
	}
	else if (MJActionType == MJActionTypeAgainst) 
	{
		document.getElementById(MJActionUpdID).innerHTML = 
			parseInt(document.getElementById(MJActionUpdID).innerHTML) + parseInt(Count);
	}
	else if (MJActionType == MJActionTypePlay) 
	{
		document.getElementById(MJActionUpdID).innerHTML = 
			parseInt(document.getElementById(MJActionUpdID).innerHTML) + parseInt(Count);
	}
}
//------------------------Main function end------------------------//

//------------------------Action Login begin------------------------//
function MJActionLoginDO()
{
	document.getElementById("MJActionLoading").style.top = MJActionObject.y + 20 + "px";
	document.getElementById("MJActionLoading").style.left = MJActionObject.x + "px";
	document.getElementById("MJActionLoading").style.display = '';
	
	try
	{
		var MJActionLoginUserName = document.getElementById("MJActionLoginUserName").value;
		var MJActionLoginUserPass = document.getElementById("MJActionLoginUserPass").value;
		
		var Parm = "&userName=" + encodeURI(MJActionLoginUserName) + 
			"&password=" + encodeURI(MJActionLoginUserPass) + 
			"&m=UserLogin";

		var proxy = document.getElementById("MJActionLoginiframeProxy").contentWindow;
		proxy.sendRequest('POST', MJActionLoginURL, Parm);
	}
	catch (e)
	{
		document.getElementById("MJActionLoading").style.display = 'none';
		alert("请求出错" + e);
	}
}

function MJActionLoginReturn(ReturnCode)
{
	if (ReturnCode.substring(0, 6) == "000000")
	{
		alert("登录成功");
	}
	else
	{
		alert("登陆失败");
	}
	
	document.getElementById("MJActionLoading").style.display = 'none';
	MJActionHideAll();
	
	if (MJActionCallJS != null)
	{
		eval(MJActionCallJS);
	}
}
//------------------------Action Login end------------------------//

//------------------------Action function begin------------------------//
function MJActionCollect(AppID, ActionID, ContentTypeID, ContentID, ColAuthor, 
						 ColCategoryId, ColDescription, ColImageUrl, ColPubDate, ColTitle, 
						 ColUrl, UpdID)
{
	MJActionClearParm();
	
	MJActionAppID = AppID;
	MJActionActionID = ActionID;
	MJActionContentTypeID = ContentTypeID;
	MJActionContentID = ContentID;
	MJActionUpdID = UpdID;
	
	MJActionColAuthor = ColAuthor;
	MJActionColCategoryId = ColCategoryId;
	MJActionColDescription = ColDescription;
	MJActionColImageUrl = ColImageUrl;
	MJActionColPubDate = ColPubDate;
	MJActionColTitle = ColTitle;
	MJActionColUrl = ColUrl;
	
	MJActionDO(MJActionTypeCollect, 1);
}

function MJActionSupport(AppID, ActionID, ContentTypeID, ContentID, UpdID)
{
	MJActionClearParm();
	
	MJActionAppID = AppID;
	MJActionActionID = ActionID;
	MJActionContentTypeID = ContentTypeID;
	MJActionContentID = ContentID;
	MJActionUpdID = UpdID;
	
	MJActionDO(MJActionTypeSupport, 1);
}

function MJActionAgainst(AppID, ActionID, ContentTypeID, ContentID, UpdID)
{
	MJActionClearParm();
	
	MJActionAppID = AppID;
	MJActionActionID = ActionID;
	MJActionContentTypeID = ContentTypeID;
	MJActionContentID = ContentID;
	MJActionUpdID = UpdID;

	MJActionDO(MJActionTypeAgainst, 1);
}

function MJActionPlay(AppID, ActionID, ContentTypeID, ContentID, UpdID)
{
	MJActionClearParm();
	
	MJActionAppID = AppID;
	MJActionActionID = ActionID;
	MJActionContentTypeID = ContentTypeID;
	MJActionContentID = ContentID;
	MJActionUpdID = UpdID;

	//于向东修改 2009-4-24 开始
	//MJActionDO(MJActionTypePlay, 1);
	LoadProxyForMJActionDO();
	//于向东修改 2009-4-24 结束
}
//于向东增加 2009-4-24 开始
//判断对象加载状态,延迟调用
function LoadProxyForMJActionDO()
{
	var objReady = false;
	if (document.getElementById("MJActioniframeProxy"))
	{
		if (document.getElementById("MJActioniframeProxy").contentWindow)
		{
			if (document.getElementById("MJActioniframeProxy").contentWindow.sendRequest)
			{
				objReady = true;
			}
		}
	}
	if (objReady)
	{
		MJActionDO(MJActionTypePlay, 1);
	}
	else
	{
		setTimeout(LoadProxyForMJActionDO, 500);
	}
}
//于向东增加 2009-4-24 结束
//------------------------Action function end------------------------//