﻿$.fn.hierarchycalView = function (options) {

    var url = options.url;
    var _this = this;
    $('.children-box .list-view', _this).listView(
		{
		    enableSorting: true,
		    sortOptions:
			{
			    sortDirections: [0],
			    orderBy: ['data-itemTitle']
			},
		    enableFilter: true,
		    filterOptions:
			{
			    type: 'quick',
			    quickFilterTitle: 'найти',
			    quickFilterAttr: 'data-itemTitle',
			    quickFilterMinChars: 1
			}
		}
	);
    var ccbCss = 'children-box-good';
    if ($.browser.msie) {
        ccbCss = 'children-box-ie';
    }
    var pcbCss = 'parent-item-good';
    if ($.browser.msie) {
        pcbCss = 'parent-item-ie';
    }

    $('.children-box', _this).addClass(ccbCss);
    $('.parent-item', _this).addClass(pcbCss);
//    $('.parent-item', _this).hover(
//		function (e) {
//		    $(this).css('background-color', 'dimgray');
//		    $(this).html('<span>Подняться на уровень выше</span>');
//		    $(this).css('cursor', 'pointer');
//		    $(this).css('font-size', '12px');
//		    $('.parent-item').corner('5px top');
//		},
//		function (e) {
//		    $(this).css('background-color', '#c0c0c0');
//		    $(this).html('<span class="shadow-text">' + $(this).attr('data-itemText') + '</span>');
//		    $(this).css('cursor', 'default');
//		    $(this).css('font-size', '14px');
//		    $('.parent-item').corner('5px top');
//		}
//	);

    $('.child-item', _this).hover(function (e) {
        $(this).css('cursor', 'pointer');
        $(this).css('background-color', '#c0c0c0');
        $('span', this).css('color', 'white');
        $(this).corner('5px');
    },
	function (e) {
	    $(this).css('cursor', 'default');
	    $(this).css('background-color', '#f8f8ff');
	    $('span', this).css('color', 'dimgray');
	});


	$('.child-item, .hierarchy-box .level-up a', _this).die('click');
    $('.child-item, .hierarchy-box .level-up a', _this).live('click', function (e) {

        var nodeId = $(this).attr('data-itemId');
        var nodeUrl = $(this).attr('data-navigationUrl');
        $.ajax({
            type: "POST",
            url: url,
            data: { nodeId: nodeId },
            dataType: "html",
            success: function (data) {
                $(_this).html(data);
                $('.children-box .inner-container', _this).hide();
                $('.children-box .inner-container', _this).show('slide', { direction: 'right' }, 500);
                if ($('.hierarchy-box', _this).attr('data-deepestLevelReached') == 'True') {
                    document.location.href = nodeUrl;
                }
                $(_this).hierarchycalView({ url: url })
                updateUi();
            }
        });
        return false;
    });
}
