/*
	Source: classbehaviours.handlers.filtercontents.js
	ClassBehaviours is a javascript framework based on class-name parsing.
	Copyright 2011 by Maurice van Creij and published on http://www.woollymittens.nl/
	This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/

	// CLASSBEHAVIOURS CLASS
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(classBehaviours)=='undefined') classBehaviours = {};

		// CLASSNAME BEHAVIOUR FUNCTIONS
		// create the handlers child object if it doesn't already exist
		if(typeof(classBehaviours.handlers)=='undefined') classBehaviours.handlers = {}

			// implements the functionality to this classname
			classBehaviours.handlers.filterContents = {
				// properties
				name: 'filterContents',
				instance: 0,
				settings: new Array(),
				// methods
				start: function(node){
					// create a node specific config object
					node.id = (node.id) ? node.id : this.name + '_' + this.instance++ ;
					this.settings[node.id] = {};
					// shortcut pointers
					var fct = classBehaviours.handlers.filterContents;
					var cu = classBehaviours.utilities;
					var cfg = fct.settings[node.id];
					// store the target objects
					cfg.targetIds = cu.getClassParameter(node, 'id', '').split('_');
					cfg.targetTags = cu.getClassParameter(node, 'for', '').split('_');
					// set the event handler for the text input
					cu.addEvent(node, 'change', function(){
						fct.inputChanged(node);
					});
					cu.addEvent(node, 'keypress', function(){
						fct.inputChanged(node);
					});
					cu.addEvent(node, 'blur', function(){
						fct.inputChanged(node);
					});
				},
				// events
				inputChanged: function(that){
					// shortcut pointers
					var node = (typeof(this.nodeName)=='undefined') ? that : this ;
					var fct = classBehaviours.handlers.filterContents;
					var cu = classBehaviours.utilities;
					var cfg = fct.settings[node.id];
					var searchParent, searchChildren;
					// get the search text
					var nodeValue = node.value;
					// exclude the help text
					if(node.value == node.title) nodeValue = '';
					// for all the target ids
					for(var a=0; a<cfg.targetIds.length; a++){
						// for all the given tags in the target node
						searchParent = document.getElementById(cfg.targetIds[a]);
						searchChildren = searchParent.getElementsByTagName(cfg.targetTags[a]);
						for(var b=0; b<searchChildren.length; b++){
							// if the node contain the given text
							if(searchChildren[b].innerHTML.replace(/(<([^>]+)>)/ig,"").indexOf(nodeValue)>-1){
								// display the node
								searchChildren[b].style.display = 'block';
							// else
							}else{
								// hide the node
								searchChildren[b].style.display = 'none';
							}
						}
					}
				}
			}

			// implements a supporting behaviour
			classBehaviours.handlers.applyContentFilter = {
				// properties
				name: 'applyContentFilter',
				// methods
				start: function(node){
					// shortcut pointers
					var fct = classBehaviours.handlers.filterContents;
					var cu = classBehaviours.utilities;
					// set event handlers here
					cu.addEvent(node, 'click', function(){
						fct.inputChanged(node.parentNode.getElementsByTagName('INPUT')[0]);
					});
				}
			}

	// JQUERY WRAPPER
	if(typeof(jQuery)!='undefined'){
		(function($){
			var methods = {
				init : function(options) {
					return this.each(function(){
						classBehaviours.handlers.filterContents.start($(this).context);
					});
				},
				applyContentFilter : function(){
					return this.each(function(){
						classBehaviours.handlers.applyContentFilter.start($(this).context);
					});
				}
			};
			$.fn.filterContents = function(method){
				if(methods[method]) {
					return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
				}else if(typeof method === 'object' || !method){
					return methods.init.apply(this, arguments);
				}else{
					$.error('Method ' +  method + ' does not exist on jQuery.filterContents');
				}
			};
		})(jQuery);

		// JQUERY EVENTS
		$(document).ready(function() {
			$(".filterContents").filterContents();
			$(".applyContentFilter").filterContents('applyContentFilter');
		});

		/* DIRECT APPLICATION
		$("#myList li").addClass('loremIpsum foo_bar');
		$("#myList li").loremIpsum();
		*/
	}

