﻿function ColumnFormating2(containerId, columns, tag) {
    var countItems = $('#' + containerId).find(tag).length;
    var countRootItems = $('#' + containerId).find(tag + '.root').length;
    var perColumn = Math.ceil(countItems / columns);
    var arrayRootItems = [];
    var arrayItems = [];
    var counter = -1;

    $('#' + containerId + ' ' + tag).each(function () {
        if ($(this).hasClass('root')) {
            if (arrayItems.length > 0) {
                arrayRootItems.push(arrayItems);
            }
            arrayItems = [];
            counter++;
        }

        arrayItems.push($(this));
    });
    arrayRootItems.push(arrayItems);

    var currentCountColumn = 1;
    var iteration = 0;

    $('#' + containerId + ' > *').remove();
    $('#' + containerId).append('<table id="' + containerId + '_table"><tr></tr></table>');
    for (i = 1; i <= columns; i++) {
        $('#' + containerId + '_table tr').append('<td id="' + containerId + '_column_' + i + '" class="column"></td>');
    }

    for (var i = 0; i < arrayRootItems.length; i++) {
        if (iteration > perColumn * currentCountColumn) {
            currentCountColumn++;
        }
        for (j = 0; j < arrayRootItems[i].length; j++) {
            $('#' + containerId + '_column_' + currentCountColumn).append(arrayRootItems[i][j]);
            $('#' + containerId + '_column_' + currentCountColumn).append('<br />');
            iteration++;
        }
    }
}



function ColumnFormating(containerId, columns, tag) {
    var countItems = $('#' + containerId).find(tag).length;
    var countRootItems = $('#' + containerId).find(tag + '.root').length;
    var perColumn = Math.ceil(countItems / columns);
    var arrayRootItems = [];
    var arrayItems = [];
    var counter = -1;

    $('#' + containerId + ' ' + tag).each(function () {
        if ($(this).hasClass('root')) {
            if (arrayItems.length > 0) {
                arrayRootItems.push(arrayItems);
            }
            arrayItems = [];
            counter++;
        }

        arrayItems.push($(this));
    });
    arrayRootItems.push(arrayItems);

    var currentCountColumn = 1;
    var iteration = 0;

    $('#' + containerId + ' > *').remove();

    for (i = 0; i < arrayRootItems.length; i++) {
        $('#' + containerId).append('<table id="' + containerId + '_table_' + i + '"></table>');
        for (j = 0; j < arrayRootItems[i].length; j++) {
            if (arrayRootItems[i][j].hasClass('root')) {
                $('#' + containerId + '_table_' + i).append('<tr><th id="' + containerId + '_td_' + i + j + '" colspan="' + columns + '"></th></tr>');
            }
            else {
                if (j % 3 == 1) {
                    $('#' + containerId + '_table_' + i).append('<tr><td id="' + containerId + '_td_' + i + j + '"></td><td id="' + containerId + '_td_' + i + (j + 1) + '"></td><td id="' + containerId + '_td_' + i + (j + 2) + '"></td></tr>');
                }
            }
            if (tag == 'a' && $(arrayRootItems[i][j]).hasClass('root')) {
                $('#' + containerId + '_td_' + i + j).append($(arrayRootItems[i][j]).text());
            }
            else {
                $('#' + containerId + '_td_' + i + j).append(arrayRootItems[i][j]);
            }
        }
    }
}

function ColumnFormatingAccordion(containerId, columns, tag) {
    $('.' + containerId).each(function (q) {
        var countItems = $(this).find(tag).length;
        var countRootItems = $(this).find(tag + '.root').length;
        var perColumn = Math.ceil(countItems / columns);
        var arrayRootItems = [];
        var arrayItems = [];
        var counter = -1;

        $(this).find(tag).each(function () {
            if ($(this).hasClass('root')) {
                if (arrayItems.length > 0) {
                    arrayRootItems.push(arrayItems);
                }
                arrayItems = [];
                counter++;
            }

            arrayItems.push($(this));
        });
        arrayRootItems.push(arrayItems);

        var currentCountColumn = 1;
        var iteration = 0;

        $(this).find('*').remove();
        var table_id = containerId + '_table_' + q;
        $(this).append('<table id="' + table_id + '" width="100%" border="0" cellpadding="0" cellspacing="0"><tr></tr></table>');
        for (i = 1; i <= columns; i++) {
            var td_id = containerId + '_' + q + '_column_' + i;
            $('#' + table_id).append('<td id="' + td_id + '" width="49%"></td>');
        }

        for (var i = 0; i < arrayRootItems.length; i++) {
            if (iteration >= perColumn * currentCountColumn) {
                currentCountColumn++;
            }
            for (j = 0; j < arrayRootItems[i].length; j++) {
                $('#' + containerId + '_' + q + '_column_' + currentCountColumn).append(arrayRootItems[i][j]);
                $('#' + containerId + '_' + q + '_column_' + currentCountColumn).append('<br />');
                iteration++;
            }
        }
    });
}


function ChangeColorScheme(schemaName) {
    $('#colorSchema').attr('href', appThemesPath + schemaName + '/' + schemaName + '.css');
    $('#carouselColorSchema').attr('href', scriptsPath + 'jcarousel/skins/' + schemaName + '/skin.css');

    $.cookie('ColorScheme', schemaName, { expires: 365, path: '/' });

    if ($('#tagscloud').length > 0)
    {
        $('#tagscloud').load(handlersPath + 'TagsCloudWrap.aspx');
    }
}



function ChangeAccordionPanel(panelIndex) {
    $.cookie('AccordionPanel', panelIndex, { expires: 365, path: '/' });
}
