﻿/// <reference path="../javascript/jQueryIntellisense.js"/>
/// <reference path="/Standard/Core/Javascript/Relational.Standard.Core.js"/>

Type.registerNamespace('Relational.Standard.Core');
Type.registerNamespace('Relational.ListManagementTools');
Type.registerNamespace('Website.Behavior');

$(function() {

    // Wire up sortable lists
    $("div._ContentSortableList").sortable({
        axis: "y",
        cursor: "move",
        containment: "parent",
        items: "div._SortableItem",
        update: function() {
            // When a drag completes, put the new ordered list of IDs in the results holder textbox so they can be processed on postback
            var sortedList = $(this).sortable("toArray");
            var sortedListText = ''
            // Loop through the items as currently ordered and build a comma-separated list of IDs
            for (var i = 0; i < sortedList.length; i++) {
                if (sortedList[i].length > 0) {
                    var matches = sortedList[i].match(/.*_(\d+)/) // Items will be of the form ArticleContentList_123; this extracts the numeric ID
                    if (matches) {
                        id = String(matches[1]);
                        sortedListText = sortedListText + id + ',';
                    };
                }
            }
            // Store comma-separated list in the hidden sort results holder
            var sortResultsHolder = $("textarea._SortResultsHolder", this.parentNode).val(sortedListText);
            Relational.ListManagementTools.refreshHighlights(this);
            Relational.ListManagementTools.showSaveChangesButton(this);
        }
    }).each(function() {
        // Highlight top N items in list on startup
        Relational.ListManagementTools.refreshHighlights(this);
    })

    // Wire up doubleclick behavior for draggable items
    $("div.SortableItem").dblclick(function(event) {
        $(this).addClass("Highlight");
        window.navigate($(this).attr("Relational_Target"));
        event.stopPropagation();
    });

    // Wire up onchange behavior for ItemCount textbox
    $("input._ItemCountTextbox").change(function() {
        // Find the sortable list that's in the same instance as me
        var sortableList = $("div._ContentSortableList", this.parentNode.parentNode.parentNode);
        if (sortableList.length > 0) {
            Relational.ListManagementTools.refreshHighlights(sortableList);
        }
        Relational.ListManagementTools.showSaveChangesButton(this.parentNode.parentNode);
    });

});

Relational.ListManagementTools.refreshHighlights = function(el) {
    var itemCount = $("input._ItemCountTextbox", $(el)[0].parentNode).val();
    $("div._SortableItem", el).each(function(i) {
        if (i <= itemCount - 1) {
            $(this).removeClass("Fade");
        } else {
            $(this).addClass("Fade");
        }
    })
}

Relational.ListManagementTools.showSaveChangesButton = function(el) {
    $("div.SaveButtonPanel", el.parentNode).slideDown();
}
