<script>
window.testimonials = [];
{% for testimonial in site.data.testimonials %}
window.testimonials.push({
	title: '{{ testimonial.title | smartify }}',
	author: '{{ testimonial.author | smartify }}',
	wpusername: '{{ testimonial.wpusername | smartify }}',
	link: '{{ testimonial.link | smartify }}',
	avatar: '{{ testimonial.avatar | smartify }}',
	content: '{{ testimonial.content | smartify }}'
});
{% endfor %}

function getRandomNumberExcept( length, max ) {
	var arr = [],
	    rnd;
	while ( arr.length < length ) {
		rnd = Math.ceil( Math.random() * max );
		if ( arr.indexOf( rnd ) > -1 ) {
			continue;
		}
		arr[ arr.length ] = rnd;
	}

	return arr;
}

function getMediaObjectHTML( key ) {
	var testimonial = window.testimonials[ key ],
	    html        = '';

	html += '<div class="media-object">';
		html += '<div class="media-object-section person">';
		if ( 'undefined' !== typeof testimonial.avatar && testimonial.avatar ) {
			html += '<img src="' + testimonial.avatar + '" alt="Testimonial Author">';
		} else {
			html += '<span class="missing-person-wrapper"><i class="fa fa-user missing-avatar" aria-hidden="true"></i></span>';
		}
		html += '</div>';
		html += '<div class="media-object-section">'
			html += '<h4><a href="' + testimonial.link + '" alt="See testimonial on wordpress.org" target="_blank">' + testimonial.title + '</a></h4>';
			html += '<blockquote>' + testimonial.content + '</blockquote>';
			html += '<div class="testimonial-author" aria-label="Missing author avatar">';
				html += '<i class="fa fa-chevron-right" aria-hidden="true"></i> ';
				if ( 'undefined' !== testimonial.author && testimonial.author ) {
					html += '<span class="author-name">' + testimonial.author + '</span> ';
				}
				html += '<a href="https://profiles.wordpress.org/' + testimonial.wpusername + '" class="author-wpusername" aria-label="author username">@' + testimonial.wpusername + '</span>';
			html += '</div>';
		html += '</div>';
	html += '</div>';

	return html;

}
$( document ).ready( function() {
	var rnd = getRandomNumberExcept( 4, window.testimonials.length - 1 ),
	    openGrid  = '<div class="grid-x grid-margin-x">',
	    openCell  = '<div class="single-testimonial cell medium-6">',
	    closeGrid = '</div>',
	    closeCell = '</div>',
	    html = '';

	html += '<h4 class="section-title">What people say about Kirki</h4>';
	html += openGrid + openCell + getMediaObjectHTML( rnd[0] ) + closeCell + openCell + getMediaObjectHTML( rnd[1] ) + closeCell + closeGrid;
	html += openGrid + openCell + getMediaObjectHTML( rnd[2] ) + closeCell + openCell + getMediaObjectHTML( rnd[3] ) + closeCell + closeGrid;

	jQuery( '#testimonials' ).append( html );
});
</script>
