<?php
if (!defined('WEBROOT')) { define('WEBROOT', $_SERVER['DOCUMENT_ROOT']); }

class ImpresarioJS
{
	/**
	 * Prints out the necessary JS header information and all the requested
	 * js files
	 *
	 */
	function get_js() 
	{
		header("Content-type: text/javascript");
		echo $this->_get_contents_of_all_requested_files();
	}
	
	/**
	 * Returns a string of the contents of all requested files, including the files in the
	 * requested sets, as well as the additional files requested individually
	 *
	 * @return string
	 */
	function _get_contents_of_all_requested_files()
	{
		$set_names	= (!empty($_GET['set']) ? $_GET['set'] : '');
		$set_names	= @trim(@str_replace(',.js', '', $set_names), ',');
		$set_names	= explode(',', $set_names);
		
		$set_files	= array();
		foreach ($set_names as $set)
		{
			// Strip off '.js' extension in case it is added accidentally to any of the set names.
			$set	= str_replace('.js', '', $set);
			$set_files	= array_merge($set_files, $this->_get_file_names_in_set($set));
		}
		return $this->_concatenate_contents_of_files_with_names($set_files);
	}
	

	
	/**
	 * Returns an array of the names of the files in the requested file set,
	 * if it exists. If an invalid file set name is passed, an empty array is returned.
	 *
	 * @param string $set
	 * @return array
	 */
	function _get_file_names_in_set($set='')
	{
		switch ($set) {
			case 'livesearch':
				return array(
					'/js/jsTools/livesearch/livesearch.js',
				);
				break;
			case 'suckerfish':
				return array(
					'/js/jsTools/miscellaneous/suckerfish.js',
				);
				break;
			case 'popup_windows':
				return array(
					'/js/jsTools/miscellaneous/popup_windows.js',
				);
				break;
			case 'overlib':
				return array(
					'/js/jsTools/overlib/overlib.js',
				);
				break;
			case 'load_unload_events':
				return array(
					'/js/jsTools/miscellaneous/load_unload_events.js',
				);
				break;
			case 'swfobject':
				return array(
					'/js/jsTools/swfobject/swfobject.js',
				);
				break;
			case 'calendars':
				return array(
				'/js/jsTools/admin/gettext.js',
				'/js/jsTools/admin/core.js',
				'/js/jsTools/admin/admin/RelatedObjectLookups.js',
				'/js/jsTools/admin/calendar.js',
				'/js/jsTools/admin/admin/RelatedObjectLookups.js',
				'/js/jsTools/admin/admin/DateTimeShortcuts.js',
				);
				break;
			case 'site':
				return array(
					'/js/scripts.js',
				);
				break;
			default:
				return array();
				break;
		}
	}
	
	/**
	 * Returns a string of the contents of the files whose names are in $set_files
	 * Searching both the current path and also the path defined in WEBROOT for the file names
	 *
	 * @param array $set_files
	 * @return string
	 */
	function _concatenate_contents_of_files_with_names($set_files = array())
	{
		$contents	= "";
		foreach ($set_files as $file)
		{
			$include	= false;
			if (file_exists($file))
			{
				$include			= true;
				$file_to_include	= $file;
			} 
			else if (file_exists(WEBROOT . $file))
			{
				$include			= true;
				$file_to_include	= WEBROOT . $file;
			}
			
			if ($include == true)
			{
				$contents	.=	"/************************************\n".
								" **\t Begin: ". basename($file_to_include) ."\n".
								" ************************************/\n\n";
				$contents	.= file_get_contents($file_to_include) ."\n";
				$contents	.=	"/************************************\n".
								" **\t End: ". basename($file_to_include) ."\n".
								" ************************************/\n\n";
			}
		}
		return $contents;	
	}
}

$js	= new ImpresarioJS();
$js->get_js();

?>