
var prefix = '';

function departmentClicked(prefixParam) {
	
	prefix = (prefixParam == null) ? '' : prefixParam;

	selectedDepartments = getSelected(prefix+'department');

	//create positionsToAdd, an array of position objects which need to be added
	var positionsToAdd = positions.select(function(position) {
										  
		if(position != null) {

			//return true if position.parent is a selected department
			return (selectedDepartments.indexOf( position.parent ) != -1);
			
		}

	});

	positionsToAdd = sortByName(positionsToAdd);

	addObjectsToSelect(prefix+'position', positionsToAdd);

	positionClicked(prefix);

}

function positionClicked(prefixParam) {
	
	
	
	prefix = (prefixParam == null) ? '' : prefixParam;

	selectedPositions = getSelected(prefix+'position');

	var subpositionsToAdd = subpositions.select(function(subposition) {
											   
		if(subposition != null) {

			return (selectedPositions.indexOf( subposition.parent ) != -1);

		}

	});

	subpositionsToAdd = sortByName(subpositionsToAdd);

	addObjectsToSelect(prefix+'subposition', subpositionsToAdd);

	subpositionClicked(prefix);

}

function subpositionClicked(prefixParam) {

	prefix = (prefixParam == null) ? '' : prefixParam;

	enableBoxes();

}

function enableBoxes() {
	
	//if only 1 position was added, then disable it
	
	$(prefix+'position').disabled = ( $(prefix+'position').options.length == 1 );
	$(prefix+'subposition').disabled = ( $(prefix+'subposition').options.length == 1 );


	if(prefix == '') {
	
		//for advanced search page, deselect those which are disabled
		if($(prefix+'position').disabled) $(prefix+'position').selectedIndex = -1;
		if($(prefix+'subposition').disabled) $(prefix+'subposition').selectedIndex = -1;

	} else {

		//otherwise, select the first one
		if($(prefix+'position').disabled) {
			$(prefix+'position').options[0].text = 'None';
			$(prefix+'position').selectedIndex = 0;
		}

		if($(prefix+'subposition').disabled) {
			$(prefix+'subposition').options[0].text = 'None';
			$(prefix+'subposition').selectedIndex = 0;
		}

	}
	
	
	//this will affect platform, software, and modifiers (if they exist)...

	if($(prefix+'platform') && $(prefix+'software') && $(prefix+'modifier')) {

		var platformDisabled = true;
		var softwareDisabled = true;
		var modifierDisabled = true;
	
		
		//loop thru selected positions, find the one its linking to, and enable if appropriate
		//reason we loop thru subpositions first is because theres a high chance they will all be turned on here, and will reduce time in code
	
		selectedSubpositions = getSelected(prefix+'subposition');
		
		selectedSubpositions.each(function(subpositionID) {
									
			if(platformDisabled || softwareDisabled || modifierDisabled) {
	
				var subposition = subpositions.find(function(subposition) {
					if(subposition != null) {
						return (subposition.id == subpositionID);
					}
				});
		
				if(subposition != null) {
		
					if(subposition.platform) platformDisabled = false;
					if(subposition.software) softwareDisabled = false;
					if(subposition.modifier) modifierDisabled = false;
					
				}
				
			}
	
		});
		
		if(platformDisabled || softwareDisabled || modifierDisabled) {
	
			//if theres a chance something could still be enabled, loop thru the positions
	
			selectedPositions = getSelected(prefix+'position');
	
			selectedPositions.each(function(positionID) {
									  
				if(platformDisabled || softwareDisabled || modifierDisabled) {
			
					var position = positions.find(function(position) {
						if(position != null) {
							return (position.id == positionID);
						}
					});
			
					if(position != null) {
			
						if(position.platform) platformDisabled = false;
						if(position.software) softwareDisabled = false;
						if(position.modifier) modifierDisabled = false;
			
					}
					
				}
		
			});
	
		}
	
		$(prefix+'platform').disabled = platformDisabled;
		$(prefix+'software').disabled = softwareDisabled;
		$(prefix+'modifier').disabled = modifierDisabled;
		
		
		//if these boxes are disabled, make sure nothing is selected
		
		if($(prefix+'platform').disabled) {
			for(var i = 0; i < $(prefix+'platform').options.length; i++) {
				$(prefix+'platform').options[ i ].selected = false
			}
			$(prefix+'platform').selectedIndex = -1;
		}
		if($(prefix+'software').disabled) $(prefix+'software').selectedIndex = -1;
		if($(prefix+'modifier').disabled) $(prefix+'modifier').selectedIndex = -1;

		if(prefix == '') {

			//if just enabled these boxes, select 'all' by default
			if(!platformDisabled) $(prefix+'platform').selectedIndex = 0;
			if(!softwareDisabled) $(prefix+'software').selectedIndex = 0;
			if(!modifierDisabled) $(prefix+'modifier').selectedIndex = 0;
			
			
		}

	}

}


function addObjectsToSelect(selectBoxName, arrayOfObjects) {

	//save list of selected items
	var selectedItems = $F(selectBoxName);
	if(selectedItems == null) selectedItems = [];
	if(!selectedItems.size) selectedItems = [selectedItems];


	//clear the select box
	$(selectBoxName).options.length = 0;


	//add first entry
	if(prefix == '') {
		
		$(selectBoxName).options[0] = new Option('All', 'all');

		//if all was already selected, then re-select it
		if(selectedItems.indexOf("all") != -1) {
			$(selectBoxName).options[0].selected = true;
		}

	} else {
		$(selectBoxName).options[0] = new Option('Select below...', '');

	}

	//loop thru each thing to add
	arrayOfObjects.each(function(theObject, index) {

		if(theObject != null) {

			//add the object to the select box
			$(selectBoxName).options[ index + 1 ] = new Option(theObject.name, theObject.id);

			//see if it should be selected
			if(selectedItems.indexOf(theObject.id) != -1) {
				$(selectBoxName).options[ index + 1 ].selected = true;
			}

		}

	});

	//if nothing is selected, select first option
	var selectedItems = $F(selectBoxName);
	if(selectedItems == null) selectedItems = [];
	if(!selectedItems.size) selectedItems = [selectedItems];

	if(selectedItems.size() == 0) {
		$(selectBoxName).options[0].selected = true;
	}

}


function getSelected(selectBoxName) {

	//get array of selections
	var selections = $F(selectBoxName);
	if(selections == null) selections = [];
	if(!selections.size) selections = [selections];

	//if all is selected...
	if(selections.indexOf("all") != -1) {

		//get array of all options in box
		allSelections = $A( $(selectBoxName).options );

		//and pretend that literally everything is selected
		selections = allSelections.collect(function(selection) {

			if(selection != null && selection.value != 'all') {
				return selection.value;
			}

		});

	}
	
	return selections;

}


function sortByName(arrayOfObjects) {

	var toReturn = arrayOfObjects.sortBy(function(theObject) {
		if(theObject != null) {
			return theObject.name;
		}
	});

	return toReturn;

}


function defaultSelects(selectBoxName, arrayOfIDs) {

	var theOptions = $A($(selectBoxName).options);
	
	theOptions.each(function(option) {
						
		if(option != null) {

			if(arrayOfIDs.indexOf(option.value) != -1) {

				$(selectBoxName).options[ option.index ].selected = true;

			} else {

				$(selectBoxName).options[ option.index ].selected = false;

			}
			
		}

	 });

}


