var prices;
var weeks;
var stop = false;

function calculate(num, week, hours)
{
    // calculate rental price from week_price table and hours
    var price = prices[num].week_price[week-1] + hours * prices[num].hour_price;
    var hour_price = prices[num].hour_price;
    var base_price = prices[num].week_price[week-1];
    return {price: price, base_price: base_price, hour_price: hour_price};
}


function update_price(price_obj)
{
    $("#base_result").text(price_obj.base_price);
    $("#moto_result").text(price_obj.hour_price);
    $("#rental_result").text(price_obj.price);
}

function get_weeks()
{
    var measure = $("input[name='time']:checked").val();
    if (measure == 'week')
	weeks = $("#rental_time").val();
    else
	weeks = Math.round($("#rental_time").val() * 4.3);
};

function change_measure()
{
    // triggers when week-month radiobutton changed
    // we need to recalculate weeks because round isn't good
    var measure = $("input[name='time']:checked").val();
    if (measure == 'week')
    {
	// now we have month in field
	$("#rental_time").remove();
	var input = $("<input>")
	    .attr("id", "rental_time")
	    .attr("name", "rental_time")
	    .css("margin-bottom", "3px")
	    .val(weeks)
	    .change(recalculate_price)	    
	    .insertAfter("#timelabel");
	recalculate_price();
    }
    else
    {
	// there we have weeks in field
	$("#rental_time").remove();
	var select = $("<select>")
	    .attr("id", "rental_time")
	    .attr("name", "rental_time")
	    .change(recalculate_price);

	var num = $("#power").val() - 1;
	var months = prices[num].max_time;
	

	for (var i = 1; i < months + 1; i++)
	    {
		select.append("<option value=\"" + i + "\">" + i + "</option>");
	    }
	    select.val(Math.round(weeks / 4.3)).insertAfter("#timelabel");
	recalculate_price();
    }  
}


check_power = function ()
{
    // if generator is none - stop all other actions
    if ($("#power").val() == 0)
    {
	$("#calculator input,select[id!=power]").each(function ()
						      {
							  $(this).attr("disabled", true);
						      });
	$("#mailsubmit").attr("disabled", true);
	update_price("");
	$("#gen_name").empty();
	stop = true;
    }
    else
    {
	$("#calculator input,select[id!=power]").each(function ()
							{
							    $(this).removeAttr('disabled');
							});
	$("#mailsubmit").removeAttr('disabled');
	$("#gen_name")
	    .empty()
	    .append("<p>Генератор " + prices[$("#power").val()-1].name +"</p>");
	stop = false;
    }
};

function recalculate_price()
{
    check_boundaries();
    if (stop)
    {
	return;
    }
    get_weeks();
    var hours = $("#hours").val();
    var num = $("#power").val() - 1;
    update_price(calculate(num, weeks, hours));
}


reset = function ()
{
    $("#power").val(0);
    $("input[value=month]").attr("checked", true);
    $("#rental_time").val(1);
    $("#hours").val(0);
};

check_boundaries = function ()
{
    // function thah checks value of week input and validates it
    var value = $("#rental_time").val();
    value = jQuery.trim(value);
    re = /^\d{1,3}$/;
    var num = $("#power").val() - 1;
    var max = Math.round(prices[num].max_time * 4.3);
    if (!value.match(re))
	$("#rental_time").val(1);
    if (value > max)
	$("#rental_time").val(max);
    if (value < 1)
	$("#rental_time").val(1);
    
};


$(document).ready(function ()
		  {
		      prices = eval("(" + $("#rental_prices").text() + ")");
		      reset();
		      check_power();
		      get_weeks();
		      
		      $("#power").change(check_power);

		      $("input[name!=time],select").change(recalculate_price);
		      $("input[name=time]").bind($.browser.msie? 'propertychange': 'change', change_measure);


		      ////////////////////////////////////////////
		      //
		      // animation for blocks
		      //
		      ///////////////////////////////////////////


		      $("#bonuslink").click(function () { $("#bonus_content").slideToggle("slow"); return false; } );
		      $("#comparelink").click(function () { $("#compare_content").slideToggle("slow"); return false;} );



		      //////// testing, remove it
		      // $("#mailsubmit").click(function () { alert("Пока данная функция недоступна"); return false; } );
		      
		      
		  });