/****
 * vB Post Templates
 * Copyright 2008; Deceptor
 * All Rights Reserved
 * Code may not be copied, in whole or part without written permission
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

function build_posttemplates_popup(menu, obj)
{
	for (var cat in posttemplates)
	{
		if (!YAHOO.lang.hasOwnProperty(posttemplates, cat))
		{
			continue;
		}

		var category = document.createElement('div');
		category.className = 'thead';
		category.innerHTML = cat;
		menu.appendChild(category);

		for (ptid in posttemplates[cat])
		{
			if (!YAHOO.lang.hasOwnProperty(posttemplates[cat], ptid))
			{
				continue;
			}

			option = document.createElement('div');
			option.editorid = EDITOR_ID;
			option.controlkey = obj.id;
			option.ptid = ptid;

			option.className = 'osmilie';
			option.style.padding = '5px';
			option.innerHTML = posttemplates[cat][ptid];

			option.onmouseover = option.onmouseout = option.onmousedown = option.onmouseup = vB_Text_Editor_Events.prototype.menuoption_onmouseevent;

			option.onclick = posttemplates_onclick;

			menu.appendChild(option);
		}
	}
}

function posttemplates_onclick(e)
{
	PostTemplate = new vB_AJAX_PostTemplate(this.ptid, vB_Editor[this.editorid].wysiwyg_mode, vB_Editor[this.editorid].parsesmilies, this.editorid);
	PostTemplate.fetch();
	vBmenu.hide();
}

function vB_AJAX_PostTemplate(template, wysiwyg, allowsmilie, editorid)
{
	this.template = template;
	this.wysiwyg = wysiwyg;
	this.allowsmilie = allowsmilie;
	this.editorid = editorid;

	this.fetch = function()
	{
		YAHOO.util.Connect.asyncRequest("POST", "ajax.php?do=posttemplate&template=" + this.template + "&wysiwyg=" + this.wysiwyg + "&allowsmilie=" + this.allowsmilie, {
			success: this.fetched,
			timeout: vB_Default_Timeout,
			scope: this
		}, SESSIONURL + "securitytoken=" + SECURITYTOKEN + "&do=posttemplate&template=" + this.template + "&wysiwyg=" + this.wysiwyg + "&allowsmilie=" + this.allowsmilie);
	}

	this.fetched = function(ajax)
	{
		if (ajax.responseXML)
		{
			if (ajax.responseXML.getElementsByTagName('message') && ajax.responseXML.getElementsByTagName('message')[0])
			{
				vB_Editor[this.editorid].insert_text(ajax.responseXML.getElementsByTagName('message')[0].firstChild.nodeValue);
			}
		}
	}
}

/**
* We'll overwrite the JS functions here since we can't hook into 'em
* It's not pretty, but what the hell =)
*/
function overload_vB_Editor(editorid)
{
  editorid = (editorid ? editorid : EDITOR_ID);

  vB_Editor[editorid].init_popup_menu_original =
    vB_Editor[editorid].init_popup_menu;

	vB_Editor[editorid].init_popup_menu = function(obj)
	{
		if (this.disabled)
		{
			return false;
		}
		var menu;

		switch (obj.cmd)
		{
			case 'posttemplate':
			{
				menu = this.init_menu_container('posttemplate', '250px', '350px', 'auto');
				build_posttemplates_popup(menu, obj);
				this.popups[obj.cmd] = this.controlbar.appendChild(menu);
				set_unselectable(menu);
				return true;
			}
		}

		return this.init_popup_menu_original(obj);
	}
}

var do_switch_editor_mode_original = do_switch_editor_mode;

do_switch_editor_mode = function(ajax)
{
	do_switch_editor_mode_original(ajax);
	overload_vB_Editor(ajax.argument[0]);
}

overload_vB_Editor();
