jQuery.fn.highlight = function (str)
{
	 	str = str.replace("&","&amp;");
    var regex = new RegExp("(" + str +")", "gi");
    //var regex = /str/gi;

    return this.each(function ()
    {
        this.innerHTML = this.innerHTML.replace(regex, "<span class=\"hl\">$1</span>");
    });
};

jQuery.fn.rmhighlight = function ()
{
    // var regex = new RegExp("<span class=\"hl\">(.*?)</span>", "gm");
    var regex = new RegExp("<span.*?hl.*?>(.*?)</span>", "gim");

		
    return this.each(function ()
    {
        this.innerHTML = this.innerHTML.replace(regex, "$1");
				// alert(this.innerHTML);
				//console.log (this.innerHTML);
				
    });
};



function searching(yes) {


	var ele = $('#searching')[0];
	if (yes) {
		ele.style.display='inline';
	} else {
		ele.style.display='none';
	}
}

function foundpapers(cnt) {

	el = $('#foundpapers')[0];

	grammar = cnt == 1 ? '' : 's';
	el.innerHTML = "<strong>" + cnt +"</strong>" + " paper" + grammar;

	
}


function searchfor(txt) {

	// searching(1);
	// tablesearch.filter();

		if (!tablesearch.timer_on) {
			$('#search').val(txt);
			searching(1);
			tablesearch.timer_on = 1;
			$('#selecttrack').attr('disabled','disabled');
			setTimeout( function() { 
				tablesearch.filter('');
				$('#selecttrack').attr('disabled','');
			},1000);
			
		}
}

var tablesearch = {
	links:new Array(),
	last_value:"",
	timer_on:0,
	results:0,
	shownItems:function (ele) {
		var lis = ele.getElementsByTagName("li");
		for(var i=lis.length-1; i>=0; i--) {
			// if(lis[i].style.display == 'table-row') return true;
		}
		return false;
	},

	showAll:function() {
		searching(1);
		var all_lis = $(".tablerow");


		setTimeout ( function() {
		
			tablesearch.results = 0;
			all_lis.each (function(i) { 

				// performance
				//if ( !($(this)[0].style.display=='none') ) {
				//}
				$(this).find('.src').rmhighlight();

				$(this)[0].style.display = ''; 
				tablesearch.results++;
			});
			searching(0);
			foundpapers(tablesearch.results);
		}, 10);

		$('#search')[0].value='';
	},

	delaysearch: function(e) {
		// if(!e) var e = window.event;

		var txt = $('#search')[0].value.toLowerCase();
		if(txt == tablesearch.last_value) return;
		if (txt.length < 4 && !(txt.length == 0) && !(e.keyCode == '13')) return;

		if (!tablesearch.timer_on) {
			searching(1);
			tablesearch.timer_on = 1;
			setTimeout( function() { 
				tablesearch.filter('');
			},1500);
			
		}

		return;
		
	},


	filter:function(e) {
		// if(!e) var e = window.event;
		var txt = document.getElementById("search").value.toLowerCase();
		
		if(txt == '') {
			tablesearch.timer_on = 0;
			searching(0);
			tablesearch.showAll();
			tablesearch.last_value=txt;
			return;
		}
		// if(txt == tablesearch.last_value) return;

		// if (txt.length < 4) return;
		var lnks = tablesearch.links;



		this.results=0;
		for(var i=lnks.length-1; lnk=lnks[i],i>=0; i--) {
			if(!(lnk.text.indexOf(txt)+1)) {
					lnk.ele.style.display='none';
			} else {
				this.results++;
				lnk.ele.style.display='';
				$(lnk.ele).find('.src').rmhighlight();
				$(lnk.ele).find('.src').highlight(txt,"hl");
				
			}
		}

		tablesearch.last_value=txt;
		tablesearch.timer_on = 0;

		foundpapers(this.results);
		searching(0);


	},
	init:function () {
		var all_links = $(".tablerow");

		all_links.each( function(i) {
			// console.log ($(this).text());
			// console.log ($(this).parents('tr .tablerow'));
			
				//'text': $(this).text().toLowerCase(),

			var lnk = {
				'text': $(this).find('.src').text().toLowerCase().replace("&amp;","&"),
				'ele'	: $(this)[0]
			}
			tablesearch.links.push(lnk);
		});


		// document.getElementById('search').onkeyup=this.delaysearch;
		$('#search').keyup( function(e) {
			tablesearch.delaysearch(e);
		});
		

		return;

	}
}

// window.onload = function() {
// 	document.getElementById("search").focus();
// 	links_search.init();
// }


