var armory = new Object;

armory.get = function(realm,name,o,host,update,todo)
{
	if (host=='cn')
		{
		var hosthost = 'cn.wowarmory.com'
		var cachepath = 'cache_cnwowarmory_new'
		var proxypath = 'http://img4.ngacn.cc/proxy'
		//this.iconbase = 'http://cn.wowarmory.com/wow-icons/_images/21x21/'
		this.iconbase = 'http://tw.wowarmory.com/wow-icons/_images/21x21/'
		var portraitpath = 'http://tw.wowarmory.com/_images/portraits/wow-70/'
		this.glyphiconbase = 'http://img4.ngacn.cc/ngabbs/glyph/'
		var dbbase = 'http://db.178.com/wow/cn/item/'
		}
	else if (host=='us')
		{
		var hosthost = 'www.wowarmory.com'
		var cachepath = 'cache_uswowarmory_new'
		var proxypath = 'http://img4.ngacn.cc/proxy'
		this.iconbase = 'http://tw.wowarmory.com/wow-icons/_images/21x21/'
		var portraitpath = 'http://tw.wowarmory.com/_images/portraits/wow-80/'
		this.glyphiconbase = 'http://img4.ngacn.cc/ngabbs/glyph/'
		var dbbase = 'http://db.178.com/wow/tw/item/'
		}
	else if (host=='eu')
		{
		var hosthost = 'eu.wowarmory.com'
		var cachepath = 'cache_euwowarmory_new'
		var proxypath = 'http://img4.ngacn.cc/proxy'
		var portraitpath = 'http://tw.wowarmory.com/images/portraits/wow-80/'
		this.iconbase = 'http://tw.wowarmory.com/wow-icons/_images/21x21/'
		this.glyphiconbase = 'http://img4.ngacn.cc/ngabbs/glyph/'
		var dbbase = 'http://db.178.com/wow/tw/item/'
		}
	else if (host=='tw')
		{
		var hosthost = 'tw.wowarmory.com'
		var cachepath = 'cache_twwowarmory_new'
		var proxypath = 'http://img4.ngacn.cc/proxy'
		this.iconbase = 'http://tw.wowarmory.com/wow-icons/_images/21x21/'
		var portraitpath = 'http://tw.wowarmory.com/_images/portraits/wow-80/'
		this.glyphiconbase = 'http://img4.ngacn.cc/ngabbs/glyph/'
		var dbbase = 'http://db.178.com/wow/tw/item/'
		}
	function e(t){return encodeURIComponent(t)}
	var self = this;
	var u = 'character-sheet.xml?r='+e(realm)+'&n='+e(name)
	if (update==-1)
		{
		o.innerHTML="[<a class='b' href='http://"+hosthost+"/"+u+"' target='_blank'>"+realm+' '+name+"</a>]"
		return;
		}
	var k = hex_md5(u)
	//var d = new Date
	cachepath = cachepath+'/'+k.substr(0,2)+'/'+k.substr(2,2);
	if (update)
		{
		var urls = new Array(proxypath+"/proxy.php?update=1&host="+hosthost+"&url=" + encodeURIComponent(u) + "&rand=" +Math.random()+((cookieFuncs && cookieFuncs.getCookie('debug'))?'&debug=1':''));
		}
	else
		{
		var urls = new Array(proxypath+"/"+cachepath+"/" + k + ".js",proxypath+"/proxy.php?host="+hosthost+"&url=" + encodeURIComponent(u));
		}
	if (todo)
	{
	httpDataGetter.script_muti_get(urls,
		function(x){
			if (!x)return false;
			else
				{
				var date = new Date
				if ((date.getTime() - x[0])>3600*24*30)return false
				if (!x[0])x[0]=false
				todo(x[0],realm,name,o,host,hosthost,u)
				return true
				}
			},
		function(){todo(x[0],realm,name,o,host,hosthost,u)},
		'utf-8'
		);
	}
	else
	{
	httpDataGetter.script_muti_get(urls,
		function(x){
			if (!x)
				return false;
			else if (!x[0] || x.error){
				o.innerHTML="<span  class='gray'>[<a class='b' href='http://"+hosthost+"/"+u+"' target='_blank'>"+realm+' '+name+"</a>] "+self.error((x.error)?x.error:1,realm,name,host)+"</span>"
				return true
				}
			else
				{
				var date = new Date
				if ((date.getTime() - x[0])>3600*24*30)
					{
					return false
					}
				o.innerHTML=self.proc(x[0],realm,name,o,host,hosthost,portraitpath,u,dbbase)
				return true
				}
			},
		function(){
			o.innerHTML="<span  class='gray'>[<a class='b' href='http://"+hosthost+"/"+u+"' target='_blank'>"+realm+' '+name+"</a>] "+self.error(0,realm,name,host)+"</span>"},
		'utf-8'
		);
	}
}//fe

