var hoverHTML = new Array();
var sliders = new Array();
var countdown = new Array();
var animations = new Array();
var scroll = [0,0];
var mouse = [0,0];
var mouse_follow="";
var mouse_offset = new Array(2);
var hover_lock = 0;
var updateSlider = -1;
var time;
var context_current=-1;
var queue_pending_command='';
var queue_pending_display='';
var queue_add='';
var battleAnim=1;

document.onmousemove = mpos;
document.onmouseup = mouseup;
window.onscroll = scrollPos;

function openspy(id){
	window.open("spy.php?bot="+id, "spyinfo", 'toolbar=0, location=0, directories=0, status=0, menubar=0, width=700, height=450');
}
function parseCookie(variable){
	var a = document.cookie+';';
	variable=variable+"=";
	if(a.search(variable)<0) { return ''; }
	cookie = a.substr(a.search(variable)+variable.length,a.length);
	return cookie.substr(0,cookie.search(';'));
}
function writeCookie(variable,value){
	document.cookie = variable+"="+value; 
}
function confirmAction(text,link)  {
 if (confirm(text)){	window.location.href=link;}
 }
function markchk(s){
for(i=0;i<document.getElementsByName(s).length;i++){
document.getElementsByName(s)[i].checked=!document.getElementsByName(s)[i].checked;
}
}
function opacity(obj,percentage) {
	var objStyle, filterValue, opacityValue;
	filterValue="Alpha(opacity="+percentage+")";
	opacityValue=""+percentage/100;
	objStyle=obj.style;
	objStyle.filter=filterValue;
	objStyle.MozOpacity=opacityValue;
	objStyle.KhtmlOpacity=opacityValue;
	objStyle.opacity=opacityValue;
}
function init(){
	setTime();
	var lowest = Number.POSITIVE_INFINITY;
	var titleCounter=-1;
	for(var i=0; i<countdown.length; i++){
		var cd = countdown[i].init();
		if(cd[0]==1) { titleCounter=i; lowest=1;} else {
			if(cd[1]<lowest) { titleCounter=i; lowest=cd[1]; }
		}
	}
	if(titleCounter>=0){countdown[titleCounter].setTitle(true); };
	if(countdown.length>0){timer();}
	if(animations.length>0){ animate(); }
	if(document.getElementById('queue')){document.getElementById('queue').value="";}
	window.setTimeout("initBattle()",2000);
	var dsp = parseCookie("dspmode");
	if(dsp==''){ dsp=0; }
	if(dsp==1){ flipDsp(); }
}
function animate(){
	for(var i=0; i<animations.length; i++){
		animations[i].animate();
	}
	window.setTimeout("animate()",40);
}
function setTime(){
	var now = new Date();
	try{
		time = Math.floor(now.getTime()/1000);
	} catch(e) {
		time = -1;
	}
}
function timer(){
	setTime();
	for(var i=0; i<countdown.length; i++){
		countdown[i].check();
	}
	window.setTimeout('timer()',1000);
}
function countHover(anim,on){
	animations[anim].interrupt(on);
}
function mpos(e){
	mouse = getPos(e);
	if(mouse_follow!="" && hover_lock==0){
		adjustToMouse(mouse_follow);
	}
	if(updateSlider>=0){
		sliders[updateSlider].slide(mouse[0]);
	}
}
function winHeight () {
var h=0;
  if (window.innerHeight) {
    h= window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    h= document.body.offsetHeight;
  } else {
    h = 400;
  }
  h+=scroll[1];
  return h;
}
function winWidth () {
var w=0;
  if (window.innerWidth) {
    w= window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    w= document.body.offsetWidth;
  } else {
    w = 1000;
  }
  w+=scroll[0];
  return w;
}

function adjustToMouse(id){
	if(mouse_offset[0]){
		document.getElementById(id).style.left = (mouse[0] - 20 - objwidth) + "px";
	}else {
		if(mouse[0] + 50 + objwidth < winWidth() ){
			document.getElementById(id).style.left = (mouse[0] + 15) + "px";
		}
	}
	if(mouse_offset[1]){
		document.getElementById(id).style.top = (mouse[1] - 20 - objheight) + "px";
	}else{
		document.getElementById(id).style.top = (mouse[1] + 15) + "px";
	}
}

function scrollPos(){
	scroll = ['n','n'];
	if (typeof(window.pageXOffset) == 'number') {
		scroll[0] = window.pageXOffset;
		scroll[1] = window.pageYOffset;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
		scroll[0] = document.body.scrollLeft;
		scroll[1] = document.body.scrollTop;
	} 
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
		scroll[0] = document.documentElement.scrollLeft;
		scroll[1] = document.documentElement.scrollTop;
	}
	if(typeof(scroll[0])!='number') { scroll[0]=0; }
	if(typeof(scroll[1])!='number') { scroll[1]=0; }
}

