
// jQuery used because it's already running, and to avoid messing with
// closures

// full reference avoids conflicts; $(obj) was tried and failed

// $(window).load preferred to $(document).ready because Safari cache
// bitchiness was originally diagnosed as Safari DOM API bitchiness

jQuery(window).load(function() {

// stores actual salary numbers and index
var origSalaries = [];

// stores sorted salary numbers keyed to to (val % 2 == 0)
var sortedSalaries = [];

// takes the element of origSalaries being sorted via .shift()
var currentComparison = [];

// pointer for salary currently being put into origSalaries (or not)
var currentSalary = 0;

// takes the cloneNode return value from original rows
var newRow;

// serves as reference to working table body
var refVar;

// stores the actual salary number to which commas are currently being added
var figureForCommas = "";

// stores individual digits of salary number to which commas are currently
// being added
var figureWithCommas = [];

// stores reconstructed salary with commas for eventual assignment to
// .innerHTML
var finalText = "_";

var isIElt8 = 4;

// most browsers seem to insert a fourth row in each set of job listing
// records, except...

if ((window.clientInformation) && (document.createElement('canvas').getContext == null)) {
	isIElt8 = 3;
}

for (var i = 0; i < document.getElementsByTagName("span").length; i++) {

// focus on the elements that have actual salary numbers in them;
// $(".salaryFigure") was returning zero elements

	if (document.getElementsByTagName("span")[i].className == "salaryFigure") {

// we're only actually interested in the value (low or high) by which we're
// going to sort

		if ((currentSalary % 2) === 0) {

// take the salary number and put it into an array along with a usable index

			origSalaries[origSalaries.length] = [];
			if (!isNaN(document.getElementsByTagName("span")[i].innerHTML)) {
				origSalaries[(origSalaries.length - 1)][0] = (document.getElementsByTagName("span")[i].innerHTML - 0);
			} else {
				origSalaries[(origSalaries.length - 1)][0] = 0;
			}			
			origSalaries[(origSalaries.length - 1)][1] = Math.round((currentSalary / 2));
		}

// we can't use i because that refers to the superset of all spans in the
// document

		currentSalary++;
	}
};

// create an array of sorted salaries

while (origSalaries.length > 0) {

// take a number off the array of "originals"

	currentComparison = origSalaries.shift();

// need to explicitly add it to the empty array, else a for loop wouldn't
// work

	if (sortedSalaries.length === 0) {
		sortedSalaries[0] = currentComparison;
		continue;
	}

// stick the value in front of the next lowest item...

	for (var i = 0; i < sortedSalaries.length; i++) {
		if (currentComparison[0] >= sortedSalaries[i][0]) {
			sortedSalaries.splice(i,0,currentComparison);
			break;
		}

// ...and if it's THE lowest item, stick it on the end

		else {
			if (i == (sortedSalaries.length - 1)) {
				sortedSalaries.splice(sortedSalaries.length,0,currentComparison);
				break;
			}
		}
	}
}

// avoid confusion between the job list and job description

if ((!document.getElementById("fullDescription")) && (document.getElementById("titlesAndSummaries"))) {

// limit the scope of DOM manipulation to the table that displays the listings
// and pass the scope by reference for the sake of brevity

	refVar = document.getElementById("titlesAndSummaries").getElementsByTagName("tbody")[0];

// i refers to the baseline index stored in sortedSalaries along with the
// actual salary figures, j to the neighboring rows that are associated with
// them

	for (var i = 0; i < sortedSalaries.length; i++) {

// there's an off-by one in here somewhere - the baseline index should demand  (j = -1;
// j < 2 ...) but this was what actually worked

		for (var j = 0; j < 3; j++) {

// clone the original row based on the index in sortedSalaries[i][1] and add it
// to the end of the table body

			newRow = refVar.getElementsByTagName("tr")[((sortedSalaries[i][1] * 3) + j)].cloneNode(true);
			refVar.appendChild(newRow);
		}
	}

// just because it might help

	refVar.normalize();

// go through the original rows and clobber them

	for (var i = 0; i < sortedSalaries.length; i++) {

// for some reason there's a fourth phantom row attached to each record?
// (see var is IElt8 above)

		for (var j = 0; j < isIElt8; j++) {
			refVar.removeChild(refVar.firstChild);
		}
	}
}

// reassign refVar with some precision, because we need to perfrom the
// following operation on both the job list and each job description

if (document.getElementById("titlesAndSummaries")) {
	refVar = document.getElementById("titlesAndSummaries").getElementsByTagName("tbody")[0];
}
else {
	if (document.getElementById("fullDescription")) {
		refVar = document.getElementById("fullDescription").getElementsByTagName("tbody")[0];
	}
}
if (refVar == null) {

// assign it to an element with a short and empty tree so that the
// loop will terminate quickly

		refVar = document.getElementsByTagName("li")[0];
}

// same song, second verse, except this time we're immediately interested in
// each one

for (var i = 0; i < refVar.getElementsByTagName("span").length; i++) {

// now we're working on the new rows
// cf about jQuery class retrieval issue above
// address each salary number, one at a time

	if (refVar.getElementsByTagName("span")[i].className == "salaryFigure") {

// grab the salary number as an actual number

		figureForCommas = parseInt(refVar.getElementsByTagName("span")[i].innerHTML);

// put each digit into an array element

		for (var j = 0; j < figureForCommas.toString(10).length; j++) {
			figureWithCommas[figureWithCommas.length] = figureForCommas.toString(10).substring(j,(j + 1));
		}

// walk through the array, first adding a trailing digit, then...

		for (var j = (figureWithCommas.length - 1); j > -1; j--) {
			if (j == (figureWithCommas.length - 1)) {
				finalText += figureWithCommas[j];
			}

// push a new digit into the (working) bottom of the stack...

			else {
				finalText = (finalText.substring(0,1) + figureWithCommas[j] + finalText.substring(1));
			}

// and push a comma onto there if we're at the right point in the process

			if (((j % 3) === (figureWithCommas.length % 3)) && (j < (figureWithCommas.length - 1)) && (j > 0)) {
				finalText = (finalText.substring(0,1) + "," + finalText.substring(1));
			}
		}

// replace with the commafied value and reset the placeholder variables

		refVar.getElementsByTagName("span")[i].innerHTML = finalText.substring(1);
		figureWithCommas = [];
		finalText = "_";
	}
}
				
});
