<?php
/**
 * bbPress Theme Options
 *
 * @since tdframework 1.0
 */

// BuddyPress scripts and styless
function theme_bp_enqueue_scripts() {
	wp_dequeue_style( 'bp-legacy-css');
	wp_enqueue_style('theme-buddypress', FRAMWORK_URI . 'addons/buddypress/theme-buddypress.css');
}
add_action('wp_enqueue_scripts', 'theme_bp_enqueue_scripts', 999999);


function locate_asset_in_stack( $file, $type = 'css' ) {
		// Child, parent, theme compat
		$locations = array();

		// No need to check child if template == stylesheet
		if ( is_child_theme() ) {
			$locations['bp-child'] = array(
				'dir' => get_stylesheet_directory(),
				'uri' => get_stylesheet_directory_uri(),
			);
		}

		$locations['bp-parent'] = array(
			'dir' => get_template_directory(),
			'uri' => get_template_directory_uri(),
		);

		$locations['bp-legacy'] = array(
			'dir' => bp_get_theme_compat_dir(),
			'uri' => bp_get_theme_compat_url(),
		);

		// Subdirectories within the top-level $locations directories
		$subdirs = array(
			'buddypress/' . $type,
			'community/' . $type,
			$type,
		);

		$retval = array();

		foreach ( $locations as $location_type => $location ) {
			foreach ( $subdirs as $subdir ) {
				if ( file_exists( trailingslashit( $location['dir'] ) . trailingslashit( $subdir ) . $file ) ) {
					$retval['location'] = trailingslashit( $location['uri'] ) . trailingslashit( $subdir ) . $file;
					$retval['handle']   = $location_type . '-' . $type;
					break 2;
				}
			}
		}

		return $retval;
	}


// Adds BuddyPress layout option page
//
function theme_bp_layoutoptions() {
	global $core_theme_options_handler;
	global $core_layout_default;

	$options = new CoreOptionGroup('z-bp-layouts', __('BuddyPress', THEME_SLUG), __('Use this page to define the layouts of BuddyPress pages.', THEME_SLUG), FRAMWORK_URI . 'addons/buddypress/options-buddypress.png');
	$core_theme_options_handler->group_add($options);

	$layouts = array(
		'layout-bp' => __('BuddyPress page', THEME_SLUG)
	);
	foreach ($layouts as $key => $value) {
		$section = new CoreOptionSection($key, $value);
		$options->section_add($section);
		$section->option_add(new CoreOption($key, null, 'layout', null, $core_layout_default));

		// Slider
		$section->option_add(new CoreOption('slider_'.$key, __('Slider', THEME_SLUG), 'sliders', __('The slider will be displayed at the top of the', THEME_SLUG) . ' ' . $value . ' .'));

		$section->option_add(new CoreOption($key.'_background', __('Background', THEME_SLUG), 'image'));

		$section->option_add(new CoreOption($key.'_colorscheme', __('Color scheme', THEME_SLUG), 'colorschemes-list'));

		$section->option_add(new CoreOption('custom_content_' .$key , __('Slogan block', THEME_SLUG), 'text-multiline', __('Any HTML put here will be included in it\'s own block above the content.', THEME_SLUG)));

		$section->option_add(new CoreOption('custom_content_' .$key.'_bg' , __('Slogan background', THEME_SLUG), 'image', __('A background for the', THEME_SLUG) . ' '.$value. ' ' . __('slogan block above the content.', THEME_SLUG)));
	}
}
add_action('after_setup_theme', 'theme_bp_layoutoptions');


function theme_bp_layout($layout) {
	if ( !is_buddypress() ) {
		return $layout;
	} else {
		return core_options_get('layout-bp');
	}
}
add_filter('core_layout', 'theme_bp_layout', 101);

// Get bbPress Sliders
function theme_bp_slider($slider){

	if ( bp_is_profile_component() ||bp_is_activity_component() || bp_is_blogs_component() || bp_is_messages_component() || bp_is_friends_component() || bp_is_groups_component() ||bp_is_settings_component() ) {

		$post_type = 'theme';
		$slug = '_layout-bp';

	}

	else{
		return $slider;
	}
	return core_options_get('slider'.$slug, $post_type);
}
add_filter('slider_area', 'theme_bp_slider');

// Get bbPress background image
function theme_bp_get_background($backgroundimage){
	if ( !is_buddypress() ) {
		return $backgroundimage;
	} else {
		return core_options_get('layout-bp_background', 'theme');
	}
}
add_filter('layout_background', 'theme_bp_get_background');

//Get bbPress colorscheme
function theme_bp_colorscheme($scheme){
	if ( !is_buddypress() ) {
		return $scheme;
	} else {
		return core_options_get('layout-bp_colorscheme', 'theme');
	}
}
add_filter('color_scheme', 'theme_bp_colorscheme');

// Get bbPress custom content
function theme_bp_custom_content($content){
	if( !is_buddypress() ) {
		return $content;
	} else {
		return core_options_get('custom_content_layout-bp', 'theme');
	}
}
add_filter('custom_content', 'theme_bp_custom_content');

// Get bbPress custom content background
function theme_bp_custom_content_bg($slogan_bg){
	if( !is_buddypress() ) {
		return $slogan_bg;
	} else {
		$slogan_bg = core_options_get('custom_content_layout-bp_bg', 'theme');
	}
	return $slogan_bg;
}
add_filter('slogan_background', 'theme_bp_custom_content_bg');

?>