/* * jquery - collapser - plugin v1.0 * http://www.aakashweb.com/ * copyright 2010, aakash download by http://www.codefans.net * released under the mit license. */ (function($){ $.fn.collapser= function(options, beforecallback, aftercallback) { var defaults = { target: 'next', targetonly: null, effect: 'slide', changetext: false, expandhtml: 'expand', collapsehtml: 'collapse', expandclass: '', collapseclass:'' }; var options = $.extend(defaults, options); var exphtml,collhtml, effectshow, effecthide; if(options.effect == 'slide'){ effectshow = 'slidedown'; effecthide = 'slideup'; }else{ effectshow = 'fadein'; effecthide = 'fadeout'; } if(options.changetext == true){ exphtml = options.expandhtml; collhtml = options.collapsehtml; } function callbeforecallback(obj){ if(beforecallback !== undefined){ beforecallback.apply(obj); } } function callaftercallback(obj){ if(aftercallback !== undefined){ aftercallback.apply(obj); } } function hideelement(obj, method){ callbeforecallback(obj); if(method == 1){ obj[options.target](options.targetonly)[effecthide](); obj.html(exphtml); obj.removeclass(options.collapseclass); obj.addclass(options.expandclass); }else{ $(document).find(options.target)[effecthide](); obj.html(exphtml); obj.removeclass(options.collapseclass); obj.addclass(options.expandclass); } callaftercallback(obj); } function showelement(obj, method){ callbeforecallback(obj) if(method == 1){ obj[options.target](options.targetonly)[effectshow](); obj.html(collhtml); obj.removeclass(options.expandclass); obj.addclass(options.collapseclass); }else{ $(document).find(options.target)[effectshow](); obj.html(collhtml); obj.removeclass(options.expandclass); obj.addclass(options.collapseclass); } callaftercallback(obj); } function toggleelement(obj, method){ if(method == 1){ if(obj[options.target](options.targetonly).is(':visible')){ hideelement(obj, 1); }else{ showelement(obj, 1); } }else{ if($(document).find(options.target).is(':visible')){ hideelement(obj, 2); }else{ showelement(obj, 2); } } } return this.each(function(){ if($.fn[options.target] && $(this)[options.target]()){ $(this).toggle(function(){ toggleelement($(this), 1); },function(){ toggleelement($(this), 1); }); }else{ $(this).toggle(function(){ toggleelement($(this), 2); },function(){ toggleelement($(this), 2); }); } // initialize if($.fn[options.target] && $(this)[options.target]()){ if($(this)[options.target]().is(':hidden')){ $(this).html(exphtml); $(this).removeclass(options.collapseclass); $(this).addclass(options.expandclass); }else{ $(this).html(collhtml); $(this).removeclass(options.expandclass); $(this).addclass(options.collapseclass); } }else{ if($(document).find(options.target).is(':hidden')){ $(this).html(exphtml); }else{ $(this).html(collhtml); } } }); }; })(jquery);