Event.observe(window, 'load', function() {
	if ($('suggestionLink')) {
		Event.observe($('suggestionLink'), 'click', function() {
			suggestionPopUp.show();
		});

		//suggestion hide
		Event.observe($('suggestionHide'), 'click', function() {
			suggestionPopUp.hide();
		});

		//suggestion send
		Event.observe($('sendSuggestion'), 'click', function() {
			suggestionPopUp.send();
		});
	}

	Event.observe($('overlayBox'), 'click', function() {
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
		new Effect.Fade($('suggestionBox'), { duration: 0.5, afterFinish: function() {
				new Effect.Fade($('overlayBox'), { duration: 0.2 });
			}
			});
		$('suggestionForm').reset();
		$('suggestionErrorMsg').update('');
		$('suggestionErrorMsg').hide();
		$('suggestionOkMsg').update('');
		$('suggestionOkMsg').hide();
		$('suggestionContent').show();
	});
});

var suggestionPopUp = {
	show: function() {
		var panel = $('suggestionBox');

		//gen captcha
		var ajax = new Ajax.Request('/index.php', {
			method: 'get',
			parameters: 'p=CaptchaGen',
			onSuccess: function(transport) {
				var response = transport.responseText.evalJSON();
				$('suggestionCaptchaImg').src = '/captcha/' + response;
				$('suggestionCaptchaText').clear();
			}
 		});

		panel.setStyle({
			left: (document.viewport.getWidth() - 280)/2 + 'px',
			top: document.viewport.getScrollOffsets().top + 250 + 'px',
			zIndex: 100
		});

		$$('select', 'object', 'embed').each(function(node){
			node.style.visibility = 'hidden'
		});
		$('overlayBox').setStyle({
			width: document.documentElement.scrollWidth + 'px',
			height: document.documentElement.scrollHeight + 'px'
		});

		$('suggestionForm').reset();
		$('suggestionErrorMsg').update('');
		$('suggestionErrorMsg').hide();
		$('suggestionOkMsg').update('');
		$('suggestionOkMsg').hide();
		$('suggestionContent').show();

    	new Effect.Appear($('overlayBox'), { duration: 0.2, from: 0.0, to: 0.6, afterFinish: function() {
    			new Effect.Appear(panel, { duration: 0.5 });
    		}
    		});
	},

	hide: function() {
		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });
		new Effect.Fade($('suggestionBox'), { duration: 0.5, afterFinish: function() {
				new Effect.Fade($('overlayBox'), { duration: 0.2 });
			}
			});

		$('suggestionForm').reset();
		$('suggestionErrorMsg').update('');
		$('suggestionErrorMsg').hide();
		$('suggestionOkMsg').update('');
		$('suggestionOkMsg').hide();

		$('suggestionContent').show();
	},

	send: function() {
		var ajax = new Ajax.Request('/index.php', {
			method: 'post',
			parameters: $('suggestionForm').serialize(),
			onSuccess: function(transport) {
				var response = transport.responseText.evalJSON();
				if (response.status == 'error') {
					$('suggestionOkMsg').update('');
					$('suggestionOkMsg').hide();

					$('suggestionErrorMsg').update(response.message);
					$('suggestionErrorMsg').show();
					//gen captcha
					var ajax = new Ajax.Request('/index.php', {
						method: 'get',
						parameters: 'p=CaptchaGen',
						onSuccess: function(transport) {
							var response = transport.responseText.evalJSON();
							$('suggestionCaptchaImg').src = '/captcha/' + response;
							$('suggestionCaptchaText').clear();
						}
			 		});
				} else if (response.status == 'ok') {
					$('suggestionErrorMsg').update('');
					$('suggestionErrorMsg').hide();

					$('suggestionOkMsg').update(response.message);
					$('suggestionOkMsg').show();

					new Effect.BlindUp($('suggestionContent'), { duration: 0.5 });
				}
			}
 		});
	}
}