<inp2:adm_SetPopupSize width="880" height="680"/>

<inp2:m_include t="incs/header"/>
<inp2:m_RenderElement name="combined_header" prefix="c" section="in-portal:browse" g[grid]="Radio" title_preset="categories_permissions" tab_preset="Default"/>

<inp2:m_include template="catalog/catalog_elements"/>
<inp2:m_include t="categories/categories_tabs"/>

<!-- ToolBar -->
<table class="toolbar" height="30" cellspacing="0" cellpadding="0" width="100%" border="0">
<tbody>
	<tr>
  	<td>
  		<script type="text/javascript" src="js/ajax.js"></script>
	  	<script type="text/javascript" src="js/catalog.js"></script>
  		<script type="text/javascript">
  				Request.progressText = '<inp2:m_phrase name="la_title_Loading" escape="1"/>';

				Catalog.prototype.AfterInit = function () {
					Grids['g'].SelectFirst();
				}

				Catalog.prototype.go_to_group = function($group_id) {
					if (!isset($group_id)) {
						$group_id = 0; // gets current group
					}
					else {
						set_hidden_field('current_group_id', $group_id);
					}

					this.switchTab(); // refresh current item tab
				}

  				Catalog.prototype.refreshTab = function($prefix, $div_id, $force) {
					var $group_id = get_hidden_field('current_group_id');
//					alert('refreshTab. GroupID: '+$group_id);
					var $tab_group_id = document.getElementById($div_id).getAttribute('group_id');
					if ($group_id != $tab_group_id || $force) {
						// query tab content only in case if not queried or category don't match
						var $url = this.URLMask.replace('#ITEM_PREFIX#', $prefix).replace('#GROUP_ID#', $group_id);
						this.BusyRequest[$prefix] = false;

						Request.makeRequest($url, this.BusyRequest[$prefix], $div_id, this.successCallback, this.errorCallback, $div_id, this);
					}
					/*else {
						alert('refresh disabled = {tab: '+this.ActivePrefix+'; group_id: '+$group_id+'}');
					}*/
				}

				// adds information about tab to tab_registry
				Catalog.prototype.registerTab = function($tab_id) {
					var $tab = document.getElementById($tab_id + '_div');
					var $index = this.TabRegistry.length;
					this.TabRegistry[$index] = new Array();
					this.TabRegistry[$index]['tab_id'] = $tab_id;
					this.TabRegistry[$index]['prefix'] = $tab.getAttribute('prefix');
					this.TabRegistry[$index]['dep_buttons'] = new Array();
					this.TabRegistry[$index]['index'] = $index;
				}

				Catalog.prototype.displaySearch = function ($prefix) {

				}

				Catalog.prototype.submit_event = function($prefix_special, $event, $t) {
					var $prev_template = get_hidden_field('t');
					if (isset($event)) set_hidden_field('events[' + $prefix_special + ']', $event);
					if (isset($t)) set_hidden_field('t', $t);

					var $tab_id = this.queryTabRegistry('prefix', this.ActivePrefix, 'tab_id');
					this.submit_kernel_form();

					set_hidden_field('t', $prev_template);
				}

	  			var $PermManager = new Catalog('<inp2:m_Link template="categories/permissions_tab" item_prefix="#ITEM_PREFIX#" group_id="#GROUP_ID#" no_amp="1" pass="m,c"/>', 'permmanager_', 'CategoryPermissionEditor');

				a_toolbar = new ToolBar();
				a_toolbar.AddButton( new ToolBarButton('select', '<inp2:m_phrase label="la_ToolTip_Save" escape="1"/>', function() {
							submit_event('c','<inp2:c_SaveEvent/>');
						}
					) );
				a_toolbar.AddButton( new ToolBarButton('cancel', '<inp2:m_phrase label="la_ToolTip_Cancel" escape="1"/>', function() {
							submit_event('c','OnCancelEdit');
						}
				 ) );

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

				a_toolbar.AddButton( new ToolBarButton('prev', '<inp2:m_phrase label="la_ToolTip_Prev" escape="1"/>', function() {
							go_to_id('c', '<inp2:c_PrevId/>');
						}
				 ) );
				a_toolbar.AddButton( new ToolBarButton('next', '<inp2:m_phrase label="la_ToolTip_Next" escape="1"/>', function() {
							go_to_id('c', '<inp2:c_NextId/>');
						}
				 ) );

				function edit(){ }

				a_toolbar.Render();

				<inp2:m_if check="c_IsSingle">
					a_toolbar.HideButton('prev');
					a_toolbar.HideButton('next');
					a_toolbar.HideButton('sep1');
				<inp2:m_else/>
					<inp2:m_if check="c_IsLast">
						a_toolbar.DisableButton('next');
					</inp2:m_if>
					<inp2:m_if check="c_IsFirst">
						a_toolbar.DisableButton('prev');
					</inp2:m_if>
				</inp2:m_if>
			</script>
		</td>

		<inp2:m_RenderElement name="search_main_toolbar" prefix="g" grid="Radio"/>
	</tr>
</tbody>
</table>

<inp2:c_SaveWarning name="grid_save_warning"/>
<inp2:m_RenderElement name="grid" PrefixSpecial="g" IdField="GroupId" per_page="-1" grid="Radio" header_block="grid_column_title_no_sorting" grid_height="220" grid_status="0"/>

<br />
<!-- item tabs: begin -->
<inp2:m_DefineElement name="item_tab" title="" special="">
	<td class="tab-spacer"><img src="img/spacer.gif" width="3" height="1"/></td>
	<td id="<inp2:m_param name="prefix"/><inp2:m_param name="special"/>_tab" class="tab">
		<img src="<inp2:m_TemplatesBase module='$icon_module'/>/img/itemicons/<inp2:m_Param name='icon'/>" width="16" height="16" align="absmiddle" alt=""/>
		<a href="#" onclick="$PermManager.switchTab('<inp2:m_param name="prefix"/>'); return false;" class="tab-link">
			<inp2:m_param name="title"/>
		</a>
	</td>
</inp2:m_DefineElement>

<inp2:m_DefineElement name="permission_tabs">
	<inp2:adm_ListCatalogTabs render_as="item_tab" title_property="PermTabText"/>
</inp2:m_DefineElement>

<inp2:m_RenderElement name="tabs_container" tabs_render_as="permission_tabs"/>
<!-- item tabs: end -->

<inp2:c-perm_PrintTabs template="categories/permissions_tab" tab_init="1"/>

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

<script type="text/javascript">
	Grids['g'].OnSelect = function ($id) {
		$PermManager.go_to_group($id);
	}

	Grids['g'].OnUnSelect = function ($id) {
		set_hidden_field('group_id', $id);
		set_hidden_field('item_prefix', $PermManager.ActivePrefix);
		$PermManager.submit_event('c', 'OnPreSave', 'categories/permissions_tab');
	}

	Grids['g'].SelectFirst = function () {
		for (var $i in this.Items) {
			this.Items[$i].Select();
			break;
		}
	}

	function update_light(perm_name, value)
	{
		document.getElementById('light_'+perm_name).src = 'img/perm_' + (value ? 'green' : 'red') + '.gif';
	}

	function inherited_click(perm_name, inherited_value, state, access_cb_id)
	{
		if (state) {
			update_light(perm_name, inherited_value);
			document.getElementById(access_cb_id).disabled = true;
		}
		else {
			update_light(perm_name, document.getElementById(access_cb_id).checked)
			document.getElementById(access_cb_id).disabled = false;
		}
	}

	$(document).ready(
		function() {
			$PermManager.Init();
		}
	);
</script>