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)
	{	
		//Remove the listing and make them pick a target race
		pfc.showPairLabel(true);
		pfc.printMountTranslationTable(pfc.getRaces().sort(pfc.sortFactions));		
		
		 var selectedFaction = 0;
		
    if(value == "1" || value == "3" || value == "4" || value == "7" || value == "11"){
      selectedFaction = 1;
      var allianceRaceSelect = document.getElementById('allianceRace_Reps');
      var targetSelect = document.getElementById(target_selectID);
      var targetSelectSize = targetSelect.options.size;
    
      for(i = targetSelect.length - 1; i >= 0; i--){
        targetSelect.remove(i);                
      }
      
      var count = 0;
      for(i = 0; i < allianceRaceSelect.options.length; i++){
        if(allianceRaceSelect.options[i].value != value){
          var temp = allianceRaceSelect.options[i].cloneNode(true);
          if(targetSelectSize == 0){
            targetSelect.add(new Option(temp.label, temp.value));
          }else{
            targetSelect.options[count] = temp;
          }
          count++;
        }
      }
      
    } else {
      selectedFaction = 0;
      
      var hordeRaceSelect = document.getElementById('hordeRace_Reps');
      var targetSelect = document.getElementById(target_selectID);
      var targetSelectSize = targetSelect.options.size;
    
      for(i = targetSelect.length - 1; i >= 0; i--){
        targetSelect.remove(i);                
      }
      
      count = 0;
      for(i = 0; i < hordeRaceSelect.options.length; i++){
        if(hordeRaceSelect.options[i].value != value){
           var temp = hordeRaceSelect.options[i].cloneNode(true);
          if(targetSelectSize == 0){
            targetSelect.add(new Option(temp.label, temp.value));
          }else{
            targetSelect.options[count] = temp;
          }
          count++;
        }
      }
    }
    
						
		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 targetRaceSelect = document.getElementById('allianceRace_Reps');
			
			document.getElementById('sourceRace').innerHTML = hordeFactionSelect.options[hordeFactionSelect.selectedIndex].text
			document.getElementById('targetRace').innerHTML = targetRaceSelect.options[targetRaceSelect.selectedIndex].text
		}
	},
	showMountPairLabel: function(isDefault)
	{	
		if(isDefault){
						
		}else{
			
			var hordeMountSelect = document.getElementById('targetRace');
			var allianceMountSelect = document.getElementById('targetRace_Mounts');
			
		}
	},
	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
		{
		  var selectedFaction = -1
      if(targetRaceId == "1" || targetRaceId == "3" || targetRaceId == "4" || targetRaceId == "7" || targetRaceId == "11"){
        selectedFaction = 0;
      } else {
        selectedFaction = 1;
      }
      
			//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, selectedFaction);
		}
	},
	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, selectedFaction)
	{
	
  	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 == selectedFaction && (mountArray[raceId][mount].name != mountArray[raceId][mount].targetName))						
					{	
          				   
            if(mountArray[raceId][mount].changed){		
			   
  						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';  						
						
            	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));	
	}	
}