characterBoxWidth=150
characterBoxHeight="*"
menuWidth=300

pcBox=new AGEBox("blue", "#1111AA", "#FFF", characterBoxWidth, characterBoxHeight)
npcBox=new AGEBox("red", "#AA1111", "#FFF", characterBoxWidth, characterBoxHeight)
pcMenu=new AGEBox("blue", "#1111AA", "#FFF", menuWidth)
npcMenu=new AGEBox("red", "#AA1111", "#FFF", menuWidth)


characters=new Array()

function add(character) {
	x=characters.length;
	character.setIndex(x)
	characters[x]=character
	characters[x].refresh()
	characters[x].show()
	SET_DHTML(character.box.elementId)

}

function removeChar(x) {
	if (confirm("Are you sure you want to delete "+characters[x].name+"? This cannot be undone!")) {
		for (var i=x+1; i<characters.length; i++) {
			characters[i].setIndex(i-1)
			alert((i-1)+" == "+characters[i].index)
			characters[i].refresh()
		}
		characters[x].box.hide()
		characters[x].menu.hide()
		characters.splice(x, 1)
	}
}

function promptDamage(x) {
	dam=prompt("How much damage does "+characters[x].name+" suffer?","")
	if (!isNaN(dam)) {
		characters[x].damage(dam)
	}
}

function promptHeal(x) {
	heal=prompt("How much damage does "+characters[x].name+" heal?","")
	if (!isNaN(heal)) {
		characters[x].heal(heal)
	}
}

function addRollPrompt(x) {
	rollName=prompt("What kind of roll would you like to give "+characters[x].name+"?")
	if (rollName!=null) {
		roll=prompt("How should "+characters[x].name+"'s "+rollName+" be rolled? (in the format XdY+Z)")
		result=dFormParse(roll)
		if (result==null) {
			while(roll!=null) {
				roll=prompt(roll+" is invalid. How should "+characters[x].name+"'s "+rollName+" be rolled? (in the format XdY+Z)")
				result=dFormParse(roll)
				if (result!=null) {
					characters[x].addRoll(rollName, result)
					break
				}
			}
		} else {
			characters[x].addRoll(rollName, result)
		}
	}
}

function toggleMenu(x) {
	if (characters[x].menuShown) {
		characters[x].hideMenu()
	} else {
		characters[x].showMenu()
	}
}

function makeRoll(x,i) {
	toReturn="Rolled "+characters[x].rollNames[i]+" for "+characters[x].name+"\n  Result: "
	toReturn+=rollDForm(characters[x].rolls[i])
	alert(toReturn)
}

function deleteRollPrompt(x, i) {
	characters[x].deleteRoll(i)
}

function _setName(name) {
	this.name=name
	this.refresh()
}

function _setInit(x) {
	this.init=x
	this.refresh()
}

function _setInitBonus(x) {
	this.initBonus=x
	this.refresh()
}

function _setAC(x) {
	this.ac=x
	this.refresh()
}

function _setMaxHp(x) {
	this.maxHp=x
	this.refresh()
}

function _setCurHp(x) {
	this.curHp=x
	this.refresh()
}

function _damage(x) {
	this.curHp-=x
	if (this.curHp<-10) {
		this.curHp=-10
	}
	this.refresh()
}

function _heal(x) {
	this.curHp=(x/1+this.curHp)
	if (this.curHp>this.maxHp) {
		this.curHp=this.maxHp
	}
	this.refresh()
}

function _showBox() {
	this.box.show()
}

function _hideBox() {
	this.box.hide()
}

function _showMenu() {
	this.menu.show()
	this.menuShown=true
}

function _hideMenu() {
	this.menu.hide()
	this.menuShown=false
}

function _putCharAt(x, y) {
	this.x=x
	this.y=y
	numX=x.substring(0,x.length-2)
	menuX=(numX/1+characterBoxWidth)+"px"
	this.box.putAt(x, y)
	this.menu.putAt(menuX, y)
}

