var lStorage = {
  intExplorer: false,
  cart: {
    userName: null,
    items: []
  },

  prop:{
    catFolderPos: null,
    historyUrl: ['main', '0'],
    limVisGoods: 50,
    sortable: {
      field: 'number_product',
      type: 'desc'
    }
  },

  updateSaveProfile: function(_data){
    var d = {};
    var k = 0;
    var c = 0;
    if(!_data){
      return false;
    }
    profile = _data;
    if(lStorage.intExplorer){
      d = {
        auth: _data.auth,
        username: _data.username,
        manager: _data.manager,
        orders_sum_current: _data.orders_sum_current,
        orders_sum_last_month: _data.orders_sum_last_month,
        overdue_payment: _data.overdue_payment,
        sum_discount: _data.sum_discount,
        organization: _data.organization,
        debt: _data.debt
      }
      lStorage.set('profile', lStorage.jsonStringify(d));

      // k = Math.ceil(lStorage.jsonStringify(_data).length / 1024) - 1;
      // for(var i = 0; i <= k; ++i){
      //   c = i == 0 ? 0 : 1;
      //   lStorage.set('profile_' + i, lStorage.jsonStringify(_data).slice(i*1024 + i-c, (i+1)*1024+i));
      // }
      // lStorage.set('profile', k);

    }else{
      lStorage.set('profile', lStorage.jsonStringify(_data));
    }
  },

  set: function(key, txt){
    
    if(lStorage.intExplorer){
      $.cookie(key, txt, { expires: 365000 });
    }else{
      localStorage.setItem(key, txt);
    }
  },

  get: function(txt){
    var txt = txt + '';
    if(lStorage.intExplorer){
      return typeof $.cookie(txt) != 'undefined' ? $.cookie(txt) : false;
    }else{
      return localStorage.getItem(txt);
    }
  },

  removeItem: function(id){
    var delElem = false;
    $.each(lStorage.cart.items, function(i, elem){
      if(elem.id == id){
        delElem = i;
      }
    });
    lStorage.cart.items.splice(delElem, 1);
    lStorage.set('cart', lStorage.jsonStringify(lStorage.cart));
    return true;
  },

  clear: function(key){
    if(key){
      if(lStorage.intExplorer){
        $.removeCookie(key);
      }else{
        localStorage.removeItem(key);
      }
    }else{
      localStorage.clear();
    }
    
  },

  existence: function(key){
    if(lStorage.get(key)){
      return true;
    }else{
      return false;
    }
  },

  jsonStringify: function(obj){
    return JSON.stringify(obj);
  },

  jsonParse: function(obj){
    return JSON.parse(obj);
  },

  updateCart: function(idx, id, article, count, elem){
    var p = true,
        min = elem.data('min'),
        max = elem.data('max'),
        price = elem.data('price'),
        sum = null,
        cartGoodsDel = -1;

    sum = iElectro.priceFormat((count * price).toFixed(2));
    elem.parent().parent().find('.product_price').html(sum);

    $.each(lStorage.cart.items, function(i, elem){
      if(elem.id == id && typeof id != 'undefined'){
        if(count == 0){
          cartGoodsDel = i;
        }
        elem.id = id;
        elem.article = article;
        elem.count = count;
        elem.sum = sum.replace(/&nbsp;/g, '');
        p = false;
      }
    });
    if(cartGoodsDel != -1){
      lStorage.cart.items.splice(cartGoodsDel, 1);
      cartGoodsDel= -1;
    }
    if(p){
      lStorage.cart.items.push({
        idx: idx,
        id: id,
        article: article,
        count: count,
        sum: sum.replace(/&nbsp;/g, '')
      });
    };
    // lStorage.cart.userName = profile.username;
    lStorage.set('cart', lStorage.jsonStringify(lStorage.cart));
    iElectro.calcTotal($('.goods_cart_table tbody'));
  }

};