$(function() {
	$(".content table[border=1]").addClass("colored").each(function() {
		$(this).attr("border", "0").after("<br/><br/>").before("<br/>");
		$("tr:last td", this).css("border-bottom", "none");

		if ($("tr:last td", this).length == 4) {
			if($("tr:first td[colspan]", this).length) {
				$("tr:first td", this).attr("colspan", "5");
				$("tr:first~tr", this).each(function(){
					$("td", this).eq(1).after("<td class='nobord'>&nbsp;&nbsp;&nbsp;</td>");			
				})
			} else {
				$("tr", this).each(function(){
					$("td", this).eq(1).after("<td class='nobord'>&nbsp;&nbsp;&nbsp;</td>");			
				})
			}		
		}
	})
		
	$("form.form input.txt, form.form textarea").each(function(){
		$(this).attr("default_value", $(this).val());

		if($(this).attr("default_value") == "Ваше имя*" || $(this).attr("default_value") == "Количество аппаратов*" || $(this).attr("default_value") == "Телефон для связи*" || $(this).attr("default_value") == "Примечания" || $(this).attr("default_value") == "Электронная почта*" ){


		$(this).focus(function(){
			if ($(this).attr("value") == $(this).attr("default_value")){
				$(this).attr("value", '')
			} 
		} );

		$(this).blur(function(){
			if ($(this).attr("value") == ""){
				$(this).attr("value", $(this).attr("default_value"));
			} 		
		} );
		};
	});


	$("#faq input.txt, #faq textarea").each(function(){
		$(this).attr("default_value", $(this).val());

		if($(this).attr("default_value") == "Ваше имя*" || $(this).attr("default_value") == "Вопрос*" || $(this).attr("default_value") == "Электронная почта*" ){


		$(this).focus(function(){
			if ($(this).attr("value") == $(this).attr("default_value")){
				$(this).attr("value", '')
			} 
		} );

		$(this).blur(function(){
			if ($(this).attr("value") == ""){
				$(this).attr("value", $(this).attr("default_value"));
			} 		
		} );
		};
	});

	$("#descr a.more").click(function() {
		$(this).next().toggle();
		return false;
	}); 
	
	function plural_form(n, form1, form2, form5) {
        n = n % 100;
        n1 = n % 10;
        if (n > 10 && n < 20) return form5;
        if (n1 > 1 && n1 < 5) return form2;
        if (n1 == 1) return form1;
        return form5;
    }

	function updatetopcart () {
		var cost  = 0;
		var count = 0;
		$('table.cartitems tr.row').each(function(){
			cost  += parseInt($(this).find("td#amount").text());
			count += parseInt($(this).find("input.count").val());
		})		

		updateCartString(count, cost);
	}

	function updateCartString(count, cost) {
		var tovar = plural_form(count, "товар", "товара", "товаров");

		$("#cart_count").empty().text(count + " " + tovar);
		$("#cart_amount").empty().text(cost);
		$("#itog").empty().text(cost);
	}

	function apdatesession (id, count){
		if(count == "0"){count = 1}
		$.post("/ajax/AddToCart.php?id="+id +"&count="+count+"");	
	}

	$(".incart a, .incart, a.incart").click(function() {
		offset = $(this).offset();
		$("#message_block").css("top", offset.top+"px");
		$("#message_block").css("left", (offset.left+192)+"px");
		$("#message_block").show().stop().fadeTo(0, 1);
		$("#message_block").fadeOut(2500);

		$.post($(this).attr("href"), {}, function(values) {
			updateCartString(values[0], values[1]);
		}, "json");
		return false;
	});
	
	$('table.cartitems input.count').each(function(){
		$(this).keyboard('aup', function() {
			var count = parseInt($(this).val());
			if (isNaN(count) || count < 1 ) count = 1;

			$(this).val(count + 1);
			apdatesession($(this).parents("tr").attr("id"), $(this).val());
		});

		$(this).keyboard('adown', function() {
			var count = parseInt($(this).val());
			if (isNaN(count) || count < 1) count = 1;

			$(this).val(count - 1);
			apdatesession($(this).parents("tr").attr("id"), $(this).val());
		});

		$(this).change(function(){apdatesession($(this).parents("tr").attr("id"), $(this).val());})
	});

	$('table.cartitems input.count').keyup(function(event){
		var count = parseInt($(this).val());

		if(isNaN(count)) { count = 1; $(this).val("1")};

		if(count < 1){
			$(this).val(1);
			count = 1;
		}

		var price =  $(this).parent().prev("#price").text();
		$(this).parent().next("#amount").html(price*count+" <img src='/img/rub.gif' />");
		updatetopcart();
		
	})

	$('table.cartitems td#del img').click(function(){
		if (confirm("Вы действительно хотите убрать данную модель из корзины заказа?"))	{
			var id = $(this).attr("item_id");
			$("tr[id = '"+id +"']").remove();
			$.post("/ajax/DelFromCart.php?id="+id +"");
			updatetopcart ();
		}
	})
});

