(function(){
	function ComplexTableCalculateItemTotalPrice(item)
	{
		var select = item.find('.sizeSelect');
		var index = select.val();
		var count = select.children('option[value='+index+']').html();
		var count = item.find('.size').html();
		var price = item.find('.price').html();
		var totalPrice = Math.round(count * price);
		item.find('.totalPrice').html(totalPrice);
	}
	
	function ComplexTableCalculateTotalPrice()
	{
		var totalPrice = 0;
		$('.fieldsTable.complex .totalPrice').each(function(){
			totalPrice += $(this).html()*1;
		});
		$('.complexTotalPrice').html(totalPrice);
	}
	
	function WorkTableCalculateTotalPrice()
	{
		$('.fieldsTable.work .complex .price').html($('.complexTotalPrice').html());
		var totalPrice = 0;
		$('.fieldsTable.work .price').each(function(){
			if($(this).parent().find('.checkbox').attr('checked'))
				totalPrice += $(this).html()*1;
		});
		$('.workTotalPrice').html(totalPrice);
	}
	
	function CalculatorEventsInitialize()
	{
		// width
		$('.fieldsTable.complex .widthSelect').change(function(){
			var index = $(this).val();
			var price = $(this).children('option[value='+index+']').attr('rel');
			var item = $(this).parent().parent();
			item.children('.size').html($(this).children('option[value='+index+']').attr('data-size'));
			item.children('.price').html(price);
			ComplexTableCalculateItemTotalPrice(item);
			ComplexTableCalculateTotalPrice();
			WorkTableCalculateTotalPrice();
		});
		
		//size
		/*$('.fieldsTable.complex .sizeSelect').change(function(){
			var item = $(this).parent().parent();
			ComplexTableCalculateItemTotalPrice(item);
			ComplexTableCalculateTotalPrice();
			WorkTableCalculateTotalPrice();
		});*/
		
		$('.fieldsTable.work .checkbox').change(function(){
			WorkTableCalculateTotalPrice();
		});
		
		//button
		$('.calculator .button').click(function(){
			WorkTableCalculateTotalPrice();
		});
	}
	
	$(document).ready(function(){
		CalculatorEventsInitialize();
		$('.fieldsTable.complex tr[class!=structureElements][class!=total]').each(function(){
			ComplexTableCalculateItemTotalPrice($(this));
		});
		ComplexTableCalculateTotalPrice();
		WorkTableCalculateTotalPrice();
		
		$('.blog_content_inner > a[rel=example_group]').remove();
		$('.houseGalleryWrapper .e2g > .like_pic:first-child').addClass('first');
	});
})();


