﻿//
// Featured List User
//

//FeaturedListList
//   TagName:
//   ListTag:
//   List:
//       DocID, Title, ItemTag, TitleTag
var FeaturedList = new Array();

// adds created featured tag to the data listing...
function AddFeaturedList(listtag, titletag, tagname, tagid)
{
    var newlist =
    {
        TagName: tagname,
        TagID: tagid,
        TitleTag: titletag,
        ListTag: listtag,
        List: []
    };


    FeaturedList.push(newlist);
}

// utility: finds a featured list by tagname
function FindFeaturedList(TagID)
{
    var result = null;

    $.each(FeaturedList, function (index, element)
    {
        if (element.TagID == TagID)
        {
            result = element;
            return false;
        }
    });

    return result;
}

// adds a new item visually and in the data listing...
function AddFeaturedListItem(TagID, DocID, Title, LinkOrder)
{
    var findlist = FindFeaturedList(TagID);

    if (findlist != null)
    {
        var listitem = $("<li>" + Title + "</li>");

        listitem.click(function ()
        {
            //its weird changing the text value, have to use .val()
            //$("#txtSearch").val(strTitle);
            RunSearchExact(DocID);
        });

        //Add to the data list.
        var newitem =
        {
            DocID: DocID,
            Title: Title,
            ItemTag: listitem,
            Order: LinkOrder
        };

        //test: log the order as they come in.
        //console.log(Title + " = " + LinkOrder);

        // if linkorder is -1, append to the end of the list.
        if (LinkOrder == -1)
        {

            findlist.List.push(newitem);
            findlist.ListTag.append(listitem);
        }
        else
        {
            // insertion sorted
            var binserted = false;
        
            $.each(findlist.List, function (index, element)
            {
                var elementindex = element.Order;

                if (elementindex != -1)
                {
                    if (LinkOrder < elementindex)
                    {
                        //console.log("placed before: " + element.Title + " = " + elementindex);

                        findlist.List.splice(index, 0, newitem);
                        element.ItemTag.before(listitem);
                        binserted = true;
                        return false;
                    }
                }
                else
                {
                    //console.log("placed before: " + element.Title + " = " + elementindex);

                    findlist.List.splice(index, 0, newitem);
                    element.ItemTag.before(listitem);

                    binserted = true;
                    return false;
                }
            });

            if(!binserted)
            {
                //console.log("no less orders found, placed at end of list.");

                findlist.List.push(newitem);
                findlist.ListTag.append(listitem);   
            }
        }

    }
}


//First, grab all the lists we need to display
$.post(baseUrl + "handlers/GetTagsByTypeName.ashx",
{
    TagTypeName: "Featured"
},
function (data)
{
    //for each featured tag
    $.each(data, function (index, element)
    {
        var TagID = element.TagID;
        var tagname = element.TagName;

        var listdiv = $("<div class=\"featuredlistuser\">");
        var heading = $("<h2>" + tagname + "</h2>");
        var listelement = $("<ul>");

        //create the featurelist tags
        listdiv.append(heading);
        listdiv.append(listelement);

        $("#featuredlists").append(listdiv);

        AddFeaturedList(listelement, heading, tagname, element.TagID);

        //now we need to grab all the featured documents for this tag...
        $.post(baseUrl + "handlers/GetTagDocs.ashx",
        {
            TagTypeName: "Featured",
            TagName: tagname
        },
        function (data)
        {

            $(data.ResultSet).each(function ()
            {
                var strTitle;
                var strURL;
                var strDocLocation = null;
                var strLinkTitle = null;
                var intLinkOrder = -1;

                $.each(this.DocTags, function ()
                {
                    switch (this.TagInfo.TypeInfo.TagTypeName)
                    {
                        case "Title":
                            strTitle = this.TagInfo.TagName;
                            break;
                        case "Featured":
                            {
                                // find the alternative LinkTitle!
                                $.each(this.Notes, function ()
                                {
                                    if (this.Title == "LinkTitle")
                                    {
                                        strLinkTitle = this.Note;
                                    }
                                    else if (this.Title == "Order")
                                    {
                                        intLinkOrder = this.Note;
                                    }
                                });

                                break;
                            }
                    }
                });

                if (strLinkTitle != null)
                {
                    strTitle = strLinkTitle;
                }

                AddFeaturedListItem(TagID, this.DocID, strTitle, intLinkOrder);

            });

        });

    });

});


