<?php
//Define the wp_content DIR for backward compatibility
if (!defined('WP_CONTENT_URL'))
	define('WP_CONTENT_URL', get_option('site_url').'/wp-content');
if (!defined('WP_CONTENT_DIR'))
	define('WP_CONTENT_DIR', ABSPATH.'/wp-content');

//Define the content width for images
$max_width = 596;
$GLOBALS['content_width'] = 596;
    
//A function to include files throughout the theme
//It checks to see if the file exists first, so as to avoid error messages.
function get_template_file($filename) {
        if (file_exists(get_stylesheet_directory()."/$filename"))
            include(get_stylesheet_directory()."/$filename");
        else if (file_exists(get_template_directory()."/$filename"))
            include(get_template_directory()."/$filename");
}

if ( function_exists( 'add_theme_support' ) ) {
	// Add 3.0+ menu support
	add_theme_support( 'menus' );
}

//A function to add the custom body background image
add_action('wp_head','flexx_body_bg');
function flexx_body_bg() {
	global $wp_theme_options;
	
	$options = array( 'background_image', 'background_color', 'background_position', 'background_attachment', 'background_repeat' );
	
?>
	<style type="text/css">
		body {
			<?php if ( 'custom_color' == $wp_theme_options['background_option'] ) : ?>
				background-color: <?php echo $wp_theme_options['background_color']; ?>;
				background-image: none;
			<?php else : ?>
				<?php foreach ( (array) $options as $option ) : ?>
					<?php if ( ! empty( $wp_theme_options[$option] ) ) : ?>
						<?php if ( 'background_image' == $option ) : ?>
							<?php echo str_replace( '_', '-', $option ); ?>: url(<?php echo $wp_theme_options[$option]; ?>);
						<?php else : ?>
							<?php echo str_replace( '_', '-', $option ); ?>: <?php echo $wp_theme_options[$option]; ?>;
						<?php endif; ?>
					<?php endif; ?>
				<?php endforeach; ?>
			<?php endif; ?>
		}
	</style>
<?php
	
}


//Theme Options code
get_template_file("lib/theme-options/theme-options.php");

//FlexLayout Code
get_template_file("lib/flexlayout.php");

//Billboard Code
get_template_file("lib/billboard/billboard.php");

//Feedburner Widget Code
get_template_file("lib/feedburner-widget/feedburner-widget.php");


add_action( 'ithemes_load_plugins', 'ithemes_functions_after_init' );
function ithemes_functions_after_init() {
	//Include Tutorials Page
	get_template_file("lib/tutorials/tutorials.php");
	
	//FlexLayout Editor Code
	include(TEMPLATEPATH.'/lib/flexx-layout-editor/flexx-layout-editor.php');
	
	//Featured Image code
	get_template_file("lib/featured-images2/featured-images2.php");
	
	//Contact Page Template code
	get_template_file("lib/contact-page-plugin/contact-page-plugin.php");
}

add_filter( 'it_featured_images_options', 'it_filter_featured_images_options' );
function it_filter_featured_images_options( $options ) {
	$options['width'] = 'container_width';
	$options['height'] = 200;
	
	return $options;
}


//Register Page Template Widget Areas
ithemes_flexx_register_sidebar( 'Page Wide Sidebar Top' );
ithemes_flexx_register_sidebar( 'Page Wide Sidebar Bottom' );
ithemes_flexx_register_sidebar( 'Page Skinny Left Sidebar' );
ithemes_flexx_register_sidebar( 'Page Skinny Right Sidebar' );

///Tracking/Analytics Code
function print_tracking() {
	global $wp_theme_options;
	echo stripslashes($wp_theme_options['tracking']);
}
global $wp_theme_options;
if ($wp_theme_options['tracking_pos'] == "header")
	add_action('wp_head', 'print_tracking');
else //default
	add_action('flexx_footer_stats', 'print_tracking');

// Function to allow theme files to be used in Shopp (if Shopp is present)
function ithemes_load_shopp() {
	global $Shopp;
	
	if ( isset( $Shopp ) && function_exists( 'template_path' ) ) {
		$template_path = addslashes( template_path( STYLESHEETPATH . '/shopp' ) );
		if ( ! empty( $Shopp->Settings ) )
			$Shopp->Settings->save( 'theme_templates', $template_path );
	}
}
add_action ( 'switch_theme', 'ithemes_load_shopp' );

// Function to insert edit post/page link
function ithemes_insert_edit_link() {
	global $wp_query;
	edit_post_link('Edit this ' . ucwords( $wp_query->post->post_type ), '( ', ' )');
}
?>