function _setIndex(x) {
	this.index=x
}

function askName(x) {
	response=prompt("What should "+characters[x].name+"'s new name be?", characters[x].name)
	if (response!=null) {
		characters[x].setName(response)
	}
}
function askInitBonus(x) {
	response=prompt("What should "+characters[x].name+"'s new initiative modifier be?", characters[x].initBonus)
	if (response!=null) {
		characters[x].setInitBonus(response)
	}
}
function askAc(x) {
	response=prompt("What should "+characters[x].name+"'s new AC be?", characters[x].ac)
	if (response!=null) {
		characters[x].setAC(response)
	}
}
function askMaxHp(x) {
	response=prompt("What should "+characters[x].name+"'s new maximum HP be?", characters[x].maxHp)
	if (response!=null) {
		characters[x].setMaxHp(response)
	}
}
function askCurHp(x) {
	response=prompt("What should "+characters[x].name+"'s new current HP be?", characters[x].curHp)
	if (response!=null) {
		characters[x].setCurHp(response)
	}
}

function askInit(x) {
	alert(x)
	initRoll=characters[x].rollInit()
	response=prompt("What does "+characters[x].name+" roll for initiative? (or press OK to use the automatically-rolled initiative)", initRoll)
	if (response!=null) {
		characters[x].setInit(response)
	}
}

function _ref() {
	boxContents=this.name+"<span style='float:right'><input type=button onClick='javascript:toggleMenu("+this.index+")' value=Menu></span><br>HP: "+this.curHp+"/"+this.maxHp+" AC: "+this.ac+"<br>Initiative: "+this.init+"<br>"
	boxContents+="<input type=button onclick='javascript:promptDamage("+this.index+")' value='Damage'>"
	boxContents+="<input type=button onclick='javascript:promptHeal("+this.index+")' value='Heal'>"

	this.box.setContents(boxContents)

	menuContents="<span style='float:left;clear:right'>"+this.name+"'s menu</span>"
	menuContents+="<span style='float:right'><input type=button onclick='javascript:characters["+this.index+"].save()' value=Save></span><br>"
	menuContents+="Initative modifier: "+this.initBonus+"<br>"
	menuContents+="Custom Rolls:</br>"
	if (this.rolls.length==0) {
		menuContents+="&nbsp;&nbsp;&nbsp;&nbsp;<i>None listed</i><br>"
	} else {
		for (var i=0; i<this.rolls.length; i++) {
			menuContents+="<span style='float:left;clear:right'><input type=button onclick='javascript:makeRoll("+this.index+", "+i+")' value='Roll'> "+this.rollNames[i]+": "+this.rolls[i]+"</span>"
			menuContents+="<span style='float:right'><input type=button onclick='javascript:deleteRollPrompt("+this.index+", "+i+")' value='X'></span><br>"
		}
	}
	menuContents+="<p><input type=button onclick='javascript:addRollPrompt("+this.index+")' value='Add custom roll'></p>"
	menuContents+="Change values:<br>"
	menuContents+="<input type=button value=Name onclick='javascript:askName("+this.index+")'>"
	menuContents+="<input type=button value='Initiative modifier' onclick='javascript:askInitBonus("+this.index+")'>"
	menuContents+="<input type=button value='Initiative' onclick='javascript:askInit("+this.index+")'>"
	menuContents+="<input type=button value=AC onclick='javascript:askAc("+this.index+")'>"
	menuContents+="<input type=button value='Max HP' onclick='javascript:askMaxHp("+this.index+")'>"
	menuContents+="<input type=button value='Current HP' onclick='javascript:askCurHp("+this.index+")'>"
	menuContents+="<hr><input type=button value='Delete Character' onclick='javascript:removeChar("+this.index+")'>"


//	alert(menuContents)

	this.menu.setContents(menuContents)
}

