{*
/*************************************
 * SPDX-FileCopyrightText: 2009-2020 Vtenext S.r.l. <info@vtenext.com> 
  * SPDX-License-Identifier: AGPL-3.0-only  
 ************************************/
*}
{* crmv@82419 crmv@104568 crmv@121366 *}

<div class="vte-card turboliftInner">
	<div class="rightMailMergeHeader">{'Relations'|getTranslatedString}</div>
	{assign var="RELATION_LIMIT" value="8"} {* crmv@103926 *}
	<ul class="nav nav-pills nav-stacked turbolift">
	{foreach item=RELATION from=$RELATIONS name="tl_related_foreach"}
		{assign var=header value=' '|@str_replace:'':$RELATION.header}
		{assign var=related value=$MODULE|cat:"_"|cat:$header}
		{if $RELATION.type eq 'card'}
			{assign var=entity_id value=$RELATION.id}
			{assign var=info value=$RELATION.card}
			{if !empty($info.onclick)}
				{assign var=CARDONCLICK value=$info.onclick}
			{elseif $info.link neq ''}
				{assign var=CARDONCLICK value=$TURBOLIFT_HREF_TARGET_LOCATION|cat:"='"|cat:$info.link|cat:"';"}
			{/if}
			{include file='Card.tpl' TURBOLIFTCARD=true CARDRECORD=$entity_id CARDID="tl_"|cat:$related CARDMODULE=$info.module CARDMODULE_LBL=$info.modulelbl PREFIX=$info.prefix CARDNAME=$info.name CARDDETAILS=$info.details CARDONCLICK=$CARDONCLICK DISPLAY=$RELATION.display IMG=$info.img} {* crmv@152802 *}
		{else}
			{if $RELATION.fixed}
				{assign var=fixedParam value="true"}
			{else}
				{assign var=fixedParam value="false"}
			{/if}
			{assign var=CARDONCLICK value="loadDynamicRelatedList(this,'`$RELATION.relationId`', '`$related`','module=`$MODULE`&action=`$MODULE`Ajax&file=DetailViewAjax&record=`$ID`&ajxaction=LOADRELATEDLIST&header=`$RELATION.header`&relation_id=`$RELATION.relationId`&actions=`$RELATION.actions`&load_header=yes', `$fixedParam`);"}
			{* crmv@43864	crmv@59091 *}
			<li class="turboliftEntry turboliftEntryWithImage {if $smarty.foreach.tl_related_foreach.index ge $RELATION_LIMIT}hidden{/if}" relation_id="{$RELATION.relationId}" id="tl_{$related}" onClick="{$CARDONCLICK}">
				{assign var="module" value=$RELATION.module}
				{assign var="module_lower" value=$module|strtolower}
				{assign var="translated_module" value=$RELATION.module|getTranslatedString:$RELATION.module}
				{assign var="module_first_letter" value=$translated_module|substr:0:1|strtoupper}
				
				{if $RELATION.header}
					{assign var=RELLABEL value=$RELATION.header|getTranslatedString:$RELATION.module}
				{else}
					{assign var=RELLABEL value=$translated_module}
				{/if}
				
				{* crmv@201796 *}
				<div class="turboliftEntryInner" title="{$RELLABEL}">
					<div class="turboliftIcon">
						<i class="icon-module icon-{$module_lower}" data-first-letter="{$module_first_letter}"></i>
					</div>
					<div class="turboliftLabel">
						<span>{$RELLABEL}</span>
					</div>
					<div class="turboliftBadge">
						<span class="badge">{$RELATION.count}</span>
					</div>
				</div>
				{* crmv@201796e *}
			</li>
			{* crmv@43864e	crmv@59091e *}
		{/if}
	{/foreach}
	{if $smarty.foreach.tl_related_foreach.total ge $RELATION_LIMIT}
		<li id="tl_MoreRelated" class="turboliftEntry turboliftEntryWithImage" onClick="Turbolift.showall(this);">
			<span>{$APP.LBL_MORE}</span>
		</li>
		{* TODO ad less*}
	{/if}
	</ul>
</div>

<script type="text/javascript">
var turboliftMode = "{$TURBOLIFT_MODE}";

{literal}
var Turbolift = {

	mode: '',
	
	init: function(mode) {
		var me = this,
			cont = jQuery('#turboLiftContainerDiv'),
			turboh = 0;

		me.mode = mode;

		if (mode === 'DetailView') {
			var offsetblocks = jQuery('#DetailViewBlocks').offset();
			var turbotop = parseInt(offsetblocks.top || 0) - 5;
			turboh = jQuery(window).height() - turbotop - jQuery('#vte_footer').height();

			cont.css('top', turbotop);
		} else {
			turboh = jQuery(window).height();
		}

		cont.mCustomScrollbar({
			set_height: turboh,
			axis: 'y',
			mouseWheelPixels: 100,
			theme: "dark-thick",
			autoHideScrollbar: true,
			scrollInertia: 0,
			advanced: {
				updateOnContentResize: true,
			}
		});
		cont.show();
		
		me.resize();
	},

	resize: function() {
		var toggleState = get_cookie('togglePin');
		if (this.mode === 'DetailView') {
			if (jQuery('#turboLiftContainerDiv').length > 0) {
				if (toggleState === 'disabled') {
					jQuery('#turboLiftContainerDiv').width('17%');
				} else {
					jQuery('#turboLiftContainerDiv').width('20%');
				}
			}
		}
	},
	
	showall: function(obj) {
		jQuery('#turboLiftRelationsContainer').find('.turboliftEntry').removeClass('hidden');
		this.checkFakeDiv();
		jQuery(obj).hide();
	},
	
	alignScroll: function() {
		var cont = jQuery('#turboLiftContainerDiv');
		var sel = cont.find('.turboliftEntrySelected');
		var pos = sel.position().top - 10;
		
		this.checkFakeDiv(function() {
			cont.mCustomScrollbar('scrollTo', pos, {
				scrollInertia: 600, // like jquery slow
			});
		});
	},

	checkFakeDiv: function(callback) {
		var cont = jQuery('#turboLiftContainerDiv'),
			fakeid = cont.find('.fakeLongDiv'),
			delay = 10;
		
		if (fakeid.length == 0) {
			var fakeHeight = parseInt(cont.height()) - parseInt(cont.find('.turboliftEntry').first().outerHeight() || 0);
			cont.find('.turboliftInner').after('<div class="fakeLongDiv" style="height:'+fakeHeight+'px"></div>');
			cont.mCustomScrollbar('update');
			delay = 150;
		}
		
		if (typeof callback == 'function') {
			setTimeout(function() {
				callback();
			}, delay);
		}
	},
	
}

jQuery(document).ready(function() {
	Turbolift.init(turboliftMode);
});
{/literal}

{* crmv@54375 *}
{if !empty($OPENRELATEDLIST)}
	if (jQuery("#{$OPENRELATEDLIST}").length > 0) jQuery("#{$OPENRELATEDLIST}").click();
{/if}
{* crmv@54375e *}
</script>