
$(document).ready(function(){
	

	// get url
	var url = window.location.href;
	
	// find tag
	var tag = parseUri(url).queryKey['tag'];
	
	if(parseUri(url).queryKey['search']) {
		tag = parseUri(url).queryKey['search'];
	}
	
	// create term variable
	var term;
	
	// report table
	var report_table = $("#report-table");
		
	var live_search;
	
	// add search box
	$(report_table).parent().before('<div class="content-block clearfix"><h3 class="inline">Search:</h3><input type="text" id="live-search" class="input-search" value="search term" /></div>')

	$("#report-table")
	.tablesorter({
		widthFixed: true, 
		sortList: [[3,1]]
	});
	
	if (!tag) {

		$('input#live-search').after('<div id="pager"><form><img src="/radio4/worldonthemove/assets/ui/icon-pager-first.gif" class="first" alt="First"/><img src="/radio4/worldonthemove/assets/ui/icon-pager-prev.gif" class="prev" alt="Previous"/><input type="text" class="pagedisplay"/><img src="/radio4/worldonthemove/assets/ui/icon-pager-next.gif" class="next" alt="Next"/><img src="/radio4/worldonthemove/assets/ui/icon-pager-last.gif" class="last" alt="Last"/><select class="pagesize"><option selected="selected"  value="10">10</option><option value="20">20</option><option value="30">30</option><option  value="40">40</option></select></form></div>')

		$("#report-table")
		.tablesorterPager({
			container: $("#pager"),
			widgets: ['zebra'],
			positionFixed : false
		});

	}

	// if tag is passed hide rows initially
	if(tag) {
		
		$('tbody tr', report_table).hide();
		
		// convert spaces to dashes
		tag = tag.replace(/-/g,' ');
		
		$('input#live-search').val(tag);
			
	}
	
	
	// get row data
	var data = new Array()

	$('table#report-table tbody tr').each(function(){
		var date = $(this).find('td.date');
		var name = $(this).find('td.name');
		var comment = $(this).find('td.comment');
		var tags = $(this).find('td.tags');
		data.push((name.text() + " " +  tags.text() + " " +  comment.text() + " " + date.text()));
	})
	
	$('#results-alert').hide();
	
	$('input#live-search').click(function() {
	
		if ($(this).val() == 'search term') {
		
			$(this).val('');
		
		}
	
	});
	
	
	// add loading gif
	$('input#live-search').after('<img id="livesearch-loading" src="/radio4/worldonthemove/assets/ui/loading-livesearch.gif" alt="Performing search" />');
	
	function filter_search() {
	
		$('#results-alert').remove();
	
		// show loading gif
		$('img#livesearch-loading').css({display : 'inline'});
	
		clearTimeout(live_search);
	
		// get search term
		if(tag) {
			
			term = tag;
			
		} else if($('input#live-search').val() == "search term") {
		
			term = '';
		
		} else {
		
			term = $('input#live-search').val();
		
		} 
		
		live_search = setTimeout(function() {
			
			var has_results = false;
		
			// loop through all of the values and check if the first n letters
			// of the value match the n letters in the search box
			for (val in data){
							
				match = (data[val].replace(/-/g,' ')).toLowerCase().indexOf(term.toLowerCase());
				
	
				// this is where you handle the match
				if (term != '' &&  match >= 0) {
				
					//$('table#report-table tbody tr:eq('+val+')').prependTo('table#report-table tbody').show();
					$('table#report-table tbody tr:eq('+val+')').addClass('visible').show();
					
					has_results = true;
					
				} else {
				
					$('table#report-table tbody tr:eq('+val+')').removeClass('visible').hide()
					
				}
				
				if (term != '') {
					
					$("#report-table").trigger("update"); 
					
				}
				
				if($.trim(term) == "") {
					$('table#report-table tbody tr').addClass('visible').show()
					
					has_results = true;
				}
				
			}
			
			// remove odd even classes
			$('table#report-table tbody tr').removeClass('odd').removeClass('even');
			
			// re apply odd even on filtered rows
			$('table#report-table tbody tr.visible:odd').addClass('odd');
			$('table#report-table tbody tr.visible:even').addClass('even');
			
			
			if(!has_results) {
			
				$('#report-table tbody').prepend('<tr id="results-alert"><td colspan="' + $('#report-table thead tr th ').length + '">Sorry, there were no results for this search.</td></tr>');
				
			} 
				
			$('img#livesearch-loading').css({display : 'none'});
		
		}, 800);
		
		tag = false;
	
	}
	
	$('input#live-search').keyup(function(){
		
		$('#pager').hide();
		
		filter_search();
		
	});
	
	// perform filter
	filter_search();
	
	
});
