// Custom page actions
;(function($){
  'use strict';

  window.sandboxTheme = function(){
    var $body = $('body'),
        eventNames = ['resize', 'scroll', 'ready', 'load', 'sticky'],
        readyStates = ['interactive', 'complete'],
        trigger_event = function(){
          var feature,
              featureName,
              eventName = typeof arguments[0] !== 'undefined' ? arguments[0] : null;

          if ( eventName && $.inArray(eventName, eventNames) !== -1 ) {
            // Trigger all features
            for ( featureName in sandboxTheme.features ) {
              if ( sandboxTheme.features.hasOwnProperty(featureName) ) {
                if ( sandboxTheme.info['feature-' + featureName] ) {
                  feature = sandboxTheme.features[featureName];
                  if ( feature.hasOwnProperty(eventName) ) {
                    feature[eventName]();
                  }
                }
              }
            }
          }
        },
        ready_events_register = function(){
          trigger_event('ready');
          $(window).on('scroll', function(){
            trigger_event('scroll');
          });
          $(window).on('debouncedresize', function(){
            trigger_event('resize');
          });
        },
        setup = function setup(){
          var featureName,
              feature,
              featureOptions,
              dataName,
              dataValue,
              data = arguments[0];

          sandboxTheme.info = {};
          sandboxTheme.page = {};

          if ( $.isPlainObject(data) ) {
            for ( dataName in data ) {
              if ( data.hasOwnProperty(dataName) ) {
                dataValue = dataName === 'id' ? parseInt(data[dataName]) : data[dataName];

                if ( dataName !== 'info') {
                  sandboxTheme.page[dataName] = dataValue;
                }
              }
            }
            if ( data.hasOwnProperty('info') ) {
              for ( dataName in data.info ) {
                if ( data.info.hasOwnProperty(dataName) ) {
                  sandboxTheme.info[dataName] = data.info[dataName];
                }
              }
            }
          }

          $.log(data);

          // Loop through all features to set all neccessary data
          for ( featureName in sandboxTheme.features ) {
            if ( sandboxTheme.features.hasOwnProperty(featureName) ) {
              feature = sandboxTheme.features[featureName];

              // Make a copy of merged page feature options and feature options
              // jQuery.extend() will take care for a boolean value of options.page.features[featureName]
              featureOptions = $.extend(true, {}, feature.options || {});

              // Merge the feature with common feature functions, options and info
              $.extend(true, feature, {
                options: featureOptions,
                info: {
                  name: featureName,
                  /**
                   * Lookup for a hash fragment that starts with the features name
                   * false if no match is found
                   */
                  hash: window.location.hash.length > 0 && window.location.hash.match('^#' + featureName) ? window.location.hash.replace('#' + featureName + '-', '') : false,
                }
              });
              sandboxTheme.info['feature-' + featureName] = Boolean(feature.setup());
            }
          }

          // Execute events
          if ( $.inArray(document.readyState, readyStates) !== -1 ) {
            ready_events_register();
          } else {
            $(document).on('DOMContentLoaded readyStates', function(){
              ready_events_register();
            });
          }
          $(window).on('load', function(){
            trigger_event('load');
          });
        };

    return {
      init: function init(){
        setup(arguments[0] || null);
      },
      $body: $body,
      is_page: function is_page(){
        var id = typeof arguments[0] !== 'undefined' ? parseInt(arguments[0]) : null;

        if ( id ) {
          return id === sandboxTheme.page.id;
        } else {
          return sandboxTheme.info.is_page;
        }
      },
      get_page: function get_page(){
        var property = typeof arguments[0] === 'string' ? arguments[0] : null;

        if ( property ) {
          if ( sandboxTheme.page.hasOwnProperty(property) ) {
            return sandboxTheme.page[property];
          } else {
            return 'ERROR: no page property called "' + property + '"';
          }
        } else {
          return sandboxTheme.page;
        }
      },
      trigger: function trigger(eventName){
        var eventName = typeof arguments[0] !== 'undefined' ? arguments[0] : null;

        if ( eventName && $.inArray(eventName, eventNames) !== -1 ) {
          trigger_event(eventName);
        }
      }
    };
  }();

  sandboxTheme.features = {


    /**
     * Scrolled
     * ========
     * Controlls a className to be set, when the page has been scrolled
     * Any layout based on this behaviour is at style.css
     */
    scrolled: {
      info: {
        isScrolled: false
      },
      scroll: function scroll(){
        if (this.$window.scrollTop() > 100){
          if ( !this.info.isScrolled ) {
            this.info.isScrolled = true;
            sandboxTheme.$body.addClass(this.info.name);
          }
        } else {
          if ( this.info.isScrolled ) {
            this.info.isScrolled = false;
            sandboxTheme.$body.removeClass(this.info.name);
          }
        }
      },
      ready: function ready(){
        var scrollTop = 0;

        this.$window = $(window);
        this.$target = window.location.hash.length > 0 ? $(window.location.hash) : false;

        if ( this.$target && this.$target.length > 0 ) {
          scrollTop = this.$target.offset().top;
        }
        $('html, body').scrollTop(scrollTop);
        this.scroll();
      },
      setup: function setup(){
        var isSetup = true; //sandboxTheme.info.is_front_page || sandboxTheme.info.is_home || sandboxTheme.info.is_archive;
        return isSetup;
      }
    },

    /**
     * Forms support
     * =============
     */
    forms: {
      options: {
        selectors: {
          form: '.wpcf7-form',
          form_container: '.content, .textwidget',
          inputs: '.inputs, .textarea',
          input_container: 'p',
          label: 'label',
          input: 'input'
        },
        classNames: {
          focus: 'focus',
          valued: 'valued',
        },
      },
      events: function events(){
        var selectors = this.options.selectors,
            classNames = this.options.classNames;

        this.$forms.each(function(index){
          var $form = $(this),
              $inputs = $form.find(selectors.input);

          $inputs.on('focusin', function(){
            $(this).parents(selectors.input_container).addClass(classNames.focus);
          });
          $inputs.on('focusout', function(){
            var $input = $(this),
                $input_container = $input.parents(selectors.input_container).first();

            $input_container.removeClass(classNames.focus);

            if ( $input.val() === '' ) {
              if ( $input_container.is('.' + classNames.valued) ) {
                $input_container.removeClass(classNames.valued);
              }
            } else if ( !$input_container.is('.' + classNames.valued) ) {
              $input_container.addClass(classNames.valued);
            }
          });
          $form.on('submit', function(){
            $inputs.each(function(index){
              $(this).parents(selectors.input_container).first().removeClass(classNames.valued);
            });
          });
        });
      },
      ready: function ready(){
        var selectors = this.options.selectors;

        this.$forms = $(selectors.form);
        this.events();
      },
      setup: function setup(){
        return true;
      }
    },

    /**
     * Back to top
     * ===========
     */
    toTop: {
      options: {
        selectors: {
          toTop: '.to-top'
        }
      },
      events: function events(){
        $(this.options.selectors.toTop).on('click', function(event){
          $.mPageScroll2id('scrollTo', this.hash);
          event.preventDefault();
        });
      },
      ready: function ready(){
        this.events();
      },
      setup: function setup(){
        return true;
      }
    },

    /**
     * Offpage side menu with toggle button
     * ====================================
     * controls the behaviour of the mobile menu
     * Visible on: Viewport lower then 987px
     * Look at CSS for Layout
     */
    sideMenu: {
      options: {
        outsideOff: true,
        selectors: {
          container:'#site-actions',
          site_menu: '#site-menu > .inner',
          source: '#site-menu .menu > li',
          blackout: 'body > .blackout',
          additional_content: '.header-bar',
          logo: '#site-logo',
          additional_items: '.add-to-sideMenu',
          has_sub_menu: '.menu-item-has-children',
          current_item: '.current-menu-item',
          current_item_parent: '.current-menu-parent',
          current_item_parents: '.current-menu-ancestor',
          main: '#main'
        },
        classNames: {
          toggler: 'toggle',
          active: 'active',
          visible: 'visible',
          menu: 'menu',
          sub_menu: 'sub-menu',
          searcher: 'search',
          additional: 'additional',
          parentLink: 'sub-menu-parent-link'
        }
      },
      templates:{
        toggler: '<a />',
        // Animated CSS Bars like svg rect elements
        icon: '<div><span></span><span></span><span></span><span></span></div>',
      },
      info: {},
      toggle_sub_menu: function toggle_sub_menu($item){
        var active_class = this.options.classNames.toggler + '-' + this.options.classNames.active,
            $sub_menu_parent = $item.parent(),
            $active_sub_menu_parent = this.$has_sub_menu.filter('.' + active_class);

        if ( $active_sub_menu_parent.length > 0 ) {
          $active_sub_menu_parent.removeClass(active_class);
          $active_sub_menu_parent.children('ul').slideUp(250);
        }
        if ( !$sub_menu_parent.is($active_sub_menu_parent) ) {
          $sub_menu_parent.addClass(active_class);
          $sub_menu_parent.children('ul').slideDown(250)
        }
      },
      events: function events(){
        var info = this.info,
            classNames = this.options.classNames,
            selectors = this.options.selectors,
            $toggler = this.$toggler,
            $sideMenu = this.$sideMenu,
            sideMenu = this;

        // Toggle menu Off/On Page with toggler
        this.$toggler.on('click', function(event){
          if ( info.isVisible ) {
            $toggler.removeClass(classNames.active);
            $sideMenu.removeClass(classNames.visible);
            sandboxTheme.$body.removeClass(info.name + '-' + classNames.visible);
            info.isVisible = false;
          } else {
            $toggler.addClass(classNames.active);
            $sideMenu.addClass(classNames.visible).find('a').eq(0).trigger('focus');
            sandboxTheme.$body.addClass(info.name + '-' + classNames.visible);
            info.isVisible = true;
          }
          event.preventDefault();
        });

        // Toggle sub_menu Display
        this.$has_sub_menu.children('a').on('click', function click(event){
          sideMenu.toggle_sub_menu($(this));
          event.preventDefault();
        });

        // Toggle menu Off/On Page with Outside Click
        if ( this.options.outsideOff === true ) {
          this.$blackout.on('click', function(){
            $toggler.trigger('click').trigger('focus');
          });
        }

      },
      ready: function ready(){
        var options = this.options,
            info = this.info,
            templates = this.templates,
            classNames = options.classNames,
            selectors = options.selectors,
            sideMenu = this;

        this.$container = $(selectors.container);
        this.$site_menu = $(selectors.site_menu);

        // Get sources
        this.$logo = $(selectors.logo).clone().removeAttr('id');
        this.$additional_content = $(selectors.additional_content).clone().addClass(classNames.additional).removeAttr('id');
        this.$source = $(selectors.source).clone();

        // include sources to $sideMenu
        this.$source.appendTo(this.$container).wrapAll('<nav class="' + info.name + '" id="' + info.name + '" />').wrapAll('<ul class="' + classNames.menu + '" />');

        // Menus
        this.$sideMenu = this.$source.parents('#' + info.name);
        this.$has_sub_menu = this.$sideMenu.find(selectors.has_sub_menu);

        this.$has_sub_menu.each(function(index){
          var $sub_menu_parent = $(this),
              $sub_menu = $sub_menu_parent.children('ul');

          if ( $sub_menu_parent.is(selectors.current_item_parents) ) {
            $sub_menu_parent.addClass(classNames.toggler + '-' + classNames.active)
            sideMenu.$active_sub_menu_parent = $sub_menu_parent;
          } else {
            $sub_menu.hide();
          }
          $sub_menu.prepend($('<li class="' + classNames.parentLink + '" />').prepend($sub_menu.prev('a').clone()))
        });
        this.$logo.appendTo(this.$sideMenu);
        this.$additional_content.appendTo(this.$sideMenu);

        // Interactions
        this.$toggler = $(templates.toggler).attr('href', '#' + info.name).attr('id', info.name + '-' + classNames.toggler).addClass(info.name + ' ' + classNames.toggler);
        this.$toggler.html(templates.icon);
        this.$toggler.appendTo(this.$site_menu);
        this.$blackout = $(selectors.blackout);

        this.events();

        //
        sandboxTheme.$body.addClass(info.name + '-' + classNames.active);
      },
      setup: function setup(){
        this.info.isVisible = false;
        return true;
      }
    },

    /**
     * Nicer comments form
     * ===================
     */
    comments: {
      options: {
        offset: 70,
        selectors: {
          comments: '#comments',
          respond: '#respond',
          commentForm: '#commentform',
          comment_field: '#comment',
          trigger: '.comment-reply-title',
          replytocom: '.comment-form-replytocom',
          need_login: '.need-login',
        }
      },
      info: {
        isTriggered: false
      },
      events: function events(){
        var $trigger = this.$trigger,
            $commentForm = this.$commentForm,
            $comment_field = this.$comment_field,
            $respond = this.$respond,
            offset = this.options.offset,
            info = this.info;

        $trigger.on('click.' + info.name, function(event){
          if ( !info.isTriggered ) {
            $('body').addClass(info.name + '-visible');
            $commentForm.trigger('focusin.' + info.name).slideDown(600, 'swing');
            info.isTriggered = true;
          } else {
            $('body').removeClass(info.name + '-visible');
            $commentForm.trigger('blur.' + info.name).slideUp(600, 'swing');
            info.isTriggered = false;
          }
          event.preventDefault();
        });

        if ( !this.$comments.is(this.options.selectors.replytocom) ){
          $commentForm.on('submit', function(){
            if ( $comment_field.val() === '' ) {
              $comment_field.val('Ohne Begründung');
            }
          });
        }
      },
      resize: function resize(){},
      ready: function ready(){
        var selectors = this.options.selectors;

        this.$comments = $(selectors.comments);
        this.$respond = $(selectors.respond);
        this.$commentForm = $(selectors.commentForm).hide();
        this.$comment_field = $(selectors.comment_field);
        this.$trigger = this.$respond.find(selectors.trigger).not(selectors.need_login);

        this.events();

        if ( window.location.hash === selectors.respond ) {
          this.$trigger.trigger('click');
        }
      },
      setup: function setup(){
        return sandboxTheme.info.comments_open && sandboxTheme.info.is_user_logged_in;
      }
    },


    /**
     * accordeon
     * =========
     */
    accordeon: {
      options: {
        speed: 300,                  // {number} Speed of the slideUp/Down animation
        initOpen: 1,                 // {number} initial accordion to show on page load, 0 = all accordions are closed!
        scrollToInitOpen: true,      // {boolean} scroll to the open item on init? Only works when initShow is set or a hash opens an item.
        scrollToInitOpenOffset: 60,   // {number} calulate an extra offset when jumpToInitOpen is true (eg. use with fixed header)
        selectors: {                 // select DOM elements
          container: '#content > *', // where to look at accordions
          title: '.title',
        },
        classNames: {                // Class names will be mixed with the name space "accordion"
          heading: 'heading',        // {string} Accordion heading class
          trigger: 'trigger',        // {string} Accordion open/close trigger class
          content: 'content',        // {string} Accordion content class
          no: 'no',                  // {string} Accordion leave out class
          opened: 'opened',          // {string} Accordion is opened
          closed: 'closed',          // {string} Accordion is closed
          enabled: 'enabled',
        },
      },
      templates: {
        div: '<div />',
        link: '<a href="" />'
      },

      /**
       * Store any dynamic information about your feature here.
       *
       * @usage
       * Use it like the Modernizr object:
       *
       * if ( this.info.myInfo ) {
       *   // do stuff
       * }
       *
       * var myInfo = this.info.myInfo
       */
      info: {

        /**
         * Index/Count of the accordion items
         *
         * We increase to 1 before the first accordion is markuped for good readabilty.
         * Always remeber that the first element of an array has an index of 0!
         */
        index: 1
      },
      events: function events(){
        var options = this.options,
            selectors = options.selectors,
            classNames = options.classNames,
            info = this.info,
            accordeon = this;

        this.$headings.each(function(){
          var $heading = $(this),
              $accordeon = $heading.parent(), // not guaranteed
              $accordeon_content = $heading.next('.' + info.name),
              accordeon_id = $heading.is(selectors.title) ? $accordeon.attr('id') : false,
              accordeon_content_id = $accordeon_content.attr('id');

          $heading.children('a').on('click', function(event){
            var scrollTop = $(window).scrollTop();

            if ( $heading.is('.' + classNames.opened) ) {
              $heading.removeClass(classNames.opened);
              $accordeon_content.stop().slideUp(options.speed, function(){
                $heading.addClass(classNames.closed);
                if ( window.location.hash === '#' + accordeon_id || window.location.hash === '#' + accordeon_content_id ) {
                  window.location.hash = '';
                  $(window).scrollTop(scrollTop);
                }
              });
            } else {
              $heading.addClass(classNames.opened);
              $accordeon_content.stop().slideDown(options.speed, function(){
                $heading.removeClass(classNames.closed);
                if ( accordeon_id ) {
                  $accordeon.removeAttr('id');
                  window.location.hash = accordeon_id;
                  $accordeon.attr('id', accordeon_id);
                } else {
                  $accordeon_content.removeAttr('id');
                  window.location.hash = accordeon_content_id;
                  $accordeon_content.attr('id', accordeon_content_id);
                }
              });
            }
            event.preventDefault();
          });
        });
      },
      ready: function ready(){
        var options = this.options,
            selectors = this.options.selectors,
            classNames = this.options.classNames,
            info = this.info,
            templates = this.templates,
            $accordeons = $(),
            accordeon_ids = [],
            open_index = options.initOpen,
            location_id = window.location.hash.replace('#', '');


        this.$container = $(selectors.container);
        this.$headings = this.$container.find('.' + info.name).not('.' + classNames.no + '-' + info.name);


        /**
         * collect accordeon data
         */
        this.$headings.each(function(){
          var $heading = $(this),
              $accordeon = $heading.parent();

          if ( $heading.is(selectors.title) ) { // then has no ".inner" div next
            $accordeons = $accordeons.add($accordeon);
            accordeon_ids.push($accordeon.attr('id'));
          }
        });

        /**
         * Set the index of the initial opened accordeon
         */
        if ( accordeon_ids.indexOf(location_id) >= 0 ) {
          open_index = accordeon_ids.indexOf(location_id)+1;
        } else if ( info.hash ) {
          open_index = info.hash;
          if ( accordeon_ids.length > 0 ) {
            window.location.hash = accordeon_ids[open_index-1];
          }
        }

        /**
         * Generate the accordion items and open the one to show initial or selected by location.hash
         *
         * Only headings that dont have the 'no-accordion' class and
         * have content until the next heading are valid accordion item selectors
         */
        this.$headings.each(function(){
          var $heading = $(this),
              $accordeon_content = $heading.nextUntil('.' + info.name);

          if ( $accordeon_content.length > 0 ) {
            $heading.removeClass(info.name).addClass(info.name + '-' + classNames.heading);

            if ( !$heading.is(selectors.title) ) { // then has no ".inner" div next
              $heading.wrapInner(templates.link).children('a').attr('href', '#' + info.name + '-' + info.index)
              $accordeon_content = $accordeon_content.wrapAll(templates.div);
              $accordeon_content.attr('id', info.name + '-' + info.index);
            }
            $accordeon_content.addClass(info.name);

            /**
             * the one to open initial
             */
            if ( open_index === info.index ) {
              $heading.addClass(classNames.opened);


              /**
               * scroll to the opened item?
               */
              if ( options.initOpen !== open_index && options.scrollToInitOpen ) {
                $(window).scrollTop($heading.offset().top-parseInt(options.scrollToInitOpenOffset));
              }
            } else {
              $heading.addClass(classNames.closed);
              $accordeon_content.hide();
            }

            /**
             * Count valid accordions
             */
            info.index++;
          }
        });

        this.$accordeons = $accordeons.length > 0 ? $accordeons : false;
        this.info.accordeon_ids = accordeon_ids.length > 0 ? accordeon_ids : false;

        this.events();
        document.body.classList.add(info.name + '-' + classNames.enabled);
      },
      setup: function setup(){
        if ( this.info.hash ) {
          // use hash as initOpen index
          this.info.hash = parseInt(this.info.hash);
        }
        return true; // sandboxTheme.is_page(8); // true on Kurssuche;
      }
    },


    /**
     * FAQ navigation
     */
    faq_nav: {
      options: {
        page_slug: 'faqs', // FAQ page slug/permalink
        scrollOffset: 60,   // {number} calulate an extra offset when scrolling to accordeon
        speed: 300,
        selectors: {
          nav: '.faq-navigation',
          menu: '.menu',
          sub_menu: '.sub-menu',
          has_children: '.menu-item-has-children',
          articles: '.articles',
          faq_heading: '.title'
        },
        classNames: {
          active: 'active',
          closed: 'closed',
          opened: 'opened',
        },
      },
      events: function events(){
        var options = this.options,
            selectors = options.selectors,
            classNames = options.classNames;

        // submenu open/close
        this.$menu.children('li').children('a').on('click', function(event){
          var $menu_item = $(this).parent(),
              $sub_menu = $(this).next(selectors.sub_menu);

          if ( $menu_item.is('.' + classNames.opened) ) {
            $menu_item.removeClass(classNames.opened);
            $sub_menu.stop().slideUp(options.speed, function(){
              $menu_item.addClass(classNames.closed);
            });
          } else {
            $menu_item.addClass(classNames.opened);
            $sub_menu.stop().slideDown(options.speed, function(){
              $menu_item.removeClass(classNames.closed);
            });
          }
          event.preventDefault();
        });

        // direct to faq accordions and open on demand
        this.$menu.find(selectors.sub_menu).find('a').on('click', function(event){
          var $heading = $(this.hash).children(selectors.title),
              trigger_index = 0;

          $('html, body').stop().animate({
            scrollTop: $heading.offset().top-parseInt(options.scrollOffset),
          }, options.speed, 'swing', function(){
            if ( trigger_index === 0 && !$heading.is('.' + classNames.opened) ) { // gets called twice for html and body
              $heading.children('a').trigger('click');
            }
            trigger_index++;
          });

          event.preventDefault();
        });
      },
      ready: function ready(){
        var selectors = this.options.selectors,
            classNames = this.options.classNames,
            location_id = window.location.hash.replace('#', ''),
            $active_menu_item,
            $opend_menu_item,
            accordeon = sandboxTheme.features.accordeon;

        // sections selection
        this.$nav = $(selectors.nav);
        this.$menu = this.$nav.find(selectors.menu);

        /**
         * Set the index of the initial opened accordeon
         */
        if ( accordeon.info.accordeon_ids && accordeon.info.accordeon_ids.length > 0 && accordeon.info.accordeon_ids.indexOf(location_id) >= 0 ) {
          $active_menu_item = this.$menu.find('a[href="' + window.location.hash + '"]');
          $opend_menu_item = $active_menu_item.parents('li').last();
        } else {
          $opend_menu_item = this.$menu.children('li').first();
        }
        this.$menu.find(selectors.sub_menu).hide().parent('li').addClass(classNames.closed);

        // event handling
        this.events();

        // initiate
        $opend_menu_item.children('a').trigger('click');
      },
      setup: function setup(){
        var is_setup = sandboxTheme.get_page('slug') === this.options.page_slug; // only on FAQ page
        return is_setup;
      }
    },


    /**
     * social widget support
     * =====================
     */
    social: {
      options: {
        selectors: {
          widget: '#social-2',
          title: '.title',
          list: 'ul',
          list_item: 'li'
        },
        classNames: {
          open: 'open',
        }
      },
      info: {

      },
      events: function events(){
        var selectors = this.options.selectors,
            classNames = this.options.classNames,
            info = this.info,
            $list = this.$list,
            $body = sandboxTheme.$body;

        this.$social.find(selectors.title).on('click', function(event){
          // $.log($body.is('.' + info.name + '-' + classNames.open));
          if ( $body.is('.' + info.name + '-' + classNames.open) ) {
            $body.removeClass(info.name + '-' + classNames.open);
          } else {
            $body.addClass(info.name + '-' + classNames.open);
          }
          event.preventDefault();
        });
      },
      ready: function ready(){
        this.$social = $(this.options.selectors.widget);

        if ( this.$social.length > 0 ) {
          this.$list = this.$social.find(this.options.selectors.list);
          this.events();
        }
      },
      setup: function setup(){
        return sandboxTheme.info.is_single && sandboxTheme.info.post_type === 'alpha_download'; // true on Lernmaterial;
      }
    },

    /**
     * Carousel
     * ========
     */
    carousel: {
      options: {
        classNames:{
          prev: 'prev',
          next: 'next'
        },
        selectors: {
          container: 'section.slider',
          carousel: '.slides',
          slides: '.slide',
          navigation: '.prev-next'
        },
        carouFredSel: {
          direction: 'up',
          height: 'variable',
          items: {
            visible: 1,
          },
          scroll: {
            easing: 'swing',
            fx: 'scroll',
            duration: 1000,
            pauseOnHover: true,
          },
          auto: {
            play: true,
            duration: 2000,
            timoutDuration: 5
          }
        },
        texts: {
          prev: 'Zur&uuml;ck',
          next: 'Vor',
        }
      },
      resize: function resize(){},
      get_size: function get_size(){
        return {
          height: this.$container.parent().height(),
          width: this.$container.parent().width()
        };
      },
      set_size: function set_size(){
        var scrollDuration = this.options.carouFredSel.scroll.duration,
            size = this.get_size();

        this.$container.animate({
          width: size.width,
          height: size.height,
        }, {
          duration: scrollDuration
        });
        this.$slides.animate({
          width: size.width,
          height: size.height,
        }, {
          duration: scrollDuration
        });
      },
      ready: function ready(){
        var selectors = this.options.selectors,
            classNames = this.options.classNames,
            carousel = this;

        this.$container = $('.slider');
        this.info.active = this.$container.length > 0;

        if ( this.info.active ) {
          this.$navigation = $('<div class="' + classNames.prev + '-' + classNames.next + '"></div>');
          this.$carousel = this.$container.find(selectors.carousel);
          this.$slides = this.$carousel.find(selectors.slides);

          $.extend(this.options.carouFredSel, {
            pagination: {
              container: '.slider-nav',
              keys: true,
              anchorBuilder: function anchorBuilder(index){
                return '<li><a href="#"><span>' + index + '</span></a></li>';
              }
            },
            prev: {
              button: function(){
                var $prev = $('<a class="' + classNames.prev + '">Zur&uuml;ck</a>');

                $prev.appendTo(carousel.$navigation);
                return $prev;
              }
            },
            next: {
              button: function(){
                var $next = $('<a class="' + classNames.next + '">Vor</a>');

                $next.appendTo(carousel.$navigation);
                return $next;
              }
            }
          });

          // Slider Javascript Powered
          this.$navigation.appendTo(this.$container);
          this.$carousel.carouFredSel(this.options.carouFredSel, {
            debug: true
          });
        }
      },
      setup: function setup(){
        var isSetup = sandboxTheme.info.is_front_page; // || sandboxTheme.info.is_home;

        this.info.active = false;
        return isSetup;
      }
    }
  };

  /**
   * Initiate theme actions
   *
   * merges WP REST API results with theme_data
   *
   * @object sandboxTheme_data   data handed over with wp_localize_script
   * @object pageData          WP REST API result
   *
   * @uses   sandboxTheme.init() initiates the page theme actions
   */
  var siteData = $.getJSON(sandboxTheme_data.urls.rest);

  siteData.done(function(pageData){
    if ( $.isPlainObject(pageData) ) {
      sandboxTheme.init($.extend(sandboxTheme_data, pageData));
    } else {
      sandboxTheme.init(sandboxTheme_data);
    }
  }).error(function(){
    sandboxTheme.init(sandboxTheme_data);
  });

})(jQuery);
