<?php

$stylesheets = array();
$javascripts = array();
$loaded_js = array();
$loaded_stylesheets = array();

$default_javascripts = 
array(
	'jquery' => array ('path' => default_url.'includes/js/jquery-3.1.0.min.js', 'ver' => '3.1.0', 'dependency' => array(), 'footer' => false),
	'bootstrap' => array ('path' => default_url.'includes/js/bootstrap.min.js', 'ver' => '3.3.7', 'dependency' => array('jquery'), 'footer' => false),
	'pace' => array ('path' => default_url.'includes/js/pace.min.js', 'ver' => '1.0.0', 'dependency' => array(), 'footer' => false)
);

$default_stylesheets =
array(
	'bootstrap' => array ('path' => default_url.'includes/css/bootstrap.min.css', 'ver' => '3.3.7'),
	'drea-styles' => array ('path' => default_url.'includes/css/drea-styles.css', 'ver' => '1.0.0')
);

function init_head($name = null) {
	//Add stylesheets
	global $stylesheets;
	foreach($stylesheets as $stylesheet) {
		echo sprintf('	<link rel="stylesheet" href="%s">'."\r\n", $stylesheet['path'].'?'.$stylesheet['ver']);
	}
	
	load_js();
}

function init_bottom() {
	load_js(true);
}

function load_js($footer = false) {
	//Add Javascript
	global $javascripts;
	global $default_javascripts;
	global $loaded_js;
	
	foreach($javascripts as $name => $javascript) {
		
		if(!is_array($javascript['dependency'])) {
			$javascript['dependency'] = array($javascript['dependency']);
		}
		if($javascript['footer'] == $footer) {
			if(!empty($javascript['dependency'])) {
				foreach($javascript['dependency'] as $dependent) {
					$loaded = true;
					if(!in_array($dependent, $loaded_js)) {
						//attempt to load;
						if(array_key_exists($dependent,$default_javascripts)) {
							echo sprintf('	<script src="%s"></script>'."\r\n", $default_javascripts[$dependent]['path'].'?'.$default_javascripts[$dependent]['ver']);
							$loaded_js[] = $dependent;
						}
						if(array_key_exists($dependent,$javascripts)) {
							echo sprintf('	<script src="%s"></script>'."\r\n", $javascripts[$dependent]['path'].'?'.$javascripts[$dependent]['ver']);
							$loaded_js[] = $dependent;
						}
						if(!in_array($dependent, $loaded_js)) {
							$loaded = false;
						}
					}
					if($loaded){
						if(!in_array($name, $loaded_js)) {
							echo sprintf('	<script src="%s"></script>'."\r\n", $javascript['path'].'?'.$javascript['ver']);
							$loaded_js[] = $name;
						}
					}
				}
			} else {
				if(!in_array($name, $loaded_js)) {
					echo sprintf('	<script src="%s"></script>'."\r\n", $javascript['path'].'?'.$javascript['ver']);
					$loaded_js[] = $name;
				}
			}
		}
	}
}

function get_header($name = null) {
	$header = $name==null ? VIEWS_DIR.'/parts/header.php' : VIEWS_DIR.'/parts/header-'.$name.'.php';
	if(file_exists($header)) {
		require_once($header);
	}
}

function get_footer($name = null) {
	$footer = $name==null ? VIEWS_DIR.'/parts/footer.php' : VIEWS_DIR.'/parts/footer-'.$name.'.php';
	if(file_exists($footer)) {
		require_once($footer);
	}
}

function get_navigation() {
	if(file_exists(VIEWS_DIR.'/parts/navigation.php')) {
		require_once(VIEWS_DIR.'/parts/navigation.php');
	}
}

function load_view_functions() {
	if(file_exists(VIEWS_DIR.'/parts/functions.php')) {
		require_once(VIEWS_DIR.'/parts/functions.php');
	}
}

function return_404() {
	return array('controller' => 'default', 'params' => '404');
}

function enqueue_style($name, $path='', $ver='') {
	global $stylesheets;
	global $default_stylesheets;
	if($path == '') {
		if(array_key_exists($name,$default_stylesheets)) {
			$stylesheets[$name] = $default_stylesheets[$name];
		}
	} else {
		$stylesheets[$name] = array('path' => $path, 'ver' => $ver);
	}
}

function enqueue_script($name, $path='', $ver='', $footer=false, $dependency=array()) {
	global $javascripts;
	global $default_javascripts;
	if($path == '') {
		if(array_key_exists($name,$default_javascripts)) {
			$javascripts[$name] = $default_javascripts[$name];
		}
	} else {
		$javascripts[$name] = array('path' => $path, 'ver' => $ver, 'dependency' => $dependency, 'footer' => $footer);
	}
}