// Htmlbox has been initialized
document.OnHtmlBoxInitialized = function(htmlbox){ };

// UBB should be inserted
document.OnInsertUBB = function(htmlbox, ubb)
{
	var editor = document.getElementById('hb_source');
	editor.value += ubb;
	editor.focus();
};

var UbbHtmlbox = function(htmlbox)
{
	// Global
	if (window.ubbhtmlboxes == undefined)
	{
		ubbhtmlboxes = [];
	}

	// Functions
	
	// Fields
	this.htmlbox = htmlbox;
	this.toolbar = htmlbox.toolbar;
	this.callbackUrl = '';
	
	//
	ubbhtmlboxes[htmlbox] = this;
};

UbbHtmlbox.prototype.getHTML = function(ubb)
{
	return syncPOST(this.callbackUrl + 'callbacks/ubb2html.php', 'ubb=' + encodeURIComponent(ubb));
};

UbbHtmlbox.prototype.getContent = function()
{
	var mode = $('#hb_source').css('display') == 'none' ? EditMode.Html : EditMode.Ubb;

	if (mode == EditMode.Ubb)
		return $('#hb_source').val();
	else
	{
		html = this.htmlbox.getContents();		
		response = syncPOST(this.callbackUrl + 'callbacks/html2ubb.php', 'html=' + encodeURIComponent(html));
		
		return response;
	}
};

/**
 * Enables the UBB editor, disabling the HTML editor.
 * @param True if the UBB editor should be enabled, otherwise false.
 */
UbbHtmlbox.prototype.enableUBB = function(enable)
{
	this.htmlbox.enable(!enable);

	if (enable)
	{
		$('#hb_source').css('display', '');
	}
	else
	{
		$('#hb_source').css('display', 'none');
	}
};

/**
 * Enables the HTML editor, disabling the UBB editor
 * @param True if the HTML editor should be enabled, otherwise false.
 */
UbbHtmlbox.prototype.enableHTML = function(enable)
{
	this.htmlbox.enable(enable);

	if (!enable)
	{
		$('#hb_source').css('display', '');
	}
	else
	{
		$('#hb_source').css('display', 'none');
	}
};

/**
 * Toggles the editor between UBB and HTML mode.
 */
UbbHtmlbox.prototype.toggle = function(instance)
{
	if (!instance)
	{
		instance = this;
	}

	var ubbEnabled = $('#hb_source').css('display') != 'none';
	
	if (ubbEnabled)
	{
		instance.htmlbox.setEditMode(EditMode.Html);
		var ubb = document.getElementById('hb_source').value;
		response = syncPOST(instance.callbackUrl + 'callbacks/ubb2html.php5', 'ubb=' + encodeURIComponent(ubb));
		instance.htmlbox.setContents(response);
		instance.setEditMode(EditMode.Html);
	}
	else
	{
		var html = instance.htmlbox.getContents();

		// Remove &nbsp;
		html = html.trim();
		response = syncPOST(instance.callbackUrl + 'callbacks/html2ubb.php5', 'html=' + encodeURIComponent(html));
		document.getElementById('hb_source').value = response;
		instance.setEditMode(EditMode.Ubb);
	}
	
	
};

UbbHtmlbox.prototype.setEditMode = function(mode)
{
	this.enableUBB(mode == EditMode.Ubb);
	this.enableHTML(mode == EditMode.Html);
	this.setSwitchButton(mode == EditMode.Ubb);
	this.htmlbox.setEditMode(mode);
};

UbbHtmlbox.prototype.setSwitchButton = function(ubb)
{
	if (this.btnSwitch == undefined)
	{
		return;
	}

	var imgUrl;
	if (ubb)
	{
		imgUrl = this.htmlbox.imgRoot + 'images/ubb.gif';
	}
	else
	{
		imgUrl = this.htmlbox.imgRoot + 'images/live.gif';
	}
	
	this.btnSwitch.firstChild.src = imgUrl;
};
