<?php
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);

add_action('wp_print_scripts', 'theme_woocommerce_scripts',12);
add_action('wp_print_styles', 'theme_woocommerce_styles',12);
add_action('after_setup_theme','theme_woocommerce_support',10);
add_action( 'woocommerce_before_main_content', 'theme_woocommerce_output_content_wrapper', 10);
add_action( 'woocommerce_after_main_content', 'theme_woocommerce_output_content_wrapper_end', 10);


add_action( 'woocommerce_before_shop_loop', 'theme_woocommerce_before_shop_loop', 10);
add_action( 'woocommerce_after_shop_loop', 'theme_woocommerce_after_shop_loop', 10);

add_action( 'woocommerce_before_shop_loop_item','theme_woocommerce_before_shop_loop_item',10);
add_action( 'woocommerce_after_shop_loop_item','theme_woocommerce_after_shop_loop_item',10);
add_action( 'woocommerce_before_subcategory','theme_woocommerce_before_shop_loop_item',10);
add_action( 'woocommerce_after_subcategory','theme_woocommerce_after_shop_loop_item',10);

add_filter( 'woocommerce_sale_flash',  'theme_sale_flash', 2, 20 );
add_filter( 'add_to_cart_fragments', 'theme_header_add_to_cart_fragment');
/*theme action*/
function theme_woocommerce_support(){
	add_theme_support('post-thumbnails', array('post', 'page', 'portfolio', 'slideshow','product'));
}
function theme_woocommerce_styles(){
	if((is_admin() && !is_shortcode_preview()) || 'wp-login.php' == basename($_SERVER['PHP_SELF'])){
		return;
	}
	wp_enqueue_style('theme-woocommerce', THEME_CSS.'/woocommerce.css', false, false, 'all');
}


function theme_woocommerce_scripts(){
	if((is_admin() && !is_shortcode_preview()) || 'wp-login.php' == basename($_SERVER['PHP_SELF'])){
		return;
	}
	$move_bottom = theme_get_option('advanced','move_bottom');
	wp_enqueue_script('jquery-textFill', THEME_JS.'/jquery-textfill-0.1.js', array('jquery'),false, $move_bottom);
	wp_enqueue_script('theme-woocommerce-js', THEME_JS.'/theme-woocommerce.js', array('jquery','jquery-textFill'), false, $move_bottom);
}


/*woocommerce layout fix*/
function theme_woocommerce_before_shop_loop(){
	$col = theme_get_option('woocommerce','woocommerce_product_list_columns');
	$column_class ='';
	switch($col){
	case 'two':
		$column_class = 'products_two_columns';
		break;
	case 'three':
		$column_class = 'products_three_columns';
		break;
	case 'four':
	default:
		$column_class = 'products_four_columns';
		break;
	}

	echo '<div class="'.$column_class.'">';
}

function theme_woocommerce_after_shop_loop(){
	echo '</div>';
}
function theme_woocommerce_before_shop_loop_item(){
	echo '<div class="product-item">';
}
function theme_woocommerce_after_shop_loop_item(){
	echo '</div>';
}
function theme_woocommerce_output_content_wrapper() {
	$id = theme_woocommerce_get_page_id();
	if(empty($id)){
		global $post;
		$id = $post->ID;
	}

	if(is_shop()){
		theme_generator('introduce');
		$layout = theme_get_option('woocommerce','shop_layout');
	}else{
		theme_generator('introduce',$id);
		$layout = get_post_meta($id, '_page_layout', true);
	}
	if( is_product() && (empty($layout) || $layout == 'default')){
		$layout = theme_get_option('woocommerce','product_layout');
	}
	if(empty($layout) || $layout == 'default'){
		$layout = theme_get_option('general','layout');
	}
	
	$fit = 'price-no-fit';
	if(theme_get_option('woocommerce','woocommerce_price_fit')){
		$fit = 'price-fit';
	}
?>
<div id="content-area">
	<div class="wrap-center <?php echo $fit;?> <?php if($layout=='right'):?>right_sidebar<?php endif;?><?php if($layout=='left'):?>left_sidebar<?php endif;?><?php if($layout=='full'):?>full_width<?php endif;?>">
		<div id="content" class="main">
<?php
	theme_woocommerce_breadcrumbs($id);
}

function theme_woocommerce_output_content_wrapper_end() {
	$id = theme_woocommerce_get_page_id();
	if(empty($id)){
		global $post;
		$id = $post->ID;
	}
	if(is_shop()){
		$layout = theme_get_option('woocommerce','shop_layout');
	}else{
		$layout = get_post_meta($id, '_page_layout', true);
	}
	if( is_product() && (empty($layout) || $layout == 'default')){
		$layout = theme_get_option('woocommerce','product_layout');
	}
	if(empty($layout) || $layout == 'default'){
		$layout = theme_get_option('general','layout');
	}
?>			
			<div class="clear"></div>
		</div>
		<?php if($layout != 'full') get_sidebar(); ?>
		<div class="clear"></div>
	</div>
</div>
<?PHP
}


function theme_woocommerce_get_page_id(){
	$id = '';
	if(is_shop())
		$id = woocommerce_get_page_id('shop');
	if(is_cart())
		$id = woocommerce_get_page_id('cart');
	return $id;
	
}

function theme_woocommerce_breadcrumbs($post_id = NULL) {
	$output = '';
	$tag = true;
	if($post_id =='blog')
		$tag = theme_get_option('blog','disable_breadcrumb');
	else
		$tag = get_post_meta($post_id, '_disable_breadcrumb', true);
	if(!theme_is_enabled($tag, theme_get_option('general','disable_breadcrumb'))){
		if ( function_exists( 'is_woocommerce' )  && is_woocommerce()){
			woocommerce_breadcrumb(array(
				'wrap_before' => '<section id="breadcrumbs" class="page-subtitle">',
				'wrap_after' => '</section>',
				'delimiter' 	=> '&raquo;',
				'home' => __( 'Home', THEME_FRONT_DOMAIN ),
			));
		}
	}


}

/*theme filter*/
// Handle cart in header fragment for ajax add to cart


function theme_header_add_to_cart_fragment( $fragments ) {
	global $woocommerce;
	
	$fragments['#top-menu-cart'] = '
	<li id="top-menu-cart" class="menu-item menu-item-type-post_type first"><a href="'.$woocommerce->cart->get_cart_url().'" title="'.__('View your shopping cart', THEME_ADMIN_DOMAIN).'">'.
						sprintf(_n('%d item &ndash; ', '%d items &ndash; ', $woocommerce->cart->cart_contents_count, THEME_ADMIN_DOMAIN),$woocommerce->cart->cart_contents_count).$woocommerce->cart->get_cart_total().'</a></li>
	';
	
	return $fragments;
	
}

// Fix sale style 
function theme_sale_flash(){
	return '<div class="onsale-wrap"><span class="onsale">'.__('Sale!', 'woocommerce').'</span></div>';
}
