/**
 * Version: 1.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
 */
(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;

		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.change(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(p); 
			i++;
		});
	};
	
	/*
	 * Apply filters to the list of products
	 */
	SFProductFilter.prototype.filter = function(){
		//fetch currently
		var activeTags = this.checkboxContainer.find('option:selected').map(function(){
			return $(this).val();
		});

		//create an array of active elements (with all active tags)
		var activeElements = this.items.filter(function(){			

			var itemTags = $(this).data('tags');
			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;
				}
				//if we find at least one inactive tag then break the loop and deactive this item
				flag = false;
				return false;				
			});		
			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){
		options = options || {};
		
		return this.each(function(){
			var pf = new SFProductFilter(this, options);
		});
	};
})();
