define(
'services/booleanSearchEngine',
[
  'underscore',
  'bookmarksApp'
],
function(_, bookmarksApp) { "use strict";

/*
* Boolean search engine.
*/
var BooleanSearchEngine = function () {

	var andExpression = 'and';
    var patterns = ['tag', 'url', 'title'];
    var tagPattern = 'tag';
	var bookmarks = {};

	// Compress some whitespaces to one. Defaults to whitespace characters.
	var clean = function(input, characters){
		if (!angular.isString(input)) return input;

        if (!characters) characters = '\\s';
        
        return String(input).replace(new RegExp('\^' + characters + '+|' + characters + '+$', 'g'), '');
    };

	// Trims defined characters from begining and ending of the string. Defaults to whitespace characters.
    var trim = function(input, characters){
    	if (!angular.isString(input)) return input;
        
        if (!characters) characters = '\\s';
        
        return String(input).replace(new RegExp('^' + characters + '+|' + characters + '+$', 'g'), '');
    };

    // Checks if string is blank or not.
    var isBlank = function(str){
        if (str == null) str = '';
        return (/^\s*$/).test(str);
    };

    // Splits input string by words. Defaults to whitespace characters.
    var words = function(str, delimiter) {
        if (isBlank(str)) return [];
        return trim(str, delimiter).split(delimiter || /\s+/);
    };

    var evaluateExpression = function(bookmark, searchText){
        // check on pattern
        var pattern = 'tag:';
        var expressionIndex = searchText.indexOf(pattern);
        // check if flitered
        if(expressionIndex == -1){
            var filteredValue = _.find(_.values(bookmark), function(propertyValue){
                return propertyValue.toString().indexOf(searchText) != -1;
            });
            return !_.isUndefined(filteredValue);
        }
        else{
            var patternText = trim(searchText.substring(expressionIndex + pattern.length));
            var tag = _.find(bookmark.tag, function(item){
                return item.text.indexOf(patternText) != -1;
            });

            return !_.isUndefined(tag);
        }
    };

	this.filterBookmark = function(bookmark, searchText){
		// var search = searchText;
		var search = 'tag:  prog  ';
		if(!search) return true;

        return evaluateExpression(bookmark, search);

		//var cleanSearch = clean(search);
		var searchWords = words(search, andExpression);


		var s = bookmark.title.indexOf(search) != -1;
		return s;
	};
};

/*
* Boolean search engine factory method.
*/
var BooleanSearchEngineFactory = function() {
  return new BooleanSearchEngine();
};

bookmarksApp.factory('booleanSearchEngine', BooleanSearchEngineFactory);

});
@@ -242,15 +242,17 @@
 'tag
+:
 ', 'url
+:
 ', '
@@ -260,40 +260,13 @@
 itle
+:
 '%5D;%0A
-    var tagPattern = 'tag';%0A
 %09var
@@ -1421,24 +1421,25 @@
 hText)%7B%0A
+%0A
         
 // check
@@ -1434,178 +1434,118 @@
     
-// check on pattern%0A        var pattern = 'tag:';%0A        var expressionIndex = searchText.indexOf(pattern);%0A        // check if flitered%0A        if(expressionIndex == -1
+var pattern  = _.find(patterns, function(item)%7B return searchText.indexOf(item) == 0; %7D);%0A%0A        if(!pattern
 )%7B%0A 
@@ -1852,26 +1852,8 @@
 ing(
-expressionIndex + 
 patt
@@ -2174,16 +2174,22 @@
   prog  
+and lx
 ';%0A%09%09if(
@@ -2211,23 +2211,26 @@
  true;%0A%0A
-
        
+ //
  return 

improve pattern selection