/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// Filter the contents of a table based on a keyword
	jQuery.classBehaviours.handlers.filterTableContents = {
		// properties
		name: 'filterTableContents',
		// methods
		start: function(node){
			// if this is the button set the click event
			if(node.nodeName == 'BUTTON'){
				node.onclick = this.filter;
			// else set the onchange event
			}else{
				node.onchange = this.filter;
				node.onkeyup = this.filter;
			}
		},
		// events
		filter: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			// if this is the button get the input field
			if(objNode.nodeName == 'BUTTON') objNode = jQuery.classBehaviours.utilities.previousNode(objNode);
			// get the value from the input field
			keyWord = objNode.value;
			// for all table rows show or hide the row if it matches the keywords or not
			parentTable = jQuery.classBehaviours.utilities.rootNode(objNode, 'TABLE');
			allRows = parentTable.getElementsByTagName('tbody')[0].getElementsByTagName('tr');
			for(var a=0; a<allRows.length; a++){
				// if(allRows[a].innerHTML.indexOf(keyWord)>-1) jQuery.classBehaviours.fader.setFade(allRows[a], 100)
				// else jQuery.classBehaviours.fader.setFade(allRows[a], 50)
				allRows[a].style.display = (allRows[a].innerHTML.indexOf(keyWord)>-1) ? jQuery.classBehaviours.utilities.getVisibleState(allRows[a]) : 'none' ;
			}
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.filterTableContents = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.filterTableContents.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".filterTableContents").filterTableContents();
			}
		);
	}
