/**
 * @version    2008-10-30 07:07:45
 * @copyright  Copyright (c) 2008, SOFTBANK MOBILE Corp.
 */

(function ($) { $(function () {
	var form = $('#enquete');
	if (!form.size())
		return;
	
	var url         = form.attr('action');
	var yesno       = form.find('input[name=yesno]');
	var comment     = form.find('textarea[name=comment]');
	var comment_def = comment.get(0).defaultValue;
	var action      = form.find('input[type=submit]');
	
	yesno.attr('checked', '').click(function () {
		action.removeAttr('disabled');
	});
	
	comment.val(comment_def).removeClass('on');
	comment.focus(function () {
		var value = $(this).val();
		$(this).val((value == comment_def) ? '' : value).addClass('on');
	});
	comment.blur(function () {
		var value = $(this).val();
		if ((value == '') || (value == comment_def))
			$(this).val(comment_def).removeClass('on');
	});
	
	action.attr('disabled', 'disabled');
	
	form.submit(function () {
		action.attr('disabled', 'disabled');
		form.unbind('submit');
		
		var loc = $('#tabs').size()
			? encodeURIComponent(document.URL + '#' + $('#tabs .on').parent().attr('class'))
			: encodeURIComponent(document.URL);
		
		$.ajax({
			url:  url,
			type: 'POST',
			data: {
				yesno:   form.find('input[name=yesno]:checked').val(),
				comment: (comment.val() == comment_def) ? '' : encodeURIComponent(comment.val()),
				url:     loc,
				corner:  form.find('input[name=corner]').val()
			},
			success: function (r) {
				form.find('div.form').hide();
				form.find('div.' +
					(($('status', r).text() == 'SUCCESS') ? 'thanks' : 'error')
				).fadeIn('slow');
			},
			error: function (r) {
				form.find('div.form').hide();
				form.find('div.error').fadeIn('slow');
			}
		});
		
		return false;
	});
})})(jQuery);
