<?php

/**
 * Container for all theme specific includes and classes.
 *
 * This framework contains only what is installed on every site.
 * Custom post types, extra menus and similar should be added
 * to the theme functions.php file instead for each theme.
 * This ensures that the framework can be overwritten at any
 * time with a new version.
 *
 * @since Ilmenite 1.0
 * @author Erik Bernskiold
 * @version 1.0
 * @package Ilmenite Framework
 **/

if(!class_exists('Ilmenite_Framework')) :

/**
 * Used to hold all framework options
 *
 * @since Ilmenite 1.0
 **/
class Ilmenite_Framework {
	
	/**
	 * Initializes theme framework. Load required files and
	 * call necessary functions.
	 *
	 * @since Ilmenite 1.0
	 **/
	function init($options) {
		
		// Define theme constants
		$this->constants($options);
		
		// Add WordPress default add_theme_support
		add_action('after_setup_theme', array(&$this, 'theme_support'));
		
		// Add localization support
		add_action('init', array(&$this, 'language'));
		
		// Load Ilmenite functions
		$this->functions();
		
		// Load Ilmenite shortcodes
		$this->shortcodes();
		
		// Load custom dashboard widgets
		$this->dashboard_widgets();
		
		// Load custom widgets
		add_action('widgets_init', array(&$this, 'widgets'));
		
		// Load Ilmenite admin files
		$this->admin();
		
		// Load various fixes
		require_once(THEME_FIXES . '/general_fixes.php');
		
		//
	}
	
	/**
	 * Defines constants: paths etc. for use in the theme
	 *
	 * @since Ilmenite 1.0
	 **/
	function constants($options) {
		// Theme Constants
		define('THEME_NAME', $options['theme_name']); // Name of the theme
		define('THEME_SLUG', $options['theme_slug']); // Slug of the theme
		
		// Theme Main Directory Constants
		define('THEME_DIR', get_stylesheet_directory()); // Path to theme directory
		define('THEME_URI', get_stylesheet_directory_uri()); // URI to theme directory
		
		// Framework Constants
		define('THEME_FRAMEWORK', THEME_DIR . '/framework'); // Path to framework folder
		define('THEME_ADMIN', THEME_FRAMEWORK . '/admin'); // Path to framework admin folder
		
		define('THEME_ADMIN_URI', THEME_URI . '/framework/admin'); // URI to framework admin folder
		
		// Constants for Sub-folders in the framework folder
		define('THEME_WIDGETS', THEME_FRAMEWORK . '/widgets'); // Path to custom widgets
		define('THEME_DASHBOARD_WIDGETS', THEME_FRAMEWORK . '/dashboard-widgets'); // Path to custom dashboard widgets
		define('THEME_FUNCTIONS', THEME_FRAMEWORK . '/functions'); // Path to theme functions
		define('THEME_SHORTCODES', THEME_FRAMEWORK . '/shortcodes'); // Path to shortcodes
		define('THEME_FIXES', THEME_FRAMEWORK . '/fixes'); // Path to fixes
		
		// Constants for Theme Admin Panel
		define('THEME_ADMIN_METABOXES', THEME_ADMIN . '/metaboxes'); // Path to metaboxes
		define('THEME_ADMIN_DOCS', THEME_ADMIN . '/docs'); // Path to theme docs
		define('THEME_ADMIN_OPTIONS', THEME_ADMIN . '/options'); // Path to theme options files
		define('THEME_ADMIN_FUNCTIONS', THEME_ADMIN . '/functions'); // Path to theme admin functions
		
		define('THEME_ADMIN_ASSETS_URI', THEME_ADMIN . '/assets'); // Path to admin panel assets
		
		// Theme Style Constants
		define('THEME_INCLUDES', THEME_URI . '/inc'); // URI to theme inc folder
		define('THEME_IMAGES', THEME_URI . '/images'); // URI to theme images folder
		define('THEME_CSS', THEME_URI . '/css'); // URI to css folder
		define('THEME_JS', THEME_URI . '/js'); // URI to javascripts folder
	}
	
