(function(q){function m(h){var i=q.data(h,"accordion").options;var j=q.data(h,"accordion").panels;var f=q(h);if(i.fit==true){var g=f.parent();g.addClass("panel-noscroll");i.width=g.width();i.height=g.height()}if(i.width>0){f._outerWidth(i.width)}var a="auto";if(i.height>0){f._outerHeight(i.height);var b=j.length?j[0].panel("header").css("height","").outerHeight():"auto";var a=f.height()-(j.length-1)*b}for(var e=0;e<j.length;e++){var c=j[e];var d=c.panel("header");d._outerHeight(b);c.panel("resize",{width:f.width(),height:a})}}function p(d){var b=q.data(d,"accordion").panels;for(var a=0;a<b.length;a++){var c=b[a];if(c.panel("options").collapsed==false){return c}}return null}function o(c,d,e){var f=q.data(c,"accordion").panels;for(var b=0;b<f.length;b++){var a=f[b];if(a.panel("options").title==d){if(e){f.splice(b,1)}return a}}return null}function r(a){var d=q(a);d.addClass("accordion");if(d.attr("border")=="false"){d.addClass("accordion-noborder")}else{d.removeClass("accordion-noborder")}var b=d.children("div[selected]");d.children("div").not(b).attr("collapsed","true");if(b.length==0){d.children("div:first").attr("collapsed","false")}var c=[];d.children("div").each(function(){var e=q(this);c.push(e);t(a,e,{})});d.bind("_resize",function(e,f){var g=q.data(a,"accordion").options;if(g.fit==true||f){m(a)}return false});return{accordion:d,panels:c}}function t(a,c,b){c.panel(q.extend({},b,{collapsible:false,minimizable:false,maximizable:false,closable:false,doSize:false,tools:[{iconCls:"accordion-collapse",handler:function(){var d=q.data(a,"accordion").options.animate;if(c.panel("options").collapsed){l(a);c.panel("expand",d)}else{l(a);c.panel("collapse",d)}return false}}],onBeforeExpand:function(){var d=p(a);if(d){var e=q(d).panel("header");e.removeClass("accordion-header-selected");e.find(".accordion-collapse").triggerHandler("click")}var e=c.panel("header");e.addClass("accordion-header-selected");e.find(".accordion-collapse").removeClass("accordion-expand")},onExpand:function(){var d=q.data(a,"accordion").options;d.onSelect.call(a,c.panel("options").title)},onBeforeCollapse:function(){var d=c.panel("header");d.removeClass("accordion-header-selected");d.find(".accordion-collapse").addClass("accordion-expand")}}));c.panel("body").addClass("accordion-body");c.panel("header").addClass("accordion-header").click(function(){q(this).find(".accordion-collapse").triggerHandler("click");return false})}function s(d,e){var f=q.data(d,"accordion").options;var a=q.data(d,"accordion").panels;var b=p(d);if(b&&b.panel("options").title==e){return}var c=o(d,e);if(c){c.panel("header").triggerHandler("click")}else{if(b){b.panel("header").addClass("accordion-header-selected");f.onSelect.call(d,b.panel("options").title)}}}function l(c){var a=q.data(c,"accordion").panels;for(var b=0;b<a.length;b++){a[b].stop(true,true)}}function k(d,e){var a=q.data(d,"accordion").options;var b=q.data(d,"accordion").panels;l(d);e.collapsed=e.selected==undefined?true:e.selected;var c=q("<div></div>").appendTo(d);b.push(c);t(d,c,e);m(d);a.onAdd.call(d,e.title);s(d,e.title)}function n(f,a){var b=q.data(f,"accordion").options;var c=q.data(f,"accordion").panels;l(f);if(b.onBeforeRemove.call(f,a)==false){return}var d=o(f,a,true);if(d){d.panel("destroy");if(c.length){m(f);var e=p(f);if(!e){s(f,c[0].panel("options").title)}}}b.onRemove.call(f,a)}q.fn.accordion=function(a,b){if(typeof a=="string"){return q.fn.accordion.methods[a](this,b)}a=a||{};return this.each(function(){var d=q.data(this,"accordion");var c;if(d){c=q.extend(d.options,a);d.opts=c}else{c=q.extend({},q.fn.accordion.defaults,q.fn.accordion.parseOptions(this),a);var e=r(this);q.data(this,"accordion",{options:c,accordion:e.accordion,panels:e.panels})}m(this);s(this)})};q.fn.accordion.methods={options:function(a){return q.data(a[0],"accordion").options},panels:function(a){return q.data(a[0],"accordion").panels},resize:function(a){return a.each(function(){m(this)})},getSelected:function(a){return p(a[0])},getPanel:function(a,b){return o(a[0],b)},select:function(a,b){return a.each(function(){s(this,b)})},add:function(a,b){return a.each(function(){k(this,b)})},remove:function(a,b){return a.each(function(){n(this,b)})}};q.fn.accordion.parseOptions=function(a){var b=q(a);return{width:(parseInt(a.style.width)||undefined),height:(parseInt(a.style.height)||undefined),fit:(b.attr("fit")?b.attr("fit")=="true":undefined),border:(b.attr("border")?b.attr("border")=="true":undefined),animate:(b.attr("animate")?b.attr("animate")=="true":undefined)}};q.fn.accordion.defaults={width:"auto",height:"auto",fit:false,border:true,animate:true,onSelect:function(a){},onAdd:function(a){},onBeforeRemove:function(a){},onRemove:function(a){}}})(jQuery);