var pfc = {
	changeTab: function(info_id, tab_id)
	{
		$j(".pfc_info").hide();
		$j("#"+info_id).show();
		
		$j(".tab").removeClass("activeTab");
		$j("#"+tab_id).addClass("activeTab");		
	},
	enableTargetRaces: function(value, target_selectID)
	{
		pfc.showPairLabel(true);
		pfc.printTranslationTable(pfc.getRaces().sort(pfc.sortFactions));
		
		pfc.showMountPairLabel(true);
		pfc.printMountTranslationTable(pfc.getMounts());					
		
		if(value == "-1"){
			document.getElementById(target_selectID).disabled = true;
			document.getElementById(target_selectID).value = "-1";
			
		}else{			
			document.getElementById(target_selectID).disabled = false;
			document.getElementById(target_selectID).value = "-1";
		}
	},
	showPairLabel: function(isDefault)
	{	
		if(isDefault){
			document.getElementById('defaultFaction').style.display = "block";
			document.getElementById('selectedFaction').style.display = "none";			
		}else{
			document.getElementById('defaultFaction').style.display = "none";
			document.getElementById('selectedFaction').style.display = "block";
			
			var hordeFactionSelect = document.getElementById('hordeRace_Reps');
			var allianceFactionSelect = document.getElementById('allianceRace_Reps');
			
			document.getElementById('hordeFaction').innerHTML = hordeFactionSelect.options[hordeFactionSelect.selectedIndex].text
			document.getElementById('allianceFaction').innerHTML = allianceFactionSelect.options[allianceFactionSelect.selectedIndex].text
		}
	},
	showMountPairLabel: function(isDefault)
	{	
		if(isDefault){
			document.getElementById('defaultMounts').style.display = "block";
			document.getElementById('selectedMounts').style.display = "none";			
		}else{
			document.getElementById('defaultMounts').style.display = "none";
			document.getElementById('selectedMounts').style.display = "block";
			
			var hordeMountSelect = document.getElementById('hordeRace_Mounts');
			var allianceMountSelect = document.getElementById('allianceRace_Mounts');
			
			document.getElementById('hordeMounts').innerHTML = hordeMountSelect.options[hordeMountSelect.selectedIndex].text
			document.getElementById('allianceMounts').innerHTML = allianceMountSelect.options[allianceMountSelect.selectedIndex].text
		}
	},
	calcFactionTranslation : function(targetRaceId, current_selectID)
	{			
		if(targetRaceId == "-1")
		{			
			pfc.showPairLabel(true);
			pfc.printTranslationTable(pfc.getRaces().sort(pfc.sortFactions));
		}
		else
		{
			//get current race
			var currentRaceId = document.getElementById(current_selectID).value.split("-")[0];
			
			//create a copy of the races array to preserve orginal translation
			var tempRaces = pfc.getRaces();
			var races = pfc.getRaces();
							
			
			if(tempRaces[currentRaceId].targetFaction != races[targetRaceId].currentFaction){
				
				//factions for current race become what was picked in dropdown
				tempRaces[currentRaceId].targetFaction = races[targetRaceId].currentFaction;
				tempRaces[targetRaceId].targetFaction = races[currentRaceId].currentFaction;
				
				//update ones that were affected
				tempRaces[races[targetRaceId].targetId].targetFaction = races[races[currentRaceId].targetId].currentFaction;
				tempRaces[races[currentRaceId].targetId].targetFaction = races[races[targetRaceId].targetId].currentFaction;
				
				//flag object as changed
				tempRaces[currentRaceId].changed = true;
				tempRaces[races[targetRaceId].targetId].changed = true;
			
				pfc.showPairLabel(false);
			}else{
				pfc.showPairLabel(true);				
			}
						
			pfc.printTranslationTable(tempRaces.sort(pfc.sortFactions));
		}
	},
	calcMountTranslation : function(targetRaceId, current_selectID)
	{			
		if(targetRaceId == "-1")
		{
			pfc.showMountPairLabel(true);
			pfc.printMountTranslationTable(pfc.getMounts());					
		}
		else
		{
			//get current race
			var currentRaceId = document.getElementById(current_selectID).value.split("-")[0];
			
			//create a copy of the races array to preserve orginal translation
			var tempMounts = pfc.getMounts();
			var mounts = pfc.getMounts();
							
			//factions for current race become what was picked in dropdown
			var currentMounts = mounts[currentRaceId];
			var targetMounts = mounts[targetRaceId];
	
			for(var m = 0; m < currentMounts.length; m++)
			{
				//assign all the current mounts to the target mounts
				if(tempMounts[currentRaceId][m].targetName != targetMounts[m].name || (tempMounts[currentRaceId][m].targetName.indexOf("*") != -1) || (targetMounts[m].name.indexOf("*") != -1))
				{
					tempMounts[currentRaceId][m].targetName = targetMounts[m].name;
					tempMounts[targetRaceId][m].targetName  = currentMounts[m].name;
					
					//update affected mounts
					tempMounts[currentMounts[m].targetRaceId][m].targetName = tempMounts[targetMounts[m].targetRaceId][m].name;
					tempMounts[targetMounts[m].targetRaceId][m].targetName = tempMounts[currentMounts[m].targetRaceId][m].name;
					
					tempMounts[currentRaceId][m].changed = true;
					tempMounts[targetMounts[m].targetRaceId][m].changed = true;
				}
			}
			
			pfc.showMountPairLabel(false);
			pfc.printMountTranslationTable(tempMounts);
		}
	},
	printTranslationTable: function(raceArray)
	{
		//make table to display results				
		var tableBody = document.getElementById('cityFactionsTableBody');
				
		//remove children
		while(tableBody.hasChildNodes()){
			tableBody.removeChild(tableBody.firstChild);
		}
		
		//counter for row class				
		var ctr = 0;
									
		for(var r in raceArray)
		{				
			if(raceArray[r].factionId == '0')
			{
				var row = document.createElement('tr');
				
				row.className = (ctr % 2 == 0) ? 'alt' : ''; 
				
				var col1 = document.createElement('td');
				col1.innerHTML = raceArray[r].currentFaction;
				col1.className = 'leftCol';
				
				var col2 = document.createElement('td');
				var arrow = document.createElement('span');
				arrow.className = "translationArrow";						 
				col2.appendChild(arrow);
				
				var col3 = document.createElement('td');
				col3.innerHTML = raceArray[r].targetFaction;
				col3.className = 'rightCol';
				
				if(raceArray[r].changed){
					col1.style.fontWeight = "bold";
					col3.style.fontWeight = "bold";
				}
				
				row.appendChild(col1);
				row.appendChild(col2);
				row.appendChild(col3);
				
				tableBody.appendChild(row);
				
				ctr++;
			}
		}
	},
	printMountTranslationTable: function(mountArray)
	{
		mountArray = mountArray.sort(pfc.sortMounts);
		
		//make table to display results				
		var tableBody = document.getElementById('cityMountsTableBody');
		
		//remove children
		while(tableBody.hasChildNodes()){
			tableBody.removeChild(tableBody.firstChild);
		}
		
		//counter for row class				
		var ctr = 0;
									
		for(var raceId in mountArray)
		{
			for(var mount=0; mount < mountArray[raceId].length; mount++)
			{
				try{
					if(mountArray[raceId][mount].factionId == '0' && (mountArray[raceId][mount].name != mountArray[raceId][mount].targetName))						
					{						
						var row = document.createElement('tr');
					
						row.className = (ctr % 2 == 0) ? 'alt' : '';
						
						var col1 = document.createElement('td');
						col1.innerHTML = mountArray[raceId][mount].name;
						col1.className = 'leftCol';
						
						var col2 = document.createElement('td');
						var arrow = document.createElement('span');
						arrow.className = "translationArrow";						 
						col2.appendChild(arrow);
						
						var col3 = document.createElement('td');
						col3.innerHTML = mountArray[raceId][mount].targetName;
						col3.className = 'rightCol';
						
						if(mountArray[raceId][mount].changed){
							col1.style.fontWeight = "bold";
							col3.style.fontWeight = "bold";
						}
					
						row.appendChild(col1);
						row.appendChild(col2);
						row.appendChild(col3);
						
						tableBody.appendChild(row);
					
						ctr++;
					}
				}catch(e){}
			}
		}
	},
	sortFactions: function(a, b)
	{		
		return (a.currentFaction > b.currentFaction);
	},
	sortMounts: function(a, b)
	{
		return ((a.raceId*1) > (b.raceId*1));	
	}	
}
