/* Filter an Table
 * Orginal by Mike Merritt
 * Modified by jt4dma
 ************************************************/

(function($){
	$.fn.liveFilter = function (aType) {
		
		
		$('input.filter').attr('autocomplete', 'off');
		// Determine what we are going to be filtering.
		var filterTarget = $(this);
		var child;
		if ($(this).is('ul')){
			child = 'li';
		} else if ($(this).is('ol')){
			child = 'li';
		} else if ($(this).is('table')){
			child = 'tr';
		}
		
		// Declare variables
		var hide;
		var show;
		var filter;
		var preview = $('#preview');
		
		$('input.filter').bind('change keyup keydown keypress focus blur', function(e){
						
			type    = e.type;
			keyCode = e.keyCode;
			filter  = $(this).val();

			//console.log(filter.length);

			if(type == 'blur'){
				$('#preview').remove();
			} else if(type == 'focus'){
				
				if(preview.length <=0){
					$('.inputfilter').append('<ul id="preview" style="display:none;"></ul>');
				}
			}
			
			if(filter.length>=1){
				$('#close').show();
			}else{
				$('#close').hide();
			}
			//console.log(keyCode);
			if((type == 'keyup' || type == 'keydown') || keyCode == 8/*&& type != 'blur'*/){
				
				// Grab the ones we need to hide and the ones we need to show
				hide  = $(filterTarget).find(child + ':not(:Contains("' + filter + '"))').not('tr.header');
				show  = $(filterTarget).find(child + ':Contains("' + filter + '")').not('tr.detail');
				nShow = show.length || 0;
				nHide = hide.length;
				
				$(this).next().text(nShow);
				
				// Animate the items we are hiding and showing
				if (aType == 'basic') {
					hide.hide();
					show.show();
				} else if (aType == 'slide'){
					hide.slideUp(500);
					show.slideDown(500);
				} else if (aType == 'fade'){
					hide.fadeOut(400);
					show.fadeIn(400);
					
				}
				
				
				for (var i=0;i<filterTarget.length;i++)
				{
					var $target = filterTarget.eq(i);
				 	var $visRows = $target.find('tr:visible').not('tr.header');
				 	if ($visRows.length > 0)
						$target.show();
					else
						$target.hide();
				}
					
					
				                                           
				
				
				//Head Elements not killing
				//var tmpCount = 0;
				//var html     = ''
				//var tmpTrVi  = $(filterTarget).find('tr:visible');
				
				/*if(tmpTrVi.length == 1 || show.length == 0){
					$('#preview').hide('');
				} else {
					$('#preview')/*.css('height', show.length * 10 ).html('').show();
				}
				
				$(show).queue(function(){
					$(this).parent().prev().find('tr.header').show();
					showText = $(this).find('td.fullname').text();
					cid      = $(this).attr('id');
					if(tmpCount <= 20 && showText != ''){
						html += '<li><a href="' + cid + '">' + showText + '</a></li>';
						tmpCount++;
					}
					$(this).dequeue();
				});
				if(html != $('#preview').html()){
					
					$('#preview').html(html).hide().slideDown(200, 'linear').css('height', 20);
					//console.log('tmpCount->' + tmpCount);
					if(tmpCount < 20){
						$('#preview');
					}
				}*/
			}
			
			
		});
		
		$('#cross').click(function(e){
			e.preventDefault();
			$('input.filter').val('').change();
			$(this).hide();
			$('tr.detail:visible').hide().prev().removeClass('tHighlight');
		});
		
		$('#preview li a').live('mouseover mouseout click', function(e){
			$(this).css('outline', 'none').focus();
			
			var eventType = e.type;
			
			if(eventType == 'click'){
				e.preventDefault();
				$('#preview').remove();
				$('input.filter').val($(this).text()).change();
				openItem = $(this).attr('href');
				$('#' + openItem).click();
			}
			
			if(eventType == 'mouseover'){
				$(this).css({
					background: '#ffffff',
					fontWeight: 'bold'
				});
			} else if(eventType == 'mouseout'){
				$(this).css({
					background: '',
					fontWeight: 'normal'
				});
			}
		});
		
		
		// Custom expression for case insensitive contains()
		jQuery.expr[':'].Contains = function(a,i,m){
		    return jQuery(a).text().toLowerCase().indexOf(m[3].toLowerCase())>=0;
		}; 

	}

})(jQuery);

