﻿jQuery(function ($) {
	$.fn.moosearch = function (target, opt) {
	var timeout, val = '', m = {}, moofound, mooact = [], fid = '',e = this, options = $.extend({ 
			delay: 100,
			selector:'em',
			searchtype:'default',
			bind: 'keyup',
			show: function () {
				this.style.display = "";
			},
			hide: function () {
				this.style.display = "none";
			}
		}, opt);
		
	this.init = function()
	{
		val = '';
		//-- init inputs fields
		for(var s=0; s < options.selectors.length; s++)
		{
			if(options.selectors[s].type == 'input')
			{
			$(options.selectors[s].input).val(options.selectors[s].value);
			}
		}
		
		
		
	}
	this.search = function () {
			//-- get results		
			rowcache = $(target).map(function () {
				return this;
			});
			var rowcache_length = rowcache.length;
			//vals = val.toLowerCase().split(' ');
		    e.idebugger(fid);
				for( var i = 0; i < rowcache_length; i++)
				{
					if (this.findme(val.toLowerCase(), m[fid][i])) {
					 moofound = true;
					 $(rowcache[i]).show();
					}
					else
					{
					 $(rowcache[i]).hide();
					}
				}
			return this;
		};
		
	this.findme = function (vals, t) {
			
			e.idebugger(options.searchtype);
			switch(options.searchtype)
			{
				case"all":
					if ( t.indexOf(vals) < 0) {
					return false;
					}
					break;
				default:
				if (t.indexOf(vals)) {
					return false;
				}
				break;
			}
			return true;
		};
	this.strip_html = function (input) {
			var output = input.replace(new RegExp('<[^<]+\>', 'g'), "");
			output = $.trim(output.toLowerCase());
			return output;
		};
	
	this.mcache = function()
	{
		for(var s=0; s < options.selectors.length; s++)
			{
					m[s] = $(options.selectors[s].selector).map(function () {
						return e.strip_html(this.innerHTML);
					});
				//moofound[s] = false;	
			}
	}
	this.ibinds = function()
	{
		//-- key binds
			for(var s=0; s < options.selectors.length; s++)
			{
				$(options.selectors[s].input).data({act:s});
				//-- Blur
				/*
				$(options.selectors[s].input).blur(function()
				{
					if(options.selectors[$(this).data('act')].type == 'input')
						{
						e.idebugger($(this).val() +' == '+options.selectors[$(this).data('act')].value);
						if($(this).val()==options.selectors[$(this).data('act')].value)
							{
							$(this).val('');
							val = '';
							}
							else
							{
							$(this).val(options.selectors[$(this).data('act')].value);
							}
						}
						else
						{
						if(val=='')
							{
							$(this).val(options.selectors[$(this).data('act')].value);
							val = '';
							}
						}
					//e.start();
				});
				*/
				//-- Click
				if(options.selectors[s].type == 'input')
				{
				 $(options.selectors[s].input).click(function()
					{
					if($(this).val() == options.selectors[$(this).data('act')].value)
					{
					e.idebugger($(this).val()+'-'+options.selectors[$(this).data('act')].value);
						$(this).val('');
						val = '';
						//e.start();
					}
					
					
					e.idebugger('value:'+$(this).val());
					/*
					if($(this).val() == '')
					{
						$(this).val(options.selectors[$(this).data('act')].value);
					}
					*/
					
					});
					
					
					$(options.selectors[s].input).blur(function()
					{
					e.idebugger('blurvalue:'+$(this).val());
						if($(this).val() == '')
						{
						$(this).val(options.selectors[$(this).data('act')].value);
						}
					});
					
					
				}
				
				
				$(options.selectors[s].input).bind(options.selectors[s].ibind, function()
				{
					e.resetfields($(this).data('act'));
					fid = $(this).data('act');
					if($(this).val()!=options.selectors[$(this).data('act')].value)
					  {
					  val = $(this).val();
					  }
					  else
					  {
					  val = '';
					  }
					e.start();
				});
				
				//-- if fill
				if(options.selectors[s].fill)
				{
					e.fill(options.selectors[s].input, m[s]);
				}
			 
			}
		
	}
	this.resetfields = function(act)
	{
		for(var s=0; s < options.selectors.length; s++)
		{
			if(s!=act)
			{
				if(options.selectors[s].type == 'input')
					{
						$(options.selectors[s].input).val('');
					}
					else
					{
						$(options.selectors[s].input).val($('option:first', $(options.selectors[s].input)).val());
					}
			}
		}
		moofound = false;
		
	}
	this.fill = function(ftarget, fdata)
	{
		var filldata = [];
		var f = 0;
		for(var fcnt = 0; fcnt < fdata.length; fcnt++)
		   {
			if($.inArray(fdata[fcnt], filldata) == -1)
			  {
				filldata[f] = fdata[fcnt];
				f++;
			  }
		   }
		//-- append data to select
		$.each(filldata, function(index, value)
		{
		$(ftarget).append("<option value="+value+">"+value.toUpperCase()+"</option>");
		});
	}
	this.start = function () {
			
			window.clearTimeout(timeout);
			timeout = window.setTimeout(function () {
				e.search();
			}, options.delay);
			return this;
	};	
	
	this.idebugger = function(str)
    {
    //-- user debugger only firefox
	/*
	if(navigator.appCodeName == "Mozilla")
      {
      console.info(str);
      }
	*/
    } 
	
	
	this.init();
	this.mcache();
	this.ibinds();
	
	};
});
