imod_tool_engine_posthistory = function(postid)
{
	var me = this;
	this.postid = postid;

	/**
	* Fetches the post history then shows it
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_show = function()
	{
		postid = this.postid;
		postcount = 0;

		if (fetch_object('posthistory_' + postid + '_postcountcache'))
		{
			postcount = parseInt(fetch_tags(fetch_object('posthistory_' + postid + '_postcountcache'), 'a')[0].innerHTML);
		}

		if (fetch_object('posthistorymenu_' + postid + '_menu'))
		{
			fetch_object('posthistorymenu_' + postid + '_menu').parentNode.removeChild(fetch_object('posthistorymenu_' + postid + '_menu'));
		}

		this.posthistory_ajax = new vB_AJAX_Handler(true);
		this.posthistory_ajax.onreadystatechange(this.posthistory_doshow);
		this.posthistory_ajax.send('ajax.php', 'do=posthistory&postid=' + postid + '&postcount=' + postcount);
	}


	/**
	* Shows the post history menu
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_doshow = function()
	{
		if (me.posthistory_ajax.handler.readyState == 4 && me.posthistory_ajax.handler.status == 200)
		{
			if (me.posthistory_ajax.handler.responseText != '')
			{
				postid = me.postid;
				historyopts = me.posthistory_ajax.handler.responseText;

				if (!fetch_object('posthistorymenu_' + postid + '_menu'))
				{
					menudiv = document.createElement('div');
					menudiv.id = 'posthistorymenu_' + postid + '_menu';
					menudiv.style.display = 'none';

					fetch_object('post' + postid).parentNode.appendChild(menudiv);

					vbmenu = vbmenu_register('posthistorymenu_' + postid, true);
				}

				menudiv = fetch_object('posthistorymenu_' + postid + '_menu');
				menudiv.innerHTML = historyopts;

				vbmenu.show(fetch_object('posthistorymenu_' + postid));
			}
		}
	}


	/**
	* Request post history to display 
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_select = function(postid, postcount)
	{
		postid = this.postid;
		historyid = 0;
		historyopts = fetch_tags(fetch_object('posthistory_selections_' + postid), 'input');

		for (var i = 0; i < historyopts.length; i++)
		{
			if (historyopts[i].type == 'radio' && historyopts[i].checked)
			{
				historyid = historyopts[i].value;
			}
		}

		if (!historyid)
		{
			alert('You did not select an item to view.');
			return false;
		}

		this.posthistory_ajax_select = new vB_AJAX_Handler(true);
		this.posthistory_ajax_select.onreadystatechange(this.posthistory_doselect);
		this.posthistory_ajax_select.send('ajax.php', 'do=posthistoryfetch&postcount=' + postcount + '&historyid=' + historyid + '&postid=' + postid);
	}


	/**
	* Shows a history item inplace of a normal post 
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_doselect = function()
	{
		if (me.posthistory_ajax_select.handler.readyState == 4 && me.posthistory_ajax_select.handler.status == 200)
		{
			if (me.posthistory_ajax_select.handler.responseText)
			{
				postid = me.postid;
				historypost = me.posthistory_ajax_select.handler.responseText;

				fetch_object('edit' + postid).innerHTML = historypost;
				PostBit_Init(fetch_object('post' + postid), postid);
			}
		}
	}
}

imod_tool_engine_modcomments = function(postid)
{
	var me = this;
	this.postid = postid;

	/**
	* Fetches the moderation comment options then shows it
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_show = function()
	{
		postid = this.postid;
		postcount = 0;

		if (fetch_object('modcomments_' + postid + '_postcountcache'))
		{
			postcount = parseInt(fetch_tags(fetch_object('modcomments_' + postid + '_postcountcache'), 'a')[0].innerHTML);
		}

		if (fetch_object('modcommentsmenu_' + postid + '_menu'))
		{
			fetch_object('modcommentsmenu_' + postid + '_menu').parentNode.removeChild(fetch_object('modcommentsmenu_' + postid + '_menu'));
		}

		this.modcomments_ajax = new vB_AJAX_Handler(true);
		this.modcomments_ajax.onreadystatechange(this.modcomments_doshow);
		this.modcomments_ajax.send('ajax.php', 'do=modcomments&postid=' + postid + '&postcount=' + postcount);
	}


	/**
	* Shows the moderation comments menu
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_doshow = function()
	{
		if (me.modcomments_ajax.handler.readyState == 4 && me.modcomments_ajax.handler.status == 200)
		{
			if (me.modcomments_ajax.handler.responseText != '')
			{
				postid = me.postid;
				commentsopts = me.modcomments_ajax.handler.responseText;

				if (!fetch_object('modcommentsmenu_' + postid + '_menu'))
				{
					menudiv = document.createElement('div');
					menudiv.id = 'modcommentsmenu_' + postid + '_menu';
					menudiv.style.display = 'none';

					fetch_object('post' + postid).parentNode.appendChild(menudiv);

					vbmenu = vbmenu_register('modcommentsmenu_' + postid, true);
				}

				menudiv = fetch_object('modcommentsmenu_' + postid + '_menu');
				menudiv.innerHTML = commentsopts;

				vbmenu.show(fetch_object('modcommentsmenu_' + postid));
			}
		}
	}


	/**
	* Request mod comment to display or post new one 
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_select = function(postid, postcount)
	{
		postid = this.postid;
		modcommentid = 0;
		modcommentopts = fetch_tags(fetch_object('modcomments_selections_' + postid), 'input');

		for (var i = 0; i < modcommentopts.length; i++)
		{
			if (modcommentopts[i].type == 'radio' && modcommentopts[i].checked)
			{
				modcommentid = modcommentopts[i].value;
			}
		}

		if (!modcommentid)
		{
			alert('You did not select an action.');
			return false;
		}

		if (modcommentid == 'new' && fetch_object('mc_menu_post_' + postid))
		{
			fetch_object('mc_menu_post_' + postid).style.display = '';
			fetch_object('mc_menu_selections_' + postid).style.display = 'none';

			vbmenu.hide();
			vbmenu.show(fetch_object('modcommentsmenu_' + postid));

			return false;
		}

		return false;
	}


	/**
	* Request mod comment to display or post new one 
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_postcomment = function(postid, postcount)
	{
		postid = this.postid;

		modcomment_type = fetch_object('modcomments_type_' + postid);
		modcomment_type = modcomment_type.options[modcomment_type.options.selectedIndex].value

		this.modcomments_ajax_select = new vB_AJAX_Handler(true);
		this.modcomments_ajax_select.onreadystatechange(this.modcomments_postcommentdone);
		this.modcomments_ajax_select.send('ajax.php', 'do=postmodcomment&postcount=' + postcount + '&comment=' + PHP.urlencode(fetch_object('modcomments_comment_' + postid).value) + '&postid=' + postid + '&type=' + modcomment_type);
	}


	/**
	* Re-fetches the comments posted
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_postcommentdone = function()
	{
		if (me.modcomments_ajax_select.handler.readyState == 4 && me.modcomments_ajax_select.handler.status == 200)
		{
			if (me.modcomments_ajax_select.handler.responseText != '')
			{
				vbmenu.hide();
				me.modcomments_show();
			}
		}
	}

	/**
	* Deletes a moderation comment 
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_deletecomment = function(modcommentid, postcount)
	{
		postid = this.postid;

		this.modcomments_ajax_select = new vB_AJAX_Handler(true);
		this.modcomments_ajax_select.onreadystatechange(this.modcomments_postcommentdone);
		this.modcomments_ajax_select.send('ajax.php', 'do=deletemodcomment&postcount=' + postcount + '&commentid=' + modcommentid + '&postid=' + postid);
	}
}

imod_tool_engine = function()
{

	this.posthistory_objs = {};
	this.modcomments_objs = {};

	/**
	* Registers the post history link
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_register = function(postid)
	{
		if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
		{
			ph_link = fetch_object('posthistory_' + postid);
			ph_link.onclick = iModTool.posthistory_click;
			this.posthistory_objs[postid] = new imod_tool_engine_posthistory(postid);

			return true;
		}

		return false;
	}


	/**
	* Registers the post history link and triggers the click
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_fastregister = function(postid)
	{
		if (this.posthistory_register(postid))
		{
			this.posthistory_objs[postid].posthistory_show(postid);
		}

		return false;
	}


	/**
	* Handles post history links being clicked
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_click = function(e)
	{
		do_an_e(e ? e : window.event);

		postid = this.id.split('_')[1];

		iModTool.posthistory_objs[postid].posthistory_show(postid);
	}


	/**
	* Alias
	* * * * * * * * * * * * * * * * * * 
	*/

	this.posthistory_select = function(postid, postcount)
	{
		this.posthistory_objs[postid].posthistory_select(postid, postcount);
	}


	/**
	* Registers the moderation comments link
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_register = function(postid)
	{
		if (AJAX_Compatible && (typeof vb_disable_ajax == 'undefined' || vb_disable_ajax < 2))
		{
			mc_link = fetch_object('modcomments_' + postid);
			mc_link.onclick = iModTool.modcomments_click;
			this.modcomments_objs[postid] = new imod_tool_engine_modcomments(postid);

			return true;
		}

		return false;
	}


	/**
	* Registers the mod comments link and triggers the click
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_fastregister = function(postid)
	{
		if (this.modcomments_register(postid))
		{
			this.modcomments_objs[postid].modcomments_show(postid);
		}

		return false;
	}


	/**
	* Handles moderation comments links being clicked
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_click = function(e)
	{
		do_an_e(e ? e : window.event);

		postid = this.id.split('_')[1];

		iModTool.modcomments_objs[postid].modcomments_show(postid);
	}


	/**
	* Alias
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_select = function(postid, postcount)
	{
		this.modcomments_objs[postid].modcomments_select(postid, postcount);
	}


	/**
	* Alias
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_postcomment = function(postid, postcount)
	{
		this.modcomments_objs[postid].modcomments_postcomment(postid, postcount);
	}

	/**
	* Alias
	* * * * * * * * * * * * * * * * * * 
	*/

	this.modcomments_deletecomment = function(postid, postcount, modcommentid)
	{
		this.modcomments_objs[postid].modcomments_deletecomment(modcommentid, postcount);
	}
}

iModTool = new imod_tool_engine;