var quote='"';
// -------------------------------------------------------------------------------
// ToolTips
// -------------------------------------------------------------------------------
function showToolTip(description) {
	document.getElementById("info_contents_inner").innerHTML=description;
	document.getElementById("info").style.visibility="visible";
}

function releaseToolTip() {
}

function hideToolTip() {
	document.getElementById("info").style.visibility="hidden";
	document.getElementById("info_contents_inner").innerHTML="";
}

// -------------------------------------------------------------------------------
// Item Container Contents
// -------------------------------------------------------------------------------
function showItemContainerContents(description) {
	document.getElementById("item_container_contents_contents").innerHTML=description;
	document.getElementById("item_container_contents").style.visibility="visible";
}

function hideItemContainerContents() {
	document.getElementById("item_container_contents").style.visibility="hidden";
	document.getElementById("item_container_contents_contents").innerHTML="";
}

// -------------------------------------------------------------------------------
// More Links
// -------------------------------------------------------------------------------
var moreLinkURL="http://eqplayers.station.sony.com";

function goToMoreLink() {
	window.open(moreLinkURL);
}

function setMoreLink(newMoreLink) {
	moreLinkURL=newMoreLink;
}

// -------------------------------------------------------------------------------
// Generic Display Functions
// -------------------------------------------------------------------------------

var currentAsc=true;
var currentField="title";
var currentFlag="General";

function displayDataByProperty(sortField) {
	if (sortField==currentField) {
		currentAsc=!(currentAsc);
	} else {
		currentField=sortField;
		currentAsc=true;
	}
	generic_array_to_innerhtml("DataField", currentField, currentAsc, currentFlag, DataList);
}

function displayDataByFlag(flag) {
	currentFlag=flag;
	generic_array_to_innerhtml("DataField", currentField, currentAsc, currentFlag, DataList);
}

function initDataDisplay(defaultAsc, defaultField, defaultFlag) {
	currentAsc=defaultAsc;
	currentField=defaultField;
	currentFlag=defaultFlag;
}

// -------------------------------------------------------------------------------
// Generic Sorting Algorithms
// -------------------------------------------------------------------------------

function generic_sort_class_by_property(firstInstance, secondInstance, propertyName) {
	if (!firstInstance) { return 0; }
	if (!secondInstance) { return 0; }
	
	value1=firstInstance.getProperty(propertyName);
	value2=secondInstance.getProperty(propertyName);
	
	if ( (typeof value1 == "string") && (typeof value1 == "string") )
	{
		value1=value1.toUpperCase();
		value2=value2.toUpperCase();
	}

	if (value1 == value2) 		{ return 0; }
	else if (value1 > value2) 	{ return 1; }
	else 						{ return -1; }
}

function generic_sort_class_by_property_desc(firstInstance, secondInstance, property) {
	return (generic_sort_class_by_property(firstInstance, secondInstance, property)*-1);
}

function generic_sort_class(firstInstance, secondInstance, property, ascending) {
	if (ascending) { return generic_sort_class_by_property(firstInstance, secondInstance, property); }
	else { return generic_sort_class_by_property_desc(firstInstance, secondInstance, property); }
}

// -------------------------------------------------------------------------------
// Generic class methods
// -------------------------------------------------------------------------------

function generic_hasFlag(flagName) {
	return this.flag[flagName];
}

function generic_property(property) {
	return this.property[property];
}

function generic_sort_by_title_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "title", true); }
function generic_sort_by_title_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "title", false); }
function generic_sort_by_current_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "current", true); }
function generic_sort_by_current_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "current", false); }
function generic_sort_by_maxVal_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "maxVal", true); }
function generic_sort_by_maxVal_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "maxVal", false); }
function generic_sort_by_cost_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "cost", true); }
function generic_sort_by_cost_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "cost", false); }
function generic_sort_by_level_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "level", true); }
function generic_sort_by_level_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "level", false); }
function generic_sort_by_category_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "category", true); }
function generic_sort_by_category_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "category", false); }
function generic_sort_by_server_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "server", true); }
function generic_sort_by_server_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "server", false); }
function generic_sort_by_serverwide_asc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "serverwide", true); }
function generic_sort_by_serverwide_desc(firstInstance, secondInstance) { return generic_sort_class(firstInstance, secondInstance, "serverwide", false); }

