/**
 * 
 * @author Artod for ООО "Экспресс лаб" http://expresslab.ru
 * http://artod.ru
 *
 */

$(document).ready(function(){

	$("a.fancybox").fancybox({
		'zoomOpacity' : false,
		'overlayShow' : true,
		'hideOnContentClick' : false,
		'overlayOpacity' : '0.8',
		'zoomSpeedIn' : '200',
		'zoomSpeedOut' : '200'
	});

	$('.errorMessage').live('click',
		function(){
			$(this).fadeOut('slow', function(){$(this).remove();});
		}
	);
	
	$('.successMessage').live('click',
		function(){
			$(this).fadeOut('slow', function(){$(this).remove();});
		}
	);

	$('#calculation').click(
		function(){
			removeMess();
			
			var court = parseInt($("select#court option:selected").val());
			var price = parseFloat($("#price").val().replace(',', '.').replace(' ', ''));

			if (!court) {
				errMess('Выберите суд');
				return false;
			}
			else if (!price) {
				errMess('Введите цену иска');
				return false;
			}
			
			var res = 0;
			
			if (court == 2) {
				if (price <= 10000) {
					//до 10 000 рублей - 4 процента цены иска, но не менее 200 рублей;
					res = price/100*4;
					res = (res<200 ? 200 : res);
				}
				else if (price > 10000 && price <= 50000) {
					//от 10 001 рубля до 50 000 рублей - 400 рублей плюс 3 процента суммы, превышающей 10 000 рублей;
					res = 400 + (price-10000)/100*3;
				}
				else if (price > 50000 && price <= 100000) {
					//от 50 001 рубля до 100 000 рублей - 1 600 рублей плюс 2 процента суммы, превышающей 50 000 рублей;
					res = 1600 + (price-50000)/100*2;
				}
				else if (price > 100000 && price <= 500000) {
					//от 100 001 рубля до 500 000 рублей - 2 600 рублей плюс 1 процент суммы, превышающей 100 000 рублей;
					res = 2600 + (price-100000)/100*1;
				}
				else if (price > 500000) {
					//свыше 500 000 рублей - 6 600 рублей плюс 0,5 процента суммы, превышающей 500 000 рублей, но не более 20 000 рублей;
					res = 6600 + (price-500000)/100*0.5;
					res = (res>20000 ? 20000 : res);
				}
			}
			else if (court == 1) {

				if (price <= 50000) {
					//до 50 000 рублей - 4 процента цены иска, но не менее 500 рублей;
					res = price/100*4;
					res = (res<500 ? 500 : res);
				}
				else if (price > 50000 && price <= 100000) {
					//от 50 001 рубля до 100 000 рублей - 2 000 рублей плюс 3 процента суммы, превышающей 50 000 рублей;
					res = 2000 + (price-50000)/100*3;
				}
				else if (price > 100000 && price <= 500000) {
					//от 100 001 рубля до 500 000 рублей - 3 500 рублей плюс 2 процента суммы, превышающей 100 000 рублей;
					res = 3500 + (price-100000)/100*2;
				}
				else if (price > 500000 && price <= 1000000) {
					//от 500 001 рубля до 1 000 000 рублей - 11 500 рублей плюс 1 процент суммы, превышающей 500 000 рублей;
					res = 11500 + (price-500000)/100*1;
				}
				else if (price > 1000000) {
					//свыше 1 000 000 рублей - 16 500 рублей плюс 0,5 процента суммы, превышающей 1 000 000 рублей, но не более 100 000 рублей;
					res = 16500 + (price-1000000)/100*0.5;
					res = (res>100000 ? 100000 : res);
				}

			}
			else {
				succMess('O_o');
			}
			
			succMess('Размер госпошлины: '+res+' руб.');
			
			return false;
		}
	);
	
	$('#reset').click(
		function(){
			removeMess();
		}
	);

});

function removeMess() {
	$(".successMessage").fadeOut('slow', function(){$(this).remove();});
	$(".errorMessage").fadeOut('slow', function(){$(this).remove();});
}

function errMess(mess) {
	if (!mess)
		return;
	var id = 'errorMessage';
	$("#duty").prepend("<div class='"+id+"'>"+mess+"</div>");
	$("."+id+"").hide();
	$("."+id+"").fadeIn();
}


function succMess(mess) {
	if (!mess)
		return;
	var id = 'successMessage';
	$("#duty").prepend("<div class='"+id+"'>"+mess+"</div>");
	$("."+id+"").hide();
	$("."+id+"").fadeIn();
}
