window.BXOnSaveAs = function(bDialog, bApply)
{
	var Save = function(){document.getElementById('ctl00_ContentPlaceHolder1_BXTabControl1_' + (bApply ? 'apply' : 'save')).onclick();};
	if (!bDialog)
		return Save();

	var  
		JSName = window[window._fileDialog.JSName],
		ExtraJSName = window[window._fileDialog.ExtraJSName],
		pSaveAs = document.getElementById(BX_DOTNET_ID.SaveAs),
		_def_path = _curDir + '\\' + pSaveAs.value;

	ExtraJSName.Properties = {ShowFiles: true, ItemsToSelect: 2, ExtensionsList: 'aspx,html', DefaultPath: _def_path};
	ExtraJSName.SaveEvent = function()
	{
		var 
			pSaveAsPath = document.getElementById(BX_DOTNET_ID.SaveAsPath),
			path = ExtraJSName.GetTargetValue(),
			_path = path.replace(/\\/g, '/'),
			ind = _path.lastIndexOf('/'),
			filename = _path.substr(ind + 1),
			form = document.getElementById('aspnetForm');

		form.action = form.action.replace(/path=[^&]*/, "path="+jsUtils.urlencode(path));
		pSaveAs.value = filename;
		pSaveAsPath.value = path;
		Save();
	};

	JSName.ShowPopupDialog();
	ExtraJSName.Reset();
	ExtraJSName.CollapseToRoot();
}


arButtons['new']	= ['BXButton',
	{
		id : 'new',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBNewPage,
		hideCondition: function(){return !window.bx_new_file_command_path;},
		handler : function ()
		{
			if (window.bx_new_file_command_path.length <= 0)
				return;
			window.location.href = APPPath + '/bitrix/admin/' + window.bx_new_file_command_path;
		}
	}
];

arButtons['save_and_exit'] = ['BXButton',
	{
		id : 'save_and_exit',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBSaveExit,
		title : BX_MESS.TBSaveExit,
		show_name : true,
		handler : function (){BXOnSaveAs(!_bEdit, false);}
	}
];

arButtons['exit'] = ['BXButton',
	{
		id : 'exit',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBExit,
		handler : function ()
		{
			this.pMainObj.OnEvent("OnSelectionChange");
			var need_to_ask = (pBXEventDispatcher.arEditors[0].IsChanged() && !pBXEventDispatcher.arEditors[0].isSubmited);

			if(need_to_ask)
			{
				this.bNotFocus = true;
				this.pMainObj.OpenEditorDialog("asksave", false, 600, {window: window, savetype: _bEdit ? 'save' : 'saveas'}, true);
			}
			else
			{
				document.getElementById('ctl00_ContentPlaceHolder1_BXTabControl1_cancel').onclick();
			}
		}
	}
];

arButtons['saveas'] = ['BXButton',
	{
		id : 'saveas',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBSaveAs,
		handler : function (){BXOnSaveAs(true, true);}
	}
];


arButtons['save'] = ['BXButton',
	{
		id : 'save',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBSave,
		handler : function (){BXOnSaveAs(!_bEdit, true);}
	}
];

arButtons['pageprops'] = ['BXButton',
	{
		id : 'pageprops',
		iconkit : '_global_iconkit.gif',
		codeEditorMode : true,
		name : BX_MESS.TBProps,
		handler : function ()
		{
			this.bNotFocus = true;
			this.pMainObj.OpenEditorDialog("pageprops", false, 600, {window: window, document: document});
		}
	}
];

arToolbars['manage'] = [FE_MESS.FILEMAN_HTMLED_MANAGE_TB, [arButtons['save_and_exit'], arButtons['exit'], arButtons['new'], arButtons['save'], arButtons['saveas'], arButtons['pageprops']]];

arDefaultTBPositions['manage'] = [0, 0, 2];

window.onbeforeunload = function(e)
{
	var pMainObj = pBXEventDispatcher.arEditors[0];
	var need_to_ask = (pMainObj.IsChanged() && !pMainObj.isSubmited);
	if (need_to_ask)
		return BX_MESS.ExitConfirm;
}