	/**
	 * Add theme support for: add_theme_support variables
	 * Also registers default sidebar
	 *
	 * @since Ilmenite 1.0
	 **/
	function theme_support() {
		if(function_exists('add_theme_support')) {
			
			// Post thumbnails are added. If they are not used in a theme,
			// they can be disabled from the functions.php again
			add_theme_support('post-thumbnails');
			
			// Enable Built-in Navigation menus
			add_theme_support('menus');
			
			// Register one default navigation menu
			register_nav_menus(array(
				'primary-menu' => __(THEME_NAME . ' Navigation', 'ilmenite_admin'),
			));
			
			// Adds post and comment RSS feeds into the <head> auomatically
			add_theme_support('automatic-feed-links');
			
			// Add support for custom editor style
			add_editor_style();
			
			// Add support for custom backgrounds
			add_custom_background();
		}
		
		if(function_exists('register_sidebar')) {
			
			// Sets up a default sidebar.
			// Other sidebars should be registered in the functions.php file
			register_sidebar(array(
				'id' => 'sidebar',
				'name' => 'Sidebar',
			    'before_widget' => '<div class="sidebar-widget">',
			    'after_widget' => '</div>',
			    'before_title' => '<h2 class="widget-title">',
			    'after_title' => '</h2>',
			));
		
		}
	}
	
	/**
	 * Loads core ilmenite functions.
	 *
	 * @since Ilmenite 1.0
	 **/
	function functions() {
		require_once(THEME_FUNCTIONS . '/common.php');
		require_once(THEME_FUNCTIONS . '/ui.php');
		require_once(THEME_FUNCTIONS . '/head.php');
	}
	
	/**
	 * Loads admin dashboard widgets
	 *
	 * @since Ilmenite 1.0
	 **/
	function dashboard_widgets() {
		require_once(THEME_DASHBOARD_WIDGETS . '/theme-welcome.php'); // Theme welcome widget.
		require_once(THEME_DASHBOARD_WIDGETS . '/xld-blog-rss.php'); // XLD Studios blog RSS widget.
	}
	
	/**
	 * Registered and includes custom framework widgets.
	 * Currently not used.
	 *
	 * @since Ilmenite 1.0
	 **/
	function widgets() {
	/* NOT ACTIVE YET
		// Loading each widgets
		require_once(THEME_WIDGETS . '/search.php'); // Custom search widget
		require_once(THEME_WIDGETS . '/twitter.php'); // Custom twitter widget
		require_once(THEME_WIDGETS . '/facebook.php'); // Custom Facebook widget
		
		// Reigster each widget
		register_widget('Theme_Widget_Search');
		register_widget('Theme_Widget_Twitter');
		register_widget('Theme_Widget_Facebook');
		
	*/
	}
	
	/**
	 * Includes custom shortcode files
	 *
	 * @since Ilmenite 1.0
	 **/
	function shortcodes() {
		require_once(THEME_SHORTCODES . '/columns.php'); // Register columns shortcode
		
		add_filter('widget_text', 'do_shortcode'); // Adds shortcode capability to Widgets
	}
	
	/**
	 * Loads the admin panel with theme options
	 *
	 * Theme options are built by the Options Framework Theme
	 * http://wptheming.com/options-framework-theme/
	 *
	 * @since Ilmenite 1.0
	 **/
	function admin() {
	
		// Inludes the Options Framework Theme
		// http://wptheming.com/options-framework-theme/
		if ( !function_exists( 'optionsframework_init' ) ) {

		/* Set the file path based on whether the Options Framework Theme is a parent theme or child theme */
		if ( get_stylesheet_directory() == get_template_directory() ) {
			define('OPTIONS_FRAMEWORK_URL', get_template_directory() . '/framework/admin/');
			define('OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/framework/admin/');
		} else {
			define('OPTIONS_FRAMEWORK_URL', get_stylesheet_directory() . '/framework/admin/');
			define('OPTIONS_FRAMEWORK_DIRECTORY', get_stylesheet_directory_uri() . '/framework/admin/');
		}
		
		require_once (OPTIONS_FRAMEWORK_URL . 'options-framework.php');
		
		}
	}
	
	/**
	 * Makes theme available for the built-in localization
	 *
	 * @since Ilmenite 1.0
	 **/
	function language(){
		$locale = get_locale();
		if (is_admin()) {
			load_theme_textdomain( 'tenacity_admin', THEME_ADMIN . '/languages' );
			$locale_file = THEME_ADMIN . "/languages/$locale.php";
		}else{
			load_theme_textdomain( 'tenacity', THEME_DIR . '/languages' );
			$locale_file = THEME_DIR . "/languages/$locale.php";
		}
		if ( is_readable( $locale_file ) ){
			require_once( $locale_file );
		}
	}
	
} // End: class Theme() {}

endif; // End: if(!class_exists('Theme') :