/*
Sift Logic 1.0 by Ari N. Karp, August 2008
using mooTools v.1.11

Blog: theuiguy.blogspot.com

You are free to use at will, just at least visit my blog and tell me
1. if you are using it and how (for curiosity sake)
2. some feedback on how you changed, enhanced, or minimized it. 

I believe in sharing code to the community, so if you feel the same, then
please share your changes with me to help make the UI a better place.

For help on this, please visit theuiguy.blogspot.com and I will get back asap.
*/


window.addEvent('domready', function(){
	app.initialize();
});

var app = {

	initialize : function(){
		this.arrayOfObjects = $$(".siftItem"); // don't do this for large sets. Find another way.
		$each(this.arrayOfObjects, function(item, i){
			item["data"] = $A(item.getAttribute("data").split("||")); 
		},this);
		window.setTimeout(function(){	
			app.setupSiftTool();
		},0);
	},

	setupSiftTool : function(){	
		/*
		Here you can decide, based on the number of expected items how you want the 
		search to be triggered. I like to use a search box when the data set gets to large,
		which means the user has to hit enter or click the box.
		Until that time, I let the search work continuously as they fire the keyup event.
		You can use if(event.keyCode === 13) to ensure it happens with enter key.		
		*/
		$("siftBox").addEvent('keyup', function(event){
			app.captureKeys();				
		});			
		$("clearButton").addEvent('click',function(event){
	    	$('siftBox').value = "";
	    	app.captureKeys();
		});
		this.cArray = [];
	},

	captureKeys : function(){
	    app.arrayOfObjects.forEach(function(obj, index){
	         if(app.testFailAttribute(obj,'siftBox')){
	              if(app.cArray.contains(obj.id)){
	                   return false;
	              }else{
	                   app.cArray[app.cArray.length] = obj.id;
	              }
	         }else{
	              if(app.cArray.contains(obj.id)){
	                   app.cArray.remove(obj.id);
	                   obj.style.display = (window.ie6) ? "inline" : "block"; // ensures a fix to ie6 margin impl.
	              }
	         }
	    },this);
    	this.tM = null;
		this.isAnd = -1;
		this.isOr = -1;
		this.isNot = -1
		this.firstStyle = "";
		this.secondStyle = "";
		this.returnNeg = false;
		this.returnPos = true;
	},
		
	testFailAttribute : function(obj,box){
	    if(!this.tM){
			 this.firstStyle = "inline";
			 this.secondStyle = "none";
			 this.returnNeg = false;
			 this.returnPos = true;
			 this.isNot = $(box).value.indexOf("!");
	         this.isAnd = $(box).value.indexOf("+");
	         this.isOr = $(box).value.indexOf("|"); //must be one or the other.
			 if(this.isOr === -1) this.tM = $(box).value.toLowerCase().split(/\+/g);
			 if(this.isAnd === -1) this.tM = $(box).value.toLowerCase().split(/\|/g);
	         if(this.isOr !== -1 && this.isAnd !== -1) return;
	    }
	    obj["doShowObj"] = 0;
	    this.tM.forEach(function(m, index){
	         index = index+1;
	         m = m.escapeRegExp();
	         if(!this.testFailMatch(obj,m)){
	              obj["doShowObj"]++;
	         }else{
	              if(this.isOr !== -1){
	                   if(index === 1 && obj["doShowObj"] > 0) obj["doShowObj"]--;
	              }else{
	                   if(obj["doShowObj"] > 0) obj["doShowObj"]--;
	              }
	         }
	    },this);
	  	if(this.isNot !== -1){
	   		this.firstStyle = "none";
			this.secondStyle = "inline";
			this.returnPos = false;
			this.returnNeg = true;
		}	
		if(this.isOr === -1){
			if(obj["doShowObj"] === this.tM.length){
				obj.style.display = this.firstStyle;
				return this.returnNeg;		
			}else{
				obj.style.display = this.secondStyle;	
				return this.returnPos;	
			}
		}
		if(this.isOr !== -1){
			if(obj["doShowObj"] > 0){
				obj.style.display = this.firstStyle;
				return this.returnNeg;		
			}else{
				obj.style.display = this.secondStyle;	
				return this.returnPos;	
			}				
		}		
	},

	testFailMatch : function(obj,testMatch){
		testMatch = testMatch.replace("!","");
		if(!(obj["data"][0].toLowerCase().test(testMatch)) && !(obj["data"][1].toLowerCase().test(testMatch)) && !(obj["data"][2].toLowerCase().test(testMatch)) && !(obj["data"][3].toLowerCase().test(testMatch))){			
			return true; // meaning, if the item passes none of these tests, hide it an add it to the array.
		}	    
	    return false;
	}
	
}