/*
 *	Highlights matching images in marked DOM nodes 
 */

function kwasiHighlightImages() {
	var HIGHLIGHT_ID = "imageArray";
	var MIN_LENGTH = 2;
	var FADED_OPACITY = 0.25;
	var restoreNode_a = new Array();	//Stores all faded Nodes
	var occurenceCounter;

	/*
	 *	prepares the STRING for use with Highlight, then calls highlight (public)
	 */
	this.query = function( KEYWORD_A ) {
		var keyWord_a = KEYWORD_A.slice( 0, KEYWORD_A.length ); //copy to not alter the keyword array
		restore();
		occurenceCounter = 0;
		
		if ( typeof( keyWord_a ) == "string" ) {
			return occurenceCounter;
		}
		
		for ( var key = 0; key < keyWord_a.length; key++ ) {
			keyWord_a[key] = new RegExp( "(" + keyWord_a[key].join( "|" ) + ")", "" );
		}
		
		if ( keyWord_a.length ) {
			highlight( keyWord_a );
		}
		
		return occurenceCounter;
	};
	
	/*
	 *	fades out image nodes where not every item in KEYWORD_A has matched (private)
	 */	
	function highlight( KEYWORD_A ) {
		var IMGNode_a = document.getElementById( HIGHLIGHT_ID );
		
		if ( IMGNode_a ) {
			IMGNode_a = IMGNode_a.getElementsByTagName( "img" );
			
			for ( var image = 0; image < IMGNode_a.length; image++ ) {
				var text = IMGNode_a[image].getAttribute( "alt" ).toLowerCase().kwasiCUTBL().kwasiSNLC();
				var found = true;
				
				for ( var key = 0; key < KEYWORD_A.length; key++ ) {
					if ( !KEYWORD_A[key].test( text ) ) {
						found = false;
						break;
					}
				}
				
				if ( found ) {
					occurenceCounter++;
				} else {
					kwasiStyleOpacity( IMGNode_a[image], FADED_OPACITY );
					restoreNode_a.push( IMGNode_a[image] );
				}
			}
		}
	}
		
	/*
	 *	restores the full opacity of faded image nodes (private)
	 */
	function restore() {
		while ( restoreNode_a.length ) {
			var node = restoreNode_a.pop();
			kwasiStyleOpacity( node, 1 );
		}
	}
}