function InitializeCPHToolbar(pMainObj)
{
	if (!pMainObj)
		throw 'System level exception. pMainObj == null';
	if (!pMainObj.cphID)
		throw 'this.pMainObj.cphID can`t have value not presented in contents list.';
	if (!pMainObj.arCPH)
		throw 'Toolbar should not be initialized when no content areas exists.';

	var cphtblist = oBXEditorUtils.createToolbar("ContentPlaceHolderTB", BX_MESS.ASPXP_TB_CPHAREAS, [], {show: true, docked: true, position: [0, 0, 2]});

	oBXEditorUtils.addToolbar(cphtblist);

	var ContentPlaceHolderList = ['BXList', {
		width: '160',
		height: '110',
		field_size: '150',
		title: '(template)',
		bSetGlobalStyles: true,
		disableOnCodeView: false,
		values: pMainObj.arCPH,
		OnCreate: function()
		{
			this.pMainObj.pCPHListbox = this;
			this.refreshList = function()
			{
				var arSC = this.pMainObj.pASPXParser.arShadowedControls;
				var cph = this.pMainObj.arTemplateParams.CPH;
				var _cph = {};
				// Refresh CPH list
				this.pMainObj.arCPH = [];
				var id, ocphid;
				for (var i = 0, l = cph.length; i < l; i++)
				{
					ocphid = cph[i];
					id = cph[i].toLowerCase();
					_cph[id] = true;
					this.pMainObj.arCPH.push({name:cph[i], value: id});
					if (arSC[id])
						continue;
					arSC[id] = { // Add new CPH to arShadowedControls
						id: id,
						ocphid: ocphid,
						attributes: {ID: id, runat: 'server'},
						prefix: 'asp',
						control: 'ContentPlaceHolder',
						close: '>',
						tagend: ['</asp:ContentPlaceHolder>']
					};
				}
				this.SetValues(this.pMainObj.arCPH);

				// Find first nonempty CPH and select it
				for (var id in arSC)
				{
					if (arSC[id].contentVal && _cph[id] && arSC[id].contentVal.length > 0)
					{
						this.SelectByVal(id);
						this.OnChange({value:id});
						break;
					}
				}
			};
			this.pMainObj.AddEventHandler("OnTemplateChanged", this.refreshList, this);
		},
		OnInit: function()
		{
			
			this.SelectByVal(this.pMainObj.cphID);
			var arSC = this.pMainObj.pASPXParser.arShadowedControls;
			var cph = this.pMainObj.arTemplateParams.CPH;
			var _cph = {};
			for (var i = 0, l = cph.length; i < l; i++)
				_cph[cph[i].toLowerCase()] = true;

			var selId = false;
			
			for (var id in arSC)
			{
				if (id.toLowerCase() == this.pMainObj.arConfig.defaultCPH.toLowerCase())
				{
					selId = id;
					break;
				}
			}
			
			if (!selId)
				for (var id in arSC)
				{
					if (!selId) selId = id;
					if (arSC[id].contentVal && _cph[id] && arSC[id].contentVal.length > 0)
					{
						selId = id;
						break;
					}
				}
			
			this.SelectByVal(selId);
			this.pMainObj.pASPXParser.SetCurrentCPH(selId, true);
		},
		OnChange: function(selected)
		{
			this.pMainObj.pASPXParser.SetCurrentCPH(selected.value);
		}
	}];

	oBXEditorUtils.appendButton('ContentPlaceHolderList', ContentPlaceHolderList, 'ContentPlaceHolderTB');
}

function RefreshValuesCPHToolbar(pMainObj)
{
	if (pMainObj && pMainObj.pCPHListbox && pMainObj.arCPH)
		pMainObj.pCPHListbox.SetValues(pMainObj.arCPH);
}

