/**
 * Class for creating quick search listing.
 * @class This is the Quick Search class.  
 *
 * @param {Array} conf
 *
 * @param {String} conf.id The id search input element
 * @param {String} conf.container The container of quick search result 
 * @param {String} conf.minLength The min length of query string
 * @param {String} conf.url The URL to script
 * @param {String} conf.limit The number listings to display in the result box
 *
 */
intelli.autofield = function(conf)
{
	var obj = (-1 != conf.id.indexOf('#')) ? $(conf.id) : $('#' + conf.id);
	var objCon = (-1 != conf.container.indexOf('#')) ? $(conf.container) : $('#' + conf.container);
	
	/* --- START MOD // nzhuchenko --- */
	var nobj = (-1 != conf.id.indexOf('#')) ? conf.id : '#' + conf.id;
	var nobjCon = (-1 != conf.container.indexOf('#')) ? conf.container : '#' + conf.container;	
	/* --- END MOD // nzhuchenko --- */
	
	var valueObj = $('#value_' + conf.id);//autocompliter

	var minLength = conf.minLingth ? conf.minLingth : 1;
	var url = conf.url ? conf.url : 'controller.php?plugin=locations&file=get-auto';
	var searchField = conf.searchField ? conf.searchField : '';
	var limit = conf.limit ? conf.limit : 15;
	var type = conf.type ? conf.type : '';

	var containerHidden = false;	
	var timeOutHandler = null;
	
	var cur_listing = -1; //hotfix

	this.init = function()
	{	
		/* --- START MOD // nzhuchenko --- */
		
		obj.keyup(function(e)//obj.keydown(function()
		{
			var code = e.which || e.keyCode;

			if(code != 38 && code != 40 && code != 13) /* set timer 500ms */
			{
				clearTimeout($.data(this, 'timer'));
				var wait = setTimeout(getElements, 500);
				$(this).data('timer', wait);
				cur_listing = -1; //hotfix
			}
		});
		
		//hotfix				
		/*var isFocus = false;
		var keepFocus = false;
		
		obj.focus(function()
		{
			isFocus = true;
			cur_listing = -1;
			console.log('focus');			
		});		
		
		obj.blur(function()
		{
			isFocus = false;
			if (!keepFocus)
			{
				console.log('blur');
				intelli.display(objCon, 'hide');
				cur_listing = -1;
				keepFocus = true;
			}
			else
			{
				keepFocus = false;
				obj.focus();
			}
		});
		
		objCon.click(function()
		{
			if ($.browser.safari || $.browser.msie)
			{
				keepFocus = isFocus;
			}
			else
			{
				console.log('blur');
				intelli.display(objCon, 'hide');
				cur_listing = -1;
				keepFocus = false;
			}
		});*/
		
		var mouseIn = false;
		
		objCon.mouseover(function()
		{
			mouseIn = true;
		});
		
		objCon.mouseout(function()
		{
			mouseIn = false;
		});
		
		obj.blur(function()
		{
			if(!mouseIn)
			{
				intelli.display(objCon, 'hide');
				cur_listing = -1;
			}
		});
		
		objCon.click(function()
		{
			intelli.display(objCon, 'hide');
			cur_listing = -1;
		});
		//hotfix		
		
		obj.keydown(function(e)
		{
			var toFocus = false;
			var num = $(nobjCon + ' div').length - 1;
		
		  	if (e.keyCode == 38 && cur_listing > 0) 
		  	{
		     	cur_listing --;
		     	toFocus = nobj + '_' + cur_listing;         	         	
		  	}
		    
		    if (e.keyCode == 40 && cur_listing < num) 
		    {
		     	cur_listing ++;
		     	toFocus = nobj + '_' + cur_listing;		     		
		  	}
		  	
		  	if (e.keyCode == 13) 
		    {
		     	$(nobj + '_' + cur_listing).click();
		     	objCon.css('display', 'none');
		     	return false;         	       	
		  	}
		  	
			if (toFocus) 
			{    				
				var height = 22; //hotfix
				var items = 9;
				
				$(nobjCon + ' div').removeClass("itemhover");
   				$(toFocus).addClass("itemhover");
   				if (($(nobjCon).scrollTop() + (items - 1)*height < cur_listing * height))
   				{
   					$(nobjCon).scrollTop((cur_listing - items + 1) * height);
   				}
   				
   				if (($(nobjCon).scrollTop() > (cur_listing - 1) * height)) //hotfix
   				{
   					$(nobjCon).scrollTop((cur_listing - 1) * height); //hotfix
   				}
				return false;
			}
		});
						
		/*objCon.click(function()
		{
			intelli.display(objCon, 'hide'); 
		});*/
		
		/* --- END MOD // nzhuchenko --- */
		
		objCon.mouseout(function()
		{
			containerHidden = true;
			timeOutHandler = setTimeout(hide, 200);
		});

		objCon.mouseover(function()
		{
			clearTimeout(timeOutHandler);
			intelli.display(objCon, 'show');
		});
	};
	
	/* --- START MOD // nzhuchenko --- */
	
	var getElements = function()
	{		
		if(obj.val().length >= minLength)
		{
			var qUrl = url;
	
			qUrl += '&q=' + obj.val();
			//qUrl += '&field=' + searchField;
			qUrl += '&limit=' + limit;
			qUrl += '&type=' + type;
					
			$.getJSON(qUrl, function(listings)
			{
				if(null != listings && '' != listings)
				{
					printElement(listings, type, obj.val());
					intelli.display(objCon, 'show');
				}
			});
		}				
	};
	
	/* --- END MOD // nzhuchenko --- */
	
	var hide = function()
	{
		if(containerHidden)
		{
			intelli.display(objCon, 'hide');
			containerHidden = false;
		}
	};

	var printElement = function(listings, type, inp)
	{
		var html = '';

		objCon.empty();		
		
		for(var i in listings)
		{
			//bold hotfix
			var normal = '';
			var bold = '';
			var bold2 = '';
			var first = '';
			var last = '';
			
			for (var j = 0; j <= inp.length - 1; j++)
			{
				var current = inp.substring(0, inp.length - j);
				var low_current = current.toLowerCase();
				var title = unescape(listings[i]['title']).toLowerCase();				
				
				if (title.indexOf(low_current) == 0)
				{
					normal = title.substring(current.length);
					bold = '<span class="bolder">' + low_current + '</span>';					
					break;
				}
				else
				{
					first = title.substring(0, title.indexOf(inp));
					last = title.substring(first.length + inp.length);
					bold2 = first;
					bold2 += '<span class="bolder">' + inp + '</span>';
					bold2 += last;
				}

			}
			
			//var normal = unescape(listings[i]['title']).substring(inp.length);
			//var bold = '<span class="bolder">' + inp + '</span>';
			
			if (normal == '' && bold2 == '')
			{
				normal = '<br>'; //hotfix
			}
			html += '<div class="detailedSearchItem" id="auto_'+ searchField + '_' + i + '">';
			html += '<input type="hidden" name="path" value=' + unescape(listings[i]['path']) + ' />';
			if (bold2 != '')
			{
				html += '<p>' + bold2 + '</p>';
			}
			else if(bold != '')
			{
				html += '<p>' + bold + normal + '</p>';
			}
			html += '</div>';				
		}

		objCon.append(html);

		$('.detailedSearchItem').click(function()
		{
			var l_title = $(this).text();
		/*	
			if(typeof(value) != 'undefined')
				valueObj.val(value);
				
			obj.val(unescape(l_title).replace('&amp;','&'));
		*/						
			//hotfix
			if(type == 'listings')
			{
				$('#auto_listing').val(l_title);
			}
			if(type == 'categories')
			{
				$('#auto_category').val(l_title);
			}
			if(type == 'locations')
			{
				$('#auto_location').val(l_title);
				
				var path = $('#' + this.id + '>input[name^="path"]').val();
								
				$('input[name="location"]').val(path);
			}
		});
	};
};

