/*!
 * Admin script of trainifyframework
 * @package MatrixAddons
 * @subpackage trainifyframework
 */
 (function($){
    'use strict';
    var winWidth, winHeight, TrainifyFramework_Front;
    var trainify_framework_document = $(document);
    TrainifyFramework_Front = {

        //Custom Snipits goes here
        Snipits: {
            
            Variables: function(){
                winWidth = $(window).width();
                winHeight = $(window).height();
            },

            MegaMenuPosts: function(){

                var megamenu_terms_item = $(this);
                var ajax_running = megamenu_terms_item.attr('data-running');
                var megamenu_tab_class = megamenu_terms_item.data('tab');
                megamenu_terms_item.siblings('li').removeClass('active-item');
                megamenu_terms_item.addClass('active-item');
                $('.'+megamenu_tab_class).siblings('.trainify-framework-megamenu-terms-posts').removeClass('active-item');
                $('.'+megamenu_tab_class).addClass('active-item');

                if(ajax_running){
                    return;
                }
                var megamenu_term_link = megamenu_terms_item.children('a');
                var ajax_args = megamenu_term_link.data('config');
                ajax_args.beforeSend = function(){
                    megamenu_terms_item.attr('data-running', 'true');
                    $('.' + megamenu_tab_class ).siblings('.trainify-framework-menu-preloader').removeClass('hidden');
                };
                ajax_args.success = function(data, status, settings){
                    console.log(data);
                    if(!data.megamenu_html){
                        return;
                    }
                    $('.' + megamenu_tab_class + ' .trainify-framework-menu-posts-wrap' ).html(data.megamenu_html);
                    $('.' + megamenu_tab_class ).siblings('.trainify-framework-menu-preloader').addClass('hidden');
                    
                };
                ajax_args.fail = function( xhr, textStatus, errorThrown ){
                    console.warn('Sorry faild megamenu ajax call');
                };
                $.ajax(ajax_args);

            },

            Reaction_Values: function(evt){
                var reaction_icon_image = $(this);
                var reaction_single_item = reaction_icon_image.closest('.trainify-framework-reaction-single-item');
                var ajax_args = reaction_single_item.data('reaction-config');
                ajax_args.beforeSend = function(){
                    reaction_single_item.find('.trainify-framework-reaction-image-wrap').removeClass('active');
                    reaction_icon_image.addClass('active');
                };
                ajax_args.success = function(data, status, settings){
                    console.log('request successful');
                    console.log(data);
                };
                ajax_args.fail = function( xhr, textStatus, errorThrown ){
                    console.warn('Sorry ajax url not found.')
                };
                $.ajax(ajax_args);
            },
        },     

        Events: function(){

            var __this = TrainifyFramework_Front;
            var snipits = __this.Snipits;

            var reaction_values = snipits.Reaction_Values;
            trainify_framework_document.on('click', '.trainify-framework-reaction-image-wrap', reaction_values);

            var megamenu_terms_posts = snipits.MegaMenuPosts;
            trainify_framework_document.on('hover', '.trainify-framework-term-list-item', megamenu_terms_posts);


        },

        Ready: function(){
            var __this = TrainifyFramework_Front;
            var snipits = __this.Snipits;

            //This is  functions
            snipits.Variables();
            __this.Events();

        },

        Load: function(){

        },

        Resize: function(){

        },

        Scroll: function(){

        },

        Init: function(){
            var __this = TrainifyFramework_Front;
            var docready = __this.Ready;
            var winload = __this.Load;
            var winresize = __this.Resize;
            var winscroll = __this.Scroll;
            $(document).ready(docready);
            $(window).load(winload);
            $(window).scroll(winscroll);
            $(window).resize(winresize);
        },

     };
     
     TrainifyFramework_Front.Init();

})(jQuery);