// code to use the jquery autotab plugin to provide optional autotab support for widgets
//  inserted into the FormControllerWidget

     
/** 
 * setup for the autotab plugin
*/
var currentDomElement;
var enableAutoTab;

function initAutoTab() {
	
	currentDomElement = null;
	enableAutoTab = true;
	
	// gotta special case IE - it triggers a change event on key press for a select or radio element, so
	// we have to handle the keydown event for IE, which is caught before the change event and block a change
	if ($j.browser.msie) {
	  $j(':input').livequery('keydown', function(e) {
			var keyCode = e.keyCode;
			//console.log('keyCode is ' + keyCode + ", type is " + this.type);
			if (keyCode == 13 && (this.type == 'select-one' || this.type == 'radio')) {
			  enableAutoTab = true;
				$j(this).change();
			} else {
				enableAutoTab = false;
			}
		});
		

	}

	$j(':input').livequery('change', function(e) {
		if (enableAutoTab == false) {
			// we were turned off by the keypress event handler
			enableAutoTab = true;
			return;
		}
		var thisEl = $j(this);
		currentDomElement = this;
		var index = $j(':input').index(this);
		if (this.type == 'select-one') {
			// single selects just set the focus on the next input element upon a change
			if (!$j(this).parent().is('.multi_part')) {
				// if this is a multi-part select, we are going to get a dominserted event for the input
				// element that will be dynamically inserted, so don't set focus to the next input element
				setFocus($j($j(':input').get(index+1)));
			}
		} else if (this.type == 'text') {
			if (this.onkeyup != null) {		// we don't want to collide with existing js code
				if (thisEl.attr('maxlength') > 0) {
					if (thisEl.val().length == this.maxlength) {
						setFocus(thisEl.next());
					}
				}				
			}
		}	else if (this.type == 'radio') {
			var nextInput = thisEl.parent().parent().parent().next().find(':input');
			setFocus(nextInput);
		}
	});
	
	// deal with dynamic widgets insertion - NB - this only works for single widget insertion, will fail for multiple widget insertion
	$j(document).bind('DOMNodeInserted', function(e) {
		var node = e.target;
		var possibleInput = $j(node).find(':input');
		if (possibleInput.attr('type') == 'select-one') {
			setFocus(possibleInput);
		}
	});
}

function setFocus(nextEl) {
	if (nextEl != null) {
		// first, see if we have to show the next pagination div
		// there is a dependency here on the jquery pagination plugin
		hiddenPaginationDiv = nextEl.parents('.pagination_section:hidden');
		if (hiddenPaginationDiv) {
			var index = $j('.pagination_section').index(hiddenPaginationDiv);
			if (index >= 0) {
				$j(".next").click();
			}
		}
		nextEl.focus();
		if (nextEl.attr('type') == 'text') {
			// set the cursor so keyboard navigation will continue to work correctly while auto tabbing
			nextEl.selectRange(0,0);
		}
	}
}

$.fn.selectRange = function(start, end) {
	return this.each(function() {
  if(this.setSelectionRange) {
		this.focus();
		this.setSelectionRange(start, end);
  } else if(this.createTextRange) {
		var range = this.createTextRange();
		range.collapse(true);
		range.moveEnd('character', end);
		range.moveStart('character', start);
		range.select();
  }
	});
};

// the js code to insert into the dom is not inserting a new input element, so
// go ahead and set the focus to the next input element
var setCurrentFocus = function() {
	if (currentDomElement != null) {
		var currIndex = $j(':input').index(currentDomElement);
		setFocus($j($j(':input').get(currIndex+1)));
	}
}

var setFocusOnNewlyInserted = function() {
  if (currentDomElement != null && $j.browser.msie) {
		var possibleInput = $j(currentDomElement).siblings().select('.subQuestions').find(":input")
		if (possibleInput && possibleInput.length > 0 && possibleInput.attr('type') == 'select-one') {
			setFocus(possibleInput);
		}
  }
}
