﻿
 	var dataArr;

	$(document).ready(function(){
		//visual effect
		$('.to_open').click(function(){
			$(this).next().slideToggle('fast');
			$(this).toggleClass('to_close');
			return false;
		});
		
		$('.kat_to_open').click ( function(){
			$(this).next('.bot_opened').slideToggle('fast');
			$(this).toggleClass('kat_to_close');
			return false;
		});
		
		//show first
		$('.kat_to_open:first').addClass('kat_to_close').next('.bot_opened').show();
		
		//price calculate
		
		var inputArr = $('#calculator input.controlbtn');
		for (var i = 0; i < inputArr.length; i++) 
		{
			val = $(inputArr[i]).val();
			if (dataArr[val].select > 0) 
			{
				dataArr[val].select = $(inputArr[i]).closest('tr').find('select');
			}
		}
		////////////////////////
		var pricearea = $("#total");
		var coast = $("#coast");
		////////////////////////
		$(".checkboxArea").click(function() 
		{
			var checkbox = $(this).parent().find("input.controlbtn:checkbox");
			if ($(checkbox).attr('checked')) 
			{
				$(checkbox).attr('checked', false);
			}
			else 
			{
				$(checkbox).attr('checked', true);
			}
			recalcPrice();
			return false;
		});
		//Price format
		     Number.prototype.triads = function (triadSeparator)
				{
					var num = parseInt(this).toString();
					var regEx = /(-?\d+)(\d{3})/;
					while (regEx.test(num)) { num = num.replace(regEx,"$1"+triadSeparator+"$2"); }
					return num;
			    }
 		///////////////////////
		$("#calculator select, #calculator input.controlbtn").click(function(){
			recalcPrice();
		});
		recalcPrice();
		function recalcPrice() 
		{
			var total = 0;
			for (var i = 0; i < inputArr.length; i++) 
			{
				if ($(inputArr[i]).attr("checked")) 
				{
					val = $(inputArr[i]).val();
					var price = dataArr[val].price*1;
					if (dataArr[val].select != 0) 
					{
						price = price*$(dataArr[val].select).val();
					}
					total = total + price;
				}
			}
			if (total > 0) 
			{
				$(pricearea).show();
				total=total.triads(' ');
				$(coast).text(total);
			}
			else 
			{
				$(pricearea).hide();
			}
		}
		$(".tarif_prices input:checked").next("label").addClass("RadioSelected");
		$(".tarif_prices input").click(function()
		{
			if ($(this).is(":checked"))
			{
				$(".RadioSelected:not(:checked)").removeClass("RadioSelected");
				$(this).next("label").addClass("RadioSelected");
			}
		});

	});