arEditorFastDialogs['asksave'] = function(pObj)
{
	var str = '<table height="100%" width="100%" id="t1" border="0">' +
	'<tr>' +
		'<td colspan="3">' +
			'<table height="100%" width="100%" id="t1" border="0" style="font-size:14px;">' +
			'<tr>' +
			'<td></td>' +
			'<td>' + BX_MESS.DIALOG_EXIT_ACHTUNG + '</td>' +
			'</tr>' +
			'</table>' +
		'</td>' +
	'</tr>' +
	'<tr id="buttonsSec" valign="top">' +
		'<td align="center"><input style="font-size:12px; height: 25px; width: 180px;" type="button" id="asksave_b1" value="' + BX_MESS.DIALOG_SAVE_BUT + '"></td>' +
		'<td align="center"><input style="font-size:12px; height: 25px; width: 180px;" type="button" id="asksave_b2" value="' + BX_MESS.DIALOG_EXIT_BUT + '"></td>' +
		'<td align="center"><input style="font-size:12px; height: 25px; width: 180px;" type="button" id="asksave_b3" value="' + BX_MESS.DIALOG_EDIT_BUT + '"></td>' +
	'</tr>' +
'</table>';

	var OnClose = function(){pObj.Close();};
	var OnSave = function(t)
	{
		pObj.pMainObj.isSubmited = true;
		pObj.params.window.BXOnSaveAs((pObj.params.savetype == 'saveas'), false);
		OnClose();
	};
	var OnExit = function(t)
	{
		pObj.pMainObj.isSubmited = true;
		document.getElementById('ctl00_ContentPlaceHolder1_BXTabControl1_cancel').onclick();
		OnClose();
	};

	return {
		title: BX_MESS.EDITOR,
		innerHTML : str,
		OnLoad: function()
		{
			document.getElementById("asksave_b1").focus();
			document.getElementById("asksave_b1").onclick = OnSave;
			document.getElementById("asksave_b2").onclick = OnExit;
			document.getElementById("asksave_b3").onclick = OnClose;
		}
	};
}

arEditorFastDialogs['pageprops'] = function(pObj)
{
	var pSettingsTab = document.getElementById(BX_DOTNET_ID.settingsTab);
	var cn = pSettingsTab.childNodes, pSettingsTable;
	for (var i = 0, l = cn.length; i < l; i++)
	{
		if (cn[i].nodeType != 1)
			continue;
		if (cn[i].nodeName.toUpperCase() == 'TABLE' && cn[i].className.toUpperCase().indexOf('EDIT-TABLE') != -1)
		{
			var pSettingsTable = cn[i];
			break;
		}
	}
	if (!pSettingsTable)
		return false;

	var arInputs = pSettingsTable.getElementsByTagName('INPUT');
	var oInputs = [], id;
	var innerHTML = pSettingsTab.innerHTML;
	innerHTML = innerHTML.replace(/\s*<table[\S|\s]*?<\/table>\s*/, '');
	for (i = 0, l = arInputs.length; i < l; i++)
	{
		id = BX_DOTNET_ID.settingsTab + "_KeywordsTable_k" + i + "_Value";
		innerHTML = innerHTML.replace(id, "bx_editor_pageprops_" + i);
		oInputs.push({input: arInputs[i]});
	}
	arInputs = null;

	innerHTML += '<div style="margin-top: 5px; text-align: center;">' +
	'<input id="bx_pageprops_saveBut" type="button" value="' + BX_MESS.TBSave + '"> ' +
	'<input type="button" value="' + BX_MESS.Cancel + '" onclick="pObj.Close();">' +
	'</div>';

	var OnSave = function(t)
	{
		for (i = 0, l = oInputs.length; i < l; i++)
			oInputs[i].input.value = oInputs[i].new_input.value;
		pObj.Close();
	};

	return {
		title: BX_MESS.TBProps,
		innerHTML : innerHTML,
		OnLoad: function()
		{
			for (i = 0, l = oInputs.length; i < l; i++)
			{
				oInputs[i].new_input = document.getElementById("bx_editor_pageprops_" + i);
				oInputs[i].new_input.value = oInputs[i].input.value;
			}
			pObj.content.style.padding = "5px";
			document.getElementById("bx_pageprops_saveBut").onclick = OnSave;
		}
	};
}