(function($) {
$(document).ready(function() {
var titles = $(".collapsableContent .collapsableTitle, .collapsableTextContent .collapsableTextTitle");
var width = $(window).width();
var height = $(window).height();
// Loop through each spry
titles.each(function(index, element) {
$(this).find('span .fa-chevron-up').hide();
$(this).find('span .fa-chevron-down').show();
var heightTitle = $(this).outerHeight();
// Click listener
// Expands & closes the content section accordingly
$(this).click(function(e) {
var $this = $(this);
animate_spry($this);
});
// Deal with anchor functionality targeting sprys IDs
var jQueryparent = $(this).parent();
var theID = jQueryparent.attr("id");
if(theID) {
var paramPattern = new RegExp("[?|&]"+theID+"=","i");
if(paramPattern.exec(window.location.search))
{
var offset = jQueryparent.offset().top;
$("html, body")
.animate({ scrollTop: (offset-heightTitle)+"px" });
jQueryparent.toggleClass("contentOpened");
return;
}
}
jQueryparent.height(heightTitle);
});
// When using find on sprys, open them up.
$('.collapsableContent').scroll(function(){
var $this = $(this);
if (! $this.hasClass('contentOpened') ) {
$this.children('.collapsableTitle, .collapsableTextTitle').click();
}
});
// Open spry on keydown
$('.collapsableContent').keydown(function(e){
if (event.which == 13 || event.keyCode == 13) {
var $this = $(this);
$this.children('.collapsableTitle, .collapsableTextTitle').click();
}
});
// Recalculate height when resizing
$( window ).resize(function() {
if($(window).width() == width && $(window).height() == height){
return;
}
titles.each(function(index, element) {
var $this = $(this);
var heightTitle = $this.outerHeight();
var jQueryparent = $this.parent();
if( jQueryparent.hasClass("contentOpened") ){
// do not collapse the item that is open
jQueryparent.css("height", "auto");
return;
}
jQueryparent.height(heightTitle);
});
});
// Function to collapse / hide spry based on current state of the spry
animate_spry = function(curr_el) {
var jQueryparent = curr_el.parent();
if(jQueryparent.hasClass("contentOpened")) {
curr_el.find('span .fa-chevron-up').hide();
curr_el.find('span .fa-chevron-down').show();
var heightTitle = $(curr_el).outerHeight();
jQueryparent.animate({"height":heightTitle}, 500);
}
else {
curr_el.find('span .fa-chevron-up').show();
curr_el.find('span .fa-chevron-down').hide();
var oldHeight = jQueryparent.height();
var newHeight = jQueryparent.css("height", "auto").height();
jQueryparent
.css("height", oldHeight)
.animate({"height":newHeight},{duration: 500, complete: function(){
$(curr_el).height("");
}});
}
jQueryparent.toggleClass("contentOpened");
}
// pass a URL paramater to expand all sprys
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var expand = getUrlParameter('expandAll');
if( true === expand ) {
//open sprys
$( ".collapsableContent" ).each(function( i ) {
$(".collapsableContent").addClass('contentOpened');
$(".collapsableContent").css("height", "auto");
});
}
});
})(jQuery);