function getPos(e){
    if(!e) e = window.event;
    var y;
	if(e.pageY){ y= e.pageY; }else{
		y = e.clientY;
		y+=scroll[1];
	}

    var x;
	if(e.pageX){ x= e.pageX; }else{
		x = e.clientX;
		x+=scroll[0];
	}
    //document.getElementById("jsdebug").innerHTML = x + " / " + y;
    return [x,y];
}

function hoverbox(id, on, html){
	if(on && hover_lock==0){
		objwidth = parseInt(document.getElementById(id).style.width);
		objheight = parseInt(document.getElementById(id).style.height);
		document.getElementById(id).innerHTML = hoverHTML[html];
		mouse_follow=id;
		stophov="";
		context_current = html+1;
		if(mouse[0] + 15 + objwidth> winWidth()) { mouse_offset[0]=1; } else { mouse_offset[0]=0; }
		if(mouse[1] + 15 + objheight > winHeight()) { mouse_offset[1]=1; } else { mouse_offset[1]=0; }
		adjustToMouse(id);
		animations[1].interrupt(1);
	}else{
		if(hover_lock==0){
			animations[1].interrupt(-1);
		}
	}
}
function contextMenu(){
	if(context_current>=0){
		hover_lock=1;
		document.getElementById('hov').innerHTML = hoverHTML[context_current];
	}
}
function contextClose(){
	hover_lock=0;
	animations[1].interrupt(-1);
}
function flipDsp(){
	if(document.getElementById('resdsp').style.display=='none') {
		writeCookie("dspmode",0);
		document.getElementById('resdsp').style.display='inline';
		document.getElementById('drodsp').style.display='none'; 
	} else {
		writeCookie("dspmode",1);
		document.getElementById('resdsp').style.display='none';
		document.getElementById('drodsp').style.display='inline';
	}
	a = new Date(1000*time + 86400000);
	try{
		writeCookie('expires',a.toGMTString());
	} catch(e) {}
}
function setMissionVis(ids){
	var cmc;
	for(i=0;i<maxmc;i++){
		var cmc = document.getElementById('mc'+i);
		if(cmc){
			cmc.style.display='none';
		}
	}
	if(ids.length>0){
		for(i=0;i<ids.length;i++){
			document.getElementById('mc'+ids[i]).style.display='block';
		}
		document.getElementById('nmc').style.display='none';
	}else{
		document.getElementById('nmc').style.display='block';
	}
}
function queue_pending(command,info,add){
	queue_pending_command=command;
	queue_pending_display=info;
	queue_add=add;
}
function queue_ignore(){
	queue_pending_command='';
	queue_pending_display='';
	queue_add='';
}
function queue_flush(){
	if(queue_pending_command!='') { document.getElementById('queue').value=document.getElementById('queue').value+"/"+queue_pending_command; }
	if(queue_pending_display!='') { document.getElementById('queuedisplay').innerHTML=document.getElementById('queuedisplay').innerHTML+queue_pending_display+"<br>"; }
	queue_pending_command='';
	queue_pending_display='';
}
function enqueue(command, info){
	document.getElementById('queue').value=document.getElementById('queue').value+"/"+command+'-'+queue_add;
	document.getElementById('queuedisplay').innerHTML=document.getElementById('queuedisplay').innerHTML+info+"<br>";
	queue_add='';
	queue_pending_command='';
	queue_pending_display='';
	showPartSelection(0);
}
function showPartSelection(id){
	if(id!=0){
		document.getElementById('chestselect').style.display="none";
		document.getElementById('weaponselect').style.display="none";
		document.getElementById('legsselect').style.display="none";
		document.getElementById('addonselect').style.display="none";
		document.getElementById(id).style.display="block";
		if(document.getElementById("queuedisplay")) { document.getElementById("queuedisplay").style.display="none"; }
	}else{
		document.getElementById('chestselect').style.display="none";
		document.getElementById('weaponselect').style.display="none";
		document.getElementById('legsselect').style.display="none";
		document.getElementById('addonselect').style.display="none";
		if(document.getElementById("queuedisplay")) { document.getElementById("queuedisplay").style.display="block"; }
	}
}
function mouseup(){
	setSlider(-1);
}
function setSlider(id){
	updateSlider=id;
	if(id>=0) { sliders[id].setStart(mouse[0]); }
}
function animation(id, opacityStart, delay, logic, interrupt){
	this.obj=null;
	this.opacity = opacityStart;
	this.delayMovement=delay;
	this.delayFader = delay;
	if(opacityStart==0){this.fader=1;}else{this.fader=-1;}
	this.faderSpeed=0;
	this.vx=0;
	this.vy=0;
	this.x=0;
	this.y=0;
	this.logic = logic;
	this.interrupt = interrupt;
	
	
	this.animate = function(){
		if(this.obj!=null){ this.logic(); } else {
			this.obj = document.getElementById(id);
			if(this.obj!=null){
				this.init=1;
				this.x = this.obj.style.left;
				this.y = this.obj.style.top;
				opacity(this.obj,this.opacity);
			}
		}
	}
	this.move = function(){
		if(this.delayMovement==0){
			this.x += this.vx;
			this.y += this.vy;
			this.obj.style.left=Math.round(this.x) + "px";
			this.obj.style.top=Math.round(this.y) + "px";
		}else{
			if(this.delayMovement>0){ this.delayMovement--; }
		}
	}
	this.setVelocity = function(vx, vy){
		this.vx = vx;
		this.vy = vy;
	}
	this.setPos = function(x, y){
		this.x = x;
		this.y = y;
		this.obj.style.left=x + "px";
		this.obj.style.top=y + "px";
	}
	this.setFaderSpeed = function(s){
		this.faderSpeed = s;
	}
	this.delayMotion = function(fps){
		this.delayMovement=fps;
	}
	this.delayFading = function(fps){
		this.delayFader=fps;
	}
	this.setFader = function(dir){
		this.fader=dir;
	}
	this.invertFader = function(){
		if(this.fader<0){ this.fader=1; } else { this.fader=-1; }
	}
	this.fade = function(){
		if(this.delayFader==0){
			this.opacity += this.fader * this.faderSpeed;
			var int_opacity = Math.round(this.opacity);
			if(int_opacity>=100) { this.opacity=100; int_opacity=100; }
			if(int_opacity<=0) { this.opacity=0; int_opacity=0; this.obj.style.display="none"; }
			opacity(this.obj,int_opacity);
			if(int_opacity>0 && this.obj.style.display!="block") { this.obj.style.display="block"; }
		}else{
			if(this.delayFader>0) { this.delayFader--; }
		}
	}
}
function slider(vmin,vmax,steps,length,field){
	this.field = field;
	this.steps = steps;
	this.pxs = length / steps;
	this.vmin = vmin;
	this.vmax = vmax;
	this.current = 0;
	this.start=0;
	this.getValue = function() {
		return this.vmin + Math.round(Math.floor(this.current / this.pxs) * ((this.vmax - this.vmin) / this.steps));
	}
	this.setStart = function(start) {
		if(this.start==0){
			this.start = start;
		}
	}
	this.slide = function(newx) {
		this.current = Math.max(1, Math.min(300,newx - this.start));
		document.getElementById(this.field+'b').style.left=this.current + "px";
		document.getElementById(this.field+'f').value=this.getValue();
	}
}
function counter(duration, relocator, title, finishing, field){
	this.duration=duration;
	this.relocator=relocator;
	this.title=title;
	this.end=-1;
	this.finishing=finishing;
	this.field=field;
	this.init = function(){
		this.end=time+this.duration;
		return [title,this.end];
	}
	this.setTitle = function(title){
		this.title=title;
	}
	this.check = function() {
		if(this.end>0){
			if(time>=this.end){
				this.end=-1;
				if(relocator!=''){window.location.href=relocator; }
				if(this.field!=""){document.getElementById(this.field).innerHTML=finishing;}
				if(this.title){document.title="AMUREC: "+finishing; }
			}else{
				var ts=this.toString();
				if(this.field!=""){document.getElementById(this.field).innerHTML=ts;}
				if(this.title){document.title="AMUREC: "+ts; }
			}
		}
	}
	this.toString = function(){
		var tl=this.end-time;
		var min=0;
		var h=0;
		var sec = tl%60;
		tl=(tl-sec)/60;
		if(tl>=0){
			min = tl%60;
			tl=Math.floor((tl-min)/60);
			if(tl>0){
				h=tl;
			}
		}
		var mz="";
		var sz="";
		if(min<10) { mz="0"; }
		if(sec<10) { sz="0"; }
		return h+":"+mz+min+":"+sz+sec;
	}
}