function _addRoll(name, roll) {
	x=this.rolls.length
	this.rolls[x]=roll
	this.rollNames[x]=name
	this.refresh()
}

function _deleteRoll(i) {
	this.rolls.splice(i,1)
	this.rollNames.splice(i,1)
	this.refresh()
}

function _rollInit() {
	return d(20)+this.initBonus
}

function _updateXY() {
	this.x=document.getElementById(this.box.elementId).style.left
	this.y=document.getElementById(this.box.elementId).style.top
}

function _getSaveText() {
	this.updateXY()
	varName=""
	if (this.type=="pc") {
		varName="pc"
	} else {
		varName="npc"
	}
	varName+=Math.ceil(Math.random()*10000)

	toSave="\/\/"+this.name+"<br>"
	toSave+=varName+"=new Character('"+this.name+"', '"+this.type+"')<br>"
	if (!isNaN(this.init)) {
		toSave+=varName+".setInit("+this.init+")<br>"
	}
	toSave+=varName+".setInitBonus("+this.initBonus+")<br>"
	toSave+=varName+".setAC("+this.ac+")<br>"
	toSave+=varName+".setMaxHp("+this.maxHp+")<br>"
	toSave+=varName+".setCurHp("+this.curHp+")<br>"
	for (var i=0; i<this.rolls.length; i++) {
		toSave+=varName+".addRoll('"+this.rollNames[i]+"', '"+this.rolls[i]+"')<br>"
	}
	if (this.menuShown) {
		toSave+=varName+".showMenu()<br>"
	}
	toSave+=varName+".putAt(\'"+this.x+"\', \'"+this.y+"\')<br>"
	toSave+="add("+varName+")<br>"
	toSave+="\/\/End of "+this.name

	return toSave
}
function _save() {
	toSave=this.getSaveText()
	showSavePrompt(this.name, toSave)
}

function Character(name, type) {
	this.x=0
	this.y=0
	this.index=null
	this.name=name
	this.initBonus=0
	this.init="Unrolled"
	this.extraRolls=new Array(0)
	this.maxHp=0
	this.curHp=this.maxHp
	this.ac=0
	this.type=type
	this.menuShown=false
	if (type=="pc") {
		this.boxWrapper=pcBox
		this.menuWrapper=pcMenu
	} else {
		this.boxWrapper=npcBox
		this.menuWrapper=npcMenu
	}
	this.rollNames=new Array()
	this.rolls=new Array()

	this.box=new AGEElement()
	this.box.setTopWrapper(this.boxWrapper.topWrapper)
	this.box.setBottomWrapper(this.boxWrapper.bottomWrapper)
	this.menu=new AGEElement()
	this.menu.setTopWrapper(this.menuWrapper.topWrapper)
	this.menu.setBottomWrapper(this.menuWrapper.bottomWrapper)

	this.setName=_setName
	this.setInit=_setInit
	this.rollInit=_rollInit
	this.setInitBonus=_setInitBonus
	this.setAC=_setAC
	this.setMaxHp=_setMaxHp
	this.setCurHp=_setCurHp
	this.setIndex=_setIndex
	this.show=_showBox
	this.hide=_hide
	this.showMenu=_showMenu
	this.hideMenu=_hideMenu
	this.damage=_damage
	this.heal=_heal
	this.addRoll=_addRoll
	this.deleteRoll=_deleteRoll
	this.rollInit=_rollInit
	this.putAt=_putCharAt
	this.updateXY=_updateXY
	this.refresh=_ref
	this.getSaveText=_getSaveText
	this.save=_save

	this.refresh()
}

function newCharacter(type) {
	response=prompt("What is the new "+type+"'s name?")
	if (response!=null) {
		x=characters.length
		newChar=new Character(response, type)
		newChar.setIndex(x)
		newChar.refresh()
		newChar.show()
		characters[x]=newChar
		ADD_DHTML(newChar.box.elementId)
	}
}
