var calcWindow = {

	_current_nums: 1,


	/**
	 * Меняем тип фрамуги
	 * @param int type Тип фрамуги (1=>Глухая, 2=>Откидная, 3=>Поворотная, 4=>Поворотно-откидная)
	 */
	_changeFramug : function (type) {
		if (type > 0 && type <=4) {
			$('#framuga').attr('class', 'state_' + type);
		}
	},

	/**
	 * Прячем фрамугу
	 */
	_hideFramug : function () {
		$('#parameters').addClass('full');
	},

	/**
	 * Показываем фрамугу
	 */
	_showFramug : function () {
		$('#parameters').removeClass('full');
	},
	
	/**
	 * Меняем тип фрамуги
	 * @param int num	Номер окна по порядку
	 * @param int type	Тип окна (1=>Глухая, 2=>Откидная, 3=>Поворотная, 4=>Поворотно-откидная)
	 */
	_changeWindow : function (num, type) {
		if (type > 0 && type <=4) {
			$('#windows #window_'+num).attr('class', 'window state_' + type);
		}
	},

	_setWindowNum : function(num) {
		
		if (num > 0 && num <=3) {
			
			this._current_nums = num;

			$('#parameters')
				.removeClass('count_1')
				.removeClass('count_2')
				.removeClass('count_3');
			$('#parameters').addClass('count_' + num);

		}
		
	}

}


var bindCalculator = function () {

	if ($('SELECT[name=flamuga_type]').length == 0) return false;
	
	
	$('#width, #height').each(function () {
		$(this).DefaultValue($(this).val());
	});
	
	$('SELECT[name=flamuga_type]').get(0).onchange = function () {		// jqTransform :(

		if ($(this).val() > 0) {
			calcWindow._showFramug();
			calcWindow._changeFramug($(this).val());
		}
		else {
			calcWindow._hideFramug();
		}
		
	};

	$('.minus').click(function () {
		
		$('.plus').removeClass('hidden');

		calcWindow._setWindowNum(calcWindow._current_nums - 1);
		
		if (calcWindow._current_nums <= 1) {
			$(this).addClass('hidden');
		}
		
	})
	
	$('.plus').click(function () {

		$('.minus').removeClass('hidden');

		calcWindow._setWindowNum(calcWindow._current_nums + 1);
		
		if (calcWindow._current_nums >= 3) {
			$(this).addClass('hidden');
		}
		
	});

	$('.minus, .plus').click(function () {

		$('#num_windows').val(calcWindow._current_nums);
		return false;

	});

	$('.preview').hover(function () {
		$('#alt').show();
	}, function () {
		$('#alt').hide();
	});

	$('.preview').click(function () {
		
		var index = $('.preview').index(this) + 1;
		var state = $('input[name=' + $(this).attr('id') + ']').val();
		state++;
		if (state > 4) {
			state = 1;
		}
		
		$(this)
				.removeClass('state_1')
				.removeClass('state_2')
				.removeClass('state_3')
				.removeClass('state_4')
				.addClass('state_' + state);
		
		$('input[name=' + $(this).attr('id') + ']').val(state);
		
		calcWindow._changeWindow(index, state);
		
		return false;
		
	});

	$('#calc_form').submit(function () {

		$('.warning').hide();
		
		if (parseInt($('#width').val()) && parseInt($('#height').val()) && $('#region').val() != -1) {

			try {
				var section = nowSection;
			} catch(e) {
				var section = document.location.toString();
			}
			
			$.post(section, $(this).serialize() + '&ajax=1', function (data) {
				$('#ajax_form').html(data);
			})

		}
		else if ($('#region').val() == -1) {
			$('#region_h5 .warning').show();
		}
		else {
			$('#proem_h5 .warning').show();
		}
		
		return false;
		
	});
	
}

$(bindCalculator);