// -------------------------------------------------------------------------------
// Generic sort, filter and render HTML methods
// -------------------------------------------------------------------------------

function generic_array_to_html(sortBy, asc, requiredFlag, source_array) {
	// ---------------------------------------------------------
	// Build the filtered list so we're sorting a smaller array
	filteredList=new Array()
	if (requiredFlag.length==0) {
		filteredList=source_array;
	} else {
		count=0;
		for (i=0; i < source_array.length; i++) {
			if (source_array[i].hasFlag(requiredFlag)) {
				filteredList[count]=source_array[i];
				count=count+1;
			}
		}
	}

	// ---------------------------------------------------------
	// Sort the filtered list
	switch (sortBy) {
		case "title" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_title_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_title_desc); }
			break;
		case "current" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_current_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_current_desc); }
			break;
		case "maxVal" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_maxVal_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_maxVal_desc); }
			break;
		case "cost" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_cost_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_cost_desc); }
			break;
		case "level" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_level_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_level_desc); }
			break;
		case "category" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_category_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_category_desc); }
			break;
		case "server" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_server_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_server_desc); }
			break;
		case "serverwide" :
			if (asc) { sortedList=filteredList.sort(generic_sort_by_serverwide_asc); }
			else { sortedList=filteredList.sort(generic_sort_by_serverwide_desc); }
			break;
		default:
			sortedList=filteredList;
			break;
	}
	
	// ---------------------------------------------------------
	// Build the output
	generic_array_to_html_result="";
	generic_array_to_html_result=generic_array_to_html_result.concat("<table border='0' cellspacing='0'>");
	for (i=0; i<sortedList.length; i++) {
		generic_array_to_html_result=generic_array_to_html_result.concat(sortedList[i].html());
	}
	generic_array_to_html_result=generic_array_to_html_result.concat("</table>");
	return generic_array_to_html_result;
}

function generic_array_to_innerhtml(target_id, sortBy, asc, requiredFlag, source_array) {
	document.getElementById(target_id).innerHTML=generic_array_to_html(sortBy, asc, requiredFlag, source_array);
}

// -------------------------------------------------------------------------------
// AA Class
// -------------------------------------------------------------------------------

function AA(title, description, current, maxVal, cost, flagName, flagValue) {
	this.title=title;
	
	this.property=new Array();
	this.property["title"]=title;
	this.property["description"]=description;
	this.property["current"]=current;
	this.property["maxVal"]=maxVal;
	this.property["cost"]=cost;
	
	this.flag=new Array();
	
	if (flagName.length > 0) {
		this.flag[flagName]=flagValue;
	}
}

function AA_html() {
	AA_html_result="<tr><td valign='top' class='AATitle'><div><a href='#' onClick='showToolTip(\""+this.property["description"]+"\"); setMoreLink(\"http://eqplayers.station.sony.com/character_aa.vm?characterId="+characterId+"\"); return false;'>"+this.property["title"]+"</a></div></td>";
	AA_html_result=AA_html_result+"<td valign='top' class='AACurrent'><div>"+this.property["current"]+" /</div></td>";
	AA_html_result=AA_html_result+"<td valign='top' class='AAMax'><div>"+this.property["maxVal"]+"</div></td>";
	AA_html_result=AA_html_result+"<td valign='top' class='AACost'><div>"+this.property["cost"]+"</div></td></tr>";
	
	return AA_html_result;
}

AA.prototype.hasFlag=generic_hasFlag;
AA.prototype.getProperty=generic_property;
AA.prototype.html=AA_html;

// -------------------------------------------------------------------------------
// Skill Class
// -------------------------------------------------------------------------------
function Skill(title, description, current, maxVal) {
	this.property=new Array();
	this.property["title"]=title;
	this.property["description"]=description;
	this.property["current"]=current;
	this.property["maxVal"]=maxVal;
}

