﻿/// <summary>
/// Creates and shows table of contents (innehållsförteckning) 
/// for Article1.aspx and SegmentList2.aspx
/// </summary>
/// <param name="headers">The headers to parse (i.e ".c-Text h2")</param>
/// <param name="tableOfContents">The element to show table of contents in (i.e ".c-TableOfContents")</param>
/// <param name="tableOfContentsHeader">Header for table of contents (i.e "Innehåll")</param>
/// <param name="tableOfContentsWrapper">Surrounding element for tableOfContents (i.e ".c-ImageRightFloat")</param>
/// <returns>void</returns>
function CreateTableOfContents(headers, tableOfContents, tableOfContentsHeader, tableOfContentsWrapper)
{
    //if more than one header => make table of contens
    if ( $(headers).length> 1) {

        //show wrapper object (table hidden by inline css)
        $(tableOfContentsWrapper).show();
        
        $(tableOfContents).append("<b>" +tableOfContentsHeader +"</b><ul></ul>");
        
        $(headers).each(function(index) {

            //get current object 
            var $this = $(this);
            
            //set current object id to index+1
            $this.attr("id", index+1);
            
            //get headline
            var headline = ("<li><a href=\"#" +$this.attr("id") +"\">" +$this.text() +"</a></li>");
            
            //add headline to list
            $(tableOfContents +' ul').append(headline);
            
            });
    }
}

function CreateTableOfContentsOnAlma(headers, tableOfContents, tableOfContentsHeader, tableOfContentsWrapper) {
    //if more than one header => make table of contens
    if ($(headers).length > 1) {

        //show wrapper object (table hidden by inline css)
        $(tableOfContentsWrapper).show();

        $(tableOfContents).addClass("article-contents");

        $(tableOfContents).append("<div class=\"article-contents-wrapper\"></div>");

        $(".article-contents-wrapper").append("<div class=\"article-contents-title\">" + tableOfContentsHeader + "</div>");
        $(".article-contents-wrapper").append("<ul></ul>");

        $(headers).each(function(index) {

            //get current object 
            var $this = $(this);

            //set current object id to index+1
            $this.attr("id", index + 1);

            //get headline
            var headline = ("<li><a href=\"#" + $this.attr("id") + "\">" + $this.text() + "</a></li>");

            //add headline to list
            $('.article-contents-wrapper ul').append(headline);
        });
    }
}