﻿var s25_cart = Class.create();
 s25_cart.prototype = {
 initialize: function(){
     var cart = getCookie('cart[' + uid + ']');
     if (cart != null) {
         cart = cart.parseJSON();
     }
     this.cHash = $H(cart);
 },
     //n - id_nom, 0 - id_opa
 add: function(n, o) {
     var tmp = new Object();
     tmp[n.toString() + o.toString()] = '1';
     this.cHash=this.cHash.merge(tmp);
 },
 // div - id div object
 redraw: function() {
     var ap = $('cart_all_price');
     var i = this.get_items();
     $('cart_all_count').innerHTML = i['c'] + '&#160;шт.';
     ap.innerHTML = add_commas(i['p']);
     if (i == null || i['c'] == 0) {
         $('cart_data').style.cursor = 'default';
         $('cart_href').style.cursor = 'default';
     } else {
         $('cart_href').style.cursor = 'pointer';
         $('cart_data').style.cursor = 'pointer';
     }
 },
 get_item: function(n, o, g) {
     var exist = this.in_cart(n,o,g);
     if (exist != null) {
         return this.cHash[n + '_' + o + '_' + g];
     } else {
         return false;
     }
 },
 get_items: function() {
     var sum = 0;
     var cnt = 0;
     var vals = this.cHash.values();
     for (var i = 0; i < vals.length; i++) {
         sum += vals[i]['c'] * parseFloat(vals[i]['p'], 10);
         cnt += vals[i]['c'];
     }
     var pr = Math.round(sum *100) / 100;
     return {'p': pr, 'c': cnt};
 },
 get_cart: function() {
     return this.cHash;
 },
 //n - id_nom, o - id_opa, g - id_grp, c - count, p - price
 add_in_cart: function(n,o,g,c,p) {
     var tmp = new Object();
     var exist = this.in_cart(n,o,g);
     if (exist == null) {
         tmp[n + '_' + o + '_' + g] = {'c': c, 'p': p};
         this.cHash=this.cHash.merge(tmp);
     } else {
         this.cHash[n + '_' + o + '_' + g]['c'] = parseInt(this.cHash[n + '_' + o + '_' + g]['c'], 10) + parseInt(c, 10);
     }
     setCookie('cart[' + uid + ']', this.cHash.toJSONString(), getExpDate(30,0,0), '/', cookie_http_path);
     this.redraw();
 },
 change_count: function(n,o,g,c) {
     var exist = this.in_cart(n,o,g);
     if (exist != null) {
         this.cHash[n + '_' + o + '_' + g]['c'] = c;
         setCookie('cart[' + uid + ']', this.cHash.toJSONString(), getExpDate(30,0,0), '/', cookie_http_path);
     }
 },
 remove: function(n,o,g) {
     var exist = this.in_cart(n,o,g);
     if (exist != null) {
         delete this.cHash[n + '_' + o + '_' + g];
         setCookie('cart[' + uid + ']', this.cHash.toJSONString(), getExpDate(30,0,0), '/', cookie_http_path);
         this.redraw();
         Element.remove("i_" + n + '_' + o + '_' + g);
         if ($("ci_" + n + '_' + o + '_' + g)) {
             Element.remove("ci_" + n + '_' + o + '_' + g);
         }
     }
 },
 ccount: function(n,o,g) {
     var exist = this.in_cart(n,o,g);
     if (exist != null) {
         var cnt = parseInt($F('cnt_' + n + '_' + o + '_' + g), 10);
         if (! isNaN(cnt)) {
             this.cHash[n + '_' + o + '_' + g]['c'] = cnt;
             setCookie('cart[' + uid + ']', this.cHash.toJSONString(), getExpDate(30,0,0), '/', cookie_http_path);
             this.redraw();
         }
     }
 },
 clear: function() {
     setCookie('cart[' + uid + ']', null, getExpDate(30,0,0), '/', cookie_http_path);
 },
 in_cart: function(n,o,g) {
     return this.cHash.keys().find(function(k){
 			return (k == (n + '_' + o + '_' + g));
 		});
 },
 is_empty: function() {
     return (this.cHash.values().length == 0) ? true : false;
 }
 }