armory.proc = function(t,realm,name,o,host,hosthost,portraitpath,u,dbbase)
{
	function d(f,c)
		{
			for (var k in c)
				{
					if (typeof(c[k])=='object')
						{
							d(f+k+'.',c[k]);
						}
					else
						{
							r = r+f+k+' = '+c[k]+'<br/>';
						}
				}
		}
if (typeof(xmlParser)!='object')
	{
		xmlParser = new AVSXmlParser();
	}
t = xmlParser.xml2Array(t)
if (!t.page || !t.page.characterInfo || !t.page.characterInfo.character || !t.page.characterInfo.characterTab )
	{
	return "<span class='gray'>[<a class='b' href='http://"+hosthost+"/"+u+"' target='_blank'>"+realm+' '+name+"</a>] "+this.error(2,realm,name,host)+"</span>";
	}
var professions = '';
if (t.page.characterInfo.characterTab.professions && t.page.characterInfo.characterTab.professions.skill)
	{
	for (var i=0; i<2; i++)
		{
		if (t.page.characterInfo.characterTab.professions.skill[i])
			{
			professions += ' '+t.page.characterInfo.characterTab.professions.skill[i].name
			}
		}
	}
if (professions)professions="<span class='gray'>专业:</span>"+professions

var talent = '';
if (t.page.characterInfo.characterTab.talentSpecs)
	{
	for (var k in t.page.characterInfo.characterTab.talentSpecs.talentSpec)
		{
		if (typeof(t.page.characterInfo.characterTab.talentSpecs.talentSpec[k])=='object')
			{
			var tmp = t.page.characterInfo.characterTab.talentSpecs.talentSpec[k];
			if(tmp.treeOne!=0 || tmp.treeTwo!=0 || tmp.treeThree!=0)
				{
				if(!tmp.prim)tmp.prim='';
				if (tmp.active)
					talent+=tmp.prim+":"+tmp.treeOne+"/"+tmp.treeTwo+"/"+tmp.treeThree+" ";
				else
					talent+="<span class='gray'>"+tmp.prim+":"+tmp.treeOne+"/"+tmp.treeTwo+"/"+tmp.treeThree+"</span> ";
				}
			}
		}
	}
else 
	{
	var tmp = t.page.characterInfo.characterTab.talentSpec[0];
	if(!tmp.prim)tmp.prim='';
	talent+=tmp.prim+":"+tmp.treeOne+"/"+tmp.treeTwo+"/"+tmp.treeThree+" "
	}

var achievement='';
tmp = t.page.characterInfo;
if (tmp.summary && tmp.summary.c)
	{
	achievement = "<span class='gray'>成就:</span>"+tmp.summary.c[0].points
	}

var glyph='';
tmp = t.page.characterInfo.characterTab;
if (tmp.glyphs && tmp.glyphs.glyph)
	{
	for (var k in tmp.glyphs.glyph)
		{
		if (typeof(tmp.glyphs.glyph[k])=='object')
			{
			glyph+="<img title='"+tmp.glyphs.glyph[k].name+" : "+tmp.glyphs.glyph[k].effect+"' src='"+this.glyphiconbase+tmp.glyphs.glyph[k].icon+".png' style='width:14px;height:14px;vertical-align:-2px'/>"
			}
		}
	}
var r = '';
//d('',t)
//return r;
realm=realm.replace("'",'&#39;')
name=name.replace("'",'&#39;')
r = "\
<table class='armorychr'>\
	<tbody>\
	<tr>\
		<td style='width:120px;height:104px;padding:0px;background:url("+this.portrait(t.page.characterInfo.character[0].genderId, t.page.characterInfo.character[0].raceId, t.page.characterInfo.character[0].classId,portraitpath)+") no-repeat 50px 34px'>\
			<div style='width:120px;height:104px;background:url("+__IMG_STYLE+"/armorymask.png) no-repeat'></div>\
		</td>\
		<td class='defaultcolor'>\
			<span class='gray'>["+realm+"] ["+t.page.characterInfo.character[0].guildName+"]</span> [<a href='http://"+hosthost+"/character-sheet.xml?"+t.page.characterInfo.character[0].charUrl+"' target='_blank' class='name'>"+t.page.characterInfo.character[0].prefix + t.page.characterInfo.character[0].name + t.page.characterInfo.character[0].suffix+"</a>] <span class='xtxt gray'>("+hosthost+")</span> <a class='xtxt' href='javascript:void(0)' onclick='armory.get(\""+realm+"\",\""+name+"\",this.parentNode.parentNode.parentNode.parentNode.parentNode,\""+host+"\",1)'>[RELOAD]</a><br/>\
			<b>"+t.page.characterInfo.character[0].level+"级 "+t.page.characterInfo.character[0]['class']+"</b> "+talent+" "+glyph+"<br/>\
			"+this.items(t.page.characterInfo.characterTab.items.item,dbbase,host)+"<br/>\
			"+professions+' '+achievement+"\
		</td>\
	</tr>\
	</tbody>\
</table>\
";
if(window.location.href.indexOf('debug')!=-1)r+="<textarea>"+this.treeDisp('',t)+"</textarea>"
return r;
}//fe

