$(document).ready(function() {
  local_get();
  var theme = $('#pagewrap').data('theme');

  theme = $.trim(theme);
  if (theme == ''){
    theme = 'default';
  }
  var themeToBackground = new Array();
  themeToBackground['default'] = ['bg-img-num1', 'bg-img-num2', 'bg-img-num3', 'bg-img-num4', 'bg-img-num5', 'bg-img-num6', 'bg-img-num7', 'bg-img-num8'];
  themeToBackground['theme-black'] = ['wall-num6'];
  themeToBackground['theme-white'] = ['wall-num1'];
  themeToBackground['theme-dark'] = ['bg-img-num9', 'bg-img-num10', 'bg-img-num11', 'bg-img-num12', 'bg-img-num13', 'bg-img-num14', 'bg-img-num15', 'bg-img-num16'];
  themeToBackground['theme-green'] = ['bg-img-num17', 'bg-img-num18', 'bg-img-num19', 'bg-img-num20'];

  $.get("assets/settings.html", function(data) {
   $("#pagewrap").append(data);
   $(".ss_theme[data-value=" + theme + "]").removeClass('active');
   $(".ss_theme[data-value=" + theme + "]").addClass('active');
    if (theme != 'default') {
    $("#pagewrap").removeClass(theme);
    $("#pagewrap").addClass(theme);
    }
    $("#ss_backgrounds").html(buildBackgroundsList(theme, themeToBackground));
    $(".ss_background[data-value=" + $("body").data("bg") + "]").addClass('active');
  });

  $(".site-settings-button").live("click", function() {
    if ($(this).parent('.site-settings').hasClass('active'))
      $(this).parent('.site-settings').removeClass('active');
    else {
      $(this).parent('.site-settings').addClass('active');
      $(this).parent('.site-settings').find('input:checkbox, input:radio').uniform();
      if ($("#pagewrap").data("container") === 'container')
        $(".ss_layout[value=fixed]").attr("checked", true).parent("span").addClass("checked");
      else
        $(".ss_layout[value=liquid]").attr("checked", true).parent("span").addClass("checked");
    }
  });

  // layout
  $(".ss_layout").live("click", function() {
    if ($(this).val() == 'fixed') {
      $("#pagewrap").removeClass("container-fluid").addClass("container");
      $("#pagewrap").data('container', 'container');
    }
    else {
      $("#pagewrap").removeClass("container").addClass("container-fluid");
      $("#pagewrap").data('container', 'container-fluid');
    }
    $(this).attr("checked", true).parent("span").addClass("checked");
    local_save();
  });
  // eof layout

  // themes and backgrounds
  $(".ss_theme").live("click", function() {
    $("body").removeAttr('class');
    $("#pagewrap").removeClass('theme-dark theme-green theme-black theme-white theme-red');
    $(".ss_theme").removeClass('active')
    if ($(this).attr('data-value') != 'default') {
      $("#pagewrap").addClass($(this).attr('data-value'));
    }
    $("#pagewrap").data('theme', $(this).attr('data-value'));

    $(this).addClass('active');
    $("#ss_backgrounds").html(buildBackgroundsList($(this).attr('data-value'), themeToBackground));

    $(".ss_background").removeClass("active");
    $(".ss_background[data-value=" + themeToBackground[$(this).attr('data-value')][0] + "]").addClass('active');
    $("body").removeClass($("body").data("bg")).addClass(themeToBackground[$(this).attr('data-value')][0]);
    $("body").data("bg", themeToBackground[$(this).attr('data-value')][0]);
    remoteSave(themeToBackground[$(this).attr('data-value')][0]);
    local_save();
    return false;
  });

  $(".ss_background").live("click", function() {
    $("body").removeAttr('class');
    $("body").removeClass($("body").data("bg"));
    $("body").addClass($(this).attr("data-value"));
    $(".ss_background").removeClass('active');
    $(this).addClass('active');
    remoteSave($(this).attr("data-value"));
    local_save();
    return false;
  });
  // eof background

});

function local_get() {
  if (typeof(Storage) !== "undefined") {
    if (sessionStorage.siteBackground != '') {
      $('body').data('bg', sessionStorage.siteBackground);
      // $('body').addClass(sessionStorage.siteBackground);
    }
    if (sessionStorage.siteContainer != '#pagewrap') {
      $('#pagewrap').data('container', sessionStorage.siteContainer);
      $('#pagewrap').removeClass('container container-fluid').addClass(sessionStorage.siteContainer);
    }
    if (sessionStorage.siteTheme != 'default') {
      $('#pagewrap').data('theme', sessionStorage.siteTheme);
      $('#pagewrap').addClass(sessionStorage.siteTheme);
    }
  }
}

function local_save() {
  if (typeof(Storage) !== "undefined") {
    sessionStorage.siteBackground = $('body').data('bg');
    sessionStorage.siteContainer = $('#pagewrap').data('container');
    sessionStorage.siteTheme = $('#pagewrap').data('theme');
  } else {
    return false;
  }
  // remoteSave();
}

function buildBackgroundsList(theme, t2b) {
  var backgrounds = '';
     
  if (t2b !== null && t2b[theme] !== null && t2b[theme].length > 1) {
    for (i = 0; i < t2b[theme].length; i++)
      backgrounds += '<a class="ss_background ' + t2b[theme][i] + '" data-value="' + t2b[theme][i] + '"></a> ';
  } else {
    for (var j in t2b) {
      if (t2b[j].length == 1) continue;
      for (i = 0; i < t2b[j].length; i++)
        backgrounds += '<a class="ss_background ' + t2b[j][i] + '" data-value="' + t2b[j][i] + '"></a> ';
    }
  }
  return backgrounds;
}

function remoteSave(dataValue) {
  // post relativo a página que chama este script
  // alert($(this).attr("data-value"));
  
  $.post( "siteTheme.php", {
    theme: $('#pagewrap').data('theme'),
    bg: $('body').data('bg'),
    container: $('#pagewrap').data('container'),
    background_config_taurus: dataValue
  } );
}
