
var ActiveItem = Class.create();
ActiveItem.prototype = {
    initialize: function(name,url) {
        this.name = name;
        this.url = url;
        this.searchName = noAccent(name.toLowerCase());
    }
};

var ActiveSearch = Class.create();
ActiveSearch.prototype = {
    initialize: function()
    {
        this.items = new Array();
        this.currentSearchItems = new Array();
        this.currentSearch = "";
        this.nbCols = 3;
        this.nbItemByCol = 10;
    },
    
    addItem: function(item)
    {
        this.items.push(item);
    },
    
    setAllItems: function(items)
    {
        this.items = items;
    },
    
    itemMatchSearch: function(item, search)
    {
        if( !item.searchName || item.searchName=="")
            item.searchName = noAccent(item.name.toLowerCase());
        return (item.searchName.indexOf(search) == 0);
    },
    
    /**
     * updates the currentSearchItems and returns it
     * if 'invalidateOldOnes' == true, it will do the search on all items,
     * othewise it will afine the current search
     */
    updateSearchItems: function(invalidateOldOnes)
    {
        var items = this.currentSearchItems;
        if (invalidateOldOnes)
            items = this.items;
        
        this.currentSearchItems = new Array();
        
        for(var i=0; i<items.length; i++)
        {
            if (this.itemMatchSearch(items[i],this.currentSearch))
            {
                this.currentSearchItems.push(items[i]);
            }
        }
        
        return this.currentSearchItems;
    },
    
    search: function(search)
    {
        search = noAccent(search.toLowerCase());
        
        if (search == this.currentSearch)
            return this.currentSearchItems;
        
        // if the search is only a "precision" of the previous, 
        // it is useless to parse the whole array
        var invalidate = this.currentSearch == "" || search.indexOf(this.currentSearch) != 0;
        this.currentSearch = search;
                
        return this.updateSearchItems(invalidate);
    },
    
    //searchs inside items, but also show results in the div
    doSearch: function(search)
    {
        if (search == "")
        {
            $('active_default_lists').show();
            $('active_search_lists').hide();
            return;
        }
        this.search(search);
        
        height = $('active_default_lists').getHeight();
        $('active_default_lists').hide();
        $('active_search_lists').show();
        $('active_search_lists').style.height = height + "px";
                
        for(i=0; i<this.nbCols; i++)
        {
            $('active_search_list_'+i).update();
        }
        
        //generates result entries
        var nb  = 0;
        var max = this.nbCols * this.nbItemByCol - 1;
        
        if (this.currentSearchItems.length < max)
            max = this.currentSearchItems.length;
        
        if (max != 0)
        {
            for(i=0; i<this.nbCols && nb < max ; i++)
            {
                var ul = $('active_search_list_'+i);
                for( ; nb < (i+1)*this.nbItemByCol && nb < max ; nb++)
                {
                    var li = new Element('li');
                    var a  = new Element('a', { href : this.currentSearchItems[nb].url });
                    a.update(this.currentSearchItems[nb].name);
                    li.appendChild(a);
                    ul.appendChild(li);
                }
            }
        } else {
            var li = new Element('li');
            li.update(Translator.translate("No active product match your search"));
            $('active_search_list_0').appendChild(li);
        }
            
    }
}

function noAccent(str)
{
    var ret = "";
    var pattern_accent = new Array("é", "è", "ê", "ë", "ç", "à", "â", "ä", "î", "ï", "ù", "ô", "ó", "ö");
    var pattern_replace_accent = new Array("e", "e", "e", "e", "c", "a", "a", "a", "i", "i", "u", "o", "o", "o");
    if (str && str!= "") {
        ret = preg_replace (pattern_accent, pattern_replace_accent, str);
    }
    return ret;
}

function preg_replace (array_pattern, array_pattern_replace, my_string)  {
    var new_string = String (my_string);
    for (i=0; i<array_pattern.length; i++) {
        var reg_exp= RegExp(array_pattern[i], "gi");
        var val_to_replace = array_pattern_replace[i];
        new_string = new_string.replace (reg_exp, val_to_replace);
    }
    return new_string;
}


