{extends file=$element.methods->getTemplatePath('general')}

{block name=input append}
	{$CSS->load("_themes/{$smarty.const.THEME}/styles/inputs/input-services.css")}
	

	<div class="input-services {$element['class']} {$element['style']['renderMode']}"  id="{$element['id']}">
		
		{* forces from null to array *}
		{$element['value']=(array)$element['value']}

		{foreach $element['possibleValues'] as $key => $item}
		
			{if $item.type == 'group'}
				<div class="services-group">					
		 			<input type="checkbox" class="noBorder inputElement groupInput"
						id="{$element['id']}_{$item['type']}_{$key}"
						
						{*On load set the container's checked state to true, if all childs are checked*}
						{if $item['list'] && count(array_intersect(array_keys($item['list']), $element['value'])) == count($item['list'])}
							checked="checked"
						{/if}
					/>
					<label for="{$element['id']}_{$item['type']}_{$key}">{$item['name']}</label>
					
					{if $item['list']}
						<div class="services-list">	
							{foreach $item['list'] as $servCode => $serv}
								<div class="service-wrapper">	
						 			<input type="checkbox" class="noBorder inputElement "
										name="{$element['name']}[]"
										value="{$servCode}"
										{if $servCode|@in_array:$element['value']}
											checked="checked"
										{/if}
										id="{$element['id']}_{$servCode}"
									/>
									<label for="{$element['id']}_{$servCode}" 
										class="{if $element['style']['highlight']=='current' && $servCode|@in_array:$element['value']}highlight {/if}"
									>{$serv['name']}</label>
								</div>
							{/foreach}
						</div>
					{/if}
				</div>
			{elseif $item['type'] == 'service'}
				<div class="service-wrapper">				
		 			<input type="checkbox" class="noBorder inputElement"
						name="{$element['name']}[]"
						value="{$key}"
						{if $key|@in_array:$element['value']}
							checked="checked"
						{/if}
						id="{$element['id']}_{$key}" 
					/>
					<label for="{$element['id']}_{$key}" 
						class="{if $element['style']['highlight'] == 'current' && $key|@in_array:$element['value']}highlight {/if}"
					>{$item['name']}</label>
				</div>
			{/if}
		{/foreach}
	</div>
{/block}

<script type="text/javascript">
	{block name=script}
		MVC.scheduleOnLoad(function() {

			$('{$element['id']}').on('click', 'input.groupInput', function(event, element) {
				element.up('.services-group').select('.services-list .service-wrapper input[type="checkbox"]').each( function(el) {
					el.checked = element.checked;
				});
			});
			
			$('{$element['id']}').on('click', '.services-list .service-wrapper input[type="checkbox"]', function(event, element) {
				var container = element.up('.services-group');
				// set the container's checked state to true, if all childs are checked
				container.down('input.groupInput').checked = (container.select('.services-list .service-wrapper input[type="checkbox"]').length == container.select('.services-list .service-wrapper input[type="checkbox"]:checked').length);
			});
			
		});				
	{/block}
</script>