/**

 * Verion 2.0 
 * 
 * Plugin should be applied to the products list and can be modified with following options
 *  - checkboxContainer : selector for container that will hold filter checkboxes, by default "#checkboxcontainer"
 *  - item : selector for product item withing the list, by default "li"
 *  - tags : selector for tags wrapper within the product item element, by default ".tags"
 *  - itemsFound : selector for the "items found" message, by default ".itemsFound". It should have at least one <span /> containing the results number
 *  - hideOnLoad : should all items be displayed or hidden when page is loaded, by default "true" (hidden)
 *  - hiddenMessage : selector for the element with message displayed when all items are hidden, by default ".hideMessage"
 *  - delimiter : delimiter used to separate tags, by default a comma
 *  - createDynamicCheckboxes : should script dynamically create filter checkboxes, by default true
 *  
 *  Version 2.0
 *  created fgctechlution
 *  date 5/11/2011
 *  new features:
 *  -add new functionality for filter rules
 *  new options:
 *  -strictFilter: values:true/flase; set up a filter rule
 *  true: keep the filter functionality in the version 1.0 which filter items meet fully to all chosen options
 *  false: new filter rule which filter items meet one of chosen options
 *  
 *  
 */
(function(){	
	
    var SFProductFilter = function(productList, options){
		
        this.productList = $(productList);				
        this.checkboxContainer = $(options.checkboxContainer || '#checkboxcontainer');
        this.items = this.productList.find(options.item || 'li');		
        this.tags = {};
		
        this.itemsFound = $(options.itemsFound || '.itemsFound');		
        this.hideOnLoad = typeof options.hideOnLoad != 'undefined' ? !!options.hideOnLoad : true;
        this.hiddenMessage = $(options.hiddenMessage || '.hideMessage');
        this.createDynamicCheckboxes = typeof options.createDynamicCheckboxes != 'undefined' ? !!options.createDynamicCheckboxes : true;
        this.strictFilter=options.strictFilter;
        var delimiter = options.delimiter || ',';
       
        var self = this, tagElement = options.tags || '.tags';
				
        /* Travers all items and parse appended tags */
        $.each(this.items, function(index, item){
            var tags = $(item).find(tagElement).text().split(delimiter);
			
            //empty or missing tags on the element
            if (!tags) {
                return true;
            }
			
            //trim all whitespace from tags and add them to the global 
            tags = $.map(tags, function(item){
                var tag = $.trim(item);
                if (tag === ''){
                    return null;
                }
				
                self.tags[tag] = tag;		
                return tag;
            });
						
            $(item).data('tags',tags);
        });
		
        if (this.hideOnLoad === true) {
            this.hiddenMessage.show();
            this.productList.hide();
        }
		
        if (this.createDynamicCheckboxes === true) {
            this.createCheckboxes();
        }
		
        this.checkboxContainer.find('input[type=checkbox]').click(function(){
            self.filter();
        });

        this.filter();
    };
	
    /*
	 * Parse all tags and create a list of checkboxes
	 */
    SFProductFilter.prototype.createCheckboxes = function(){
        var self = this;
        var i = 0;
        $.each(this.tags, function(index, item){
            var checkbox, label, p;
            checkbox = $('<input type="checkbox" value="' + item + '" id="product-list-tag-'+i+'"/>');
			
            label = $('<label for="product-list-tag-'+i+'">'+item+'</label>');
            p = $('<p/>');
            p.append(checkbox, ' ', label);
            self.checkboxContainer.append; 
            i++;
        });
    };
	
    /*
	 * Apply filters to the list of products
	 */
    SFProductFilter.prototype.filter = function(){
        //fetch currently
        var activeTags = this.checkboxContainer.find('input:checked').map(function(){
            return $(this).val();
        });
        var strictFilter= this.strictFilter;
        //create an array of active elements (with all active tags)
        var activeElements = this.items.filter(function(){			

            var itemTags = $(this).data('tags');
            
            if (strictFilter) {
                 var flag = true;
                        
                    $.each(activeTags, function(index, tag){
                        //active tag found on item, search for next inactive tag
                        if ($.inArray(tag, itemTags) > -1) {
                         
                            return true;
                        }
                        flag=false;
                        return false;
                   				
                    });
            } else {
                if (activeTags.length==0) {
                   var flag=true;
                }
            
                else {
                    var flag = false;
                        
                    $.each(activeTags, function(index, tag){
                        if (flag==true) return true;
                        //active tag found on item, search for next inactive tag
                        if ($.inArray(tag, itemTags) > -1) {
                            flag=true;
                            return true;
                        }
                   				
                    });
                }
            }
					
            return flag;
        });

        var resultsFound = activeElements.length;

        //no tags checked + hideOnLoad function is active
        if (this.hideOnLoad === true && activeTags.length === 0) {
            this.productList.hide();
            this.hiddenMessage.show();
			
            resultsFound = 0;
        }
        else {
            this.productList.show();
            this.hiddenMessage.hide();			
        }
		
        //update number of found items
        this.itemsFound.find('span').text(resultsFound);	
		
        //toggle active items
        this.items.hide(); 
        activeElements.show();
    };
	
    jQuery.fn.sfProductFilter = function(options){
         
        var defaults={
            hideOnLoad : false,
            checkboxContainer : '#checkboxcontainer',
            item : 'li',
            tags : 'span.keywords',
            itemsFound : '.itemsFound',
            hiddenMessage : '.hideMessage',
            delimiter : ',',
            createDynamicCheckboxes : true,
            strictFilter:false 
        };
        var settings = $.extend({}, defaults, options);
       		
        return this.each(function(){
            var pf = new SFProductFilter(this, settings);
        });
    };
})();
