'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) {

      // Cache component elements
      var Body = $('body');
      var Sidebar = $('#aside');
      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 accent blue danger info success warn warning";
      var darkSkins = "dark grey black";
      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 = {
        'sidebarState': '',
        'boxed': '',/*boxed*/
        'bgSkin': '',
        'darkSkin': '',/*dark*/
        'primary': "primary"
      };

      // 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 "primary":
              Primary.attr("href", "assets/styles/skin/sk." + e + ".css");
              $('#skintoolsPrimary input[value="' + e + '"]').prop('checked', 'true');
              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 'darkSkin':
              Body.removeClass(darkSkins).addClass(e);
              $('#skinDark a[data-toggle="' + e + '"]').addClass('current');
              break;


            case 'sidebarState':
              if (e === "show") {
                Sidebar.addClass('show');
                $('#sidebar-option').prop('checked', true);
              } else {
                Sidebar.find('.nano-content').attr('style', '');
                Sidebar.removeClass('show');
                $('#sidebar-option').prop('checked', false);
              }
              break;

            case 'boxed':
              if (e === "boxed") {
                Body.addClass('container');
                $('#body-option').prop('checked', true);
              } else {
                Body.removeClass('container');
                $('#body-option').prop('checked', false);
              }
              break;
          }
        });

      })();




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

        var This = $(this);
        var Val = This.val();

        // Swap Change Skin

        Primary.attr("href", "assets/styles/skin/sk." + Val + ".css");


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

      $("#skinDark a").on("click", function() {

        var This = $(this);
        var Val = This.attr('data-toggle');

        // Swap Header Skin
        Body.removeClass(darkSkins).addClass(Val);
        $("#skinDark a").removeClass('current');
        $('#skinDark a[data-toggle="' + Val + '"]').addClass('current');
        // Save new Skin to Settings Key
        settingsObj['darkSkin'] = Val;
        localStorage.setItem(themeKey, JSON.stringify(settingsObj));
        return false;
      });

      // 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('container');
          $('#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));
        return false;
      });



      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',
              'min-height':'300px'
            });
          },
          function() {
            "use strict";
            jQuery(this).parent().next().css('backgroundImage', 'none');
          }
      );


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

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


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

          Body.removeClass(bgSkins).addClass(Val);
          $('#co_bg_images_list a,#co_bg_pattern_list a').removeClass(Vbl);
          $(this).addClass(Vbl);

          settingsObj['bgSkin'] = Val;
          localStorage.setItem(themeKey, JSON.stringify(settingsObj));

        } else {
          Body.addClass('container');
          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('show')) {


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

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

          sidebarState = "show";

        }

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

        // Save new setting to Settings Key
        settingsObj['sidebarState'] = sidebarState;
        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');
        $('#co_bg_pattern_list a,#co_bg_images_list a').removeClass('current');
        $('#aside').removeClass('show');
        $('#header-option').prop('checked', true);
        $('#body-option,#sidebar-option').prop('checked', false);
        $('#primary1').prop('checked', true);

        var $link = $("#skinStyle", $("head")),
            href = "assets/styles/skin/sk.primary.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();
    }
  }
}();