/**
 * SimpleFlame Product Filter
 * Version: 1.0
 * URL: http://www.simpleflame.com/
 *  
 * 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';$.each(this.items,function(index,item){var tags=$(item).find(tagElement).text().split(delimiter);if(!tags){return true;}
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();};SFProductFilter.prototype.createCheckboxes=function(){var self=this;$.each(this.tags,function(index,item){var checkbox,label,p;checkbox=$('<input type="checkbox" value="'+item+'" id="product-list-tag-'+index+'"/>');label=$('<label for="product-list-tag-'+index+'">'+item+'</label>');p=$('<p/>');p.append(checkbox,' ',label);self.checkboxContainer.append(p);});};SFProductFilter.prototype.filter=function(){var activeTags=this.checkboxContainer.find('input:checked').map(function(){return $(this).val();});var activeElements=this.items.filter(function(){var itemTags=$(this).data('tags');var flag=true;$.each(activeTags,function(index,tag){if($.inArray(tag,itemTags)>-1){return true;}
flag=false;return false;});return flag;});var resultsFound=activeElements.length;if(this.hideOnLoad===true&&activeTags.length===0){this.productList.hide();this.hiddenMessage.show();resultsFound=0;}
else{this.productList.show();this.hiddenMessage.hide();}
this.itemsFound.find('span').text(resultsFound);this.items.hide();activeElements.show();};jQuery.fn.sfProductFilter=function(options){options=options||{};return this.each(function(){var pf=new SFProductFilter(this,options);});};})();
