
var FReqBox = (function(){
	return {
		currentDialog: null,

		clicked: false,
		
		show: function() {
			var $rb     = jQuery(".request_form_content");
			var content = jQuery(".boxcontent", $rb).html();

			this.clicked = false;
			/*this.currentDialog = new Boxy(
				content,
				{
					title:      title,
					modal:      true,
					behaviours: this.boxyFormBehaviours,
					closeText:  "[X]"
				}
			);*/

			//Boxy.alert( "ez is fos?", null, { title: "fos-e" } );
			jQuery.modal(
				content,
				{
					onShow: this.initControls
				}
			);
		},

		initControls: function(dialog) {
			var $content = dialog.data;
			
			jQuery(".message", $content).focus(function(target){
				if ( !FReqBox.clicked ) {
					FReqBox.clicked = true;

					jQuery(this).val( "" );
				}
			});

			jQuery("input.send", $content).click(function(){
				var category, message, captcha;

				$form = jQuery("form", $content);

				category = jQuery('.category', $form).val();
				message  = jQuery('.message',  $form).val();
				captcha  = jQuery('.captcha',  $form).val();
				acct     = jQuery('.acct',     $form).val();
				email    = jQuery('.email',    $form).val();

				jQuery.ajax({
					url:      "/scripts/freqbox/send.php",
					type:     "POST",
					dataType: "text",
					data: {
						category: category,
						message:  message,
						captcha:  captcha,
						acct:     acct,
						email:    email
					},

					success: function(data) {
						var $rr     = jQuery(".request_reply_content");
						var title   = jQuery(".title",      $rr).text();
						var content = jQuery(".boxcontent", $rr).html();

						if ( data.length ) {
							//new Boxy.alert( "Error: " + data, null, { title: "Error" } );
							//jQuery.modal( "Error: " + data );
							alert( "Error: " + data );
						} else {
							//FReqBox.currentDialog.hide();

							//jQuery.modal.close();
							jQuery.modal.close();
							jQuery.modal(
								content,
								{
									onShow: FReqBox.initControls
								}
							);

							//new Boxy.alert( content, null, { title: title } );
						}
					},

					error: function() {
						alert( "There was an error while sending message!" );
					}
				});
			});

			jQuery("input.close", $content).click(function(){
				jQuery.modal.close();
			});
		}
	};
})();

