﻿$(function (){
    $.fn.categories = function(options){
        var defaults = {
            showCategoryBtnCssClass: 'more',        
            hideCategoryBtnCssClass: 'close',
            moreCategoriesPanelCssClass:'more-categories-panel', 
            speed:'fast', //("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000). 
            headerCssClass: 'expand-collapse-header',
            contentCssClass: 'expand-collapse-content',
            collapseCssClass:'collapse',
            expandCssClass:'expand', 
            moreCssClass:'more-cat',
            lessCssClass:'less-cat',
            moreCategories:'more-categories' , 
            closeIconCssClass:'close-icon' ,                    
            withArrow:false,
            activeCssClass:'active',
            withAnimation:true,
            subCategoryOpenCloseCssClass:'open_close_category',
            subCategoryListCssClass:'sub-list'
        } 
        var options =  $.extend(defaults, options);  
        return this.each(function() { 
           var o = options;  
           var obj = $(this);
           var moreCategoriesPanelObj = $("."+ o.moreCategoriesPanelCssClass, obj);
           var openCategoriesObj=$("."+ o.showCategoryBtnCssClass, obj);
           var closeCategoriesObj=$("."+ o.hideCategoryBtnCssClass, obj);
           var headerObj =$("."+ o.headerCssClass, obj);
           var contentObj =$("."+ o.contentCssClass, obj);
           
           openCategoriesObj.click(function(){
              moreCategoriesPanelObj.slideDown(o.speed);
              closeCategoriesObj.show();
              $(this).hide();
           });
           closeCategoriesObj.click(function(){
              moreCategoriesPanelObj.slideUp(o.speed);
              openCategoriesObj.show();
              $(this).hide();
           });
           //מונציפליות לקטגוריות שנפתחות רוחבית
           var childs = $('.with-childs', obj);
           childs.each(function (){
                var child_container =  $(this);
                var height =child_container.height();
                var moreCategory =  $('.' + o.moreCategories, this);
                var moreCategory_width = moreCategory.width();
                moreCategory.css("height",height);
                var moreBtn =  $('.' + o.moreCssClass, this);
                var lessBtn =  $('.' + o.lessCssClass, this);
                moreBtn.click(function(){
                        child_container.addClass("opened");
                        moreCategory.animate({width:moreCategory_width}, o.speed);
                        $(this).hide();
                        lessBtn.show();

                });
                lessBtn.click(function(){
                        child_container.removeClass("opened");
                        moreCategory.animate({width:'0'}, o.speed);
                        lessBtn.hide();
                        moreBtn.show();
                });
//                $('.' + o.closeIconCssClass, this).click(function(){
//                        child_container.removeClass("opened");
//                        moreCategory.animate({width:'0'}, o.speed);
//                        moreBtn.show();
//                        lessBtn.hide();
//                });                
                moreCategory.css({width:'0', overflow:'hidden'});
                
                
           });
           
           $('.' + o.subCategoryOpenCloseCssClass).click(function(){
            var obj =  $('.' + o.subCategoryListCssClass +':first', $(this).parents(".cat-item"));
             if (obj.is(":hidden")) {
                obj.slideDown(o.speed);
                $(this).removeClass(o.expandCssClass);
              } else {
                obj.slideUp(o.speed);
                $(this).addClass(o.expandCssClass);
              }
           // obj.css("border","solid 2px #000");
           });
         
           
           moreCategoriesPanelObj.hide();
           //show hide all the content
           headerObj.click(function(){
           
           if (contentObj.is(":hidden")) {
                contentObj.slideDown(o.speed);
                $('.expand-all', this).removeClass(o.collapseCssClass + '-all');
              } else {
                contentObj.slideUp(o.speed);
                $('.expand-all', this).addClass(o.collapseCssClass + '-all');
              }
           });
           
           
        });   
    }
})