﻿(function ($) {
    $.fn.treemenu = function (options) {
        var defaults = {
            ajaxContainerId: 'treemenu-content',
            ajaxLoadingText: '<p>loading..</p>',
            ajaxOptions: {},
            animDuration: 'fast',
            folderTooltip: 'click to expand/collapse',
            rememberState: false,
            startCollapsed: true,
            treeHeight: 'auto',
            treeWidth: 'auto'
        };
        var opts = $.extend(defaults, options);

        function getState($uls) {
            return $uls.map(function () {
                if ($(this).is(':hidden')) {
                    return 0
                } else {
                    return 1
                }
            }).get().join(',')
        }
        function treemenuCookie(p, id, arr) {
            if (opts.rememberState) {
                if (p === 'set') {
                    document.cookie = 'treemenu-' + id + '=' + escape(arr)
                } else {
                    var dc = document.cookie;
                    var index = dc.indexOf('treemenu-' + id + '=');
                    if (index >= 0) {
                        if (p === 'check') {
                            return true
                        }
                        var start = index + id.length + 7;
                        var end = dc.indexOf(';', start);
                        if (start !== id.length) {
                            if (end === -1) {
                                end = dc.length
                            }
                            return unescape(dc.substring(start, end))
                        }
                    }
                    return false
                }
            }
            return false
        }
        return this.each(function () {
            var $tree = $(this);
            var treeId = $tree.attr('id');
            $tree.addClass('treemenu-tree').css({
                'height': opts.treeHeight,
                'width': opts.treeWidth
            }).find('li:not(:last-child) > ul').addClass('treemenu-line-fix').end().find('li').prepend('<span class="treemenu-line"></span>').not(':has(ul)').addClass('icon-text').end().has('ul').prepend('<span class="treemenu-plus" title="' + opts.folderTooltip + '"></span>').delegate('.treemenu-plus, a[href="#"]', 'click', function (e) {
                $(this).siblings('ul').slideToggle(opts.animDuration, function () {
                    treemenuCookie('set', treeId, getState($tree.find('ul')))
                }).siblings('.treemenu-plus').toggleClass('treemenu-minus');
                return false
            });
            $tree.delegate('a[rel*="treemenu-ajax"]', 'click', function () {
                $('#' + opts.ajaxContainerId).html(opts.ajaxLoadingText);
                var ajaxDefault = {
                    dataType: 'html',
                    url: $(this).attr('href'),
                    error: function (xhr) {
                        $('#' + opts.ajaxContainerId).html(xhr.statusText + ' ' + xhr.status)
                    },
                    success: function (data) {
                        $('#' + opts.ajaxContainerId).html(data)
                    }
                };
                $.ajax($.extend(ajaxDefault, opts.ajaxOptions));
                return false
            });
            if (treemenuCookie('check', treeId)) {
                var uls = treemenuCookie('get', treeId).split(',');
                var ulIndex = 0;
                $tree.find('ul').each(function () {
                    if (parseInt(uls[ulIndex], 10)) {
                        $(this).siblings('.treemenu-plus').addClass('treemenu-minus')
                    } else {
                        $(this).hide()
                    }
                    ulIndex += 1
                })
            } else if (opts.startCollapsed) {
                //$tree.find('ul').hide().end().find('.treemenu-expand').show().siblings('.treemenu-plus').addClass('treemenu-minus')
                $tree.find('ul').hide().end().find('ul>li.CMSListMenuHighlightedLI').parent().show().end().find('ul>li.CMSListMenuHighlightedLI').show().siblings('.treemenu-plus').addClass('treemenu-minus')
            } else {
                $tree.find('.treemenu-collapse').hide().end().find('ul:not(.treemenu-collapse)').siblings('.treemenu-plus').addClass('treemenu-minus')
            }
            if ($.browser.msie) {
                $tree.addClass('treemenu-ie')
            }
        })
    }
}(jQuery));

