/**
 * 
 * @author:			randy johnson
 * 					highschoolalums.com
 * @date:			2008 02 15
 * @function:		this plugin manages a cascading select where the current selector is dependent up prior
 * 					selects and their corresponding values 
 *                the current selector's options are setup just prior to receiving focus
 * 
 * @attributes:	'next' - points to the next selector (used to reset when the current selector is changed)
 * 					'datasrc' - identifies the fields from which values are required to establish this selector
 * 
 * @changes:		2009 04 12 - fixed clearing our cascades
 */
jQuery.fn.HSASelector = function(params)
	{
	params = jQuery.extend(
		{
		ajaxCall:		'selectorLookUpAJX.php',
		cascadeClass:	'selectorLookUp',
		updateFirst:	false,
		button:			null,
		selectorKeys:	'HSASelectorKeys',
		idPrefix:		'sel',
		idSuffix:		'lu'
		},params);
		
	return this.each(function() 
		{
		$(this).addClass(params.cascadeClass);
		$(this).bind('change',populateOptions); 
		$(this).bind('blur',populateOptions);
		$(this).bind('focus',clearCascade);
		}); 
		
	function clearCascade(e)
		{
//		alert('S[' + e.type + '] [' + $(this).attr('id') + ']');
		var c = identifyCascade(this);
		var i = identifySelect(this,c);
		clearOptions(this,c,i);
		return true;
		}
	
	function populateOptions(e)
		{
//		alert('S[' + e.type + '] [' + $(this).attr('id') + ']');
		var c = identifyCascade(this);
		var i = identifySelect(this,c) + 1;   // work the next cascade as we are tabbing to it
		clearOptions(this,c,i);
		obtainOptions(this,c,i)
		return false;
		}
		
	function identifyCascade(caller)
		{
		return	$('.' + params.cascadeClass);
		}
		
	function identifySelect(caller,cascade)
		{
		name = $(caller).attr('name');
		var i = 0;
		var j = 0;
		cascade.each(function() { if ($(this).attr('name') == name) i = j; j++; });
		return i;
		}
		
	function clearOptions(caller,cascade,index)
		{
		cascade.slice(index+1).each(function() { $('#' + $(this).attr('id') + ' option:gt(0)').remove(); });
		cascade.slice(index).each(function()   { $('#' + $(this).attr('id')).removeAttr(params.selectorKeys); });
		}
		
	function obtainOptions(caller,cascade,index)
		{
		parameters = {};
		cascade.eq(index).each(function(){ parameters.t = $(this).attr('name'); });
		keys = establishKeys(cascade,index,parameters);
			
		if ($(caller).attr(params.selectorKeys) != 'undefined' && $(caller).attr(params.selectorKeys) != keys)
			{
			$(caller).attr(params.selectorKeys,keys);
			if (params.updateFirst || index > 0)
				{
				$.post(params.ajaxCall,parameters,function(data)
					{
					$('#' + params.idPrefix + parameters.t + ' option').remove();
					var s = $('#' + params.idPrefix  + parameters.t);
					var a = data.split(';');
					for (x=0;x<a.length-1;x++)
						if (x != 0 || a.length - 1 != 2)
							{
							var b = a[x].split('|');
							s.append('<option value="' + b[0] + '">' + b[1] + '</option>');
							}
					});
				}
			}
		}
				
	function establishKeys(cascade,index,parameters)
		{
		var last = '';
		cascade.slice(0,index).each(function()
			{ 
			parameters[$(this).attr('name')] = $(this).val();	
			last = last + $(this).attr('name') + '|' + $(this).val() + ';'; 
			});
		return last;			
		}
	}
				

