<?php
add_action( 'ithemes_init', 'ithemes_flexx_layout_loader' );

function ithemes_flexx_layout_loader() {
	global $wp_theme_options, $container_width, $content_width, $feature_top_width, $feature_bottom_width;
	
	foreach ( (array) $wp_theme_options['flexx'] as $name => $val )
		$GLOBALS[$name] = $val;
	
	extract( (array) $wp_theme_options['flexx'] );
	
	//How wide should the container be?
	if(preg_match('/^(2_right|2_left|split)$/', $sidebars)) $container_width = 960;
	else $container_width = 780;
	$container_width = intval( $container_width ); //just in case something screws up
	
	//How wide should the content be?
	$content_width = ($sidebars != 'none') ? 600 : 780;
	
	//How wide should each of the boxes in
	//the feature_top/feature_bottom sections be?
	if ( ! empty( $feature_top ) && ( $feature_top != 'none' ) )
		$feature_top_width = ( $container_width / $feature_top );
	if ( ! empty( $feature_bottom ) && ( $feature_bottom != 'none' ) )
		$feature_bottom_width = ( $container_width / $feature_bottom );
	
	//The container width
	add_action( 'container_style', 'flexx_container_style' );
	function flexx_container_style() {
		global $container_width;
		echo 'c' . $container_width . ' center wrap';
	}
	
	//The content width
	add_action('content_style', 'flexx_content_style');
	function flexx_content_style() {
		global $content_width;
		echo 'w'.$content_width.'-';
	}
	
	//What menu goes above the header (if any)
	add_action('above_header', 'flexx_menu_above_header');
	function flexx_menu_above_header() {
		global $above_header;
		
		if ( 'none' === $above_header )
			return;
		
		echo '<div id="above-header">';
		
		if ( 'pages' === $above_header )
			ithemes_flexx_render_menu( 'page', 'above' );
		else if ( 'categories' === $above_header )
			ithemes_flexx_render_menu( 'cat', 'above' );
		else if ( 'pages_categories' === $above_header ) {
			ithemes_flexx_render_menu( 'page', 'above' );
			ithemes_flexx_render_menu( 'cat', 'above' );
		}
		else if ( 'categories_pages' === $above_header ) {
			ithemes_flexx_render_menu( 'cat', 'above' );
			ithemes_flexx_render_menu( 'page', 'above' );
		}
		
		echo '</div>';
	}
	
	//What menu goes below the header (if any)
	add_action('below_header', 'flexx_menu_below_header');
	function flexx_menu_below_header() {
		global $below_header;
		
		if ( 'none' === $below_header )
			return;
		
		echo '<div id="below-header">';
		
		if ( 'pages' === $below_header )
			ithemes_flexx_render_menu( 'page', 'below' );
		else if ( 'categories' === $below_header )
			ithemes_flexx_render_menu( 'cat', 'below' );
		else if ( 'pages_categories' === $below_header ) {
			ithemes_flexx_render_menu( 'page', 'below' );
			ithemes_flexx_render_menu( 'cat', 'below' );
		}
		else if ( 'categories_pages' === $below_header ) {
			ithemes_flexx_render_menu( 'cat', 'below' );
			ithemes_flexx_render_menu( 'page', 'below' );
		}
		
		echo '</div>';
	}
	
	//Feature Top
	add_action('below_header', 'flexx_feature_top', 11);
	function flexx_feature_top() {
		global $feature_top;
		
		if ( ! empty( $feature_top ) && ( 'none' !== $feature_top ) )
			get_template_file( 'feature-top.php' );
	}
	//Feature Top Style
	add_action('feature_top_style','flexx_feature_top_style');
	function flexx_feature_top_style() {
		global $feature_top_width;
		if($feature_top_width) echo 'w'.$feature_top_width.'-';
	}
	
	//Feature Bottom
	add_action('above_footer', 'flexx_feature_bottom');
	function flexx_feature_bottom() {
		global $feature_bottom;
		
		if ( ! empty( $feature_bottom ) && ( 'none' !== $feature_bottom ) )
			get_template_file( 'feature-bottom.php' );
	}
	//Feature Bottom Style
	add_action( 'feature_bottom_style', 'flexx_feature_bottom_style' );
	function flexx_feature_bottom_style() {
		global $feature_bottom_width;
		if($feature_bottom_width) echo 'w'.$feature_bottom_width.'-';
	}
	
	//Sidebars
	add_action( 'before_content', 'flexx_before_content' );
	function flexx_before_content() {
		global $sidebars;
		if($sidebars == '2_left') get_sidebar();
		if(preg_match('/^(1_left|split)$/', $sidebars)) get_sidebar('left');
	}
	add_action( 'after_content', 'flexx_after_content' );
	function flexx_after_content() {
		global $sidebars;
		if($sidebars == '2_right') get_sidebar();
		if(preg_match('/^(1_right|split)$/', $sidebars)) get_sidebar('right');
	}
	
	//Register Widget Areas (conditionally)
	if ( function_exists( 'register_sidebar' ) ) {
		if ( preg_match( '/^(2_right|2_left)$/', $sidebars ) ) {
			ithemes_flexx_register_sidebar( 'Skinny Left Sidebar' );
			ithemes_flexx_register_sidebar( 'Skinny Right Sidebar' );
			ithemes_flexx_register_sidebar( 'Wide Sidebar Bottom' );
			ithemes_flexx_register_sidebar( 'Wide Sidebar Top' );
		}
		else if ( 'split' === $sidebars ) {
			ithemes_flexx_register_sidebar( 'Skinny Left Sidebar' );
			ithemes_flexx_register_sidebar( 'Skinny Right Sidebar' );
		}
		else if ( '1_left' === $sidebars )
			ithemes_flexx_register_sidebar( 'Skinny Left Sidebar' );
		else if ( '1_right' === $sidebars )
			ithemes_flexx_register_sidebar( 'Skinny Right Sidebar' );
		
		if ( '1' === $feature_top )
			ithemes_flexx_register_sidebar( 'Feature Top' );
		else if ( '2' === $feature_top ) {
			ithemes_flexx_register_sidebar( 'Feature Top Left' );
			ithemes_flexx_register_sidebar( 'Feature Top Right' );
		}
		else if ( '3' === $feature_top ) {
			ithemes_flexx_register_sidebar( 'Feature Top Left' );
			ithemes_flexx_register_sidebar( 'Feature Top Middle' );
			ithemes_flexx_register_sidebar( 'Feature Top Right' );
		}
		
		if ( '1' === $feature_bottom )
			ithemes_flexx_register_sidebar( 'Feature Bottom' );
		else if ( '2' === $feature_bottom ) {
			ithemes_flexx_register_sidebar( 'Feature Bottom Left' );
			ithemes_flexx_register_sidebar( 'Feature Bottom Right' );
		}
		else if ( '3' === $feature_bottom ) {
			ithemes_flexx_register_sidebar( 'Feature Bottom Left' );
			ithemes_flexx_register_sidebar( 'Feature Bottom Middle' );
			ithemes_flexx_register_sidebar( 'Feature Bottom Right' );
		}
	}
	
	//Register Menu Locations
	if ( function_exists( 'register_nav_menu' ) ) {
		if ( 'pages' === $above_header )
			register_nav_menu( 'page_above', 'Above Header' );
		else if ( 'categories' === $above_header )
			register_nav_menu( 'cat_above', 'Above Header' );
		else if ( 'pages_categories' === $above_header ) {
			register_nav_menu( 'page_above', 'Above Header 1' );
			register_nav_menu( 'cat_above', 'Above Header 2' );
		}
		else if ( 'categories_pages' === $above_header ) {
			register_nav_menu( 'cat_above', 'Above Header 1' );
			register_nav_menu( 'page_above', 'Above Header 2' );
		}
		
		if ( 'pages' === $below_header )
			register_nav_menu( 'page_below', 'Below Header' );
		else if ( 'categories' === $below_header )
			register_nav_menu( 'cat_below', 'Below Header' );
		else if ( 'pages_categories' === $below_header ) {
			register_nav_menu( 'page_below', 'Below Header 1' );
			register_nav_menu( 'cat_below', 'Below Header 2' );
		}
		else if ( 'categories_pages' === $below_header ) {
			register_nav_menu( 'cat_below', 'Below Header 1' );
			register_nav_menu( 'page_below', 'Below Header 2' );
		}
	}

}

function ithemes_flexx_register_sidebar( $name ) {
	$sidebar = array(
		'name'				=> $name,
		'before_widget'		=> '<div class="widget">',
		'after_widget'		=> '</div>',
		'before_title'		=> '<h4>',
		'after_title'		=> '</h4>'
	);
	
	register_sidebar( $sidebar );
}

if ( ! function_exists( 'wp_nav_menu' ) ) {
	function wp_nav_menu( $args ) {
		call_user_func( $args['fallback_cb'] );
	}
}

function ithemes_flexx_render_menu( $type, $position ) {
	wp_nav_menu( array( 'container_class' => 'menu-header', 'container_id' => "{$type}menu", 'theme_location' => "{$type}_{$position}", 'fallback_cb' => "ithemes_flexx_render_{$type}_menu" ) );
}

function ithemes_flexx_render_cat_menu() {
	get_template_file( 'menu-cats.php' );
}

function ithemes_flexx_render_page_menu() {
	get_template_file( 'menu-pages.php' );
}

?>
