// JavaScript Document

$(function() {
   //console.log(base_url);
   $('#function_name').focus();
    $("#function_name").autocomplete(base_url + "application/ajaxsearch",
		{resultsClass: 'autocomplete', loadingClass:'ac_loading', minChars:1, matchSubset:1, matchContains:1, cacheLength:10, onItemSelect:selectItem, formatItem:formatItem, selectOnly:1}
	); //end autocomplete
	
	  /**
	  * form submit: this will do two things. 1. display the results, 2. show the data in sidebar
	  */
	  
	$('form#function_search_form').submit(function() {																			
				var name = $('#function_name').val();
				if(name!=''){
				$("#search_button").get(0).disabled=true;
				$('#function_description').append('<img src="'+base_loc+'img/loading.gif" alt="Currently Loading" id="loading" />')				
				inline_results(name);
				inline_results_sidebar(name)
				}
				return false;
			});//end submit
	
	$('#form#editedform').submit(function(){
		update_data();
		return false;
	});
}); //end main

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else {
			//var sValue = li.selectValue;				
			var name = $('#function_name').val();				
			inline_results(name);
			inline_results_sidebar(name);
	}//end else

	//alert("The value you selected was: " + sValue);
}//end function findValue

function selectItem(li) {
	findValue(li);
}

function formatItem(row) {
	return "<strong>" + row[0] + "</strong>";
}

/**
* for display the search details
*/
function inline_results(name){
	if (name=="Sorry, no results returned."){
		//do nothing
	}else{
	$.ajax({
		url: base_url + 'application/search_details',
		type: 'POST',
		data: 'function_name=' + name,
		success: function(result) {						
			$('#response').remove();
			$('#function_description').append('<p id="response">' + result + '</p>');
			$('#loading').fadeOut(50, function() {
				$(this).remove();
			});			
		}//end success
	});//end ajax
	}//end else
	$("#search_button").get(0).disabled=false;
}//end function inline_results

/**
* for display the search result in nav list
*/
function inline_results_sidebar(name){
	if (name=="Sorry, no results returned."){
		//do nothing
	}else{	
	$.ajax({
		url: base_url + 'application/ajaxsearch_navlist',
		type: 'POST',
		data: 'function_name=' + name,
		success: function(result) {						
			$('#wordlist').remove();
			$('#sidebarleft').append(result);
			$('#loading').fadeOut(50, function() {
				$(this).remove();
			});
		}//end success
	});//end ajax	
	$('#sidebarleft').css({'border-width': '1px'});
	}//end else
	$("#search_button").get(0).disabled=false;
}//end function inline_results_sidebar

function show_data(word){
	//var word = $('#sidebarleft #wordlist a').val();
	$('#function_description').append('<img src="'+base_loc+'img/loading.gif" alt="Currently Loading" id="loading" />')
	inline_results(word);
	$('#function_name').val(word);
	
}

function show_data_in_nav(word){
	//var word = $('#sidebarleft #wordlist a').val();
	$('#function_description').append('<img src="'+base_loc+'img/loading.gif" alt="Currently Loading" id="loading" />')
	inline_results_sidebar(word);
	$('#function_name').val(word);	
	inline_results(word);
}

function edit_entry(){
	var word = $('#function_name').val();
	if(word!=''){
	$.ajax({
		url: base_url + 'application/edit_data',
		type: 'POST',
		data: 'function_name=' + word,
		success: function(result) {						
			$('#edit').remove();
			$('#editdata').append('<div id="edit">'+result+'</div>');
		}//end success
	});//end ajax		
	}//end if
	//console.log("sdfsa "+word);
}

function update_data(){
	var id = $('#wordid').val();
	var word = $('#bword').val();
	var pronun = $('#pronunciation').val();
	var ipa = $('#ipa').val();
	//var pos = $('#pos').val();
	if(id!='' && word!='' && pronun!='' && ipa!=''){
		$.ajax({
		url: base_url + 'application/update_data',
		type: 'POST',
		data: 'wordid='+ id +'&bword='+ word +'&pronunciation='+pronun+'&ipa='+ ipa,  
		success: function(result) {
			$('#response').remove();
			$('#update_data').append('<p id="response">'+result+'</p>');
		}//end success
	});//end ajax		
	}//end if
	//return false;
}
/*function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	alert("The value you selected was: " + sValue);
}

function selectItem(li) {
	if (li.extra) {
		alert("That's '" + li.extra[0] + "' you picked.")
	}
}*/
