'use strict';


/*! main.js - v0.1.1
 * http://www.mobanbox.net/
 * Copyright (c) 2015 Yeti Designs;*/

/* Mobanbox theme functions. Required for
 * Settings Pane and misc functions */
var Mobanbox = function() {

  // Mobanbox FUNCTIONS - primarily trash
  var runMobanboxSettings = function() {

    if ($('#changeskins').length) {

      // Toggles Theme Settings Tray
      $('#changeskins .panel-heading').on('click', function() {
        $('#changeskins').toggleClass('toolbox-open');
      });

      // Cache component elements
      var Body = $('body');
      var Breadcrumbs = $('#topbar');
      var Sidebar = $('#sidebar_left');
      var Header = $('.navbar');
      var Primary = $("#skinStyle");
      var Branding = Header.children('.navbar-branding');

      // Possible Component Skins
      var headerSkins = "bg-primary bg-success bg-info bg-warning bg-danger bg-black bg-white bg-dark";
      var sidebarSkins = "sidebar-light light dark";
      var primary = "primary success info warning danger dark";
      var bgSkins = "bg_image_1 bg_image_2 bg_image_3 bg_image_4 bg_image_5 bg_image_6 bg_image_7 bg_image_8 bg_image_9";

      // Theme Settings
      var settingsObj = {
        // 'headerTone': true,
        'headerSkin': '',
        'sidebarSkin': 'sidebar-default',
        'headerState': 'navbar-fixed-top',
        'sidebarState': 'affix',
        'sidebarAlign': '',
        'breadcrumbState': 'relative',
        'breadcrumbHidden': 'visible',
        'boxed': 'on',
        'bgSkin': '',
        'primary': "success"
      };

      // Local Storage Theme Key
      var themeKey = 'skin-settings';

      // Local Storage Theme Get
      var themeGet = localStorage.getItem(themeKey);

      // Set new key if one doesn't exist
      if (themeGet === null) {
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
        themeGet = localStorage.getItem(themeKey);
      }





      // Restore Theme Settings onload from Local Storage Key
      (function() {

        var settingsParse = JSON.parse(themeGet);
        settingsObj = settingsParse;

        $.each(settingsParse, function(i, e) {
          switch (i) {
            case 'headerSkin':
              Header.removeClass(headerSkins).addClass(e);
              Branding.removeClass(headerSkins).addClass(e + ' dark');
              if (e === "bg-light") {
                Branding.removeClass(headerSkins);
              } else {
                Branding.removeClass(headerSkins).addClass(e);
              }
              //$('#toolbox-header-skin input[value="bg-success"]').prop('checked', false);
              $('#toolbox-header-skin input[value="' + e + '"]').prop('checked', true);
              break;
            case 'sidebarSkin':
              Sidebar.removeClass(sidebarSkins).addClass(e);
              $('#toolbox-sidebar-skin input[value="bg-light"]').prop('checked', false);
              $('#toolbox-sidebar-skin input[value="' + e + '"]').prop('checked', true);
              break;




            case "primary":
              Primary.attr("href", "assets/css/skin/skin-" + e + ".css");
              $('#skintoolsPrimary li[data-toggle="' + e + '"]').addClass('current');
              break;


            case 'bgSkin':
              Body.removeClass(bgSkins).addClass(e);
              $('#co_bg_images_list a[data-toggle="' + e + '"]').addClass('current');
              $('#co_bg_pattern_list a[data-toggle="' + e + '"]').addClass('current');
              break;





            case 'headerState':
              if (e === "navbar-fixed-top") {
                Header.addClass('navbar-fixed-top');
                $('#header-option').prop('checked', true);
              } else {
                Header.removeClass('navbar-fixed-top');
                $('#header-option').prop('checked', false);


                Sidebar.find('.nano-content').attr('style', '');
                Sidebar.removeClass('affix');
                $('#sidebar-option').prop('checked', false);
              }
              break;
            case 'sidebarState':
              if (e === "affix") {
                Sidebar.addClass('affix');
                $('#sidebar-option').prop('checked', true);
              } else {
                Sidebar.find('.nano-content').attr('style', '');
                Sidebar.removeClass('affix');
                $('#sidebar-option').prop('checked', false);
              }
              break;
            case 'sidebarAlign':
              if (e === "sb-top") {
                Body.addClass('sb-top');
                $('#sidebar-align').prop('checked', true);
              } else {
                Body.removeClass('sb-top');
                $('#sidebar-align').prop('checked', false);
              }
              break;
            case 'breadcrumbState':
              if (e === "affix") {
                Breadcrumbs.addClass('affix');
                $('#breadcrumb-option').prop('checked', true);
              } else {
                Breadcrumbs.removeClass('affix');
                $('#breadcrumb-option').prop('checked', false);
              }
              break;
            case 'breadcrumbHidden':
              if (Breadcrumbs.hasClass('hidden')) {
                $('#breadcrumb-hidden').prop('checked', true);
              } else {
                if (e === "hidden") {
                  Breadcrumbs.addClass('hidden');
                  $('#breadcrumb-hidden').prop('checked', true);
                } else {
                  Breadcrumbs.removeClass('hidden');
                  $('#breadcrumb-hidden').prop('checked', false);
                }
              }
              break;
            case 'boxed':
              if (e === "boxed") {
                Body.addClass('boxed');
                $('#body-option').prop('checked', true);
              } else {
                Body.removeClass('boxed');
                $('#body-option').prop('checked', false);
              }
              break;
          }
        });

      })();




      $("#skintoolsPrimary li").on("click", function() {

        var This = $(this);
        var Val = This.attr('data-toggle');
        var Vbl = 'current';
        var ID = This.attr('id');

        // Swap Change Skin

        Primary.attr("href", "assets/css/skin/skin-" + Val + ".css");
        $('#skintoolsPrimary li').removeClass(Vbl);
        $(this).addClass(Vbl);


        // Save new Skin to Settings Key
        settingsObj['primary'] = Val;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });




        // Header Skin Switcher
      $('#toolbox-header-skin input').on('click', function() {
        var This = $(this);
        var Val = This.val();
        var ID = This.attr('id');

        // Swap Header Skin
        Header.removeClass(headerSkins).addClass(Val);
        Branding.removeClass(headerSkins).addClass(Val + ' dark');

        // Save new Skin to Settings Key
        settingsObj['headerSkin'] = Val;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));

      });

      // Sidebar Skin Switcher
      $('#toolbox-sidebar-skin input').on('click', function() {
        var Val = $(this).val();

        // Swap Sidebar Skin
        Sidebar.removeClass(sidebarSkins).addClass(Val);

        // Save new Skin to Settings Key
        settingsObj['sidebarSkin'] = Val;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });


      // Background Skin Switcher
      $('#co_bg_images_list a,#co_bg_pattern_list a').on('click', function() {
        var Val = $(this).attr('data-toggle');
        var Vbl = 'current';








        if (Body.hasClass(Val)) {



        } else {

          // Swap Background Skin
          Body.removeClass(bgSkins).addClass(Val);
          $('#co_bg_images_list a,#co_bg_pattern_list a').removeClass(Vbl);
          $(this).addClass(Vbl);
          Body.addClass('boxed');
          $('#body-option').parent('checkbox-disabled').addClass('.checkbox-custom').end().prop('checked', true).attr('disabled', false);
          settingsObj['boxed'] = "boxed";
          localStorage.setItem(themeKey, JSON.stringify(settingsObj));
        }







        // Save new Skin to Settings Key
        settingsObj['bgSkin'] = Val;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });








      jQuery('#co_bg_pattern_list a').hover(
          function() {
            "use strict";
            var pattern = jQuery(this).parent().attr('id')=='co_bg_pattern_list';
            jQuery(this).parent().parent().css({
              'backgroundImage': 'url('+jQuery(this).find('img').attr('src').replace('_thumb2', '_thumb')+')',
              'backgroundRepeat': pattern ? 'repeat' : 'no-repeat'
            });
          },
          function() {
            "use strict";
            jQuery(this).parent().parent().css('backgroundImage', 'none');
          }
      );


      jQuery('#co_bg_images_list a').hover(
          function() {
            "use strict";
            var pattern = jQuery(this).parent().attr('id')=='co_bg_pattern_list';
            jQuery(this).parent().next().css({
              'backgroundImage': 'url('+jQuery(this).find('img').attr('src').replace('_thumb2', '_thumb')+')',
              'backgroundRepeat': 'no-repeat'
            });
          },
          function() {
            "use strict";
            jQuery(this).parent().next().css('backgroundImage', 'none');
          }
      );





      // Fixed Header Switcher
      $('#header-option').on('click', function() {
        var headerState = "navbar-fixed-top";

        if (Header.hasClass('navbar-fixed-top')) {
          Header.removeClass('navbar-fixed-top');
          headerState = "relative";

          // Remove Fixed Sidebar option if navbar isnt fixed
          Sidebar.removeClass('affix');


          Sidebar.find('.nano-content').attr('style', '');
          Sidebar.removeClass('affix');
          $('#sidebar-option').prop('checked', false);

          $('#sidebar-option').parent('.checkbox-custom').addClass('checkbox-disabled').end().prop('checked', false).attr('disabled', true);
          settingsObj['sidebarState'] = "";
          localStorage.setItem(themeKey, JSON.stringify(settingsObj));

          // Remove Fixed Breadcrumb option if navbar isnt fixed
          Breadcrumbs.removeClass('affix');
          $('#breadcrumb-option').parent('.checkbox-custom').addClass('checkbox-disabled').end().prop('checked', false).attr('disabled', true);
          settingsObj['breadcrumbState'] = "";
          localStorage.setItem(themeKey, JSON.stringify(settingsObj));

        } else {
          Header.addClass('navbar-fixed-top');
          headerState = "navbar-fixed-top";
          // Enable fixed sidebar and breadcrumb options
          $('#sidebar-option').parent('.checkbox-custom').removeClass('checkbox-disabled').end().attr('disabled', false);
          $('#breadcrumb-option').parent('.checkbox-custom').removeClass('checkbox-disabled').end().attr('disabled', false);
        }

        // Save new setting to Settings Key
        settingsObj['headerState'] = headerState;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });




      // Fixed Body Switcher
      $('#body-option').on('click', function() {
        var boxed = "boxed";

        if (Body.hasClass('boxed')) {
          Body.removeClass('boxed');
          boxed = "relative";

        } else {
          Body.addClass('boxed');
          boxed = "boxed";
          // Enable fixed sidebar and breadcrumb options
          $('#body-option').parent('.checkbox-custom').removeClass('checkbox-disabled').end().attr('disabled', false);
        }

        // Save new setting to Settings Key
        settingsObj['boxed'] = boxed;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });




      // Fixed Sidebar Switcher
      $('#sidebar-option').on('click', function() {
        var sidebarState = "";

        if (Sidebar.hasClass('affix')) {


          Sidebar.find('.nano-content').attr('style', '');
          Sidebar.removeClass('affix');

          sidebarState = "";
        } else {
          Sidebar.addClass('affix');

          sidebarState = "affix";

        }

        $(window).trigger('resize');

        // Save new setting to Settings Key
        settingsObj['sidebarState'] = sidebarState;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });

      // Sidebar Horizontal Setting Switcher
      $('#sidebar-align').on('click', function() {

        var sidebarAlign = "";

        if (Body.hasClass('sb-top')) {
          Body.removeClass('sb-top');
          sidebarAlign = "";
        } else {
          Body.removeClass('sb-top');
          sidebarAlign = "sb-top";
        }

        // Save new setting to Settings Key
        settingsObj['sidebarAlign'] = sidebarAlign;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });

      // Fixed Breadcrumb Switcher
      $('#breadcrumb-option').on('click', function() {

        var breadcrumbState = "";

        if (Breadcrumbs.hasClass('affix')) {
          Breadcrumbs.removeClass('affix');
          breadcrumbState = "";
        } else {
          Breadcrumbs.addClass('affix');
          breadcrumbState = "affix";
        }

        // Save new setting to Settings Key
        settingsObj['breadcrumbState'] = breadcrumbState;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });

      // Hidden Breadcrumb Switcher
      $('#breadcrumb-hidden').on('click', function() {
        var breadcrumbState = "";

        if (Breadcrumbs.hasClass('hidden')) {
          Breadcrumbs.removeClass('hidden');
          breadcrumbState = "";
        } else {
          Breadcrumbs.addClass('hidden');
          breadcrumbState = "hidden";
        }

        // Save new setting to Settings Key
        settingsObj['breadcrumbHidden'] = breadcrumbState;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
      });



      // Clear local storage button and confirm dialog
      $("#clearLocalStorage").on('click', function() {

        localStorage.clear();


        $('body').removeClass('boxed container bg_image_1 bg_image_2 bg_image_3 bg_image_4 bg_image_5 bg_image_6 bg_image_7 bg_image_8 bg_image_9');
        $('#skintoolsPrimary li,#co_bg_pattern_list a,#co_bg_images_list a').removeClass('current');
        $('#skintoolsPrimary li.skin-success').addClass('current');
        $('#header-option').prop('checked', true);
        $('#body-option,#sidebar-option,#breadcrumb-option,#breadcrumb-hidden').prop('checked', false);




        var $link = $("#skinStyle", $("head")),
            href = "/assets/css/skins/skin-success.css";
        return void(0 === $link.length ? $("head").append('<link id="skinStyle" href="' + href + '" rel="stylesheet" type="text/css"/>') : $link.attr("href", href));




      });


    }
  }

  return {
    init: function() {
      runMobanboxSettings();
    }
  }
}();