<?php
/*-----------------------------------------------------------
   $Id:$

   modified eCommerce Shopsoftware
   http://www.modified-shop.org

   Copyright (c) 2009 - 2013 [www.modified-shop.org]
  -----------------------------------------------------------
   based on: (c) 2003 - 2006 XT-Commerce (general.js.php)
  -----------------------------------------------------------
   Released under the GNU General Public License
   -----------------------------------------------------------
*/
define('DIR_TMPL_JS', 'templates/'.CURRENT_TEMPLATE. '/javascript/');
// this javascriptfile get includes at the TOP of every template page in shop
// you can add your template specific js scripts here

$js_app_plain = DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/javascript/app.js';
$js_app_min = DIR_FS_CATALOG.'templates/'.CURRENT_TEMPLATE.'/javascript/app.min.js';

function compress_javascript($js_plain, $js_min, $js_filename = 'jquery.js', $js_filename_min = 'jquery.min.js'){
	// function should be moved to /includes/somefilename.php
	require_once(DIR_FS_EXTERNAL.'jsmin/min-js.php');

	$js_file = $js_filename;

	if (($js_content = file_get_contents($js_plain)) !== false) {

		$jSqueeze = new JSqueeze();
		$js_content = $jSqueeze->squeeze($js_content, true, false);
		try {
			if (file_put_contents($js_min, $js_content, LOCK_EX) !== false) {
				$js_file = $js_filename_min;
			}
		} catch (Exception $e) {
			trigger_error('func compress_javascript', $e->getMessage());
		}
	}
	return $js_file;

}


function auto_compress_javascript($js_plain, $js_min, $js_filename = 'jquery.js', $js_filename_min = 'jquery.min.js'){

	$fs_file = $fs_filename;
	
	//TODO sql:  INSERT INTO `configuration` (`configuration_id`, `configuration_key`, `configuration_value`, `configuration_group_id`, `sort_order`, `last_modified`, `date_added`, `use_function`, `set_function`) VALUES (NULL, 'COMPRESS_JAVASCRIPT', 'true', '14', '5', '2016-02-26 00:00:00', '2016-02-26 00:00:00', NULL, 'xtc_cfg_select_option(array(''true'', ''false''),');
	
		if (COMPRESS_JAVASCRIPT == 'true') {
		//require_once(DIR_FS_EXTERNAL.'compactor/compactor.php');
		if (file_exists($js_min)){
			if (filemtime($js_plain) > filemtime($js_min)) {
				compress_javascript($js_plain, $js_min, $js_filename, $js_filename_min);
			} else {
				//stylesheet.css is older than stylesheet.min.css => nothing to do
				$js_file = $js_filename_min;
			}
		} else{
			// stylesheet.min.css does not exist => create
			compress_javascript($js_plain, $js_min, $js_filename, $js_filename_min);
		}
	}

	return $js_file;

}

$js_file = auto_compress_javascript($js_app_plain,$js_app_min, 'app.js', 'app.min.js');

?>
<script src="<?php echo DIR_WS_BASE.DIR_TMPL_JS.$js_file; ?>" type="text/javascript"></script>

