﻿$(document).ready(function() {
    //initializing send timer loop
    setInterval("sendProcessor()", 500);
    $(window).unload(sendProcessor());

    //Assigning the filtering processing logic
    $('.filter').click(function() {
        //todo group box togle

        if ($(this).is(":checked")) {


        } else {
            // setting the hidden type
            setCommand("/Shared/FilterBox/RegisterFilter/" + $(this).attr('filter') + "?mode=set");
        }
        filterItems();
    });

    //Applying the default filters
    filterItems();

    //Assigning the filtering processing logic
    $('.filterOr').click(function() {
        if ($(this).is(":checked")) {
            setCommand("/Shared/FilterBox/RegisterFilter/" + $(this).attr('filter') + "?mode=clear");
        } else {
            // setting the hidden type
            setCommand("/Shared/FilterBox/RegisterFilter/" + $(this).attr('filter') + "?mode=set");
        }

        filterItems();
    });

    //Assigning the visibility logic
    $('.containerVisibility').click(function() {
        $('#' + $(this).attr('target')).toggle();

        $(this).empty();
        if ($('#' + $(this).attr('target') + ":visible").length > 0) {
            $(this).append("<img src='/Content/icons/up_arrow.png'/>");
        } else {
            $(this).append("<img src='/Content/icons/down_arrow.gif'/>");
        }

        // Change the image
    });

    $('.containerVisibility').each(function() {
        $(this).empty();
        if ($('#' + $(this).attr('target') + ":visible").length > 0) {
            $(this).append("<img src='/Content/icons/up_arrow.png'/>");
        } else {
            $(this).append("<img src='/Content/icons/down_arrow.gif'/>");
        }
    });
});

var commands = Array();

function sendProcessor() {
    while(commands.length >0) $.post(commands.pop());
}

function setCommand(uri) {
    commands.push(uri);
}

function filterItems() {
    $('.filteringItem').hide();
    //show with the visible id
    $.each($('.filterOr:checked'), function(i, val) {
        if (val.checked) $('.' + val.attributes['filter'].value).show();
    });

    $.each($('.filter'), function(i, val) {
        if (!val.checked) $('.' + val.attributes['filter'].value).hide();
    });

    //applying the odd rows highliting
    $('.filteringItem').removeClass('pagerItem');
    $('.rowItem').removeClass('evenRow');
    $('.header').hide();
    $('.rowItem:visible:even').addClass('evenRow');
    $('.filteringItem:visible').addClass('pagerItem');
    $('.header').show();

    if (jQuery.historyCallback != undefined) {
        var hash = location.hash;
        hash = hash.replace(/^.*#/, '');
        // moves to a new page. 
        // pageload is called at once. 
        // hash don't contain "#", "?"
        Pager(hash);
    }
}

$(".groupBox").click(function() {
    //change image (unselect/select all)

    if ($(this).attr('src') == 'unchecked') {
        $($(this).attr('filter')).attr('checked', 'checked')

        //change image
    } else {
        $($(this).attr('filter')).removeAttr('checked');
        //change image
    }
});

function Pager(hash) {
    //1. Определение текущей страницы
    var currentPage = 1;

    if (hash) {
        // restore ajax loaded state
        if ($.browser.msie) {
            // jquery's $.load() function does't work when hash include special characters like aao.
            hash = encodeURIComponent(hash);
        }
        currentPage = parseInt(hash.replace('#', ''));
    }

    $("#pager").empty();

    //3. Отобразить внизу слайдер количества страниц
    var pageLength = 5;
    $(".pagerItem").hide();
    var count = $(".pagerItem").size();
    var pages;
    if (count % pageLength > 0) pages = (count - (count % pageLength)) / pageLength + 1;
    else pages = count / pageLength;

    if (pages > 1) {

        if (currentPage != 1) {
            $("&nbsp;<a href='#" + (currentPage - 1) + "'><<</a>&nbsp;").appendTo("#pager");
        }

        for (var iPage = 1; iPage <= pages; iPage++) {
            if (iPage != currentPage)
                $("<a href='#" + iPage + "'>" + iPage + "</a>").appendTo("#pager");
            else
                $("<span>" + iPage + "</span>").appendTo("#pager");
        }

        if (currentPage != pages) {
            $("&nbsp;<a href='#" + (currentPage + 1) + "'>>></a>&nbsp;").appendTo("#pager");
        }

        $("#pager a").click(function() {
            // 
            var hash = this.href;
            hash = hash.replace(/^.*#/, '');
            // moves to a new page. 
            // pageload is called at once. 
            // hash don't contain "#", "?"
            $.historyLoad(hash);
            return false;
        });
    }


    //4. Скрыть элементы вне фильтра
    // 0 - первый элемент
    var startIndex = (currentPage - 1) * pageLength;
    var finishIndex = (currentPage) * pageLength - 1;
    $(".pagerItem").filter(function(index) {
        if (index >= startIndex && index <= finishIndex) return true;
        return false;
    }).show();

}

function confirmDelete() {
    if (confirm('Вы действительно хотите выполнить удаление?')) this.parentNode.submit();
}