/**
 * ������ ���������-������������ (��������) ��� ������� ��������
 */
$(document).ready(function(){
	
		//Init sliders
		$( "#slider_seo").slider({
			range: "min",
			value:50000,
			min: 0,
			max: 100000,
			step: 1000,
			slide: function( event, ui ) {
				seo = ui.value;
				$('#input_seo').val( my_format( seo ) );			
				update();
			}
		});
		

		$( "#slider_site").slider({
			range: "min",
			value:40000,
			min: 40000,
			max: 500000,
			step: 1000,
			slide: function( event, ui ) {
				site = ui.value;
				$('#input_site').val( my_format( site) );
				update();
			}
		});
		
		
		$( "#slider_profit").slider({
			range: "min",
			value:10000,
			min: 500,
			max: 100000,
			step: 1,
			slide: function( event, ui ) {
				profit_one = ui.value;
				$('#input_profit').val( my_format( profit_one) );
				update();
			}
		});
		
		
		//Init global values
		var seo = $('#slider_seo').slider('value');
		var site = $('#slider_site').slider('value');
		var profit_one = $('#slider_profit').slider('value');

		//Init Input values
		$('#input_seo').val(my_format( seo ));
		$('#input_site').val(my_format( site ));
		$('#input_profit').val( my_format( profit_one ) );
		
		//Handler on Input
		$('#input_seo, #input_site, #input_profit').change(function(){
			name = $(this).attr('id').split('_');
			if(name[1] == 'profit') name[1] = 'profit_one';
			eval('value = ' + name[1] + '=' + parseInt($(this).val().replace(' ','')));
			$('#slider_' + name[1]).slider('value', value);
			update();
		});
		
		update();
		
		function update() {

			cost = seo + site;
			visitors = seo / 50; //���� �� ����
			seo_percent = seo / $('#slider_seo').slider('option','max') * 100;
			conversion = calc_conversion(site);
			clients = conversion * 0.01 * visitors;
			profit = clients * profit_one - cost; 
			payback = Math.ceil(cost / profit_one);
			
			$('#cost_summ').text( my_format( cost ));
			$('.convertion .absolute, .seo img, .seo_up img').css('width',  seo_percent + '%');	
			
			if(seo_percent ==0)
				$('.convertion .absolute').css('width',  '1%');	

			$('#count_visitors').text(my_format(visitors));
			$('#conversion_value').text(conversion.toFixed(1)+ '%');
			$('.absolute img').css('width', conversion + '%');
			$('#count_clients').text(my_format(clients));
			$('.count_profit').text(my_format(profit));
			$('#count_payback').text(my_format(payback));

			//Устанавливаем текстовые значения (Клиента, клиентов, клиент)
			$('#text_number_payback').text( numberOf(Math.round(payback), 'клиент окупит', 'клиента окупят', 'клиентов окупят', 0) );
			$('#text_number_clients').text(numberOf(Math.round(clients), 'клиента', 'клиентов', 'клиентов', 0) );
			$('#text_number_clients_preposition').text((Math.round(clients) > 99 && Math.round(clients) < 200) ? 'со' : 'с');
			
			if(profit < 0) {
				$('#text_profit_plus').hide(); 
				$('#text_profit_minus').show(); 
				$('#top_count_profit').text(my_format(profit*-1));
			}
			else  {
					$('#text_profit_minus').hide(); 
					$('#text_profit_plus').show();
			}
			
 
		}
		
		function my_format(value) {
	
			return number_format(value, 0, '.', ' ');
		}
		
		function calc_conversion(price) {
		
			price *= 0.001;			
		
			a = -43.409733381326;
			b = 17.848152058318;
			c = -0.17006758795737;
			d = 42.480274637639;
			

			return Math.pow(price, 1 / 3) * a + Math.pow(price, 1 / 2) * b + price * c + d;
			
			a =  1.6427639264524;
			b = -0.031399055509758;
			c = -9,0337890773032;
			

			return Math.pow(price, 1 / 2) * a + price * b + c;


		}

			
		
		function numberOf(number, w1, w2, w5, appendNumber)
		{
			var w = '';	
			var n1 = number % 10;   // 123 % 10 = 3
			var n12 = number % 100; // 123 % 100 = 23
		 
			if (n1 >= 5 || n1 == 0 || (n12 >= 11 && n12 <= 19))
				w = w5;
			else if (n1 == 1)
				w = w1;
			else
				w = w2;
		 
			if (appendNumber == null)
				appendNumber = true;
		 
			return appendNumber ? (number + ' ' + w) : w;
		}
});
