(function($) {
    $.fn.reza_mobilebar = function() {

        function mobbar_loading(main) {
            if (main !== undefined) {
                $('.rd-mobbar').addClass('rd-mobbar-active');
                var text_html = $('header .rd-nav-social').html();
                var text_html = $('header .rd-nav-contact-us').html();
                var contact_html = $('header .rd-nav-contact-us').html();
                var call_html = $('header .rd-nav-call').html();
                var out = '';
                out += main;
                var social_html = $('.rd-nav-social [class*="rd-social-icon"]').parent().html();
                out += '<div class="rd-nav-content-warp">';
                if (social_html !== undefined) {
                    out += '<div class="rd-nav-mobbar-social"><li>' + social_html + '</li></div>';
                }
                var text_html = $('header .rd-nav-text-header').html();
                if (text_html !== undefined) {
                    out += '<div class="rd-nav-text-header">' + text_html + '</div>';
                }

                var contact_html = $('header .rd-nav-contact-us').html();
                if (contact_html) {
                    out += '<div class="rd-nav-contact-us">' + contact_html + '</div>';
                }

                var call_html = $('header .rd-nav-call').html();
                if (call_html !== undefined) {
                    out += '<div class="rd-nav-call">' + call_html + '</div>';
                }
                out += '</div>';

                $('.rd-mobbar-warp').append(out);
            }
        }
        jQuery(document).on("click", ".rd-mobbar-close", function() {
            $('.rd-mobbar').removeClass('rd-mobbar-active');
            $('.rd-mobbar-warp').html('');
        });


        jQuery('.rd-toolbar-mobile').on("click", ".rd-nav-mobile-menu a", function() {

            var html = $(this).parent().next().html();

            var out = '<ul class="rd-nav-mobbar-menu">';
            if (html !== undefined) {
                out += html;
            }
            out += '</ul>';

            var wish_html = $('.rd-toolbar-mobile .rd-nav-wish').html();

            if (wish_html !== undefined) {
                out += '<div class="rd-nav-wish">' + wish_html + '</div>';
            }

            mobbar_loading(out);
        });

        jQuery(document).on("click", ".rd-nav-mobbar-menu i,.rd-mob-drop i", function() {
            if ($(this).parent().hasClass('rd-mob-drop-active')) {
                $(this).parent().parent().find('.rd-mob-drop-active').each(function(index, element) {
                    $(this).removeClass('rd-mob-drop-active');
                });
            } else {
                $(this).parent().parent().find('li').each(function(index, element) {
                    $(this).removeClass('rd-mob-drop-active');
                });
                $(this).parent().addClass('rd-mob-drop-active');
                $(this).parent().find('> .rd-mob-drop > .rd-mob-drop').parent().addClass('rd-mob-drop-active');
            }
        });


        jQuery('.rd-toolbar-mobile').on("click", ".rd-nav-mobile-login a", function() {
            if ($('.rd-nav-login > li > ul').hasClass('rd-drop')) {
                var html = $('.rd-nav-login > li > ul').html();
            } else {
 				var html = $(this).parents('.rd-nav-mobile-login').find('.rd-nav-login').html();

            }
            if (html !== undefined) {
                mobbar_loading('<ul class="rd-nav-mobbar-login">' + html + '</div>');
            }
        });



        jQuery('.rd-toolbar-mobile').on("click", ".rd-nav-mobile-search a", function() {
            var classes = $('.rd-nav-search').attr('class');
            var html = $('.rd-nav-search .rd-search').parent().html();
            mobbar_loading('<div class="' + classes + '">' + html + '</div>');
            jQuery('.rd-mobbar').find('.rd-nav-search select,.rd-nav-search  > li > a').remove();
            jQuery('.rd-mobbar').find('.rd-nav-search').removeClass('rd-search-woo');
        });


        jQuery('.rd-toolbar-mobile').on("click", ".rd-nav-mobile-cart a", function() {
            var html = $('header .widget_shopping_cart_content').html();
            mobbar_loading('<div class="rd-nav-mobbar-cart"><div class="widget_shopping_cart_content">' + html + '</div></div>');
        });
    }
})(jQuery);