/*************************
 * - www.ucreation.nl    *
 *************************
 Dit document bevat JavaScript/jQuery codering. 
 */

bvk_googlesitesearch_pi1_suggestion = new Object();
bvk_googlesitesearch_pi1_suggestion.searchSuggestions = new Array();

bvk_googlesitesearch_pi1_suggestion.attachSuggestion = function(value, form_id)
{
	// Action when the value is empty
	if(value == '')
	{
		// No IE
		if(!$.browser.msie)
		{
			jQuery('#' + form_id + ' .bvk_search_suggestion').val('');
		}
		jQuery('#' + form_id + ' .bvk_suggestion_box').hide().children('table').children('tbody').html('');
	// Action when the value is not empty
	} else
	{
		// Array with content for  the suggestion box
		bvk_googlesitesearch_pi1_suggestion.getSuggestions(value,  form_id);
	}
}

bvk_googlesitesearch_pi1_suggestion.fillSuggestion =  function(value, form_id){
	// Show the suggestion box
	jQuery('#' + form_id + ' .bvk_suggestion_box').show();
	// Give old suggestions a class 'old' so whe can remove the current rows after the new connection
	jQuery('#' + form_id + ' .bvk_suggestion_box table tbody').children('.suggestionrow').addClass('old');
	// Loop the array when there are more than 0 results
	if(bvk_googlesitesearch_pi1_suggestion.searchSuggestions != false)
	{
		jQuery.each(bvk_googlesitesearch_pi1_suggestion.searchSuggestions, function(i, val)
		{
			// The first suggestion is our shadow
			if(i == 0)
			{
				var size = value.length;
				var shadow = value;
				shadow += val.substr(size);
				// No IE
				if(!$.browser.msie)
				{
					jQuery('#' + form_id + ' .bvk_search_suggestion').val(shadow);
				}
			}
			if(i <= 4)
			{
				// Create some html rows
				jQuery('<tr></tr>').addClass('suggestionrow').appendTo('#' + form_id + ' .bvk_suggestion_box table tbody');
				jQuery('<td></td>').html(val).appendTo('#' + form_id + ' .bvk_suggestion_box table tbody tr:last');
				jQuery('#' + form_id + ' .bvk_suggestion_box table tbody .old').remove();
			}
		});
		// Hover class
		jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').mouseenter(function()
		{
			if(jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val() != '')
			{
				jQuery('#' + form_id + ' .bvk_seachinput').val(jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val());
				jQuery('#' + form_id + ' .bvk_search_suggestion').val(jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val());
			}
			jQuery('#' + form_id + ' .bvk_memory_arrows').val(0);
			jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').removeClass('hover');
			jQuery(this).addClass('hover');
		}).mouseleave(function()
		{
			jQuery(this).removeClass('hover');
		});
		jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').click(function()
		{
			// No IE
			if(!$.browser.msie)
			{
				jQuery('#' + form_id + ' .bvk_search_suggestion').html(jQuery(this).children('td').html());
			}
			jQuery('#' + form_id + ' .bvk_suggestion_box').hide().children('table').children('tbody').html('');
			jQuery('#' + form_id + ' .bvk_textinput input').val(jQuery(this).children('td').text());
			jQuery('#' + form_id).parent().submit();
		});
	// There are no suggestions, close the suggestion box
	} else
	{
		jQuery('#' + form_id + ' .bvk_search_suggestion').val('');
		// No IE
		if(!$.browser.msie)
		{
			jQuery('#' + form_id + ' .bvk_search_suggestion').html(jQuery(this).children('td').html());
		}
		jQuery('#' + form_id + ' .bvk_suggestion_box').hide().children('table').children('tbody').html('');
	}
}
bvk_googlesitesearch_pi1_suggestion.decode_utf8 = function( s ){  return decodeURIComponent( escape( s ) );}
bvk_googlesitesearch_pi1_suggestion.getSuggestions = function(value, form_id){
		$.ajax({
			url: '/index.php',
			type: 'GET',
			dataType: 'html',
			processData: false,
			data: 'eID=bvk_googlesitesearch_suggest&bvk_googlesitesearch[cx]=' + $('#bvk_googlesitesearch_pi1_cx').val() + '&bvk_googlesitesearch[q]=' + bvk_googlesitesearch_pi1_suggestion.sanitzeInput(value),
			success: function(text) {
				text = bvk_googlesitesearch_pi1_suggestion.decode_utf8(text);
				bvk_googlesitesearch_pi1_suggestion.searchSuggestions = (text.length>0) ? text.split(',') : false;
				bvk_googlesitesearch_pi1_suggestion.fillSuggestion(value, form_id);
			}
		});
}
bvk_googlesitesearch_pi1_suggestion.sanitzeInput = function(str) {
		// URL-encodes string  
		// version: 1008.1718
		// discuss at: http://phpjs.org/functions/urlencode    // +   original by: Philip Peterson
	  	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

// jQuery document ready
jQuery(document).ready(function()
{
	// Each bvk_googlesitesearch_pi1
	jQuery('.bvk_googlesitesearch_pi1').each(function(i)
	{
		// Give every form a unique id
		jQuery(this).attr({'id' : 'googlesitesearch' + i + '', 'rel' : i});
		// Adds a autocomplete="off" to the text input
		jQuery(this).children('.bvk_textinput').children('input').addClass('bvk_seachinput').attr({'autocomplete' : 'off'});
		// No IE
		if(!$.browser.msie)
		{
			// Adds the suggestion element
			jQuery('<input />').attr({'type' : 'text','readonly':'readonly','disabled':'disabled'}).addClass('bvk_search_suggestion').appendTo(jQuery(this).children('.bvk_textinput'));
		}
		// Create some memory
		jQuery('<input />').attr({'type' : 'hidden', 'value' : 0}).addClass('bvk_memory_arrows').appendTo(this);
		jQuery('<input />').attr({'type' : 'hidden', 'value' : ''}).addClass('bvk_memory_arrows_originalvalue').appendTo(this);
		// Create the suggestion box
		jQuery('<div></div>').addClass('bvk_suggestion_box').hide().appendTo(this);
		jQuery('<table></table>').appendTo(jQuery(this).children('.bvk_suggestion_box'));
		jQuery('<tbody></tbody>').appendTo(jQuery(this).children('.bvk_suggestion_box').children('table'));
	});
	// Textinput onChange
	jQuery('.bvk_googlesitesearch_pi1 .bvk_textinput input').keyup(function(event)
	{
		var form_id = jQuery(this).closest('.bvk_googlesitesearch_pi1').attr('id');
		var current_value = jQuery(this).val();
		var current_selection = parseInt(jQuery('#' + form_id + ' .bvk_memory_arrows').val());
		// Arrow control system
		if(event.keyCode == 40 || event.keyCode == 38)
		{
			// Save the originalvalue
			if(current_selection == 0)
			{
				jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val(current_value);
			}
			var current_results = jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').size();
			// There are suggestions
			if(current_results > 0)
			{
				// Arrow down
				if(event.keyCode == 40 && current_selection <= current_results - 1)
				{
					var new_selection = current_selection + 1;
					var eq_selection = new_selection - 1;
					var current_text = jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr:eq(' + eq_selection + ')').text();
					jQuery('#' + form_id + ' .bvk_memory_arrows').val(new_selection);
					jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').removeClass('hover');
					jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr:eq(' + eq_selection + ')').addClass('hover');
					jQuery('#' + form_id + ' .bvk_search_suggestion').val(current_text);
					jQuery('#' + form_id + ' .bvk_seachinput').val(current_text);
				// Arrow up
				} else if(event.keyCode == 38 && current_selection > 0)
				{
					var new_selection = current_selection - 1;
					var eq_selection = new_selection - 1;
					var current_text = jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr:eq(' + eq_selection + ')').text();
					jQuery('#' + form_id + ' .bvk_memory_arrows').val(new_selection);
					jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr').removeClass('hover');
					jQuery('#' + form_id + ' .bvk_suggestion_box table tbody tr:eq(' + eq_selection + ')').addClass('hover');
					if(new_selection != 0)
					{
						jQuery('#' + form_id + ' .bvk_search_suggestion').val(current_text);
						jQuery('#' + form_id + ' .bvk_seachinput').val(current_text);
					} else
					{
						// Write the originalvalue back
						jQuery(this).val(jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val());
						jQuery('#' + form_id + ' .bvk_search_suggestion').val(jQuery('#' + form_id + ' .bvk_memory_arrows_originalvalue').val());
					}
				}
			}
		} else
		{
			jQuery('#' + form_id + ' .bvk_memory_arrows').val(0);
			bvk_googlesitesearch_pi1_suggestion.attachSuggestion(current_value, form_id);
		}
	});
});
