var note_opened = 1;
var price = 0;
var base_price = 0;
var $cur_obj;

function isInteger(s)
{
  return /^-?\d+$/.test(s);
}

function refreshPrice(price) {

	var p = price;

	if(p >= summa1)
		$("#calc_skidka5").css("display", "block");
	else {
		$("#calc_skidka5").css("display", "none");
		$("#calc_skidka5 input").attr("checked", "");
		$("#calc_skidka5 input").change();
	}

	if(p >= summa2)
		$("#calc_skidka10").css("display", "block");
	else {
		$("#calc_skidka10").css("display", "none");
		$("#calc_skidka10 input").attr("checked", "");
		$("#calc_skidka10 input").change();
	}

	new_price = p;

	if($("#calc_skidka5 input").attr("checked"))
		new_price *= (100 - procent1) / 100;

	if($("#calc_skidka10 input").attr("checked"))
		new_price *= (100 - procent2) / 100;

	if(price < minimum) {
		$("#calc_price span").html(minimum + "&nbsp;ð.");
		$("#calc_minimum").css("visibility", "visible");
	}else {
		$("#calc_price span").html(new_price + "&nbsp;ð.");
		$("#calc_minimum").css("visibility", "hidden");
	}

}


function calc_base_price(obj) {

	base_price = 0;

	$(".calc_cats input:text").each(function() {
		if($(this)[0] != $cur_obj[0]) {
			var $parent = $(this).parent().parent().parent();

			if( $parent.find("td:eq(2) input:checkbox").attr("checked") )
				base_price += parseInt($parent.find("td:eq(5) span").html());
		}
	});
}


function check() {

	var $parent = $cur_obj.parent().parent().parent();

	v = $cur_obj.val();
	v = parseInt(v);
	v = Math.abs(v);
	if( v > 9999 )
		v = 9999;
	if( v == 0 )
		v = 1;
	if( !v )
		v = "";
	$cur_obj.val(v);

	if( v == "" )
		$parent.find("td:eq(5) span").html("0&nbsp;ð.");
	else
		$parent.find("td:eq(5) span").html($parent.find("td:eq(1) span").html() * $parent.find("td:eq(3) input").val() + "&nbsp;ð.");

	price = base_price + parseInt($parent.find("td:eq(5) span").html());

	refreshPrice(price);
}


function calc_reset() {
	$(".calc_cats input:checkbox").each(function() {
		if($(this).attr("checked")) {
			$(this).attr("checked", "");
			var $parent = $(this).parent().parent();
			$parent.find("td:gt(2) span").addClass('calc_not_vis');
			price -= parseInt($parent.find("td:eq(5) span").html());
		}
	});

	refreshPrice(price);

	document.location = "#calc_top";
	$(".calc_cats div").next("table").hide("fast");
	$(".calc_header").next().hide("fast");
	$("#calc_global_note").show("fast");
	note_opened = 1;
}


$(document).ready(function(){

	$('.calc_cats').find("tr:odd").addClass('calc_odd');

	$('.calc_cats tr').find("td:eq(1)").wrapInner("<span></span>");
	$('.calc_cats tr').find("td:eq(1)").append("&nbsp;ð.");
	$('.calc_cats tr').find("td:eq(2)").html("<input type = \"checkbox\" />");
	$('.calc_cats tr').find("td:eq(3)").html("<span><input type = \"text\" /></span>");
	$('.calc_cats tr').find("td:eq(4)").wrapInner("<span></span>");
	$('.calc_cats tr').find("td:eq(5)").html("<span></span>");

	$('.calc_cats tr').each(function() {
		$(this).find("td:eq(5) span").html($(this).find("td:eq(1) span").html() + "&nbsp;ð.");
	});

	$(".calc_cats table input:text").val("1");

	$('.calc_cats tr').find("td:gt(2) span").addClass('calc_not_vis');

	$('.calc_cats tr').find("td:eq(5) span").addClass('calc_bold');

	$('.calc_cats tr').find("td:eq(3) span").css("display", "block");

	$(".calc_cats tr").each(function() {
		$(this).find("td").each(function(i) {
			$(this).addClass("calc_td" + i);
		});
	});

	$('.calc_cats tr').find("td:eq(0)").css("width", $("#calc_width").attr("clientWidth") - 250);

    $(".calc_header").css("background-color", header_off);

    $(".calc_header").mouseover(function() {
		$(this).css("background-color", header_on);
	});

	$(".calc_header").mouseout(function() {
		$(this).css("background-color", header_off);
	});

	$(".calc_header").click(function() {
		if(note_opened) {
			$("#calc_global_note").hide("fast");
			note_opened = 0;
		}
		$(this).next().toggle("fast");
	});

	$(".calc_cats div").mouseover(function() {
			$(this).css("background-color", "#6D7398");
	});

	$(".calc_cats div").mouseout(function() {
			$(this).css("background-color", "#323A6B");
	});

	$(".calc_cats div").click(function() {
		$(this).next("table").toggle("fast");
	});

	$(".calc_cats table input:checkbox").click(function() {
		$parent = $(this).parent().parent();

		if($(this).attr("checked")) {
			$parent.find("td:gt(2) span").removeClass('calc_not_vis');
			price += parseInt($parent.find("td:eq(5) span").html());
		}
		else {
			$parent.find("td:gt(2) span").addClass('calc_not_vis');
			price -= parseInt($parent.find("td:eq(5) span").html());
		}
		refreshPrice(price);
	});

	$(".calc_cats input:text").focus(function() {
		$cur_obj = $(this);
		calc_base_price();
		interval = setInterval("check()", 500);
	});

	$(".calc_cats table input:text").blur(function() {
		if($(this).val() == "") {
			$(this).val("1");
		}
		clearInterval(interval);
		check();
	});

	$("#calc_skidka5 input").click(function() {
		if($(this).attr("checked")) {
			$("#calc_skidka10 input").attr("disabled", "disabled");
			$("#calc_skidka10").css("color", "#aaaaaa");
		}
		else {
			$("#calc_skidka10 input").attr("disabled", "");
			$("#calc_skidka10").css("color", "#000000");
		}
	});

	$("#calc_skidka10 input").click(function() {
		if($(this).attr("checked")) {
			$("#calc_skidka5 input").attr("disabled", "disabled");
			$("#calc_skidka5").css("color", "#aaaaaa");
		}
		else {
			$("#calc_skidka5 input").attr("disabled", "");
			$("#calc_skidka5").css("color", "#000000");
		}
	});

	$("#calc_skidka5 input, #calc_skidka10 input").click(function() {
		refreshPrice(price);
	});

});
