/*					new Draggable('pm', {
					handle: '.top'
					,onStart: function(d, evt) { Element.setStyle(d.element, { cursor: 'move' }); }
					,onEnd: function(d, evt) { Element.setStyle(d.element, { cursor: 'default' }); }
				});*/

// pm ad autore post
var CPostPM = Class.create({
	initialize: function() {
		this.container = null;
		this.target = 'pm';
		
		var setup = function()
		{
			this.container = $(this.target);
		};
		Event.observe(document, 'dom:loaded', setup.bindAsEventListener(this));
	},

	load: function(user, post)
	{
		var onComplete = function() { 
			this.container.writeAttribute('user', user);
			this.container.select('form').invoke('reset');
		};
		Ubk.follow(this.target, 'forum.php', 'FILE=hp/forum/inside/topic-new.xml&AZIONE=mostra_pm&TABELLA=t_a_forum&MODE=W&PM-USER=' + user, true, true, onComplete.bind(this));
	},
	
	hide: function()
	{
		Ubk.hide(this.target);
		Element.update(this.target, '');
	},

	toggle: function(user, post)
	{
		if (Ubk.isVisible(this.target)) {
			this.hide();
		} else {
			this.load(user, post);
		}
	}
});

var CPostModeration = Class.create({
	// muovi / non muovere il post
	toggleMove: function(check, post)
	{
		var onComplete = function() {
			Ubk.working(false);
			if (check.src.indexOf('SI.png') != -1) {
				check.src = 'img/ico/check/checkNO.png';
			} else {
				check.src = 'img/ico/check/checkSI.png';
			}
		};
		Ubk.follow(null, 'forum.php', 'AZIONE=toggle_post_move&TABELLA=t_s_post&ID=' + post, false, true, onComplete.bind(this));
	}
});

var CPostPreview = Class.create({
	initialize: function() {
		this.content = null;
		this.target = null;
		this.updater = null;
	},

	load: function(post)
	{
		var posID, formEL, preview;

		if (post == 'new') {
			postID = '-1';
			formEL = 'new_post_content';
			preview = 'new_post_preview';
		} else if (post == 'topic'){
			postID = '-1';
			formEL = 'new_topic_content';
			preview = 'new_topic_preview';
		} else if (post == 'pm'){
			postID = '-1';
			formEL = 'new_pm_content';
			preview = 'new_pm_preview';
		} else {
			postID = post;
			formEL = 'post_'+post+'_content';
			preview = 'post_'+post+'_preview';
		}
		this.target = preview;
		Ubk.show(this.target);

		var f = function(pe) {
			if ($(formEL)) {
				var content = $(formEL).serialize();
				if (content != this.content) {
					this.content = content;
					var options = {
							method: 'post'
							,asynchronous: true
							,parameters: 'AZIONE=bbcode&POST=' + postID + '&' + content
					}
					new Ajax.Updater(this.target, 'autocomplete.php', options );
				}
			} else {
				pe.stop();
				this.updater = null;
			}
		};
		this.updater = new PeriodicalExecuter(f.bind(this), 0.5);
	},

	hide: function()
	{
		if (this.updater) {
			Ubk.hide(this.target);
			this.updater.stop();
			this.updater = null;
		}
	},

	toggle: function(post)
	{
		if (this.updater) {
			this.hide();
		} else {
			this.load(post);
		}
	}
});

var CPost = Class.create({
	initialize: function() {
		this.target = 'new_post';
		this.PM = new CPostPM();
		this.Mod = new CPostModeration();
		this.Preview = new CPostPreview();
	},

	check: function(id, onOk)
	{
		var error = function(fld) {
			Ubk.error(Locale.string(3001));
			Form.Element.focus(fld);
		};
		try {
			if (id == -1) {
				if ($('guest') && !$F('guest')) error('guest');
				if (!$F('t_style[-1]')) error('t_style[-1]');
				if (!$F('new_post_content')) error('new_post_content');
			} else {
				if (!$F('t_style[' + id + ']')) error('t_style[' + id + ']');
				if (!$F('post_' + id + '_content')) error('post_' + id + '_content');
			}
		} catch (e) {
			return false;
		}

		Ubk.tryThis(onOk);
	},

	reply: function ()
	{
		var afterFinish = function()
		{
			$$('a.reply').invoke('hide');
			Element.scrollTo(this.target);
			$(this.target).select('form').invoke('reset');
		};
		Ubk.show(this.target, afterFinish.bind(this));
	},

	close: function()
	{
		$(this.target).select('form').invoke('reset');
		Emo.hide();
		Post.Preview.hide();
		Ubk.hide(this.target);
		$$('a.reply').invoke('show');
	},

	quote: function(post)
	{
		var afterFinish = function()
		{
			var onComplete = function(request) {
				
				$$('a.reply').invoke('hide');

				var o = request.responseText.evalJSON(true);

				if ($F('new_post_reply').strip().length == 0) {
					Element.scrollTo(this.target);
					$('new_post_content').focus();
				}

				$('new_post_content').value += o.body+"\n";
				if ($('new_post_title').value.strip().length == 0)
					$('new_post_title').value = o.title;
				$('new_post_reply').value = o.replyTo;
			};

			Ubk.follow(null
				, 'forum.php'
				, 'AZIONE=post_quote&TABELLA=t_s_post&CAMPO-ID=i_post_id&ID='+post
				, false, true, onComplete.bind(this));
		};

		Ubk.show(this.target, afterFinish.bind(this));
	},

	remove: function(post)
	{
		Ubk.confirm(Locale.string(3033), function() {
			Ubk.follow(null
				,'general.php'
				,'AZIONE=elimina&TABELLA=t_s_post&CAMPO-ID=i_post_id&ID='+post
				,false
				,true
				,null);
		});
	}
	
});