armory.items = function(t,dbbase,host)
{
if (typeof(t)!='object')
	{
	return '';
	}
var r = '';
for (i=0; i<19; i++)
	{
	if (t[i] && t[i].id)
		r = r+ "<a href='"+dbbase+t[i].id+".html' target='_blank' onmouseover='wowdb_tooltip.delay_get_item(\""+t[i].id+"\",false,\""+host+"\");delay_showdscp(event,wowdb_tooltip.tip[\"tip\"],700);' onmousemove='movedscp(event,wowdb_tooltip.tip[\"tip\"])' onmouseout='wowdb_tooltip.clear()'><img src='"+this.iconbase+t[i].icon+".png' style='width:21px;height:21px'/></a>";
	}
return r
}

armory.portrait = function(g,r,c,hosthost)
{
var p = hosthost+g+'-'+r+'-'+c+'.gif'
return (p)
}

armory.error = function(c,realm,name,host)
{
var cc='  未知错误  ';
if (typeof(c)=='number'){
	switch(c){
		case 0:
			cc = '  无法从代理获得数据，或者代理不可用  '
			break;
		case 1:
			cc = '  无法通过代理获得Armory数据，或Armory服务器不可用  '
			break;
		case 2:
			cc = '  Armory中无此玩家数据，或Armory服务器在维护中  '
			break;
		}
	}
else{
	switch(c){
		case 'get data timeout':
			cc = '  代理读取Armory数据超时  '
			break;
		case 'connect error':
			cc = '  代理连接Armory服务器失败  '
			break;
		}
	}
return "<span class='x_txt' title='"+cc+"'>(error code "+c+" <a href='javascript:void(0)' onclick='armory.get(\""+realm+"\",\""+name+"\",this.parentNode,\""+host+"\",1)'>[RELOAD]</a>)</span>"
}

armory.treeDisp = function(f,c)
{
	var x = '';
	for (var k in c)
		{
			if (typeof(c[k])=='object')
				{
					x+=this.treeDisp(f+k+'.',c[k]);
				}
			else
				{
					x+=f+k+' = '+c[k]+'\n';
				}
		}
	return x;
}