var _talentsArray = new Array();

// spacing between icons in final image
var iconSpacing = 1;

// get width and height of first icon, assume all icons are the same size
var iconWidth  = 32;
var iconHeight = 34;

var xLoc = iconSpacing;
var yLoc = iconHeight + iconSpacing + iconSpacing;

function initTalentObj(jsonTalent)
{
	var tal = new TalentObj();

	tal.init(jsonTalent);

	return tal;
}

/*
// -- Talent obj def --
//   Note: init with initTalentObj() to automatically add to its corresponding school
*/
function TalentObj()
{
	this.init = function(jsonTalent)
	{
		this.id = jsonTalent.talent_id;
		this.school_id = jsonTalent.school_id;

		//this.img_on = jsonTalent.icon_name_on;
		//this.img_off = jsonTalent.icon_name_off;

		this.school = getSchoolById(this.school_id);	

		if (this.school != null)
		{
			//this.imgSrcOn = this.getImgPath(true);
			//this.imgSrcOff = this.getImgPath(false);
		}

		this.name = jsonTalent.name;
		this.description = jsonTalent.description;
		this.row = jsonTalent.tier;
		this.col = jsonTalent.col;		

		this.talent_prereq = jsonTalent.talent_prereq;
		this.school_prereq = jsonTalent.school_prereq;
		this.level_prereq = jsonTalent.level_prereq;

		this.active = false;

		this.rank = 0;
		this.rank_max = jsonTalent.ranks.length;

		this.ranks = new Array();
		for (var i=0; i<jsonTalent.ranks.length; i++)
			this.ranks[jsonTalent.ranks[i].rank] = jsonTalent.ranks[i];

		_talentsArray[this.id] = this;


		if (this.school != null)
			this.school.addTalent(this);
			
		// Setup the x/y location to pull the icon from
		this.xLoc = xLoc;
		this.yLoc = yLoc;
		
		// increment the global x value so the next request pulls from the correct spot		
		xLoc = xLoc + iconWidth + iconSpacing;
	}

/*
	this.getImgPath = function(isActive)
	{
		return Utils.computeImgPathRoot(CurrentClass.name_encd, this.school.name_encd) + "/" + (isActive ? this.img_on: this.img_off);
	}
*/
/*
	this.getPreloadImgsStr = function()
	{
		var imgPathOn = this.getImgPath(true);
		var imgPathOff = this.getImgPath(false);

		var s =  '<IMG SRC="' + imgPathOn  + '">'
			s += '<IMG SRC="' + imgPathOff + '">'

		return s;
	}
*/

	this.repaint = function()
	{

		var sid = Utils.getCellTalentId(this.school_id, this.row, this.col);

		var cell =  document.getElementById(sid);

		if (cell === null)
		{
			alert("Can't repaint talent [" + this.toString() + "]\nbecause its td [" + sid + "] wasn't found");
			return;
		}

		cell.innerHTML = this.getHtml();

		cell.setAttribute("tooltip", this.getTooltipHtml() );

		// just re-enable the events
		tooltip.initElement(cell);

		if ( Utils.isOpera() )
			this.repaintOperaRank()
	}


	this.repaintOperaRank = function()
	{
		setTimeout('repaintOperaRankAsynch("'+ this.getRankElId() +'")', 10);
	}

	this.toString = function()
	{
		var str = 'Talent[' + this.id + ', active: ' + this.active + ', '
			str += 'sc: ' + this.school_id + ', '
			str += this.row + ' x '+ this.col + ', '
			str += this.rank + '/' + this.rank_max + ']';

		return str;
	};

	this.getHtml = function()
	{
		var onClick = 'onmousedown = "onTalentClick(' + this.id + ', event);return true;" ';

		// note this doesn't fire in ff, nor does the onload event if the image is not found - bummer
//					var onError = 'onerror="this.src=\'missing.jpg\';return true;" ';

		var st = 'inactive';
		if (this.active) {
			
			this.yLoc = 1; // if talent is active set the y location to pull the icon from
			
			if (this.rank == 0)
				st = 'rank0';
			else
			if (this.rank < this.rank_max)
				st = 'rank1';
			else
				st = 'rank2';
		} else {
			 // if talent is inactive reset the y location to pull the icon from
			this.yLoc = iconHeight + iconSpacing + iconSpacing;;
		}
		
			
		// Create div for icon
		var div = '<div ';
		
		div += 'class="talent_img talent_border ' + st + '" ';

		div += onClick;
		div += '>';
		
			div += '<div ';
			
			//if (this.active) {  div += 'class="' + this.div_id_on + '" ';  }
			//else {				div += 'class="' + this.div_id_off + '" '; }
			
			//div += ' style="width: 32px; height: 34px;"></div>';
			div += ' style="';
				var divWidth = iconWidth - 1;
				var divHeight = iconHeight;
				div += 'width: '+ divWidth +'px; ';
				div += 'height: '+ divHeight +'px; ';
				div += 'background-image: url(http://images.tentonhammer.com/talentcalc/aoc/'+ CurrentClass['name_encd'] +'.jpg); ';
				div += 'background-position: -' + this.xLoc + 'px -' + this.yLoc + 'px;';
									
				//var iconSpacing = 1;
				//var iconWidth  = 29;
				//var iconHeight = 34;
				
			div += ' "></div>';
		
		div += '</div>';
		

		
		
		var str  = '<SPAN class="talent">'
			str += div;

		if ( this.rank_max != null && this.rank_max>0)
		{
			var rkSt = 'rank';
			if ( Utils.isIE() )
				rkSt = 'rank_alt';
			else
			if ( Utils.isOpera() )
				rkSt = 'rank_alt_2';

			str += '<SPAN class="' + rkSt + ' talent_border ' + st + '" '
			str += onClick

			if ( Utils.isOpera() )
				str += 'style="visibility: hidden;" '

			str += 'id="' + this.getRankElId() + '" '
			str += '>'+ this.rank +'/' + this.rank_max + '</SPAN>';
		}

		str += '</SPAN>'  ;

		return str;
	};


	this.getRankElId = function()
	{
		return 'rank_' + this.id;
	}

/*
	this.getImgUrl = function ()
	{
		return 'img/' + this.id + '_' + (this.active ? 'COLOR.jpg':'GRAY.jpg');
	};
*/

	this.getTooltipHtml = function()
	{
		var s = '';

		if (this.rank >0 )
		{

			var r = this.ranks[this.rank].details;
			r = Utils.strReplace(r, "\\n", "<BR>")
			r = Utils.strReplace(r, "\n", "<BR>")

			s += '<SPAN class="txtTitle">' + str_rank + ': </SPAN><SPAN class="txtCount"> ' + this.rank + '/'+ this.rank_max + '</SPAN>'
			s += '<P>' + r +  '</P>'

		}
		if (this.rank < this.rank_max)
		{
			var nr = this.ranks[this.rank+1].details;

			if (nr.length > 0)
			{
				nr = Utils.strReplace(nr, "\\n", "<BR>")
				nr = Utils.strReplace(nr, "\n", "<BR>")

				s+= '<SPAN class="txtTitle">' + str_nextRank + ': </SPAN>'
				s+= '<P>' + nr + '</P>'
			}
		}

		s+= '<SPAN class="txtTitle">' + str_description + ':</SPAN>'
		s+= '<P>' + this.description + '</P>'

		if (this.level_prereq >0)
			s+= '<SPAN class="txtTitle">' + str_levelReq + ': </SPAN><SPAN class="txtCount">' + this.level_prereq + '</SPAN>'
		if (this.school_prereq >0)
			s+= '<BR><SPAN class="txtTitle">' + str_schoolReq + ': </SPAN><SPAN class="txtCount">' + this.school_prereq + '</SPAN>'
		if (this.talent_prereq >0)
		{
			var tal = getTalentById(this.talent_prereq );
			if (tal != null)
				s+= '<BR><SPAN class="txtTitle">' + str_talentReq + ': </SPAN><SPAN class="txtCount">' + tal.name + '</SPAN>'
		}

		if (this.active) // nTODO - make sure that some dependancies don't prevent it to decrease
		{
			s+= '<BR>'
			if (this.rank < this.rank_max)
				s+= '<SPAN class="rkInc"><BR>' + str_learn + '</SPAN>'
			if (this.rank >0 )
			{
				if (Utils.isOpera())
					s+= '<SPAN class="rkDec"><BR>' + str_unlearnOpera + '</SPAN>'
				else
					s+= '<SPAN class="rkDec"><BR>' + str_unlearn + '</SPAN>'

			}
		}

		var str  = '<TABLE><TR class="txtTitleLrg"><TH>';
			str += this.name
			str += '</TH></TR><TR><TD>'
			str += s;
			str += '</TD></TR></TABLE>'
		return str;
	}

	this.changeRank = function(decrement)
	{
		if (decrement)
		{
			if (this.rank > 0)
				this.rank--;
			else
				return;

		} else {

			if (this.rank < this.rank_max &&   Utils.getPointsTotal() < CurrentGame.max_points)
				this.rank ++;
			else
				return;
		}

		this.repaint();
		this.validateSchool();
		this.updateSchoolPoints();
		this.updateSchoolSummary();
		matchPresetsList();
	}

	this.updateSchoolPoints = function()
	{
		if (this.school != null)
			this.school.updatePointsDisplay();
	}

	this.updateSchoolSummary = function()
	{
		if (this.school != null)
			this.school.updateSummary();
	}

	this.validateSchool = function()
	{
		if (this.school != null)
			this.school.validateTalents();
	}

	this.updateState = function(schoolPoints, forceRepaint)
	{
		var newact = this.validateTalentPrereq();
		newact &= this.validateSchoolPrereq(schoolPoints);

		if (newact != this.active)
		{
			this.active = newact;

			if ( !this.active )
			{
				this.rank = 0;
				this.updateSchoolPoints();
			}
			this.repaint();

		} else
		if (forceRepaint)
		{
			this.updateSchoolPoints();
			this.repaint();
		}
	}

	this.validateSchoolPrereq = function(schoolPoints)
	{
		return this.school_prereq <= schoolPoints;
	}
	
	this.validateTalentPrereq = function()
	{
		if (this.talent_prereq != 0)
		{
			var tr = getTalentById(this.talent_prereq);
			if (tr != null && tr.rank < tr.rank_max)
				return false;
		}
		return true;
	}

	//	school_prereq (num points required in school SchoolId to unlock the talent, or zero if none)
	//	talent_prereq (id of talent that must be maxed to unlock the talent, or zero if none)
	//	level_prereq (character level required, or zero if none -> not part of the locking / unlocking rules)
}
// -- Talent obj def --

function repaintOperaRankAsynch(rankElId)
{
	var rk = document.getElementById(rankElId);
	if (rk!= null)
		rk.style.visibility = 'visible';

}

function getTalentById(talentId)
{
	return _talentsArray[talentId]
}

/*
	left: event.button:
		ff: 0
		opera: 0
		safari: 0
		ie: 1

	right: event.button:
		ff: 2
		ie: 2
		safari: 2
*/
function isRightClick(event)
{
	if (2 == event.button)
		return true
	else
		return false;
}

function onTalentClick(talentId, event)
{
	var talent = getTalentById(talentId);
	if (talent != null && talent.active)
	{
		var decrement = isRightClick(event) || event.shiftKey || event.ctrlKey;
		talent.changeRank(decrement);
	}

}

