var hasFocus = false;
$( function() {
	/********************************************************
	onkeydown event handler for the input elem.
	Tab key = use the highlighted suggestion, if there is one.
	Esc key = get rid of the autosuggest dropdown
	Up/down arrows = Move the highlight up and down in the suggestions.
	 ********************************************************/
	$("#searchDetails")
			.keydown(
					function(ev) {
						var key = getKeyCode(ev);
						switch (key) {
						case TAB:
							useSuggestion('searchDetails', '');
							break;

						case ENTER:
							if(highlighted!=-1){
								useSuggestion('searchDetails', '');
							}
							$("#autosuggest_searchDetails").hide();
							break;

						case ESC:
							$("#autosuggest_searchDetails").hide();
							break;

						case KEYUP:
							if (highlighted > 0) {
								highlighted--;
							}
							changeHighlight('searchDetails');
							if (mode == 'suggestion')
								document.getElementById('searchDetails').value = suggestions[highlighted];
							break;

						case KEYDN:
							if (highlighted < (suggestions.length - 1)) {
								highlighted++;
							}
							changeHighlight('searchDetails');
							if (mode == 'suggestion')
								document.getElementById('searchDetails').value = suggestions[highlighted];
							break;

						}
					});
	$("#searchDetails").blur( function(ev) {
			//$("#autosuggest_searchDetails").hide();
			var genYabhath = new AjandGenric();
			var searchPage = genYabhath.getYabhathFile().trim();
			if($("#searchDetails").val().trim()==''){
				if(searchPage=='vidSearch'){
					if(logha=='En')
						$('#searchDetails').css('background','url(images/bg/utube.JPG) no-repeat');
					else
						$('#searchDetails').css('background','url(images/bg/utube_ar.JPG) no-repeat');
				}else if((searchPage == 'blgSearch') || (searchPage == 'imgSearch') || (searchPage == 'newsSearch')){
					if(logha=='En')
						$('#searchDetails').css('background','url(images/bg/google.JPG) no-repeat');
					else
						$('#searchDetails').css('background','url(images/bg/google_ar.JPG) no-repeat');
				}else{
					if (logha == 'En')
						$('#searchDetails').css('background',
								'url(images/bg/bing.jpg) no-repeat');
					else
						$('#searchDetails').css('background',
								'url(images/bg/bing_ar.JPG) no-repeat');
				}
				$('#searchDetails').css('background-color','white');
			}
			hasFocus=false;
	});
	$("#searchDetails").focus( function(ev) {
		hasFocus=true;
		$('#searchDetails').css('background','');
		$('#searchDetails').css('background-color','');
	});
	$(document).click( function(ev) {
		if(hasFocus==false)
			if( $('#autosuggest_searchDetails').is(':visible') ) 
				$("#autosuggest_searchDetails").hide();
	});
	//
	$("#searchDetails")
			.keyup( function(ev) {
				setCursorPos('searchDetails');// for getting cursor location
					// later
					var key = getKeyCode(ev);
					// key < 48, keys left-right-up-down-enter-tab...except 8
					// and 46 (delete keys),32 space bar
					if ((key < 48)
							&& !((key == 46) || (key == 8) || (key == 32))) {
						return;
					} else if (key == 8) {
						var query = document.getElementById('searchDetails').value;// see
						// if
						// there
						// trim
						// function
						// you
						// can
						// use
						if ((query == null) || (query.trim() == '')) {
							$("#autosuggest_searchDetails").hide();
						} else {
							if (logha == 'Ar') {
								var currentTypingArabic = isArabic(query
										.substring(globalCursorPos,
												globalCursorPos + 1));
								// alert('currentTypingArabic'+currentTypingArabic);
								// alert('1+'+query.substring(globalCursorPos,globalCursorPos+1)+'2');
								if (currentTypingArabic == 1) {
									mode = 'suggestion';// --ok
									getSuggestions('searchDetails',
											'autosuggest_searchDetails',
											'suggestionUL_searchDetails', '1');
								} else {// in case of space, currentTypingArabic == 0
									if ((query.substring(globalCursorPos,
											globalCursorPos + 1)).trim() != '') {
										mode = 'translateration';// --ok
										process1('searchDetails', 'optionView',
												'autosuggest_searchDetails',
												'suggestionUL_searchDetails');
									}
								}//

							} else {
								getSuggestions('searchDetails',
										'autosuggest_searchDetails',
										'suggestionUL_searchDetails', '1');
							}
						}
					} else if (key == 32) { //space entered
						if (logha == 'Ar') {
							var content = document
									.getElementById('searchDetails').value;
							// alert('1'+content.substring(globalCursorPos,globalCursorPos+2).trim()+'+2');
							if ((content.substring(globalCursorPos,
									globalCursorPos + 2)).trim() == '') {//to make sure space was not entered in the middle of a word or just before a word					   
								if (mode == 'translateration') {
									useSuggestion('searchDetails', ' ');
									$("#autosuggest_searchDetails").hide();
									// to avoid english autocomplete if English
									// ,ie suggestions[0], is selected
									var query = document
											.getElementById('searchDetails').value;// see
									// if
									// there
									// trim
									// function
									// you
									// can
									// use
									var currentTypingArabic = 1;// =
									// isArabic(query.substring(globalCursorPos,globalCursorPos+1));
									getSuggestions('searchDetails',
											'autosuggest_searchDetails',
											'suggestionUL_searchDetails', '1');
								}
							}//content.substring(globalCursorPos,globalCursorPos+2)).trim() != ''
							else {
								$("#autosuggest_searchDetails").hide();
							}
						}//logha=='Ar'
					}//key==32
					else {
						mode = 'translateration';// when typing, mode is
						// always translateration
						// //--ok
						process1('searchDetails', 'optionView',
								'autosuggest_searchDetails',
								'suggestionUL_searchDetails');
					}
					if(logha == 'Ar'){
						$("#autosuggest_searchDetails").attr("dir","rtl");
						$("#suggestionUL_searchDetails").attr("dir","rtl");
					}else{
						$("#autosuggest_searchDetails").attr("dir","ltr");
						$("#suggestionUL_searchDetails").attr("dir","ltr");
					}
				});
	// Come back for understanding
	$("#searchDetails").click( function(ev) {//interchange keyup<>keydown in the future
				setCursorPos('searchDetails');// for getting cursor location
				// later
				mode = 'translateration';// --ok
				processOnClick('searchDetails', 'autosuggest_searchDetails',
						'suggestionUL_searchDetails');
			});
	// Come back for understanding

	/***************************************************************************
	 * Helper function to cancel an event in a browser-independent manner.
	 * (Returning false helps too).
	 **************************************************************************/
	function cancelEvent(ev) {
		if (ev) // Moz
		{
			ev.preventDefault();
			ev.stopPropagation();
		}
		if (window.event) // IE
		{
			window.event.returnValue = false;
		}
	}
	;

	/***************************************************************************
	 * Helper function to determine the event source element in a
	 * browser-independent manner.//come bak understanding
	 **************************************************************************/
	function getEventSource(ev) {
		if (ev) // Moz
		{
			return ev.target;
		}

		if (window.event) // IE
		{
			return window.event.srcElement;
		}
	}
	;

	/***************************************************************************
	 * click handler for the dropdown ul insert the clicked suggestion into the
	 * input
	 **************************************************************************/
	$("#suggestionUL_searchDetails").click(function(ev){ //comeback to this function if/else below.  Take another look
						if (logha == 'En') {
							useSuggestion('searchDetails', '');
							$("#autosuggest_searchDetails").hide();
						} else {//arabic
							if (mode == 'translateration') {
								if (showClick == 1) {
									document.getElementById("divHide").style.display = "";
									document
											.getElementById("divShow")
											.removeChild(
													document
															.getElementById("showM"));
									showClick = 0;
								} else {
									useSuggestion('searchDetails', '');
									$("#autosuggest_searchDetails").hide();
									var query = document
											.getElementById('searchDetails').value;// see
									// if
									// there
									// trim
									// function
									// you
									// can
									// use
									getSuggestions('searchDetails',
											'autosuggest_searchDetails',
											'suggestionUL_searchDetails', '1');
								}
							} else {
								useSuggestion('searchDetails', '');
								$("#autosuggest_searchDetails").hide();
							}
						}
						cancelEvent(ev);
						return false;
					});

	/***************************************************************************
	 * mouseover handler for the dropdown ul move the highlighted suggestion
	 * with the mouse
	 **************************************************************************/
	$("#suggestionUL_searchDetails").mouseover( function(ev) { //come back understanding
				// Walk up from target until you find the LI.
				var target = getEventSource(ev);
				while (target.parentNode
						&& target.tagName.toUpperCase() != 'LI') {
					target = target.parentNode;
				}
				//added 8/1
				autoSuggestDiv = document
						.getElementById('suggestionUL_searchDetails');
				var lis = autoSuggestDiv.getElementsByTagName('LI');
				for (i in lis) {
					var li = lis[i];
					if (li == target) {
						highlighted = i;
						if (li.id == "showM") {//added so that +More Options work
							showClick = 1;
						} else {
							showClick = 0;
						}
						break;
					}
				}
				changeHighlight('searchDetails');
			});
});
