(function($) {

    $.fn.buttonval = function(){
        if ($.browser.msie) { // ie workaround
            var label = $(this).text();
            $(this).text('');
            var value = $(this).val();
            $(this).text(label);
            return value;
        } else {
            return $(this).val();
        }
    };

    $.fn.idle = function(time){
        var elem = $(this);
        elem.queue(function(){
            setTimeout(function(){
                elem.dequeue();
            }, time);
        });
    };

    $.fn.disable = function() {
        return this.each(function() {
            $(this).addClass('disabled');
            $(this).attr('disabled', 'disabled');
            $(this).css('opacity', 0.7);
        });
    };

    $.fn.enable = function() {
        return this.each(function() {
            $(this).removeClass('disabled');
            $(this).removeAttr('disabled');
            $(this).css('opacity', 1);
        });
    };

    /**
     * menu
     */
    $.fn.navigation = function() {
        return this.each(function() {
            var container = $(this);

            $('ul li ul', container).hide();
            $('li.active ul', container).show();
            $('ul li', container).hover(
                function() {
                    $(this).addClass('current').find('ul').show(); //.css({'display': 'none', 'overflow': 'hidden', 'height': '0px'}).animate({'height': '100px'});
                },
                function() {
                    $(this).removeClass('current');
                    if (!$(this).is('.active')) {
                        $(this).find('ul').hide(); //.css({'overflow': 'hidden', 'height': '0px'}).animate({'height': '0px'});
                    }
                });
        });
    };

    /**
     * navigation
     */
    $.fn.navigationAdmin = function() {
        return this.each(function() {
            var container = $(this);

            $('li', container).prepend('<div class="dropzone"></div>');
            $('dl, .dropzone', container).droppable({
                accept: '#nav li',
                tolerance: 'pointer',
                drop: function(e, ui) {
                    var li = $(this).parent();
                    var child = !$(this).hasClass('dropzone');
                    if (child && li.children('ul').length == 0) {
                        li.append('<ul/>');
                    }
                    if (child) {
                        li.addClass('sm2_liOpen').removeClass('sm2_liClosed').children('ul').append(ui.draggable);
                    } else {
                        li.before(ui.draggable);
                    }
                    $('#nav li.sm2_liOpen').not(':has(li:not(.ui-draggable-dragging))').removeClass('sm2_liOpen');
                    li.find('dl,.dropzone').css({backgroundColor: '', borderColor: ''});

                    var source = ui.draggable.attr('id');
                    var target = li.attr('id');
                    var append = (li.attr('class').split(' ').length > 2);

                    $.ajax({
                        url: '/pages/admin/sort/source/' + source + '/target/' + target + '/append/' + append
                    });
                },
                over: function() {
                    $(this).filter('dl').css({backgroundColor: '#ccc'});
                    $(this).filter('.dropzone').css({borderColor: '#aaa'});
                },
                out: function() {
                    $(this).filter('dl').css({backgroundColor: ''});
                    $(this).filter('.dropzone').css({borderColor: ''});
                }
            });
            $('li.draggable', container).draggable({
                handle: '> dl span.move',
                opacity: .8,
                addClasses: false,
                helper: 'clone',
                zIndex: 100
            });
        });
    };

})(jQuery);

