// Преобразует объект в число
function intval(e)
{
	e = (typeof(e) != 'undefined' && e)
		? parseInt(e.toString().replace(/\D/gi, ''), 10)
		: 0;
	return isNaN(e) ? 0 : e;
}

// Форматирует денежные величины
function rubformat(e)
{
	return e
		? e.toString().replace(/(\d{2,})(\d{3})/g, '$1&nbsp;$2').replace('-', '&minus;').replace('.', ',')
		: '&minus;'
}

// Калькулятор зорбобизнеса
function Calc(){
	
	this.tpu_ball_price = 19500;
	this.pvh_ball_price = 14900;
	this.zorb_price =     22900;
	this.dop_price =       5000;
	
	// Считает начальные затраты и кол-во шаров
	this.calc_init_cost = function(){
		
		// Шары ТПУ
		jQuery('#tpu_balls_price').html(rubformat(
			this.tpu_ball_price * intval(jQuery('#tpu_balls').val())
		));
		
		// Шары ПВХ
		jQuery('#pvh_balls_price').html(rubformat(
			this.pvh_ball_price * intval(jQuery('#pvh_balls').val())
		));
		
		// Зорбы
		jQuery('#zorbs_price').html(rubformat(
			this.zorb_price * intval(jQuery('#zorbs').val())
		));
		
		// Допобор
		jQuery('#dop').html(rubformat(this.dop_price));
		
		// Сумма всех первоначальных затрат
		this.init_cost =
			intval(jQuery('#tpu_balls').val()) * this.tpu_ball_price
			+ intval(jQuery('#pvh_balls').val()) * this.pvh_ball_price
			+ intval(jQuery('#zorbs').val()) * this.zorb_price
			+ this.dop_price;
		jQuery('#init_cost').html(rubformat(this.init_cost));
		
		// Общее число шаров
		this.balls_count = intval(jQuery('#tpu_balls').val())
			+ intval(jQuery('#pvh_balls').val())
			+ intval(jQuery('#zorbs').val());
		jQuery('#balls_count').html(this.balls_count);
	};
	
	this.calc_oper_costs = function() {
		// Операционные затраты, руб./мес.
		this.oper_costs = intval(jQuery('#places').val()
			* jQuery('#rent_price').val()
			+ jQuery('#wage').val() * this.days * jQuery('#staff').val());
		jQuery('#oper_costs').html(rubformat(this.oper_costs)
			+ ' руб./мес.');
		jQuery('#total_oper_costs').html(rubformat(this.oper_costs)
			+ ' руб.');
	};
	
	this.calc_income = function() {
		// Доходы
		this.daily_income = intval(jQuery('#our_price').val())
			* intval(jQuery('#people').val()) * this.balls_count;
		jQuery('#daily_income').html(rubformat(this.daily_income)
			+ ' руб./день');
		
		this.monthly_income = this.daily_income * this.days;
		jQuery('#monthly_income').html(rubformat(this.monthly_income)
			+ ' руб./мес.');
		jQuery('#total_monthly_income').html(rubformat(this.monthly_income)
			+ ' руб.');
	};
	
	this.calc_totals = function(){
		// Итоги
		this.profit = this.monthly_income * 0.93 - this.oper_costs;
		jQuery('#profit').html(rubformat(this.profit) + ' руб.');
		
		jQuery('#fm_profit').html(rubformat(this.profit - this.init_cost)
			+ ' руб.');
		
		jQuery('#total').html(rubformat(this.profit * this.month
			- this.init_cost) + ' руб.');
	};
	
	this.recalc = function(changed){
		if (changed.id == 'tpu_balls' || changed.id == 'pvh_balls'
			|| changed.id == 'zorbs')
		{
			this.calc_init_cost();
			this.calc_income();
		}
		else if (changed.id == 'places'
			|| changed.id == 'rent_price' || changed.id == 'wage'
			|| changed.id == 'staff')
		{
			this.calc_oper_costs();
		}
		else if (changed.id == 'our_price' || changed.id == 'people')
		{
			this.calc_income();
		}
		else if (changed.id == 'days')
		{
			this.days = intval(jQuery('#days').val());
			this.calc_oper_costs();
			this.calc_income();
		}
		
		this.calc_totals();
	};
	
	// Инициализация
	this.calc_init_cost();
	this.days = intval(jQuery('#days').val());
	this.month = intval(jQuery('#month').val());
	this.calc_oper_costs();
	this.calc_income();
	this.calc_totals();
};

jQuery(document).ready(function(){
	window.calc = new Calc();
	
	jQuery('#calc input').keyup(function(){
		window.calc.recalc(this);
	});
});


