(function($, undefined){
var defaultOptions={
event: 'click', 
hideText: 'Fold', 
showText: 'Unfold', 
titleWrapper: '<h2/>', 
titleText: undefined, 
titleAsHtml: false, 
titleSearchSelector: 'h1,h2,h3,h4,h5,h6', 
defaultFold: false, 
showTime: 100, 
hideTime: 100, 
animationTime: 'slow', 
init_done: false,
};
var api={
log: function (){
console.log(this);
},
toggle: function (){
var options=$(this).data('foldable').options;
return this.find('.foldable-row').trigger(options.event + '.foldable');
},
show: function (){
var options=$.extend({ 'action': 'show' }, defaultOptions, $(this).data('foldable').options||{ });
;
return api.showHide(this.find('.foldable-row'), options, true);
},
hide: function (){
var options=$.extend({ 'action': 'hide' }, defaultOptions, $(this).data('foldable').options||{ });
;
return api.showHide(this.find('.foldable-row'), options, true);
},
showHide: function(object, options, returnValue){
$object=$(object);
var hideTime=options.hideTime;
var showTime=options.showTime;
var animationTime=options.animationTime;
var action=(options.action) ? options.action:'toggle';
var $elementToFoldUnfold=$object.siblings('.foldable-wrapper');
var $btn=$object.find('.foldable-button');
var $descriptionTitle=$elementToFoldUnfold.find('.cmtoc_table_of_contents_description');
if(action==='show'){
$elementToFoldUnfold.slideDown(animationTime);
$btn.text(options.hideText);
}else{
$elementToFoldUnfold.slideUp(animationTime);
$btn.text(options.showText);
}
$object.toggleClass('unfolded folded');
return returnValue;
},
init: function(options){
var $this=$(this), data=$this.data('foldable');
options=$.extend({ }, defaultOptions, options||{ });
if(options.defaultFold){
var quickOptions=$.extend({ quickHide: true }, options);
}
if(!data){
$(this).data('foldable', {
options: options
});
}
return this.each(function (){
var $foldableTitleText;
var $foldable=this;
var $foldableWrapper=$('<div class="foldable-wrapper" />');
if(options.defaultFold){
var $foldableRow=$('<div class="foldable-row folded" />');
var $button=$('<span class="foldable-button screen-only">' + options.showText + '</button>');
}else{
var $foldableRow=$('<div class="foldable-row unfolded" />');
var $button=$('<span class="foldable-button screen-only on">' + options.hideText + '</button>');
}
if(! $($foldable).find(".foldable-wrapper").length){
$($button).appendTo($foldableRow);
$($foldable).children().wrapAll($foldableWrapper);
$($foldable).prepend($foldableRow);
}
var $title=$($foldable).find(options.titleSearchSelector);
if($title.length > 0){
$foldableTitleText=$($title[0]).text();
}
if(options.titleText){
if(typeof options.titleText==='function'){
var test=options.titleText;
$foldableTitleText=test.apply($foldable);
}else{
$foldableTitleText=options.titleText;
}}
if($foldableTitleText){
var $foldableTitle=$(options.titleWrapper, {
"class": 'foldable-title'
});
if(options.titleAsHtml){
if(typeof $foldableTitleText==='object'){
$foldableTitleText.detach();
$foldableTitle.append($foldableTitleText.html());
}
else if(typeof $foldableTitleText==='string'){
$foldableTitle.html($foldableTitleText);
}}else{
$foldableTitle.text($foldableTitleText);
}
$foldableRow.prepend($foldableTitle);
}
$($foldable).on(options.event + '.foldable', '.foldable-row', function(e){
e.stopPropagation();
e.preventDefault();
var $target=$(e.target), returnValue=true;
var $closestFoldableRow=$target.closest('.foldable-row');
if($target.hasClass('foldable-button') ) returnValue=false;
if($closestFoldableRow.hasClass('unfolded')){
options.action='hide';
}else{
options.action='show';
}
api.showHide($closestFoldableRow, options, returnValue);
});
});
},
destroy: function (){
return this.each(function (){
var $this=$(this),
data=$this.data('foldable');
var options=data.options;
var $foldableWrapper=$this.find('.foldable-wrapper');
var $foldableRow=$this.find('.foldable-row');
$foldableRow.find('.folded').trigger(options.event);
$foldableWrapper.children().unwrap();
$foldableRow.off('.foldable');
$foldableRow.remove();
$this.removeData('foldable');
});
}};
$.fn.foldable=function(options){
if(api[options]){
return api[options].apply(this, Array.prototype.slice.call(arguments, 1) );
}
else if(typeof options==='object'||!options){
return api.init.apply(this, arguments);
}else{
$.error('Method ' + options + ' does not exist on jQuery.foldable');
}};})(jQuery);