function addInput() {
	var add = true;
	$('#form-order .fo__inputs-file').each(function() {
		if (!$(this).val().length) {
			add = false;
		}
	});

	if (add) {
		var html = $('<input type="file" name="files[]" size="35" class="fo__inputs-file">');

		$('#f-container').append(html);

		bindInput(html);

		$.nyroModalSettings({
			height: ($('#form-order').outerHeight() + 20)
		});

	}
	
	$('#nyroModalContent').css('height', '1000px');
}

function bindInput(input) {
	if ($.browser.msie) {
		input.bind('propertychange', addInput);
	} else {
		input.bind('change', addInput);
	}
}


$(function() {

	$('#form-order .fo__form').live('submit', function(event) {
		var error = false;

		if ($('#fo__name').val() == '') {
			$('#fo__name').addClass('fo__inputs-input-error');
			error = true;
		} else {
			$('#fo__name').removeClass('fo__inputs-input-error');
		}

		if ($('#fo__contacts').val() == '') {
			$('#fo__contacts').addClass('fo__inputs-input-error');
			error = true;
		} else {
			$('#fo__contacts').removeClass('fo__inputs-input-error');
		}

		if (error) {
			return false;
		}

		//pagetrack
		try {
			nimaxcounter.reachGoal('orderForm_submit');
			_gaq.push(['_trackEvent', 'orderForm', 'order', 'submit']);
			var tid = Math.rand();
			_gaq.push(['_trackEvent', 'multitouch', 'TID' + tid, mt.multitouch_event(tid, 0) ]);
		} catch(e){}

		$('.fo__inputs-file-loader').show();

		$(this).nyroModalManual();
	});


	/*$('.menu_order a, .form-footer__link a, a[href="/order/"]').click(function(event) {
		if ($.browser.msie && parseInt($.browser.version, 10) <= 6) {
			return;
		}
		
		event.preventDefault();
		var left_position = function() {
			return ($(window).width() + $(window).scrollLeft() - $(window).width() / 2);
		};
		var top_position = function() {
			return ($(window).height() + $(window).scrollTop() - $(window).height() / 2);
		};

		var bg_width = $(document).width();
		if ($.browser.msie) {
			bg_width -= 22;
		}

		$.nyroModalSettings({
			cssOpt: {
				bg: {
					width: bg_width,
					height: $(document).height(),
					background: '#fff'
				},
				wrapper: {
					left: left_position,
					top: top_position
				},
				loading: {
					left: left_position,
					top: top_position
				}
			}
		});
		
		$.nyroModalManual({
			url: '/templates/order_form/submit.php',
			closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="Закрыть">Закрыть</a>',
			zIndexStart: 10000,
			resizable: true,
			autoSizable: true,
			windowResize: true,
			width: 555,
			minHeight:100,
			endShowContent: function(elts, settings) {
				// pagetrack
				try {
					nimaxcounter.reachGoal('orderForm_open');
					_gaq.push(['_trackEvent', 'orderForm', 'order', 'open']);
				} catch(e){}


				$('#f-view').click(function() {
					$('#f-container').toggle();
					$('#nyroModalContent').css('height', '1000px');
					return false;
				});
			}
		});

		return false;
	});*/
});
