/*

FTS functions used on the left nav and all FTS result pages

To use this:

<script type="text/javascript">

var queryBox = document.getElementById('userQuery');
queryBox.onkeyup = onFTSTextChange;
queryBox.onblur = onFTSTextBlur;

// if this is the main column:
queryBox.focus();

</script>


*/

var autoData;
var originalQuery;
var autoRow = 0;

// Auto-complete field text changed
function onFTSTextChange(event) {
	// determine the text field that caused the event
	var targ;
	if (!event) var event = window.event;
	if (event.target) targ = event.target;
	else if (event.srcElement) targ = event.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

//alert('key:'+event.which);
	if ((event.which == 13) || (event.which == 39) || (event.which == 37) || 
	    (event.which == 16) || (event.which == 17) || (event.which == 18)) return; // Left,Right,Enter,Shift,Ctrl,Alt
	else if (event.which == 27) { // Esc
		if (autoRow > 0) {
			autoRow = 0;
			selectAuto(targ);
		}
		hideFTSPopup('acDiv');
	}
	else if (event.which == 38) { // Up
		if (undefined != autoData.Results) {
			--autoRow;
			if (autoRow < 0)
				autoRow = autoData.Results.length;
			selectAuto(targ);
		}
	} else if (event.which == 40) { // Down
		if (undefined != autoData.Results) {
			++autoRow;
			if (autoRow > autoData.Results.length)
				autoRow = 0;
			selectAuto(targ);
		}
	} else {
		var txt = targ.value;
		if (txt.length > 2) {
			$.ajax({
				async: true,
				url: "autoComplete",
				dataType: "json",
				data: {
					userQuery:txt,
					theaterId:theaterId
				},
				type: "POST",   
				success: function (data, textStatus) {
					suggest(data,targ);
				}   
			});
		} else {
			hideFTSPopup('acDiv');
		}
	}
}

// auto complete item selection
function selectAuto(inputField) {
	var popupDiv = document.getElementById('acDiv');
	if (autoRow == 0)
		inputField.value = originalQuery;
	else
		inputField.value = autoData.Results[autoRow-1].auto;
	for (i=0;i<popupDiv.childNodes.length;i++) {
		if (i==autoRow-1)
			popupDiv.childNodes[i].className = "AutoCompleteHighlight";
		else
			popupDiv.childNodes[i].className = "AutoCompleteUnselected";
	}
}

// The AJAX callback to display auto-complete suggestions
function suggest(data,inputField) {
	var popupDiv = document.getElementById('acDiv');
	popupDiv.inputField = inputField;
	autoData = data;
	autoRow = 0;
	originalQuery = inputField.value;
	while ( popupDiv.hasChildNodes() )
		popupDiv.removeChild(popupDiv.firstChild);
	if ((undefined != data.Results)&&(data.Results.length > 0)) {
		var i, n = data.Results.length;
		for ( i = 0; i < n; i++ ) {
			var oDiv = document.createElement('div');
			popupDiv.appendChild(oDiv);
			oDiv.innerHTML = data.Results[i].auto;
			oDiv.className = "AutoCompleteUnselected";
			oDiv.onmousedown = onFTSDivMouseDown;
			oDiv.onmouseover = onFTSDivMouseOver;
			oDiv.onmouseout = onFTSDivMouseOut;
			oDiv.AutoComplete = this;
		}
		$("#acDiv").css("left",$("#"+inputField.id).position().left + "px");
		$("#acDiv").css("top",$("#"+inputField.id).position().top + $("#"+inputField.id).height() + 6 + "px");
		popupDiv.style.display = "block";
		
		if (inputField.id == 'userQueryLeftNav') {
			var iesuxorz = document.getElementById('categoryId');
			iesuxorz.style.visibility = "hidden";
		}
		
	} else { // hide the popup-div 
		popupDiv.innerHTML = "";
		hideFTSPopup('acDiv');
	}
}	

function onFTSDivMouseDown() {
	var popupDiv = document.getElementById('acDiv');
	popupDiv.inputField.value = this.innerHTML;
	// always attempt to submit the full FTS form as opposed to the left nav one
	if (popupDiv.inputField.id == 'userQueryLeftNav')
		document.ftsFormLeftNav.submit();
	else	
		document.ftsForm.submit();
}
function onFTSDivMouseOver() {
	this.className = "AutoCompleteHighlight";
}
function onFTSDivMouseOut() {
	this.className = "AutoCompleteUnselected";
}
function onFTSTextBlur() {
	hideFTSPopup('acDiv');
}

function hideFTSPopup(idName) {
	var popupDiv = document.getElementById(idName);
	popupDiv.style.display = "none";

	var iesuxorz = document.getElementById('categoryId');
	iesuxorz.style.visibility = "visible";
}


// FTS Search page only functions (not used in the left nav)

// Swap out the userQuery param with the latest text input data
function changeFTSTab(link) {
	var latestSearch = document.ftsForm.userQuery.value;
	var href = link.href;
	var paramIdx = href.indexOf("userQuery=");
	if (paramIdx != -1) {
		var paramIdx2 = href.indexOf("&",paramIdx);
		if (paramIdx2 == -1)
			paramIdx2 = href.length;
		href = href.substring(0,paramIdx+10)+latestSearch+href.substring(paramIdx2,href.length);
	}
	// Don't forget, we have to URL param encode the search string
	var paramsIdx = href.indexOf("?");
	if (paramsIdx != -1) {
		var paramStr = href.substring(paramsIdx,href.length);
		while (paramStr.indexOf("+") != -1)
			paramStr = paramStr.replace("+","%2B");
		while (paramStr.indexOf(":") != -1)
			paramStr = paramStr.replace(":","%3A");
		href = href.substring(0,paramsIdx)+paramStr;
	}
	link.href = href;
}

function changePage(pageNo) {
	document.ftsForm.page.value=pageNo;
	document.ftsForm.submit();
}