function bar(obj,vc,vm,xm){
	this.id=obj;
	this.obj=null;
	this.vc=vc;
	this.vm=vm;
	this.xm=xm;
	this.init=function(){
		if(document.getElementById(this.id)!=null){
			if(this.obj==null){
				this.obj=document.getElementById(this.id).style;
				this.change(0);
			}
			return true;
		}else{
			return false;
		}
	}
	this.change = function(value){
		this.vc+=parseInt(value);
		
		if(this.vc<=0) {
			this.obj.width=this.xm+'px';
			this.obj.backgroundColor="#b30000";
		}else{
			this.obj.width=Math.round(this.vc*this.xm/this.vm)+'px';
		}
	}
}
function multibar(bars){
	this.bars = bars
	this.change = function(id,value){
		if(typeof(value)!='d'){
			this.bars[id].change(value);
		}
	}
	this.init=function(){
		return this.bars[0].init() && this.bars[1].init() && this.bars[2].init() && this.bars[3].init() && this.bars[4].init() && this.bars[5].init();
	}
}
function orderList(field,select,descriptions){
	this.field=document.getElementById(field);
	this.select=document.getElementById(select);
	this.order = this.field.value.split('/');
	this.entries = new Array(this.order.length);
	this.selections = new Array(this.order.length);
	for(var i=0;i<this.order.length;i++){
		this.entries[i] = new Option(descriptions[i],i);
	}
	this.repaint = function(){
		for(var i=0;i<this.order.length;i++){
			this.select.options[i]=new Option(this.entries[i].text,this.entries[i].value);
			if(this.selections[i]) { this.select.options[i].selected=true; } else { this.select.options[i].selected=false; }
		}
		this.temp++;
	}
	this.swap = function(a,b){
		if(a>=0 && b>=0 && a<this.order.length && b<this.order.length){
			var t = this.entries[a];
			this.entries[a]=this.entries[b];
			this.entries[b]=t;
			t = this.order[a];
			this.order[a]=this.order[b];
			this.order[b]=t;
			this.field.value=this.order.join('/');
			t = this.selections[a];
			this.selections[a]=this.selections[b];
			this.selections[b]=t;
		}
	}
	this.move = function(up){
		for(var i=0;i<this.order.length;i++){
			this.selections[i]=(this.select.options[i].selected);
		}
		if(up){
			for(var i=1;i<this.order.length;i++){
				if(this.select.options[i].selected){ this.swap(i,i-1);	}
			}
		}else{
			for(var i=this.order.length-2;i>=0;i--){
				if(this.select.options[i].selected){ this.swap(i,i+1); }
			}
		}
		this.repaint();
	}
	this.repaint();
}
var numberAnim = function(){
	this.fade();
	if(this.y>10){this.move();}
}
var numberIR = function(x,y,d,on){
	if(on){
		this.setVelocity(0,-4);
		this.setFader(-1);
		this.setFaderSpeed(7);
		this.obj.style.display='block';
		this.setPos(x,y);
		this.opacity=100;
		this.fade();
		this.delayMotion(d);
		this.delayFading(d+9);
	}else{
		this.obj.style.display='none';
		this.setPos(0,0);
	}
}
function initBattle(){
	if(typeof(battleData)=='string'){
		var barinit = 1;
		for(i=0; i<hpbars.length;i++){
			barinit = barinit && hpbars[i].init();
		}
		if(barinit){
			battleSteps = battleData.split('/');
			document.getElementById('pm').style.display='block';
			battleStep();
		}else{
			window.setTimeout("initBattle()",300);
		}
	}
}
function battleStep(){
	var currentStep = battleSteps[battleCounter].split('#');
	battleCounter++;
	var ap = 'mp-'+currentStep[0]+'-'+currentStep[1];
	if(battleAnim){
		if(currentStep[0]>=0){
			document.getElementById('pm').style.left = parseInt(document.getElementById('m-'+currentStep[0]).style.left) + parseInt(document.getElementById(ap).style.left)+'px';
			document.getElementById('pm').style.top = parseInt(document.getElementById('m-'+currentStep[0]).style.top) + parseInt(document.getElementById(ap).style.top)+'px';
			document.getElementById('pm').style.width=document.getElementById(ap).style.width;
			document.getElementById('pm').style.height=document.getElementById(ap).style.height;
		}
		ap = 'mp-'+currentStep[2]+'-'+currentStep[3];
		document.getElementById('bd'+bdcounter).innerHTML = currentStep[4];
		var tx=parseInt(document.getElementById('m-'+currentStep[2]).style.left)+ parseInt(document.getElementById(ap).style.left)+ Math.round(parseInt(document.getElementById(ap).style.width)/2)-35;
		var ty=parseInt(document.getElementById('m-'+currentStep[2]).style.top)+ parseInt(document.getElementById(ap).style.top)+ Math.round(parseInt(document.getElementById(ap).style.height)/2)-10;
		animations[3+bdcounter].interrupt(tx,ty+bdcounter*20,bdcounter*2,1);
	}
	hpbars[currentStep[2]].change(currentStep[3],currentStep[4]);
	if(battleCounter<=battleSteps.length-1){
		if(battleAnim) {
		if(currentStep[5]=='c'){
			bdcounter++;
			window.setTimeout('battleStep()',50);
		}else{
			window.setTimeout('battleStep()',1000);
			bdcounter=0;
		}
		}else{ battleStep(); }
	}else{
		document.getElementById('pm').style.display='none';
		animations[6].interrupt();
	}
}
