﻿$EditTaxonomies = function(taxonomyId, heading, divId, taxonomyInputsName, taxonomyRootPath) {
    // Logic
    $newWindow = window.open();

    var form = $newWindow.document.createElement("form");

    form.setAttribute("method", "POST");

    form.setAttribute("action", $taxonomySelectorUrl);

    // Add taxonomy ID
    $headingInput = $newWindow.document.createElement("input");

    $headingInput.setAttribute("type", "hidden");

    $headingInput.setAttribute("name", $taxonomyIdMetaName);

    $headingInput.setAttribute("value", taxonomyId);

    form.appendChild($headingInput);

    // Add heading
    $headingInput = $newWindow.document.createElement("input");

    $headingInput.setAttribute("type", "hidden");

    $headingInput.setAttribute("name", $taxonomyHeadingMetaName);

    $headingInput.setAttribute("value", heading);

    form.appendChild($headingInput);

    // Add taxonomy paths
    $taxonomyPathsInput = $newWindow.document.createElement("input");

    $taxonomyPathsInput.setAttribute("type", "hidden");

    $taxonomyPathsInput.setAttribute("name", $taxonomyPathsMetaName);

    $taxonomyPaths = '';

    $('#' + divId + ' ul li').each(function() {
        $value = $(this).text();

        if (0 == $taxonomyPaths.length) {
            $taxonomyPaths = $value;
        }
        else {
            $taxonomyPaths += ";" + $value;
        }
    });

    $taxonomyPathsInput.setAttribute("value", $taxonomyPaths);

    form.appendChild($taxonomyPathsInput);

    // Add taxonomy DIV ID
    $taxonomiesDivIdInput = $newWindow.document.createElement("input");

    $taxonomiesDivIdInput.setAttribute("type", "hidden");

    $taxonomiesDivIdInput.setAttribute("name", $taxonomiesDivIdMetaName);

    $taxonomiesDivIdInput.setAttribute("value", divId);

    form.appendChild($taxonomiesDivIdInput);

    // Add TaxonomyInputsName
    $inputsNameInput = $newWindow.document.createElement("input");

    $inputsNameInput.setAttribute("type", "hidden");

    $inputsNameInput.setAttribute("name", $taxonomyInputsNameMetaName);

    $inputsNameInput.setAttribute("value", taxonomyInputsName);

    form.appendChild($inputsNameInput);

    $newWindow.document.body.appendChild(form);

    // Add taxonomy root path
    $taxonomyRootPathInput = $newWindow.document.createElement("input");

    $taxonomyRootPathInput.setAttribute("type", "hidden");

    $taxonomyRootPathInput.setAttribute("name", $taxonomyRootPathMetaName);

    $taxonomyRootPathInput.setAttribute('value', taxonomyRootPath);

    form.appendChild($taxonomyRootPathInput);

    form.submit();

    return false;
};

$ClearTaxonomyPaths = function(divId) {
    $('#' + divId).html('');
};

$AddTaxonomyPaths = function(divId, taxonomyInputsName, taxonomyPaths) {
    $taxonomies = $('#' + divId);

    $taxonomies.append($(document.createElement("ul")));

    $(taxonomyPaths.split(";")).each(function(index, value) {
        $taxonomyItems = value.split("||");

        $taxonomyId = $taxonomyItems[0];

        $taxonomyPath = $taxonomyItems[1];

        $taxonomies.find("ul").append(
                $(document.createElement("li")).text($taxonomyPath)
                .append(
                    $(document.createElement("input")).attr(
                    {
                        type: 'hidden',
                        checked: true,
                        id: $taxonomyId,
                        name: taxonomyInputsName,
                        value: $taxonomyPath
                    })
                )
            );
    });

    $taxonomies.find(" :input:hidden").each(function() {
        $(this).attr('checked', 'checked');
    });
};
