(function ($, undefined) {

    $(document).ready(function () {


        /* Side */
        var lastScrollPos = 0;

        $(window).on('scroll.sleep', function (e) {
            if ($(this).scrollTop() < lastScrollPos || $(this).scrollTop() < 60) {
                $('#side').removeClass('asleep');
            } else {
                $('#side').addClass('asleep');
            }
            lastScrollPos = $(this).scrollTop();
        });

        /* Menu */
        $('#menu .menu-toggler').on('click', function (e) {
            $('body').toggleClass('menu-open');
            if (!$('body').hasClass('menu-open')) {
                $('#menu').data('menu').options.closeall.call(this);
            }
        });

        $('#menu .menu-wrapper').on('click', function (e) {
            if (e.target == this) {
                $('#menu').data('menu').options.closeall.call(this);
                e.preventDefault();
            }
        });

        // iOS fix for closing menu
        if (/iPad|iPhone|iPod/.test(navigator.platform)) {
            $('#menu').on('click', function(e) {
                if ($('body').hasClass('menu-open')) {
                    $('#menu').data('menu').options.closeall.call(this);
                }
            });
        }
        
        $('#menu .menu-back').on('click', function (e) {
            var li = $('#menu li.open').last();

            if (li.length > 0) {
                var ul = li.parent(),
                    title = $.trim(ul.closest('li').find('> a').text()) || $(this).data('default'),
                    to = ((ul.data('level') - 1) * -100);

                $('span', this).html(title);

                if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                    li.removeClass('open');
                } else {
                    TweenLite.to($('#menu .menu-container > ul'), 0.3, {xPercent: to, ease: Power2.easeInOut, onComplete: function () {
                        li.removeClass('open');
                    }});
                }

            } else {
                $('#menu').data('menu').options.closeall.call(this);
            }
            e.preventDefault();
        });

        $('#menu li a[href="#"]').on('click', function (e) {
            var li = $(this).parent().addClass('open'),
                to = ($('> .menu-sub > ul', li).data('level') - 1) * -100;

            $('#menu .menu-back span').html($.trim($('> a', li).text()));

            if (!(/iPad|iPhone|iPod/.test(navigator.platform))) {
                TweenLite.to($('#menu .menu-container > ul'), 0.3, {xPercent: to, ease: Power2.easeInOut});
            }

            e.preventDefault();
        });

        $('#menu').menu({
            start: function () {
                $('body').addClass('menu-hover');
            },
            open: function (el) {
                el.closest('.menu-sub').addClass('open');
            },
            close: function (el) {
                el.closest('.menu-sub').removeClass('open');
            },
            end: function () {
                $('body').removeClass('menu-hover');
            },
            closeall: function () {
                if (!(/iPad|iPhone|iPod/.test(navigator.platform))) {
                    TweenLite.set($('.menu-container > ul', this.element), {xPercent: 0, delay: 0.3});
                }
                $('.menu-back span', this.element).html($('.menu-back', this.element).data('default'));
                $('li.open', this.element).removeClass('open');
                $('body').removeClass('menu-open');
            }
        });

        /* Search */
        $('#search > button').on('click.search', function (e) {
            if (!$(this).parent().hasClass('open')) {
                $(this).parent().find('input').focus();
                e.preventDefault();
            }
            $(this).parent().toggleClass('open');
        });

        $(document).on('click.search', function (e) {
            var el = $('#search');
            if (!$(e.target).closest($('button', el)).length && e.target != $('input[name="search"]', el)[0]) {
                el.removeClass('open');
            }
        });

        /* Selectall */
        $('input[type="checkbox"].selectall').selectall({
            select: function (items) {
                $('.actions .actions-options').trigger('toggle', [items]);
            }
        });

        /* Responsive tables */
        $('.table').table();

        /* Actions */
        $('.actions .actions-options').on('toggle', function (e, items) {
            var selected = items.filter(function () {
                return $(this).is(':checked');
            });
            if (selected.length) {
                $(this).removeClass('hidden');
            } else {
                $(this).addClass('hidden');
            }
        });

        /* Thumbs */
        $('.thumb').on('click', function (e) {
            var thumbs = $(this).parent().find('.thumb'),
                checkbox = $(':checkbox', this);

            $(this).toggleClass('selected');
            checkbox.prop('checked', !checkbox.is(':checked'));

            $('.actions .actions-options').trigger('toggle', [$(':checkbox', thumbs)]);

            if (/iPad|iPhone|iPod/.test(navigator.platform)) {
                this.reverse();
            } else {
                TweenLite.to($(this), 0.2, {z: -30, ease: Power2.easeInOut, onComplete: function () {
                    this.reverse();
                }});
            }

            e.stopImmediatePropagation();
        });

        $('.thumb').on('contextmenu', function (e) {
            if (e.which === 3) {
                $('a[data-toggle="dropdown"]', this).dropdown('toggle');
                e.preventDefault();
            }
        });

        $('.thumb').hammer().bind('press', function (e) {
            $('a[data-toggle="dropdown"]', this).dropdown('toggle');
        });

        $('.thumb a[data-toggle="dropdown"]').on('click', function (e) {
            $(this).dropdown('toggle');
            e.stopImmediatePropagation();
        });

        $(document).on('click.thumb', function (e) {
            var that = this,
                thumbs = $('.thumb');

            $.each(thumbs, function () {
                if (!e.target || (e.target !== that && !$(e.target).closest($(this)).length)) {
                    var checkbox = $(':checkbox', this);

                    $(this).removeClass('selected');
                    checkbox.prop('checked', false);

                    $('.actions .actions-options').trigger('toggle', [$(':checkbox', thumbs)]);
                }
            });
        });



        $('form .form-actions').each(function () {

            var org_actions = $(this),
                new_actions = $(org_actions.html()),
                target = $('header .form-actions-container');


            new_actions.find('button[id][type="submit"]').each(function () {
                var new_btn = $(this),
                    org_btn = org_actions.find('#' + new_btn.attr('id'));

                    new_btn
                        .attr('data-id', new_btn.attr('id'))
                        .removeAttr('id')
                        .attr('type', 'button')
                        .on('click', function () {
                            org_btn.trigger('click');
                        });
            });

            target.append(new_actions);


        });

    });

})(jQuery);