
var submitButton = 'undefined'; // this will handle the value of submit button (yes/no)
var feedbackUrl;
var successTimeout = 3000;

function feedbackFormExists()
{
	return !($('form[name=feedback]').length == 0)
}

function processNegativeForm()
{
	$('.flb_va span').wrap('<span class="mright"></span>');

	// Remove plain template definitions from all links
	$('#facebox a').each(function() {
		var url = $(this).attr('href');
		$(this).attr('href', url.replace('.tpl-plain', ''));
	});

	$('form[name=feedback]').submit(function() {
		// Remove submit button and replace it with loading image
		$('#facebox input[type=submit]').replaceWith('<div class="loading"><img src="'+$.facebox.settings.loadingImage+'"/></div>');

		// Submit feedback form
		var formData = $(this).serialize();
		$.post(
			feedbackUrl,
			formData,
			function(data) {
				$('#facebox-container').html(data);
				processPositiveForm();
			}
		);
		return false;
	});
}

function processPositiveForm()
{
	setTimeout(function() {$(document).trigger('close.facebox');}, successTimeout);
}

function feedbackOnlick()
{
	// Init facebox
	$.facebox({
		loadingImage    : location.protocol + '//' + location.host + '/stc/tpl/crp/img/facebox/loading.gif',
		closeImage      : location.protocol + '//' +location.host + '/stc/tpl/crp/img/facebox/closelabel.gif',
		opacity         : 0.3
	});
	$.facebox.loading();

	// Post form
	var formData = $(this).serialize() + '&answ=' + submitButton;

	$.post(
		feedbackUrl,
		formData,
		function(data) {
			$.facebox.reveal('<div id="facebox-container">' + data + '</div>');

			if (feedbackFormExists()) processNegativeForm();
			else processPositiveForm();
		},
		'html'
	);
	return false;
}

function faqFeedback()
{
	// Feedback url will be taken from feedback form action
	feedbackUrl = $('form[class=faq_yn]').attr('action') + '.tpl-plain';

	// "Close" button
	$(document).bind('reveal.facebox', function(){
		$('#fb-close').live('click', function(event){
			event.preventDefault();

			$(document).trigger('close.facebox');
		});
	}); 

	// Feedback form events
	$('input[name=answ]').click(function() {submitButton = $(this).val()});
	$('form[class=faq_yn]').submit(feedbackOnlick);
}

$(document).ready(function(){
	faqFeedback();
});




