<?php
/*
 * Styles and scripts registration and enqueuing
 *
 * @package nirvana
 * @subpackage Functions
 */

/* = Loading CSS
--------------------------------------*/
 
/* Register all styles */
function nirvana_register_styles() {
	global $nirvanas;
	foreach ($nirvanas as $key => $value) { ${"$key"} = $value ;}

	wp_register_style( 'nirvanas', get_stylesheet_uri() ); //style.css
	if($nirvana_mobile=="Enable") { wp_register_style( 'nirvana-mobile', get_template_directory_uri() . '/styles/style-mobile.css' );} //mobile
	if($nirvana_frontpage=="Enable" ) { wp_register_style( 'nirvana-frontpage', get_template_directory_uri() . '/styles/style-frontpage.css' );}// frontpage
	/* Google fonts */
	if($nirvana_googlefont) wp_register_style( 'nirvana_googlefont', esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+', $nirvana_googlefont )));
	if($nirvana_googlefonttitle) wp_register_style( 'nirvana_googlefonttitle', esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+',$nirvana_googlefonttitle )));
	if($nirvana_googlefontside) wp_register_style( 'nirvana_googlefontside',esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+',$nirvana_googlefontside )));
	if($nirvana_headingsgooglefont) wp_register_style( 'nirvana_headingsgooglefont', esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+',$nirvana_headingsgooglefont )));
	if($nirvana_sitetitlegooglefont) wp_register_style( 'nirvana_sitetitlegooglefont', esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+',$nirvana_sitetitlegooglefont )));
	if($nirvana_menugooglefont) wp_register_style( 'nirvana_menugooglefont', esc_attr("//fonts.googleapis.com/css?family=".preg_replace( '/\s+/', '+',$nirvana_menugooglefont )));

}
add_action('init', 'nirvana_register_styles' );


/* Enqueue styles except PP styles*/
function nirvana_enqueue_styles() {
	global $nirvanas;
	foreach ($nirvanas as $key => $value) { ${"$key"} = $value ;}
	wp_enqueue_style( 'nirvanas');//style.css
	wp_enqueue_style( 'nirvana_googlefont');
	wp_enqueue_style( 'nirvana_googlefonttitle');
	wp_enqueue_style( 'nirvana_googlefontside');
	wp_enqueue_style( 'nirvana_headingsgooglefont');
	wp_enqueue_style( 'nirvana_sitetitlegooglefont');
	wp_enqueue_style( 'nirvana_menugooglefont');
}
if( !is_admin() ) { add_action('wp_head', 'nirvana_enqueue_styles', 5 ); }


/* Enqueue all custom styles */
function nirvana_styles_echo() {
	global $nirvanas;
	foreach ($nirvanas as $key => $value) { ${"$key"} = $value ;}
	
	echo preg_replace("/[\n\r\t\s]+/"," " ,nirvana_custom_styles())."\n"; // Custom-styles.php
	if ($nirvana_frontpage == "Enable" && is_front_page() && 'posts' == get_option( 'show_on_front' ) ) { echo preg_replace("/[\n\r\t\s]+/"," " ,nirvana_presentation_css())."\n";} // PP styles also in custom-styles.php
	echo preg_replace("/[\n\r\t\s]+/"," " ,nirvana_customcss())."\n"; // User custom CSS
}
add_action('wp_head', 'nirvana_styles_echo', 20);

/* Enqueue PP styles */
function nirvana_enqueue_frontpage() {
	global $nirvanas;
	foreach ($nirvanas as $key => $value) { ${"$key"} = $value ;}
	if (($nirvana_frontpage=="Enable") && is_front_page() && 'posts' == get_option( 'show_on_front' )) { wp_enqueue_style( 'nirvana-frontpage' ); }
}
if( !is_admin() ) { add_action('wp_head', 'nirvana_enqueue_frontpage', 21 ); }


/* Enqueue mobile styles */
function nirvana_load_mobile_css() {
	global $nirvanas;
	foreach ($nirvanas as $key => $value) {${"$key"} = $value ;}
	if ($nirvana_mobile=="Enable") {wp_enqueue_style( 'nirvana-mobile' );}
}
add_action ('wp_head','nirvana_load_mobile_css', 30);

/* = Loading JS
--------------------------------------*/


// User Custom JS
add_action('wp_head', 'nirvana_customjs', 35 );

// Scripts loading and hook into wp_enque_scripts
function nirvana_scripts_method() {
global $nirvanas;
foreach ($nirvanas as $key => $value) {
									${"$key"} = $value ;
									}
// If frontend - load the js for the menu and the social icons animations
	if ( !is_admin() ) {
		wp_register_script('cryout-frontend',get_template_directory_uri() . '/js/frontend.js', array('jquery') );
		wp_enqueue_script('cryout-frontend');
  		// If nirvana from page is enabled and the current page is home page - load the nivo slider js
		if($nirvana_frontpage == "Enable" && is_front_page()) {
			wp_register_script('cryout-nivoSlider',get_template_directory_uri() . '/js/nivo-slider.js', array('jquery'));
			wp_enqueue_script('cryout-nivoSlider');
		}
  	}

	/* We add some JavaScript to pages with the comment form
	 * to support sites with threaded comments (when in use).
	 */
	if ( is_singular() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );
}
if( !is_admin() ) { add_action('wp_enqueue_scripts', 'nirvana_scripts_method'); }
?>