$(function() {
	$('body').prepend('<div id="convert_panel" style="width: 100%; height: 100%; display:none;"><input type="hidden" id="convert_panel_target_id" value="" /><iframe id="convert_panel_content" src="" style="width: 100%; height: 100%; border: none;"></iframe></div>');

	add_convert_button_for_all_editors();
});

function add_convert_button_for_all_editors() {
	for (editor_name in vB_Editor) {
		add_convert_button(editor_name);
	}
}

function add_convert_button(editor_name) {
	// already have the button
	if ($('#' + editor_name + '_cmd_converthand').size() > 0)
		return;
	
	target_space = $('#' + editor_name + ' .editor .editor_controls ul[class!="right"]:first li:last');

	if (target_space.size() < 1)
		return;

	target_space.after('<li><div class="imagebutton convert_hand" id="' + editor_name + '_cmd_converthand" style="white-space: nowrap; cursor: default;"> &nbsp; <span style="color: #00f; text-decoration: underline;">Paste Hand History</span> &nbsp; </div></li>\n');
	
	vB_Editor[editor_name].init_command_button($('#' + editor_name + '_cmd_converthand').get(0));
	$('#' + editor_name + '_cmd_converthand').click(convert_hand_click);
}

function convert_hand_click(e) {
	e.preventDefault();
	$('#convert_panel_target_id').val($(this).parents('.texteditor').attr('id'));
	$('#convert_panel_content').attr('src', location.protocol + '//' + location.host + '/pokertools/import/vbulletin/integration');
	$('#convert_panel').modal({
		onOpen: function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.fadeIn('fast', function () {
					dialog.data.hide().fadeIn('fast');
				});
			});
		},
		onClose: function (dialog) {
			dialog.data.fadeOut('fast', function() {
				dialog.container.fadeOut('fast', function () {
					dialog.overlay.fadeOut('fast', function() {
						$.modal.close();
					});
				});
			});
		}
	});
}





// Hook into vB ajax functions. Needed for quick edit editors.
YAHOO.util.Connect.successEvent.subscribe(function(){
    add_convert_button_for_all_editors();
});



function switch_editor_mode_extended(editorid, new_text)
{
	if (AJAX_Compatible)
	{
		var mode = (vB_Editor[editorid].wysiwyg_mode ? 0 : 1);

		if (vB_Editor[editorid].influx == 1)
		{
			// already clicked, go away!
			return;
		}
		else
		{
			vB_Editor[editorid].influx = 1;
		}

		if (typeof vBmenu != 'undefined')
		{
			vBmenu.hide();
		}

		YAHOO.util.Connect.asyncRequest("POST", 'ajax.php?do=editorswitch', {
			success: do_switch_editor_mode_extended,
			timeout: vB_Default_Timeout,
			argument: [editorid, mode, new_text]
		//	scope: this
		}, SESSIONURL
			+ 'securitytoken=' + SECURITYTOKEN
			+ '&do=editorswitch'
			+ '&towysiwyg='	+ mode
			+ '&parsetype=' + vB_Editor[editorid].parsetype
			+ '&allowsmilie=' + vB_Editor[editorid].parsesmilies
			+ '&message=' + PHP.urlencode(vB_Editor[editorid].get_editor_contents())
			+ (vB_Editor[editorid].ajax_extra ? ('&' + vB_Editor[editorid].ajax_extra) : '')
			+ (typeof vB_Editor[editorid].textobj.form['options[allowbbcode]']  != 'undefined' ? '&allowbbcode=' + vB_Editor[editorid].textobj.form['options[allowbbcode]'].checked : '')
		);
	}
}

function do_switch_editor_mode_extended(ajax)
{
	if (ajax.responseXML)
	{
		do_switch_editor_mode(ajax);
		
		if (ajax.argument[2] != '') {
			var new_text = vB_Editor[ajax.argument[0]].get_editor_contents().replace(/\*\*\*\*\* Converted Hand History \*\*\*\*\*/, ajax.argument[2]);
			vB_Editor[ajax.argument[0]].write_editor_contents(new_text, false);
			switch_editor_mode_extended(ajax.argument[0], '')
		}
	}
}

