/**
 * Class used on the VehicleSearchResults page
 * Performs sorting and pagination functionality by submitting a hidden form in the page.
 * Also controls button states for the column headers.
*/

function ResultsHead(rowPointer, sortColumn, sortDirection){
	this.classNames = {"off": "sortHead", "over": "sortHeadOver", "on": "sortHeadOn", "disabled": "sortHeadDisabled"}
	this.formName = "";
	this.rowPointer = (rowPointer != null) ? rowPointer : 0;
	this.isDefaultSearch = ((sortColumn == "") && (sortDirection == "")) ? true : false;
	this.sortCol = (this.isDefaultSearch) ? "" : sortColumn;
	this.sortDir = (this.isDefaultSearch) ? "" : sortDirection;
	this.btnMan = new BtnStateManager();
}
ResultsHead.prototype.init = function(formName){
	this.formName = (formName != null) ? formName : document.forms.searchParamsForm;
	this.btnMan.setClassNames(this.classNames);
	this.btnMan.init((this.sortCol) ? "resultsHead_" + this.sortCol : "resultsHead_year");
}
ResultsHead.prototype.itemOut = function(id){
	this.btnMan.itemOut("resultsHead_" + id);
}
ResultsHead.prototype.itemOver = function(id){
	this.btnMan.itemOver("resultsHead_" + id);
}
ResultsHead.prototype.nextPage = function(incNum){
	this.rowPointer += incNum;
	this.setFormValues();
	this.submitForm();
}
ResultsHead.prototype.prevPage = function(incNum){
	this.rowPointer -= incNum;
	this.setFormValues();
	this.submitForm();
}
ResultsHead.prototype.sortBy = function(column){
	if (column == "" || column == null) return;
	// set new sort column, reset page back to page 1
	if (this.sortCol != column) {
		this.sortCol = column;
		this.rowPointer = 0;
	}
	// if still a default search and user sorted by year: assume current search is year/desc and switch to year/asc;
	// if sorting by new column, default to: year/desc or everything else/asc;
	// if same column, swap sort direction
	if (this.isDefaultSearch && (column == "year")) this.sortDir = "asc";
	else if (this.sortCol != column) this.sortDir = (column == "year") ? "desc" : "asc";
	else this.sortDir = (this.sortDir == "asc") ? "desc" : "asc";
	this.setFormValues();
	this.submitForm();
}
ResultsHead.prototype.setFormValues = function(){
	this.formName.rowPointer.value = this.rowPointer;
	this.formName.sortDir.value = this.sortDir;
	this.formName.sortCol.value = this.sortCol;
}
ResultsHead.prototype.submitForm = function(){
	this.formName.submit();
}

