
<?php if($this->context == 'listElement'): ?>
<script>
/* <![CDATA[ */

/**
 * Pass grid related information to list elements rows and build grid class toggler
 */
jQuery(document).ready(function() 
{
	var objElement = jQuery('#li_<?= $this->id; ?>');
	if(objElement.length < 1)
	{
		return;
	}
	
	<?php // return when AG is disabled
	if(!$this->autogrid): ?>
	return;
	<?php endif; ?>
	
	var strType = '<?= $this->type; ?>';
	objElement.addClass(strType);
	
	var blnShowButtons = true;
	<?php if(in_array($this->type, $GLOBALS['PCT_AUTOGRID']['wrapperElements'])): ?>
	blnShowButtons = false;
	<?php endif; ?>
	<?php if( $GLOBALS['PCT_AUTOGRID']['autogridRowStarted'] && $this->type == 'autogridColStart' ): ?>
	blnShowButtons = true;
	<?php endif; ?>
	
	objElement.attr('data-type',strType);
	objElement.addClass('<?= $this->input_classes; ?>');
		
	// build PLUS/MINUS toggler
	if(blnShowButtons === true)
	{
		objElement.attr('data-grid','<?= $this->AutoGrid->desktop; ?>');
		objElement.addClass('hasButtons');
		var arrClasses = JSON.parse('<?= json_encode( array_reverse($GLOBALS['PCT_AUTOGRID']['classes']) ); ?>');
		
		<?php 
		$objButtons = new \Contao\BackendTemplate('be_autogrid_buttons');
		$objButtons->setData($this->getData());
		$strButtons = $objButtons->parse();
		?>
		var strButtons = '<?= \Contao\StringUtil::substrHtml($strButtons,strlen($strButtons)); ?>'
		objElement.find('> div').prepend(strButtons);
		
		var objButtons = objElement.find('.autogrid_buttons .button');
		var objRequest = null;
		var intDelay = <?= $GLOBALS['PCT_AUTOGRID']['BACKEND']['ajaxInteractionDelay'] ?: 500; ?>;
		var varTimeout = 0;

		objButtons.click(function(e) 
		{
			var currClass = objElement.attr('data-grid');
			var index = arrClasses.indexOf(currClass);
			var action = jQuery(this).data('action');
			
			// up
			if(action == 'up' && index < arrClasses.length )
			{
				index++;
			}
			// down
			else if(action == 'down' && index >= 0)
			{
				index--;
			}
			
			// index out of range
			if(index >= arrClasses.length || index < 0)
			{
				return;
			}
			
			// remove the old class
			objElement.removeClass(currClass);
			// new class
			var newClass = arrClasses[index];
			// apply new class
			objElement.attr('data-grid',newClass);
			objElement.parent('.column').attr('data-grid',newClass);
			
			// apply to parent div.columns
			objElement.closest('.column').removeClass(currClass);
			objElement.closest('.column').addClass(newClass);
			
			// update infobox
			objElement.find('.autogrid .infobox').html(newClass);
			
			// cancel running timeouts
			if( varTimeout > 0 )
			{
				clearTimeout(varTimeout);
			}

			varTimeout = setTimeout(function() 
			{
				// stop running request
				if( objRequest !== null && objRequest != undefined )
				{
					objRequest.abort();
				}

				objRequest = jQuery.ajax(
				{
				url: location.href,
				method:'GET',
				data: {'autogrid':1,'elem':<?= $this->id; ?>,'class':newClass},
				complete: function()
				{
					// trigger event
					jQuery(document).trigger('grid_resize',{'id':<?= $this->id; ?>,'grid':newClass});
				}
				});
			}, intDelay);		
		});
	}
});

/* ]]> */
</script>
<?php endif; ?>


<?php if( in_array($this->type, $GLOBALS['PCT_AUTOGRID']['startElements']) ): ?>
<script>
/* <![CDATA[ */
	
/**
 * Toggle visibility for a whole AG block
 */
