window.project=(window.project)?window.project:{};(function($)
{project.ExpandableBehaviour=function(dom_element,options)
{this.dom_element=dom_element;this.ul_element=null;this.li_elements=[];this.button_element=null;this.options={"trim_margin":false,"additional_trim":0,"visible_rows":1,"animate":true,"row_height":null,"txt_btn_open":"alle anzeigen","txt_btn_close":"schließen","overflow":"hidden"};$.extend(this.options,options);this.is_open=false;this.open_height=0;this.close_height=0;this.initialize();};project.ExpandableBehaviour.prototype.initialize=function()
{var that=this;var expandable_label_id="expandable_label_"+project.getNextIdSequence();var expandable_body_id="expandable_body_"+project.getNextIdSequence();var expandable_button_id="expandable_button_"+project.getNextIdSequence();this.ul_element=this.dom_element.find("ul").first();this.li_elements=this.ul_element.children("li");this.li_elements_length=this.li_elements.length;this.dom_element.find("h2").first().attr("id",expandable_label_id);this.ul_element.attr({"id":expandable_body_id,"aria-expanded":false,"aria-labelledby":expandable_label_id});this.ul_element.css("overflow",this.options.overflow);this.button_element=this.dom_element.find(".js_expandable_button").first();this.button_element.attr({"id":expandable_button_id,"tabindex":0,"role":"button","aria-expanded":false,"aria-controls":expandable_body_id});if(!this.options.row_height)
{var li_element=$(this.li_elements[0]);this.options.row_height=li_element.outerHeight(true);}
this.li_elements_each_row=Math.floor(this.ul_element.innerWidth()/li_element.outerWidth());var li_element_margin=li_element.outerHeight(true)-li_element.outerHeight();var rows=Math.ceil(this.li_elements_length/this.li_elements_each_row);var tmp_li_index=0;for(var i=0;i<rows;i++)
{var tmp_max_row_height=0;for(var k=0;k<this.li_elements_each_row;k++)
{if(tmp_max_row_height<$(this.li_elements[tmp_li_index]).outerHeight(true))
{tmp_max_row_height=$(this.li_elements[tmp_li_index]).outerHeight(true);}
tmp_li_index++;}
this.open_height+=tmp_max_row_height;if(i===(this.options.visible_rows-1))
{this.close_height=this.open_height;}}
if(this.options.trim_margin)
{this.open_height=this.open_height-(li_element_margin+this.options.additional_trim);this.close_height=this.close_height-(li_element_margin+this.options.additional_trim);}
this.ul_element.css("height",this.close_height);if(this.options.visible_rows*this.li_elements_each_row>=this.li_elements_length)
{this.dom_element.find(".js_expandable_button").first().css("display","none");return;}
this.addEvents();this.invalidate();};project.ExpandableBehaviour.prototype.addEvents=function()
{var that=this;var button_function=function()
{if(that.is_open)
{that.close();that.button_element.removeClass("expanded");that.button_element.text(that.options.txt_btn_open);}
else
{that.open();that.button_element.addClass("expanded");that.button_element.text(that.options.txt_btn_close);}};this.button_element.bind({"click":button_function,"keyup":function(event)
{if(event.which===13)
{button_function();}}});};project.ExpandableBehaviour.prototype.invalidate=function()
{var i=this.options.visible_rows*this.li_elements_each_row;for(i;i<this.li_elements_length;i++)
{$(this.li_elements[i]).css("display",(this.is_open)?"":"none");}};project.ExpandableBehaviour.prototype.open=function()
{if(this.is_open===true)
{return;}
var that=this;this.is_open=true;this.invalidate();this.button_element.attr("aria-expanded",true);this.ul_element.attr("aria-expanded",true);if(this.options.animate)
{this.ul_element.animate({"height":this.open_height},{"duration":500,"queue":false,"complete":function()
{if(that.is_open===false)
{that.invalidate();}}});}
else
{this.ul_element.css("height",this.open_height);}};project.ExpandableBehaviour.prototype.close=function()
{if(this.is_open===false)
{return;}
var that=this;this.is_open=false;this.button_element.attr("aria-expanded",false);this.ul_element.attr("aria-expanded",false);if(this.options.animate)
{this.ul_element.animate({"height":this.close_height},{"duration":500,"queue":false,"complete":function()
{if(that.is_open===false)
{that.invalidate();}}});}
else
{this.ul_element.css("height",this.close_height);this.invalidate();}};JsBehaviourToolkit.registerHandler("expandable",project.ExpandableBehaviour);})(jQuery);
