jQuery(document).ready(function() {
/*
		FILTER TABLE FUNCTION!
*/

/*
		Constants
*/

	var all = 'Kaikki';
	var blank = '{ Tyhjä }';
	
	//Constants

	//Filters whole Table with an input
	function filterMain(selector, match, caller) {
		match = jQuery.trim(match);
		match = match.replace(/ /gi, '|');
		

		
	  jQuery(selector).each(function() {  
	    if (jQuery(this).text().search(new RegExp(match, "i")) < 0) {
				hideRow(jQuery(this), caller);
			} else {
				showRow(jQuery(this), caller);
			}
	  });
	}

	//filter function for select inputs
	function filterSelect(selector, match, caller) {
		match =  jQuery.trim(match);

		jQuery(selector).each(function() {
			if (match == blank) {
				match = '';
			} 
 			if ((jQuery.trim(jQuery(this).text()) != match) && (match != all)) {
				hideRow(jQuery(this).parent(), caller);
			} else {
				showRow(jQuery(this).parent(), caller);
			}
  	});
	}

	//Function that creates select options per column
	function createOptions(table, index, select) {
		var keywords = {};
		keywords[all] = all;
		//keywords[blank] = blank;
	
			table.find('tbody.content tr td:nth-child('+(index)+')').each(function() {
				word =  jQuery.trim(jQuery(this).text());
				if (word != "") {
			 		keywords[word] = word;
				}
			});
				
		jQuery.each(keywords, function (i, keyword) {
			jQuery(select).append('<option>' + keyword + '</option>');
		});
	}

	function createCourses(table, index) {
		var keywords = {};
	
		table.find('tbody.content tr td:nth-child('+(index)+')').each(function() {
			if (!jQuery(this).parent().hasClass('hidden')) {
				word = jQuery.trim(jQuery(this).html());
				if (word != "") {
						keywords[word] = word;
				}
			}
		});

		jQuery('div.series ol li').remove();
		jQuery.each(keywords, function (i, keyword) {
			jQuery('div.series ol').append('<li>' + keyword + '</li>');
		});
	}


	//Show row function - removes the appropriate classes for state saving
	function showRow(row, caller) {		
		if (row.hasClass(caller)) {
			row.removeClass(caller);
			if (row.attr('class').match(/\bhide/) == null) {
			 row.show().removeClass('hidden');
			}
		}
		row.addClass('visible');
	}

	//Hide row function - sets the appropriate classes for state saving
	function hideRow(row, caller) {
		if(!row.hasClass('hidden') ) { 
			row.hide().addClass(caller).addClass('hidden').removeClass('visible');
		}	else {
			row.addClass(caller)
		}
	}

	//delays call so that function only runs when pause is big enough in the calls
	function callDelay() {
		var timer = 0;
	  return function(callback, ms){
	    clearTimeout (timer);
	    timer = setTimeout(callback, ms);
	  };
	}

	//main function that controls everything
	jQuery.fn.filterTable = function() {
		var table = jQuery(this);

		//MAIN FILTER - If table has a main filter input activate it and filter it
		var delayCall = callDelay();
		
		
		jQuery('input#mainSearch').val('');

		jQuery('input#mainSearch').keyup(function(event) {
				element = jQuery(this);
				
				//Delay filter so only activates when 500ms pause
				delayCall(function(){
		    filterMain(table.find('tbody.content tr'), element.val(), 'hideMain');
					createCourses(table, 3); //function scans column and creates all filtering possibilities
					
					/*showing = jQuery('tbody.content tr:visible').length;
					jQuery('div#viewing').html('Haulla löytyi ' + showing + ' oppituntia');
					
					if (showing == 0) {
						jQuery('pre#no-lessons-found').show()
					} else {
						jQuery('pre#no-lessons-found').hide();
					}*/
				}, 150 );
				

		});
		

		createOptions(table, 2, 'select#focusOptions');
		jQuery('select#focusOptions').change(function(event) {
			filterSelect(table.find('tbody.content tr td:nth-child('+(2)+')'), jQuery(this).find('option:selected').val(), 'hide2');
			createCourses(table, 3); //function scans column and creates all filtering possibilities
			
			/*showing = jQuery('tbody.content tr:visible').length;
			jQuery('div#viewing').html('Haulla löytyi ' + showing + ' oppituntia');
			
			if (showing == 0) {
				jQuery('pre#no-lessons-found').show()
			} else {
				jQuery('pre#no-lessons-found').hide();
			}*/
		});
		
		createOptions(table, 5, 'select#teacherOptions');
		jQuery('select#teacherOptions').change(function(event) {
			filterSelect(table.find('tbody.content tr td:nth-child('+(5)+')'), jQuery(this).find('option:selected').val(), 'hide5');
			createCourses(table, 3); //function scans column and creates all filtering possibilities
			
			/*showing = jQuery('tbody.content tr:visible').length;
			jQuery('div#viewing').html('Haulla löytyi ' + showing + ' oppituntia');
			
			if (showing == 0) {
				jQuery('pre#no-lessons-found').show()
			} else {
				jQuery('pre#no-lessons-found').hide();
			}*/
			
		});
		
		createOptions(table, 4, 'select#difficultyOptions');
		jQuery('select#difficultyOptions').change(function(event) {
			filterSelect(table.find('tbody.content tr td:nth-child('+(4)+')'), jQuery(this).find('option:selected').val(), 'hide4');
			createCourses(table, 3); //function scans column and creates all filtering possibilities
			
			/*showing = jQuery('tbody.content tr:visible').length;
			jQuery('div#viewing').html('Haulla löytyi ' + showing + ' oppituntia');
			
			if (showing == 0) {
				jQuery('pre#no-lessons-found').show()
			} else {
				jQuery('pre#no-lessons-found').hide();
			}*/
		});




		
		
		
		
		/*
		selector = jQuery('table#lesson-view tr.Tom-Stillman');
		jQuery(selector).each(function() {  
			hideRow(jQuery(this), 'caller');
		});*/
		
		//MAIN FILTER
		/*
		// COLUMN FILTER
		return table.find('tbody.filter td').each(function(i) {
			var header = jQuery(this);
		
			//Activate filter for column input
			header.children('input').keyup(function(event) {
				element = jQuery(this);
				var index = jQuery('tbody.filter td').index(header)+1;
			
				//Delay filter so only activates when 500ms pause
			//	delayCall(function(){
					filterInput(table.find('tbody.content tr td:nth-child('+index+')'), element.val(), 'hide' + index);
			//	}, 500 );
				table.alternateRowColors();
			});
		
			//Create the options for string selects 
			header.children('select').each(function() {
				var index = jQuery('tbody.filter td').index(header)+1;
				if (jQuery(this).hasClass('checkbox')) {
					var caption = table.find('thead th:nth-child('+index+')').text();
					jQuery(this).append('<option>'+all+'</option>');
					jQuery(this).append('<option value="1">' + caption + '</option>');
					jQuery(this).append('<option value="0">Ei ' + caption.toLowerCase() + '</option>');
				} else	if (jQuery(this).hasClass('image')) {
					createOptions(table, index, 'image');
				} else {
					createOptions(table, index); //function scans column and creates all filtering possibilities
				}
			});
		
			//Activate filter on change for string selects
			header.children('select').change(function(event) {
				var index = jQuery('tbody.filter td').index(header)+1;	
				type = (jQuery(this).hasClass('checkbox')) ? 'checkbox' : (jQuery(this).hasClass('image')) ? 'image' : '';
				filterSelect(table.find('tbody.content tr td:nth-child('+(index)+')'), jQuery(this).find('option:selected').val(), 'hide' + index, type);
				table.alternateRowColors();
			});
		
		});
		//COLUMN FILTER*/
	}


/*
		/FILTER TABLE FUNCTION
*/


	//Enables filters for table
	jQuery('.filter_table').filterTable();
	//jQuery('.sort_table').sortTable();
});
