<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

$callback = $this->getRequest()->request('callback', 'string', '');
$callback = preg_replace('#[^a-z0-9.]#i', '', $callback);

if ($callback) {
	ob_start();
	?>
jQuery(function($) {
	$('.articlelist').delegate('a', 'click', function() {
		var self     = $(this);
		var id       = self.data('id');
		var name     = self.data('name');
		var link     = 'sally://' + id + '/';
		var callback = '<?php echo $callback ?>' in window.opener ? window.opener['<?php echo $callback ?>'] : null;
		var close    = true;
		var fullName = name.length === 0 ? <?php echo json_encode(t('unnamed_article')) ?> + ' [' + id + ']' : name;

		// execute callback (link widget, WYMeditor, ...)
		if (callback !== null) {
			close = callback(id, fullName, name, link, window);
		}

		// close the popup
		if (close) window.close();
		return false;
	});
});
	<?php
	$javascript = ob_get_clean();
	sly_Core::getLayout()->addJavaScript($javascript);
}