function Skill_html() {
	Skill_html_result="<tr><td valign='top' class='SkillTitle'><div>"+this.property["title"]+"</div></td>";
	Skill_html_result=Skill_html_result+"<td valign='top' class='SkillCurrent'><div>"+this.property["current"]+" /</div></td>";
	Skill_html_result=Skill_html_result+"<td valign='top' class='SkillMax'><div>"+this.property["maxVal"]+"</div></td></tr>";
	return Skill_html_result;
}

Skill.prototype.getProperty=generic_property;
Skill.prototype.html=Skill_html;

// -------------------------------------------------------------------------------
// Spell Class
// -------------------------------------------------------------------------------
function Spell(title, description, level, category, flagHas, flagAvailable, flagAcquirable) {
	this.property=new Array();
	this.property["title"]=title;
	this.property["description"]=description;
	this.property["level"]=level;
	this.property["category"]=category;
	
	this.flag=new Array();
	this.flag["Has"]=flagHas;
	this.flag["Available"]=flagAvailable;
	this.flag["Acquirable"]=flagAcquirable;
}

function Spell_html() {
	Spell_html_result="<tr><td valign='top' class='SpellLevel'><div>"+this.property["level"]+"</div></td>";
	Spell_html_result=Spell_html_result+"<td valign='top' class='SpellTitle'><div><a href='#' onClick='showToolTip(\""+this.property["description"]+"\"); setMoreLink(\"http://eqplayers.station.sony.com/my_spells.vm?characterId="+characterId+"\"); return false;'>"+this.property["title"]+"</a></div></td>";
	Spell_html_result=Spell_html_result+"<td valign='top' class='SpellCategory'><div>"+this.property["category"]+"</div></td></tr>";
	return Spell_html_result;
}

Spell.prototype.getProperty=generic_property;
Spell.prototype.hasFlag=generic_hasFlag;
Spell.prototype.html=Spell_html;

// -------------------------------------------------------------------------------
// KeyFlag Class
// -------------------------------------------------------------------------------

function KeyFlag(title, flagName, flagValue) {
	this.title=title;
	
	this.property=new Array();
	this.property["title"]=title;
	
	this.flag=new Array();
	
	if (flagName.length > 0) {
		this.flag[flagName]=flagValue;
	}
}

function KeyFlag_html() {
	KeyFlag_html_result="<tr><td valign='top' class='KeyFlagTitle'><div>"+this.property["title"]+"</div></td><tr>";
	
	return KeyFlag_html_result;
}

KeyFlag.prototype.hasFlag=generic_hasFlag;
KeyFlag.prototype.getProperty=generic_property;
KeyFlag.prototype.html=KeyFlag_html;

// -------------------------------------------------------------------------------
// Achievement Class
// -------------------------------------------------------------------------------

function Achievement(title, current, server, serverwide, flagName, flagValue) {
	this.title=title;
	
	this.property=new Array();
	this.property["title"]=title;
	this.property["current"]=current;
	this.property["server"]=server;
	this.property["serverwide"]=serverwide;
	
	this.flag=new Array();
	
	if (flagName.length > 0) {
		this.flag[flagName]=flagValue;
	}
}

function Achievement_html() {
	Achievement_html_result="<tr><td valign='top' class='AchievementTitle'><div>"+this.property["title"]+"</div></td>";
	Achievement_html_result=Achievement_html_result+"<td valign='top' class='AchievementCurrent'><div>"+this.property["current"]+"</div></td>";
	Achievement_html_result=Achievement_html_result+"<td valign='top' class='AchievementServer'><div>"+this.property["server"]+"</div></td>";
	Achievement_html_result=Achievement_html_result+"<td valign='top' class='AchievementServerwide'><div>"+this.property["serverwide"]+"</div></td>";
	return Achievement_html_result;
}

Achievement.prototype.hasFlag=generic_hasFlag;
Achievement.prototype.getProperty=generic_property;
Achievement.prototype.html=Achievement_html;