//NameSpace
var mainApp = mainApp || {};

//Direct Aangeroepen Functie Uitdrukking
//Immediately Invoked Function Expression
//http://benalman.com/news/2010/11/immediately-invoked-function-expression/
(function ($) {

	//Applicatie Introductie
	mainApp.bonedaddy = {
		name: "Bone Daddy",
		justChecking: function () {
		},
		introduction: function () {
			this.justChecking();
		}
	};

	//The Controller, een manier om de activatie van de objecten in het script te organizeren en gestructureerd aan te roepen.
	mainApp.controller = {

		init: function() {

			mainApp.bonedaddy.introduction(),
			mainApp.events.init(),
			mainApp.orderPage.init(),
            mainApp.mmenu.init(),
            mainApp.wayPoints.init()

		}

	};

	mainApp.events = {

		init: function() {

			var $el = $('ul.col-half--switch');

            $('section.col-half--switch__section article', $el).on('click', function (e) {
                e.preventDefault();

                var
                    self = $(this),
                    $li = self.parents('li');

                $li.find('section.expander').addClass('open');
                $li.find('article').addClass('open');

            });

            $('a.close', $el).on('click', function (e) {
                e.preventDefault();

                var
                    self = $(this),
                    $li = self.parents('li');

                $li.find('section.expander').removeClass('open');
                $li.find('article').removeClass('open');
            });
		}
	};

	// WooCommerce orderPage jQuery code for the __selected class on the plan.
	mainApp.orderPage = {

		init: function() {

			var $var_select = $("#pa_traffic-plan");

			// Init selected value; added by Volodymyr for WooCommerce backend
			$var_select.val($(".order-step__section__plans").find(".__selected .order-step__section__plans__plan__frame").data("value")).trigger("change");

			$(".order-step__section__plans > .order-step__section__plans__plan").on("click", function(e) {

				// If this method is called, the default action of the event will not be triggered.
                e.preventDefault();

                // Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
                e.stopPropagation();

                // .closest() For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.
                $(this).closest(".order-step__section__plans").find(".order-step__section__plans__plan").removeClass("__selected");
                $(this).addClass("__selected");

				// Magic when selecting plan :) ; added by Volodymyr for WooCommerce backend
                $var_select.trigger('focusin');
                $var_select.val($(this).find(".order-step__section__plans__plan__frame").data("value"));
                $var_select.trigger("change");

            });

            // Choose Payment period
            $(".frequency__input").change(function() {

                var $this = $(this);
                var $parent = $this.closest(".frequency");
                var $select = $('#' + $parent.attr('data-variation'));
                var variation = $this.val();
                var info = $(".frequency-info");

               switch (variation) {
                   case "monthly": info.show(); break;
                   case "one-time": info.hide(); break;
               }

                $select.trigger('focusin');
                $select.val(variation);
                $select.trigger('change');

            });

            // Select Monthly payment period
            $(".frequency .frequency__input:first").prop('checked', true).trigger("change");

            // Chosen init
            $(".chosen-select").chosen({
                max_selected_options: 5
            });

            // Pikaday.js and Moment.js variable and init on element #datepicker
            var picker = new Pikaday({
                field: document.getElementById('datepicker'),
                format: 'DD/MM/YYYY',
                minDate: moment().toDate(),
                firstDay: 1
            });
            picker.setDate(order.request.date ? order.request.date : moment().toDate());

            // Frequency
            //var $frequency_total = $(".price");
            //if ($(".frequency__input:checked").val() == "One Time") {
            //    $frequency_total.html("p/m");
            //}
            //$(".frequency__input").on("change", function(e) {
            //   switch ($(this).val()) {
            //       case "Monthly": $frequency_total.html("p/m"); break;
            //       case "One Time": $frequency_total.html(""); break;
            //   }
            //});

		}

	};

    // Mmenu.js settings
    mainApp.mmenu = {

        init: function() {

            $(function() {
 
                // initiate mmenu
                $("#sd-mmenu").mmenu({
                    // options go here
                }, {
                    // configuration:
                    clone : true,
                });
 
            });
            
        }

    };

    // Waypoints.js settings and Smooth Scroll settings
    mainApp.wayPoints = {

        init: function() {

            var waypoint = new Waypoint({
              element: document.querySelector(".site-header"),
              handler: function() {
                document.querySelector(".site-header").classList.toggle("fixed"),
                document.querySelector(".site-inner").classList.toggle("fixed-margin-top")
              }
            });
            
            var features = new Waypoint({
              element: document.querySelector("#home__features--1"),
              handler: function() {
                document.querySelector("#home__features--1").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#home__features--2"),
              handler: function() {
                document.querySelector("#home__features--2").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#home__features--3"),
              handler: function() {
                document.querySelector("#home__features--3").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#home__features--4"),
              handler: function() {
                document.querySelector("#home__features--4").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#section--1"),
              handler: function() {
                document.querySelector("#section--1").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#section--2"),
              handler: function() {
                document.querySelector("#section--2").classList.add("animate")
              },
                offset: '70%'
            });
            
            var features = new Waypoint({
              element: document.querySelector("#section--3"),
              handler: function() {
                document.querySelector("#section--3").classList.add("animate")
              },
                offset: '70%'
            });

            smoothScroll.init({
                speed: 1000
            });
            
        }

    };

	mainApp.controller.init();

})(jQuery);