// Wait for the <div> to be loaded
$( document ).ready(function() {
	setTimeout(addLinksToPortalBanner, 100, 0);
});

// Convert the plain text of the banner to a link
function addLinksToPortalBanner(idx) {	
	if ( $( ".announcement-container" ).length ) {
		var HTML_ELEMENT = '.title-announcement'; // We can add the link to different elements using their classes: .text-container // .icon-title // .title-announcement

		// Execute again if the announcements are expanded and new options are displayed
		// Click on div
		$( ".text-container" ).on( "click", function() {
			addLinksToPortalBanner();
		});
		// Click on button 'EXPAND ALL / HIDE ALL'
		$( ".sp-announcement-list-actions" ).on( "click", function() {
			addLinksToPortalBanner();
		});

		// Add the link of the hidden button 'Learn More' to the HTML_ELEMENT selected
		$( ".announcement-container" ).each(function( index ) {
			var messageOriginalColor = $( this ).find( ".info-link" ).css("color");
			var announcementLinkUrl = $( this ).find( ".info-link" ).attr('href');
			var announcementLinkTarget = $( this ).find( ".info-link" ).attr('target');
			$( this ).find( HTML_ELEMENT ).html('<a href="' + announcementLinkUrl + '" target="' + announcementLinkTarget + '" style="color:' + messageOriginalColor + ';">' + $( this ).find( HTML_ELEMENT ).html() + '</a>');
		});

		// Add style to the links
		$( HTML_ELEMENT ).hover(function(){
			$(this).css("text-decoration", "underline");
		}, function(){
			$(this).css("text-decoration", "none");
		});
	} else if(idx < 10){
		// If the container was not loaded yet, wait and retry until 10 times (avoid infinite loop if there is no announcement)
		setTimeout(addLinksToPortalBanner, idx*100, ++idx); // after each execution, the timeout is incremented 
	}
}