jQuery(document).ready(function() 
{
	var objElement = jQuery('#li_<?= $this->id; ?>');
	if(objElement.length < 1)
	{
		return;
	}

	objButton = objElement.find('.toggle');
	// remove contaos onclick Ajax
	objButton.removeAttr('onclick');
	//objButton.attr('onclick="Backend.getScrollOffset()"');

	var arrElements = [objElement];
	
	<?php // find elements in block 
	$objElements = \PCT\AutoGrid\Models\ContentModel::findAllByStartId( $this->id );
	if( $objElements !== null ): ?>
	<?php foreach( $objElements as $model ): ?>
	arrElements.push( jQuery('#li_<?= $model->id; ?>') );
	<?php endforeach; ?>
	<?php endif; ?>
	
	var src_visible = AjaxRequest.themePath + '<?= (version_compare(VERSION,'4.4','>=') ? 'icons/' : '') ?>visible.<?= (version_compare(VERSION,'4.4','>=') ? 'svg' : 'gif') ?>';
	var src_invisible = AjaxRequest.themePath + '<?= (version_compare(VERSION,'4.4','>=') ? 'icons/' : '') ?>invisible.<?= (version_compare(VERSION,'4.4','>=') ? 'svg' : 'gif') ?>';

	objButton.click(function(e)
	{
		e.preventDefault();
		
		var _this = jQuery(this);

		// invoke the Backend methods
		Backend.getScrollOffset();

		var state = parseInt( _this.find('img').attr('data-state') );
			
			// toggle the img src and element state
		if( state < 1 )
		{
			jQuery.each(arrElements, function(i,elem)
			{
				icon = elem.find('.toggle img');
				icon.attr('src', src_visible);
				icon.attr('data-state',1);
				elem.find('.cte_type').removeClass('unpublished');
				elem.find('.cte_type').addClass('published');
			});
		}
		else
		{
			jQuery.each(arrElements, function(i,elem)
			{
				icon = elem.find('.toggle img');
				icon.attr('src', src_invisible);
				icon.attr('data-state',0);
				elem.find('.cte_type').removeClass('published');
				elem.find('.cte_type').addClass('unpublished');
			});
		}

		
		// send ajax
		jQuery.ajax(
		{
			url: location.href,
			method:'GET',
			data: {'action':'toggleAutoGridBlockVisibility','elem':<?= $this->id; ?>,'state':state}
		});
	});
});
/* ]]> */
</script>
<?php endif; ?>
	
<?php if($this->context == 'body'): ?>
<script>
/* <![CDATA[ */

<?php if(is_array($GLOBALS['PCT_AUTOGRID']['autogridGridPreviews']) && !empty($GLOBALS['PCT_AUTOGRID']['autogridGridPreviews']) ): ?>
/**
 * Build Grid [flex, grid] preview
 */
jQuery(document).ready(function() 
{
	<?php foreach($GLOBALS['PCT_AUTOGRID']['autogridGridPreviews'] as $data): ?>
	var objStart = jQuery('#li_<?= $data['start']; ?>');
	var objStop = jQuery('#li_<?= $data['stop']; ?>');
	var container = jQuery('<div class="grid_preview"><?= $data['html']; ?></div>').insertBefore(objStart);
	
	<?php foreach($data['ids'] as $id): ?>
	var placeholder = jQuery('.placeholder[data-id="<?= $id; ?>"]');
	var tl_content = jQuery('#li_<?= $id; ?>');
	//tl_content.appendTo(placeholder);
	placeholder.replaceWith(tl_content);
	<?php endforeach; ?>
	
	//objStart.remove();
	//objStop.remove();
	//objBetween.remove();	
	<?php endforeach; ?>

	// add body class
	jQuery('body').addClass('grid_preview_active');
});
<?php endif; ?>


/**
 * Add a body class with the current user action as class name
 */
jQuery(document).ready(function() 
{
	<?php 
	$objSession = \Contao\Session::getInstance();
	if( version_compare(VERSION, '4.4', '>=') )
	{
		$objSession = \Contao\System::getContainer()->get('session');
	}
	$arrClipboard = $objSession->get('CLIPBOARD');
	$arrClass = array( $arrClipboard[ \Contao\Input::get('table') ]['mode'] );
	
	if( \Contao\Input::get('act') != '' )
	{
		$arrClass[] = \Contao\Input::get('act');
	}
	
	if( \Contao\Input::get('mode') != '' )
	{
		$arrClass[] = \Contao\Input::get('mode');
	}
	
	$arrClass = array_unique(array_filter($arrClass));
	?>
	
	<?php if( empty($arrClass) === false ): ?>
	jQuery('body').addClass('<?= \implode(' ', $arrClass); ?> ');
	<?php endif; ?>
});

/* ]]> */
</script>
<?php endif; ?>