function recalc(){
   var totalPrice = parseInt($('#total_cart_price').attr('original')) ||
         $('.items tr').each(function(i, item){
            var item = $(item);
            var price1 = parseInt(item.find('.f_price1').text()) || 0;
            var count = parseInt(item.find('.f_count').val()) || 0;
            totalPrice += price1 * count;
         });

   /*var usePrice = 1;
    if(totalPrice > 75000){
    usePrice = 4;
    } else if(totalPrice > 25000){
    usePrice = 3;
    } else if(totalPrice > 15000){
    usePrice = 2;
    }*/
   var usePrice = 3;
   if(totalPrice > 75000){
      usePrice = 4;
   } else if(totalPrice > 25000){
      usePrice = 3;
   } else if(totalPrice > 15000){
      usePrice = 3;
   } 

   //console.log(totalPrice, usePrice);
   totalPrice = 0;
   $('.items tr').each(function(i, item){
      var item = $(item);
      var price1 = parseInt(item.find('.f_price' + usePrice).text()) || 0;
      var count = parseInt(item.find('.f_count').val()) || 0;
      totalPrice += price1 * count;
   });
   if(totalPrice){
      $('#total_price span').text(totalPrice);
      $('#fix_price').show();
   } else{
      $('#fix_price').hide();
   }
}

$(document).ready(function(){

   $('.validated').validationEngine();

   $('.category .link').click(function(e){
      var items = $(this).parent().find('> .items').toggle();

      var fillWith = $(items).attr('fillWith');
      if(!$(items).attr('filled')){
         items.html(
               '<img src="/images/ajax-loader.gif" alt="loading..." />'
         )
         $.ajax({
            type:'GET',
            cache:false,
            url:'/port/',
            data:{group_id:fillWith},
            success:function(result){
               $(items).html(result).attr('filled', true);
               $(items).find('tr:nth-child(even)').addClass('even_tr');
               $('.lightbox').lightBox();
            }});
      }
   });

   $('#buyAll').live('click', function(e){
      e.preventDefault();
      if(confirm('Вы уверены, что хотите добавить все выбранные товары в корзину?')){
         var countElements = $('.f_count').filter(
               function(index, item){
                  return parseInt($(item).val()) > 0;
               });
         var requestStr = '';
         countElements.each(function(i, item){
            requestStr += $(item).attr('itemid') + '-' + parseInt($(item).val()) + ',';
         });
         $.ajax({
            url:'/shop/cart/?data=' + requestStr + '&ajax_add_item_id=1',
            type:'GET',
            cache:false,
            success:function(result){
               if(result){
                  $('.f_count').val('');
                  $('#shortCart').html(result);
                  alert('Товары добавлены.');
               }
               //window.location = '/shop/cart/';
            }
         })
      }
   });

   $('.addtocart').live('click', function(e){
      e.preventDefault();
      var fCount = $(this).closest('tr').find('.f_count');
      var count = fCount.val() || 0;
      var itemId = fCount.attr('itemid');
      var requestStr = '';
      if(count > 0){
         fCount.val('');
         requestStr += itemId + '-' + parseInt(count);
         $.ajax({
            url:'/shop/cart/?data=' + requestStr + '&ajax_add_item_id=1',
            type:'GET',
            cache:false,
            success:function(result){
               if(result){
                  $('#shortCart').html(result);
                  alert('Товары добавлены.');
               }
               //window.location = '/shop/cart/';
            }
         })
      }
   });

   $('.items').live('change', function(){
      recalc();
   });
});

jQuery.cookie = function(key, value, options){

   // key and at least value given, set cookie...
   if(arguments.length > 1 && String(value) !== "[object Object]"){
      options = jQuery.extend({}, options);

      if(value === null || value === undefined){
         options.expires = -1;
      }

      if(typeof options.expires === 'number'){
         var days = options.expires, t = options.expires = new Date();
         t.setDate(t.getDate() + days);
      }

      value = String(value);

      return (document.cookie = [
         encodeURIComponent(key), '=',
         options.raw ? value : encodeURIComponent(value),
         options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
         options.path ? '; path=' + options.path : '',
         options.domain ? '; domain=' + options.domain : '',
         options.secure ? '; secure' : ''
      ].join(''));
   }

   // key and possibly options given, get cookie...
   options = value || {};
   var result, decode = options.raw ? function(s){
      return s;
   } : decodeURIComponent;
   return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


$(document).ready(function(){
   $('.uppender').click(function(){
      var menuItem = $(this).closest('.l_menu');
      if(menuItem.hasClass('l_hasSubs')){
         menuItem.toggleClass('l_menu_noitems');
      }
      var opened = [];
      $('.l_menu').each(function(){
         opened.push(0 + !$(this).hasClass('l_menu_noitems'));
      });
      $.cookie('menu_opened', opened.join('|'), { 'expires':7, path:'/' });
   })

   var menuOpened = $.cookie('menu_opened');
   if(menuOpened){
      menuOpened = menuOpened.split('|');
      var menus = $('.l_menu');
      for(var i = 0; i < menuOpened.length; i++){
         if(menuOpened[i] == 1){
            menus.eq((i)).find('.uppender').click();
         }
      }
   }


})
