<?php
class sidebarGenerator {
	var $sidebar_names = array();
	var $footer_sidebar_count = 0;
	var $footer_sidebar_names = array();
	
	function sidebarGenerator(){
		$this->sidebar_names = array(
			'home'=>__('Homepage Widget Area',THEME_ADMIN_DOMAIN),
			'page'=>__('Page Widget Area',THEME_ADMIN_DOMAIN),
			'blog'=>__('Blog Widget Area',THEME_ADMIN_DOMAIN),
			'portfolio' =>__('Portfolio Widget Area',THEME_ADMIN_DOMAIN),
		);
		if ( function_exists( 'is_woocommerce' )){
			$this->sidebar_names['shop'] = __('Shop Widget Area',THEME_ADMIN_DOMAIN);
			$this->sidebar_names['products'] = __('Product Widget Area',THEME_ADMIN_DOMAIN);
		}
		$this->footer_sidebar_names = array(
			__('First Footer Widget Area',THEME_ADMIN_DOMAIN),
			__('Second Footer Widget Area',THEME_ADMIN_DOMAIN),
			__('Third Footer Widget Area',THEME_ADMIN_DOMAIN),
			__('Fourth Footer Widget Area',THEME_ADMIN_DOMAIN),
			__('Fifth Footer Widget Area',THEME_ADMIN_DOMAIN),
			__('Sixth Footer Widget Area',THEME_ADMIN_DOMAIN),
		);

	}

	function register_sidebar(){
		foreach ($this->sidebar_names as $name){
			register_sidebar(array(
				'name' => $name,
				'description' => $name,
				'before_widget' => '<section id="%1$s" class="sidepanel %2$s">',
				'after_widget' => '</section>',
				'before_title' => '<h3 class="widgettitle">',
				'after_title' => '</h3>',
			));
		}
		
		//register footer sidebars
		foreach ($this->footer_sidebar_names as $name){
			register_sidebar(array(
				'name' =>  $name,
				'description' => $name,
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget' => '</section>',
				'before_title' => '<h3 class="widgettitle">',
				'after_title' => '</h3>',
			));
		}
		$top_area_type = theme_get_option('general','top_area_type');
		if($top_area_type == 'widget'){
			register_sidebar(array(
				'name' =>  __('Header Widget Area',THEME_ADMIN_DOMAIN),
				'description' => __('Header Widget Area',THEME_ADMIN_DOMAIN),
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget' => '</section>',
				'before_title' => '',
				'after_title' => '',
			));
		}

		$footer_right_area_type = theme_get_option('footer','footer_right_area_type');
		if($footer_right_area_type == 'widget'){
			register_sidebar(array(
				'name' =>  __('Sub Footer Widget Area',THEME_ADMIN_DOMAIN),
				'description' => __('Sub Footer Widget Area',THEME_ADMIN_DOMAIN),
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget' => '</section>',
				'before_title' => '',
				'after_title' => '',
			));
		}
		//register custom sidebars
		$custom_sidebars = theme_get_option('sidebar','sidebars');
		if(!empty($custom_sidebars)){
			$custom_sidebar_names = explode(',',$custom_sidebars);
			foreach ($custom_sidebar_names as $name){
				register_sidebar(array(
					'name' =>  $name,
					'description' => $name,
					'before_widget' => '<section id="%1$s" class="widget sidepanel %2$s">',
					'after_widget' => '</section>',
					'before_title' => '<h3 class="widgettitle">',
					'after_title' => '</h3>',
				));
			}
		}

	}
	
	function get_sidebar($post_id){
		$sidebar = '';
		if(is_active_sidebar(2)&&is_page()){
			$sidebar = $this->sidebar_names['page'];
		}
		if(is_active_sidebar(3) && get_option('page_for_posts') && is_home()){
			$sidebar = $this->sidebar_names['blog'];
			$blog_custom = theme_get_option('blog', 'sidebar');
			if(!empty($blog_custom)){
				$sidebar = $blog_custom;
			}
		}
		if( is_active_sidebar(1) && ( is_front_page() || (!get_option('page_on_front') && is_home()))){
			$sidebar = $this->sidebar_names['home'];
		}

		if(is_active_sidebar(3) && is_singular('post')){
			$sidebar = $this->sidebar_names['blog'];
		}elseif(is_active_sidebar(4) && is_singular('portfolio')){
			$sidebar = $this->sidebar_names['portfolio'];
		}

		if(is_active_sidebar(3) && ( is_search() || is_archive() )){
			$sidebar = $this->sidebar_names['blog'];
		}
		if(!empty($post_id)){
			$custom = get_post_meta($post_id, '_sidebar', true);
			if(!empty($custom)){
				$sidebar = $custom;
			}
		}
		if(class_exists( 'Woocommerce' ) && theme_get_option('woocommerce', 'woocommerce') && is_woocommerce()){
			$sidebar = $this->sidebar_names['shop'];
			if(is_product()){
				$sidebar = $this->sidebar_names['products'];
			}
			if(function_exists('is_post_type_archive')){
				if(is_post_type_archive('product')){
					$custom = theme_get_option('woocommerce','woocommerce_archive_sidebar');
					if(!empty($custom)){
						$sidebar = $custom;
					}else{
						$sidebar = $this->sidebar_names['shop'];
					}
				}
			}
			if(is_product_category()){
				$custom = theme_get_option('woocommerce','woocommerce_cat_sidebar');
				if(!empty($custom)){
					$sidebar = $custom;
				}else{
					$sidebar = $this->sidebar_names['products'];
				}
			}
			if(is_product_tag()){
				$custom = theme_get_option('woocommerce','woocommerce_tag_sidebar');
				if(!empty($custom)){
					$sidebar = $custom;
				}else{
					$sidebar = $this->sidebar_names['products'];
				}
			}
		}
		
		if(! empty($sidebar)){
			echo '<aside id="sidebar"><div class="sidebar-top"><div class="sidebar-bottom">';
			dynamic_sidebar($sidebar);
			echo '</div></div></aside>';
		}
	}
	
	function get_footer_sidebar(){
		dynamic_sidebar($this->footer_sidebar_names[$this->footer_sidebar_count]);
		$this->footer_sidebar_count++;
	}
}
global $_sidebarGenerator;
$_sidebarGenerator = new sidebarGenerator;

add_action('widgets_init', array($_sidebarGenerator,'register_sidebar'));

function sidebar_generator($function){
	global $_sidebarGenerator;
	$args = array_slice( func_get_args(), 1 );
	return call_user_func_array(array( &$_sidebarGenerator, $function ), $args );
}
