var edit_hbf = [ //hbf - head, body, footer
	'BXButton', 
	{
		id : 'edit_hbf',
		iconkit : '_global_iconkit.gif',
		name : TE_MESS.FILEMAN_EDIT_HBF,
		handler : function ()
		{
			this.bNotFocus = true;
			this.pMainObj.OpenEditorDialog("edit_hbf", false, 700, {window: window, document: document});
		}
	}
];

var insert_cph = [
	'BXButton', 
	{
		id : 'insert_cph',
		iconkit : '_global_iconkit.gif',
		name : TE_MESS.INSERT_CPH,
		handler : function ()
		{
			this.bNotFocus = true;
			this.pMainObj.OpenEditorDialog("insert_cph", false, 500, {});
		}
	}
];

var preview_tmpl = [
	'BXButton', 
	{
		id : 'preview_tmpl',
		iconkit : '_global_iconkit.gif',
		name : TE_MESS.FILEMAN_PREVIEW_TEMPLATE,
		title : TE_MESS.FILEMAN_PREVIEW_TEMPLATE_TITLE,
		handler : function () {preview_template(__ID);}
	}
];

delete arToolbars['template'];

var edit_template = oBXEditorUtils.createToolbar("edit_template", TE_MESS.templateToolbar, [edit_hbf, insert_cph]);
oBXEditorUtils.addToolbar(edit_template);

function TEContentParser(str)
{
	str = str.replace(/<asp\:\s*contentplaceholder\s*id\s*=\s*("|')(\w*)\1[^>]*?>[^<]*?<\/asp:ContentPlaceHolder>/ig, "<IMG src='" + image_path + "/cph.gif' __bxtagname='cph' id='bx_$2'/>");
	return str;
}
oBXEditorUtils.addContentParser(TEContentParser);

function TEUnparser(_node)
{
	if (_node.arAttributes["__bxtagname"] == 'cph')
	{
		var id = (_node.arAttributes["id"].substr(3)) || 'BXContentPlaceHolder';
		return '<asp:ContentPlaceHolder ID="' + id + '" runat="server"></asp:ContentPlaceHolder>';
	}
	return false;
}
oBXEditorUtils.addUnParser(TEUnparser);


pPropertybarHandlers['cph'] = function(bNew, pTaskbar, pElement)
{
	pTaskbar.pHtmlElement = pElement;
	if(bNew)
	{
		pTaskbar.arElements = [];
		var tProp;
		var arBarHandlersCache = pTaskbar.pMainObj.arBarHandlersCache;
		if(arBarHandlersCache['cph'])
		{
			tProp = arBarHandlersCache['cph'][0];
			pTaskbar.arElements = arBarHandlersCache['cph'][1];
		}
		else
		{
			tProp = pTaskbar.pMainObj.CreateElement("TABLE", {className: "bxtaskbarprops", cellSpacing: 0, cellPadding: 1}, {width: "100%"});
			var row = tProp.insertRow(-1);
			var cell = row.insertCell(-1); cell.align = 'right'; cell.width="40%";
			cell.appendChild(pTaskbar.pMainObj.CreateElement("SPAN", {innerHTML: TE_MESS.CPH_ID + ':&nbsp;'}));

			cell = row.insertCell(-1); cell.width="60%";
			pTaskbar.arElements['id'] = cell.appendChild(pTaskbar.pMainObj.CreateElement("INPUT", {size: '40', title: TE_MESS.CPH_ID,  type: 'text'}));
			arBarHandlersCache['cph'] = [tProp, pTaskbar.arElements];
		}
		pTaskbar.pCellProps.appendChild(tProp);
	}
	pTaskbar.arElements['id'].value = pElement.id.substr(3);
	pTaskbar.arElements['id'].onchange = function (){pElement.id = 'bx_' + this.value;};
};


arEditorFastDialogs['insert_cph'] = function(pObj)
{
	var innerHTML = '<div class="inner_content" style="font-size: 11px;">' + TE_MESS.CPH_ID + 
	': <input type="text" size="30" value="" id="bx_cph_id" style="width: 180px;" /></div>'+
	'<div style="margin-top: 5px; text-align: center;">' +
	'<input id="bx_cph_saveBut" type="button" value="' + BX_MESS.TBSave + '"> ' +
	'<input type="button" value="' + BX_MESS.Cancel + '" onclick="pObj.Close();"></div>';

	return {
		title: TE_MESS.INSERT_CPH,
		innerHTML : innerHTML,
		OnLoad: function()
		{
			pObj.content.style.padding = "5px";
			document.getElementById("bx_cph_saveBut").onclick = function()
			{
				var id = document.getElementById("bx_cph_id").value;
				if (id.length <= 0)
					return;
				pObj.pMainObj.insertHTML("<IMG src='" + image_path + "/cph.gif' __bxtagname='cph' id='bx_" + id + "'/>");
				var pComponent = pObj.pMainObj.pEditorDocument.getElementById('bx_' + id);
				if(pObj.pMainObj.pEditorWindow.getSelection)
					pObj.pMainObj.pEditorWindow.getSelection().selectAllChildren(pComponent);
				pObj.Close();
			};
		}
	};
}


