<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" no_editing="1" escape="1"/>';

				Catalog.prototype.AfterInit = function () {
					$('#current_group_id').change();
				}

				Catalog.prototype.go_to_group = function($group_id) {
					this.switchTab(); // refresh current item tab
				}

  				Catalog.prototype.refreshTab = function($prefix, $div_id, $force) {
					var $group_id = $('#current_group_id').val();
//					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"/>

<div id="scroll_container" mode="minimal">
	<table class="edit-form" style="border-bottom: 1px solid black;">
		<inp2:m_RenderElement name="subsection" title="la_section_General"/>
			<tr class="<inp2:m_odd_even odd='edit-form-odd' even='edit-form-even'/>">
				<td class="label-cell">
					<inp2:m_Phrase name="la_fld_Group"/>:
				</td>
				<td class="control-mid">&nbsp;</td>
				<td class="control-cell">
					<select name="current_group_id" id="current_group_id">
						<inp2:m_DefineElement name="group_element">
							<option value="<inp2:Field name='GroupId'/>"><inp2:Field name="Name"/></option>
						</inp2:m_DefineElement>

						<inp2:g_PrintList render_as="group_element" per_page="-1"/>
					</select>
				</td>
			</tr>

			<inp2:m_RenderElement name="inp_edit_filler"/>
	</table>
</div>

<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">
	var $last_group_id = false;

	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() {
			// last selected group
			set_hidden_field('group_id', $('#current_group_id').val());

			$('#current_group_id').change(
				function ($e) {
					var $group_id = $(this).val();

					if ($last_group_id !== false) {
						// save permissions from previous selected group
						set_hidden_field('group_id', $last_group_id);
						set_hidden_field('item_prefix', $PermManager.ActivePrefix);
						$PermManager.submit_event('c', 'OnPreSave', 'categories/permissions_tab');
					}

					// get permissions from new group
					$PermManager.go_to_group($group_id);

					// remember previous selected group
					$last_group_id = $group_id;
				}
			);

			$PermManager.Init(false);
		}
	);
</script>