

window.addEvent('load', function() {
	/**
	 * Some options for the large photos.
	 *
	 * The first argument is the argument for $$ (can be an array of elements or a selector)
	 */
	
	$('comment_button').addEvent('click', function(e) {
		// var commentUrl = '<?=base_url()?>hatsuon/comment';
		var commentUrl = baseUrl + 'index.php/mailer/comment';
		$$('.comment_sending').removeClass('hide').addClass('show');
		$$('.nickname_error').set('html', '');
		$$('.email_error').set('html', '');
		$$('.comment_error').set('html', '');

		var req = new Request({url:commentUrl,onComplete:commentSubmit})
		.post({company:$('company').value, nickname:$('nickname').value,email:$('email').value,comment:$('comment').value});
	});
	
	function commentSubmit(ret) {
		//window.alert(ret);
		$$('.comment_sending').removeClass('show').addClass('hide');
		var count = 0;
		var results;
		var result;
		try
		{
			results = JSON.decode(ret);
			for(result in results) {
				if (result == 'OK')
				{
					count = 0;
					break;
				}
				var oel = $$('.' + result + '_error');
				oel.setStyle('opacity', '0');
				//oel.set('html', results[result]);
				oel.set('html', 'x');
				oel.fade(1);
				(function(){ this.fade(0); }).delay(3000, oel);
				count++;
			}
		}
		catch (e)
		{
			// do nothing
		}

		if(count == 0) {
			var thankyouMsg = $$('.thankyou_msg');
			var fadeOutDelay;
			thankyouMsg.setStyle('opacity', '0');
			if (result == 'OK')
			{
				$('company').value = '';
				$('nickname').value = '';
				$('email').value = '';
				$('comment').value = '';
				
				thankyouMsg.set('html', 'ご意見いただき、ありがとうございます。');
				fadeOutDelay = 8000;
			}
			else
			{
				thankyouMsg.set('html', '<font color="#ee2222">送信に失敗しました。<br/>support@rakuraku-inc.comまでご連絡下さい。</font>');
				fadeOutDelay = 10000;
			}
			thankyouMsg.fade(1);
			(function(){ this.fade(0); thankyouMsg.set('html', ''); }).delay(fadeOutDelay, thankyouMsg);
		}
	}
});
