var estmark = function ()
{
	var that = {};

	that.init = function(h_param)
	{
		if (h_param['list'] && h_param['counter'])
		{
			that.o_est_objects = {};
			var s_ids = '';
			h_param.list.each(function(){
				var id = $(this).attr('request_id');
				that.o_est_objects[id] = $(this);
				s_ids += id+',';
			});
			if (s_ids.length>0)
			{
				$.ajax({
				   type: 'POST',
				   dataType: 'json',
				   url: '/_ajax/estmark_list.html',
				   data: 'estmark_list='+s_ids.substring(0,s_ids.length-1),
				   success: function(o_resp){
						if(o_resp && o_resp['js']){
							var ids_response = o_resp.js;
							jQuery.each(that.o_est_objects, function(i, e){
								e.addClass(ids_response.indexOf('['+i+']')>=0 ? 'minus' : 'plus');
								that.ieActionsFix(e);
								e.find('.estmark-action span').click(function(){that.estmark(e, h_param.counter)});
							});
						}
				   }
				});
			}
		}
	}
	
	that.estmark = function(e, counter)
	{
		if (null == that.e_counter_container) {
			that.e_counter_container = $(counter);
			that.e_counter = that.e_counter_container.find('span.count');
			that.e_counter_label = that.e_counter.find('span.label');
		}
		
		var sign = e.hasClass('minus') ? '-' : '';
		e.toggleClass('load');
		$.ajax({
		   type: 'POST',
		   dataType: 'json',
		   url: '/_ajax/estmark.html',
		   data: 'estmark='+sign+e.attr('request_id'),
		   success: function(o_resp){
				if(o_resp && o_resp['js'] && o_resp.js == 1){
					e.toggleClass('minus').toggleClass('plus').toggleClass('load');
					that.ieActionsFix(e);
			
					var count = parseInt(that.e_counter.text());
					count = sign ? count-1 : count+1;
					var label = '';
					if(count>10 && Math.floor((count%100) / 10) == 1)
					{
						label = that.e_counter.attr('genitive-plural'); 
					}
					else
					{
						var rem = count%10;
						if (rem == 1)
						{
							label = that.e_counter.attr('nominative');
						}
						else if (rem == 2 || rem == 3 || rem == 4)
						{
							label = that.e_counter.attr('genitive-singular');
						}
						else
						{
							label = that.e_counter.attr('genitive-plural');
						}
					}
					that.e_counter.text(count);
					that.e_counter_label.text(label);
					that.e_counter_container[count>0?'removeClass':'addClass']('hidden');
				}
		   }
		 });
	}
	
	that.ieActionsFix = function(e) {
		if ($.browser.msie) {
			if (e.hasClass('plus')) {
				$('.item-actions .plus-action', e).show();
				$('.item-actions .minus-action', e).hide();
			}
			if (e.hasClass('minus')) {
				$('.item-actions .minus-action', e).show();
				$('.item-actions .plus-action', e).hide();
			}
		}
	}

	return that;
};
