// JavaScript Document
var applications = {
	siteCode: 'KN',
	SID: '',
	requests: 0,
	orderCol: 'name',
	orderDir: 'asc',
	
	init: function (newSiteCode, newSID, loadParams) {
		this.siteCode = newSiteCode;
		this.SID = newSID;
		$('noJava').remove();
		$$('a').invoke('observe', 'click', function (s) {
			window.location.hash = $('applicationControls').serialize() + '&orderCol=' + applications.orderCol + '&orderDir=' + applications.orderDir;
		});
		if (loadParams) {
			this.loadControls(loadParams);
		}
		else {
			this.loadControls('');
		}
	},
	
	loadControls: function (params) {
		$('controls').getElementsBySelector('select, input').invoke('enable');
		$('controls').getElementsBySelector('select').invoke('observe', 'change', function (t) {
			applications.updateControls(Event.findElement(t, 'select').id);
		});
		$('searchString').observe('keyup', function(t) {applications.updateString();});
		$('allButton').observe('click', function (t) {applications.showAll()});
		$('resetButton').observe('click', function (t) {applications.resetForm();});
		$('applicationControls').observe('submit', function (e) {Event.stop(e);});
		$$('a.infoLink').invoke('observe', 'click', function (t) {
			popupWindow(Event.findElement(t, 'a').href, '', '390', '252', '', '', '', '');
			Event.stop(t);
		});
		
		if (params != '') {
			var test = params.toQueryParams();
			if (test.type && test.type != -1) {
				var type = $('type');
				for (i = 0; i < type.options.length; i++) {
					if (type.options[i].value == test.type) {
						type.selectedIndex = i;
					}
				}
			}
			if (test.industry && test.industry != -1) {
				var industry = $('industry');
				for (i = 0; i < industry.options.length; i++) {
					if (industry.options[i].value == test.industry) {
						industry.selectedIndex = i;
					}
				}
			}
			if (test.taskList && test.taskList != -1) {
				var task = $('taskList');
				for (i = 0; i < task.options.length; i++) {
					if (task.options[i].value == test.taskList) {
						task.selectedIndex = i;
					}
				}
			}
			if (test.searchString && test.searchString != '') {
				$('searchString').value = test.searchString;
				
			}
			if (test.orderCol) {
				applications.orderCol = test.orderCol;
			}
			
			if (test.orderDir) {
				applications.orderDir = test.orderDir;
			}
			applications.getResults();
		}
		$('results').getElementsBySelector('th.sortable').each(function (t) {
			t.addClassName('sortHead');
			t.observe('click', function (s) {
				var order = Event.findElement(s, 'th').id;
				order = order.substring(0, order.length - 3);
				$(applications.orderCol + 'Col').removeClassName('sort' + applications.orderDir);
				if (applications.orderCol == order) {
					if (applications.orderDir == 'asc') {
						applications.orderDir = 'desc';
					}
					else {
						applications.orderDir = 'asc';
					}
				}
				else {
					applications.orderCol = order;
				}
				$(applications.orderCol + 'Col').addClassName('sort' + applications.orderDir);
				applications.getResults();
			});
		});
	},
	
	updateControls: function (element) {
		$('searchString').value = '';
		if (element == 'taskList') {
			applications.getResults();
		}
		else {
			var params = $('applicationControls').serialize() + '&site=' + this.siteCode + '&control=' + element;
			new Ajax.Request('controls.php', {
				method: 'get',
				parameters: params,
				onSuccess: function (t) {
					t = t.responseText.evalJSON(true);
					if (t.industry) {
						$('industryWrap').update(t.industry);
						$('industry').observe('change', function (t) {
							applications.updateControls('industry');
						});
					}
					if (t.taskList) {
						$('taskWrap').update(t.taskList);
						$('taskList').observe('change', function (t) {
							applications.updateControls('taskList');
						});
					}
					applications.getResults();
				}
			});
		}
	},
	
	updateString: function () {
		$('controls').getElementsBySelector('select').each(function (t) {
			t.selectedIndex = 0;
		});
		applications.getResults();
	},
	
	resetForm: function () {
		$('controls').getElementsBySelector('select').each(function (t) {
			t.selectedIndex = 0;
		});
		applications.resetControls();
		$('searchString').value = '';
		$('resultsTable').update();
		$('results').addClassName('initial');
		$('spacing').show();
		$('message1').update('Currently ');
		$('message2').update('are available.');
		$('totalNum').addClassName('initial');
		$('redTotal').update($('totalNum').innerHTML);
	},
	
	showAll: function () {
		applications.resetControls();
		$('controls').getElementsBySelector('select').each(function (t) {
			t.selectedIndex = 0;
		});
		$('searchString').value = '';
		applications.getResults();
	},
	
	resetControls: function () {
		var params = 'type=-1&industry=-1&taskList=-1&searchString=&site=' + this.siteCode + '&control=type';
		new Ajax.Request('controls.php', {
				method: 'get',
				parameters: params,
				onSuccess: function (t) {
					t = t.responseText.evalJSON(true);
					if (t.industry) {
						$('industryWrap').update(t.industry);
						$('industry').observe('change', function (t) {
							applications.updateControls('industry');
						});
					}
					if (t.taskList) {
						$('taskWrap').update(t.taskList);
						$('taskList').observe('change', function (t) {
							applications.updateControls('taskList');
						});
					}
				}
			});
	},
	
	getResults: function () {
		var params = $('applicationControls').serialize() + '&site=' + this.siteCode + '&orderCol=' + this.orderCol + '&orderDir=' + this.orderDir + '&SID=' + this.SID;
		applications.requests = applications.requests + 1;
		new Ajax.Request ('results.php', {
			method: 'get',
			parameters: params,
			onSuccess: function (t) {
				applications.requests = applications.requests - 1;
				if (applications.requests == 0 && t.responseText != '') {
					$('spacing').hide();
					$('resultsTable').update(t.responseText);
					if ($('results').hasClassName('initial')) {
						$('results').removeClassName('initial');
					}
					$('resultsTable').getElementsBySelector('a.imagePop').invoke('observe', 'click', function (s) {
						popupWindow(Event.findElement(s, 'a').href, '', '390', '252', '', '', '', '');
						Event.stop(s);
					});
					applications.updateCrumbs();
				}
				else if (applications.requests == 0 && $F('searchString').length > 0) {
					$('searchWarning').update("Your query didn't return any results. Please try again.");
				}
			}
		});
	},
	
	updateCrumbs: function () {
		if ($F('searchString').length > 0) {
			$('crumbs').update('Search query: ' + $F('searchString'));
		}
		else if ($F('type') != -1 || $F('industry') != -1 || $F('taskList') != -1) {
			var f = false;
			var newCrumbs = '';
			if ($F('type') != -1) {
				newCrumbs = newCrumbs + $('type').options[$('type').selectedIndex].text;
				f = true;
			}
			if ($F('industry') != -1) {
				if (f) {
					newCrumbs = newCrumbs + ' > ';
				}
				newCrumbs = newCrumbs + $('industry').options[$('industry').selectedIndex].text;
				f = true;
			}
			if ($F('taskList') != -1) {
				if (f) {
					newCrumbs = newCrumbs + ' > ';
				}
				newCrumbs = newCrumbs + $('taskList').options[$('taskList').selectedIndex].text;
				f = true;
			}
			$('crumbs').update(newCrumbs);
		}
		else {
			$('crumbs').update('All');
		}
		var num = $('resultsTable').getElementsBySelector('tr.mainRow').size();
		$('numFound').update(num);
		$('redTotal').update(num);
		$('message1').update();
		$('message2').update('out of');
		$('totalNum').removeClassName('initial');
		if (num == 1) {
			$('plural').update();
			$('plural2').update();
		}
		else {
			$('plural').update('s');
			$('plural2').update('s');
		}
		$('searchWarning').update();
	}
}
