﻿//****************************************************************************
var index=1;
function changeList(_index)
{
	if(_index==index)
		return;
	document.getElementById("ph_" + _index).className="ph_1";
	document.getElementById("ph_" + index).className="ph_2";
	document.getElementById("ph_list_" + _index).style.display="";
	document.getElementById("ph_list_" + index).style.display="none";
	index=_index;
}
//******************************************************************************
//获取随机游戏
function RandomGame()
{
	ShowMsg("请稍候...");
	ajaxPost("response.aspx?action=1","","WebResponse(value)");
}
//******************************************************************************

function getPK(id)
{
	ajaxPost("response.aspx?action=5","id=" + id,"WebResponse(value)");
}
function CreatePK(id)
{
	var money=parseInt(document.getElementById("pk_money").value,10);
	if(isNaN(money))
	{alert("无效的金币数目！");return;}
	if(money<=0)
	{alert("请输入一个大于0的整数！");return;}
	if(money>3000)
	{alert("最大值为3000！");return;}
	ShowMsg("请稍候...");
	ajaxPost("response.aspx?action=10","id=" + id + "&m=" + money,"WebResponse(value)");
}

//提示信息
var Msg;
//用户PK状况
var PkStatus=-1;

function changePK(value)
{
	//if(PkStatus!=value)
	setPkStatus(value);
	PkStatus=value;
	if(PkStatus==0)
		document.getElementById("pk_status_box").innerHTML="自由游戏";
	else if(PkStatus==1)
		document.getElementById("pk_status_box").innerHTML="攻(守)擂";
	if(document.getElementById("show_flash").style.display=="none")
		document.getElementById("show_flash").style.display="";
}

function WebResponse(value)
{
    var obj=null;
	try	{obj=eval(value);}catch(e)	{}
	if(obj==null){CloseMsg();alert("无法获取数据！");return;};
	if(obj.Status!=1){CloseMsg();alert(obj.Error);return;}
    switch(obj.Action)
    {
        case 1:
        {
			CloseMsg();
			window.location.href="show.aspx?id=" + obj.GameID;
        }
        break;
		case 5:
		{
			if(obj.Exist)
				document.getElementById("pk_status").style.display="";
			else
			{
				document.getElementById("pk_status").style.display="none";
			}
			document.getElementById("pk_body").innerHTML=obj.Text;
		}
		break;
		case 10:
		{
			CloseMsg();
			alert("摆擂成功！哈哈，就等着别人来挑战了！");
			location.reload(); 
		}
		break;
    }

}

var cid;
function Cns(value)
{
	cid=value;
	return cid;
}
function Cng()
{
	return cid;
}

function fetchOffset(obj) {
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
}

function ShowPkStatusMenu(){   
	var menu = document.getElementById("pk_status_menu");
	if(menu==null){
		menu = document.createElement("DIV");
		menu.id = "pk_status_menu";
		menu.innerHTML = "<a href=\"javascript:changePK(0);ShowPkStatusMenu()\">自由游戏</a><br /><a href=\"javascript:changePK(1);ShowPkStatusMenu()\">攻(守)擂</a>";	
		var target=fetchOffset(document.getElementById("pk_status_box"));
		menu.style.left=target.left + "px";
		menu.style.top=target.top+document.getElementById("pk_status_box").offsetHeight + 2  +"px";
		document.body.appendChild(menu);
	}
	else
	{
		if(menu.style.display=="none")
			menu.style.display="";
		else
			menu.style.display="none";
	}
}

//读取用户的PK状态
function readPkStatus()
{
	var status=getCookie("PkStatus_" + GameID);
	if(status==undefined || status==null)
	{ PkStatus=-1; return; }
	PkStatus=parseInt(status);
	if(isNaN(PkStatus))
		PkStatus=-1;
}

//设置用户的PK状态
function setPkStatus(value)
{
    var pathname,myDomain
	pathname = location.pathname;
    myDomain = pathname.substring(0,pathname.lastIndexOf('/')) +'/';
    var largeExpDate = new Date ();
    largeExpDate.setTime(largeExpDate.getTime() + 24 * 60 * 60 * 1000); //+1 day
    setCookie("PkStatus_" + GameID,value,largeExpDate,myDomain);
}

//读取Cookie
function getCookie(name)
{
  var arg = name + "=";
  var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		   var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
					return getCookieVal (j);
			i = document.cookie.indexOf(" ", i) + 1;
					if (i == 0)
							break;
		   }
	return null;
}
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
     endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//设置Cookie
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}


function showInit()
{
	if(PkStatus!=1)
		readPkStatus();
	changePK(PkStatus);
	if(Msg!=undefined /*&& PkStatus==-1*/)
	{document.getElementById("show_flash").style.display="none";ShowMsg(Msg,1);}
	getPK(GameID);	
}

function Big()
{
	var arrow=document.getElementById("show_arrow");
	if(arrow.className=="show_arrow_2")
	{
		document.getElementById("page_left").style.width="665px";
		document.getElementById("page_right").style.display="";
		arrow.className="show_arrow_1";
	}
	else
	{
		document.getElementById("page_left").style.width="100%";
		document.getElementById("page_right").style.display="none";	
		arrow.className="show_arrow_2";
	}
}