<inp2:m_include t="incs/header" noform="yes"/>
<inp2:m_include template="catalog/catalog_elements"/>

<inp2:m_RenderElement name="combined_header" section="in-portal:browse" prefix="c" title_preset="catalog" tabs="catalog/catalog_tabs" additional_blue_bar_render_as="theme_selector"/>

<!-- main kernel_form: begin -->
<inp2:m_RenderElement name="kernel_form"/>
<!-- ToolBar -->

<table class="toolbar" height="30" cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
	<tr>
	  	<td>
	  		<input type="hidden" name="m_cat_id" value="<inp2:m_get name="m_cat_id"/>"/>
			<link rel="stylesheet" rev="stylesheet" href="incs/nlsmenu.css" type="text/css" />
			<script type="text/javascript" src="js/nlsmenu.js"></script>
			<script type="text/javascript" src="js/nlsmenueffect_1_2_1.js"></script>

	  		<script type="text/javascript" src="js/catalog.js"></script>
	  		<script type="text/javascript">
	  			<inp2:m_if check="adm_CheckPermCache">
	  				$(document).ready(
	  					function() {
	  						Application.SetVar('continue', 1);
	  						openSelector('c', '<inp2:m_t t="categories/cache_updater" pass="m"/>');
	  					}
	  				);
	  			</inp2:m_if>

  				var menuMgr = new NlsMenuManager("mgr");
				menuMgr.timeout = 500;
				menuMgr.flowOverFormElement = true;

	  			Request.progressText = '<inp2:m_phrase name="la_title_Loading" no_editing="1" escape="1"/>';
	  			var $is_catalog = true;
	  			var $Catalog = new Catalog('<inp2:m_Link template="#TEMPLATE_NAME#" m_cat_id="#CATEGORY_ID#" no_amp="1"/>', 'catalog_', 'Catalog');
				$Catalog.TabByCategory = <inp2:m_if check="m_GetConfig" name="Catalog_PreselectModuleTab">true<inp2:m_else/>false</inp2:m_if>;

				var a_toolbar = new ToolBar();

				a_toolbar.AddButton(
					new ToolBarButton(
						'upcat',
						'<inp2:m_phrase label="la_ToolTip_Up" escape="1"/>::<inp2:m_phrase label="la_ShortToolTip_GoUp" escape="1"/>',
						function() {
							$Catalog.go_to_cat($Catalog.ParentCategoryID);
						}
					)
				);

				a_toolbar.AddButton(
					new ToolBarButton(
						'homecat',
						'<inp2:m_phrase label="la_ToolTip_Home" escape="1"/>',
						function() {
							$Catalog.go_to_cat(<inp2:c_HomeCategory/>);
						}
					)
				);

				a_toolbar.AddButton( new ToolBarSeparator('sep1') );

				<inp2:m_ModuleInclude template="catalog_tab" tab_init="1" replace_m="yes"/>

				a_toolbar.AddButton(
					new ToolBarButton(
						'edit',
						'<inp2:m_phrase label="la_ToolTip_Edit" escape="1"/>',
						edit
					)
				);

				a_toolbar.AddButton(
					new ToolBarButton(
						'delete',
						'<inp2:m_phrase label="la_ToolTip_Delete" escape="1"/>',
						function() {
							var $template = $Catalog.queryTabRegistry('prefix', $Catalog.getCurrentPrefix(), 'view_template');
							std_delete_items($Catalog.getCurrentPrefix(), $template, 1);
						}
					)
				);

				<inp2:m_ModuleInclude template="catalog_buttons" main_template="catalog"/>

				a_toolbar.AddButton( new ToolBarSeparator('sep2') );

				a_toolbar.AddButton(
					new ToolBarButton(
						'approve',
						'<inp2:m_phrase label="la_ToolTip_Approve" escape="1"/>',
						function() {
							askCategoryPropagate();
							$Catalog.submit_event(null, 'OnMassApprove');
						}
			 		)
			 	);

				a_toolbar.AddButton(
					new ToolBarButton(
						'decline',
						'<inp2:m_phrase label="la_ToolTip_Decline" escape="1"/>',
						function() {
							askCategoryPropagate();
							$Catalog.submit_event(null, 'OnMassDecline');
						}
			 		)
			 	);

			 	function askCategoryPropagate() {
			 		if ($Catalog.getCurrentPrefix() == 'c') {
						var $propagate_status = confirm('<inp2:m_Phrase name="la_msg_PropagateCategoryStatus" escape="1"/>');

						$form_name = $Catalog.queryTabRegistry('prefix', 'c', 'tab_id') + '_form';
						Application.SetVar('propagate_category_status', $propagate_status ? 1 : 0);
					}
			 	}

				a_toolbar.AddButton( new ToolBarSeparator('sep3') );

				a_toolbar.AddButton(
					new ToolBarButton(
						'cut',
						'<inp2:m_phrase label="la_ToolTip_Cut" escape="1"/>',
						function() {
							$Catalog.submit_event(null, 'OnCut');
						}
			 		)
			 	);

			 	a_toolbar.AddButton(
			 		new ToolBarButton(
			 			'copy',
			 			'<inp2:m_phrase label="la_ToolTip_Copy" escape="1"/>',
			 			function() {
							$Catalog.submit_event(null, 'OnCopy');
						}
			 		)
			 	);

				a_toolbar.AddButton(
					new ToolBarButton(
						'paste',
						'<inp2:m_phrase label="la_ToolTip_Paste" escape="1"/>',
						function() {
							submit_event('c', 'OnPasteClipboard', 'catalog/catalog');

							/*$Catalog.submit_event(
								'c', 'OnPasteClipboard', null,
								function($object) {
									$object.resetTabs(true);
									$object.switchTab();
								}
							);*/
						}
			 		)
			 	);

			 	/*a_toolbar.AddButton( new ToolBarButton('clear_clipboard', '<inp2:m_phrase label="la_ToolTip_ClearClipboard" escape="1"/>', function() {
			 			if (confirm('<inp2:m_phrase name="la_text_ClearClipboardWarning" js_escape="1"/>')) {
							$Catalog.submit_event('c', 'OnClearClipboard', null, function($object) {
								$GridManager.CheckDependencies($object.ActivePrefix);
							}  );
			 			}
					}
			 	) );*/

			 	a_toolbar.AddButton( new ToolBarSeparator('sep5') );

			 	a_toolbar.AddButton(
			 		new ToolBarButton(
			 			'move_up',
			 			'<inp2:m_phrase label="la_ToolTip_MoveUp" escape="1"/>::<inp2:m_phrase label="la_ToolTipShort_Move_Up" escape="1"/>',
			 			function() {
							$Catalog.submit_event(null, 'OnMassMoveUp');
						}
			 		)
			 	);

				a_toolbar.AddButton(
					new ToolBarButton(
						'move_down',
						'<inp2:m_phrase label="la_ToolTip_MoveDown" escape="1"/>::<inp2:m_phrase label="la_ToolTipShort_Move_Down" escape="1"/>',
						function() {
							$Catalog.submit_event(null, 'OnMassMoveDown');
						}
			 		)
			 	);

			 	a_toolbar.AddButton( new ToolBarSeparator('sep6') );

			 	a_toolbar.AddButton(
			 		new ToolBarButton(
			 			'tools',
			 			'<inp2:m_Phrase label="la_ToolTip_Tools" escape="1"/>',
			 			function() {
							var $menu = menuMgr.createMenu(rs('tools_menu'));
							$menu.applyBorder(false, false, false, false);
							$menu.dropShadow('none');
							$menu.showIcon = true;

							$menu.addItem(rs('editcat'), '<inp2:m_Phrase name="la_ToolTip_Edit_Current_Category" js_escape="1"/>', 'javascript:executeButton("editcat");');
							$menu.addItem(rs('export'), '<inp2:m_Phrase name="la_ToolTip_Export" js_escape="1"/>', 'javascript:executeButton("export");');
							$menu.addSeparator();
							$menu.addItem(rs('rebuild_cache'), '<inp2:m_Phrase name="la_ToolTip_RebuildCategoryCache" js_escape="1"/>', 'javascript:executeButton("rebuild_cache");');

							if ($Catalog.ActivePrefix == 'c') {
								$menu.addItem(rs('recalculate_priorities'), '<inp2:m_Phrase name="la_ToolTip_RecalculatePriorities" js_escape="1"/>', 'javascript:executeButton("recalculate_priorities");');
							}

							renderMenus();

							nls_showMenu(rs('tools_menu'), a_toolbar.GetButtonImage('tools'));
						}
					)
				);

				function executeButton($button_name) {
					switch ($button_name) {
						case 'editcat':
							var $edit_url = '<inp2:m_t t="#TEMPLATE#" m_opener="d" c_mode="t" c_event="OnEdit" c_id="#CATEGORY_ID#" pass="all,c" no_amp="1"/>';
							var $category_id = get_hidden_field('m_cat_id');
							var $redirect_url = $edit_url.replace('#CATEGORY_ID#', $category_id);
							$redirect_url = $redirect_url.replace('#TEMPLATE#', $category_id == 0 || $category_id == <inp2:c_HomeCategory/> ? 'categories/categories_edit_permissions' : 'categories/categories_edit');
							direct_edit('c', $redirect_url);
							break;

						case 'export':
							var $export_templates = <inp2:c_PrintCatalogExportTemplates prefixes="l,n,p"/>;

							if ($export_templates[$Catalog.ActivePrefix] != undefined) {
								$Catalog.storeIDs('export_categories');

								open_popup($Catalog.ActivePrefix, 'OnExport', $export_templates[$Catalog.ActivePrefix]);
							}
							else {
								alert('<inp2:m_phrase name="la_Text_InDevelopment" escape="1"/>');
							}
							break;

						case 'rebuild_cache':
							openSelector('c', '<inp2:m_t t="categories/cache_updater" pass="m"/>');
							break;

						case 'recalculate_priorities':
							$Catalog.submit_event('c', 'OnRecalculatePriorities');
							break;

					}
				}

				a_toolbar.AddButton(
					new ToolBarButton(
						'view',
						'<inp2:m_phrase label="la_ToolTip_View" escape="1"/>',
						function() {
							show_viewmenu(a_toolbar, 'view');
						}
					)
				);

				a_toolbar.Render();

				function edit() {
					var $current_prefix = $Catalog.getCurrentPrefix();
					$form_name = $Catalog.queryTabRegistry('prefix', $current_prefix, 'tab_id') + '_form';
					std_edit_item($current_prefix, $Catalog.queryTabRegistry('prefix', $current_prefix, 'edit_template'));
				}

				function add_item() {
					var $current_prefix = $Catalog.getCurrentPrefix();
					$form_name = $Catalog.queryTabRegistry('prefix', $current_prefix, 'tab_id') + '_form';
					std_precreate_item($current_prefix, $Catalog.queryTabRegistry('prefix', $current_prefix, 'edit_template'));
				}
			</script>
		</td>

		<inp2:m_RenderElement name="catalog_search_box"/>
	</tr>
</tbody>
</table>
<inp2:m_RenderElement name="kernel_form_end"/>
<!-- main kernel_form: end -->

<inp2:m_ModuleInclude template="catalog_tab" tab_init="2" strip_nl="2"/>

<script type="text/javascript">
	var $menu_frame = getFrame('menu');
	if (typeof $menu_frame.ShowStructure != 'undefined') {
		<inp2:m_DefineElement name="structure_node"><inp2:m_param name="section_url"/></inp2:m_DefineElement>
		$menu_frame.ShowStructure('<inp2:adm_PrintSection escape="1" render_as="structure_node" section_name="in-portal:browse"/>', true);
	}

	Application.setHook(
		'm:OnAfterWindowLoad',
		function() {
			$Catalog.Init();

			getFrame('head').$('#extra_toolbar').html('<inp2:m_RenderElement name="extra_toolbar" js_escape="1"/>');
		}
	);
</script>

<inp2:m_include t="incs/footer" noform="yes"/>