/**
 * Include some admin files conditonally.
 *
 * @access public
 * @return void
 */
function woocommerce_admin_init()
{
    global $pagenow, $typenow, $post;
    ob_start();
    // Install - Add pages button
    if (!empty($_GET['install_woocommerce_pages'])) {
        require_once 'woocommerce-admin-install.php';
        woocommerce_create_pages();
        // We no longer need to install pages
        delete_option('_wc_needs_pages');
        delete_transient('_wc_activation_redirect');
        // What's new redirect
        wp_safe_redirect(admin_url('index.php?page=wc-about&wc-installed=true'));
        exit;
        // Skip button
    } elseif (!empty($_GET['skip_install_woocommerce_pages'])) {
        // We no longer need to install pages
        delete_option('_wc_needs_pages');
        delete_transient('_wc_activation_redirect');
        // Flush rules after install
        flush_rewrite_rules();
        // What's new redirect
        wp_safe_redirect(admin_url('index.php?page=wc-about'));
        exit;
        // Update button
    } elseif (!empty($_GET['do_update_woocommerce'])) {
        include_once 'woocommerce-admin-update.php';
        do_update_woocommerce();
        // Update complete
        delete_option('_wc_needs_pages');
        delete_option('_wc_needs_update');
        delete_transient('_wc_activation_redirect');
        // What's new redirect
        wp_safe_redirect(admin_url('index.php?page=wc-about&wc-updated=true'));
        exit;
    }
    // Includes
    if ($typenow == 'post' && isset($_GET['post']) && !empty($_GET['post'])) {
        $typenow = $post->post_type;
    } elseif (empty($typenow) && !empty($_GET['post'])) {
        $post = get_post($_GET['post']);
        $typenow = $post->post_type;
    }
    if ($pagenow == 'index.php') {
        include_once 'woocommerce-admin-dashboard.php';
    } elseif ($pagenow == 'admin.php' && isset($_GET['import'])) {
        include_once 'woocommerce-admin-import.php';
    } elseif ($pagenow == 'post-new.php' || $pagenow == 'post.php' || $pagenow == 'edit.php') {
        include_once 'post-types/writepanels/writepanels-init.php';
        if (in_array($typenow, array('product', 'shop_coupon', 'shop_order'))) {
            add_action('admin_print_styles', 'woocommerce_admin_help_tab');
        }
    } elseif ($pagenow == 'users.php' || $pagenow == 'user-edit.php' || $pagenow == 'profile.php') {
        include_once 'woocommerce-admin-users.php';
    }
    // Register importers
    if (defined('WP_LOAD_IMPORTERS')) {
        include_once 'importers/importers-init.php';
    }
}
    function woocommerce_settings()
    {
        global $woocommerce, $woocommerce_settings;
        $current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'general';
        if (isset($_POST) && $_POST) {
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'woocommerce-settings')) {
                die(__('Action failed. Please refresh the page and retry.', 'woothemes'));
            }
            switch ($current_tab) {
                case "general":
                case "pages":
                case "catalog":
                case "inventory":
                case "shipping":
                case "tax":
                case "email":
                case "integration":
                    woocommerce_update_options($woocommerce_settings[$current_tab]);
                    break;
            }
            do_action('woocommerce_update_options');
            do_action('woocommerce_update_options_' . $current_tab);
            // Backwards compat 1.4 <
            if ($current_tab == 'shipping') {
                do_action('woocommerce_update_options_shipping_methods');
            }
            flush_rewrite_rules(false);
            wp_redirect(add_query_arg('subtab', esc_attr(str_replace('#', '', $_POST['subtab'])), add_query_arg('saved', 'true', admin_url('admin.php?page=woocommerce&tab=' . $current_tab))));
        }
        if (isset($_GET['saved']) && $_GET['saved']) {
            echo '<div id="message" class="updated fade"><p><strong>' . __('Your settings have been saved.', 'woothemes') . '</strong></p></div>';
            flush_rewrite_rules(false);
            do_action('woocommerce_settings_saved');
        }
        // Install/page installer
        $install_complete = false;
        $show_page_installer = false;
        // Add pages button
        if (isset($_GET['install_woocommerce_pages']) && $_GET['install_woocommerce_pages']) {
            require_once 'woocommerce-admin-install.php';
            woocommerce_create_pages();
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
            // Skip button
        } elseif (isset($_GET['skip_install_woocommerce_pages']) && $_GET['skip_install_woocommerce_pages']) {
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
            // If we have just activated WooCommerce...
        } elseif (isset($_GET['installed']) && $_GET['installed']) {
            flush_rewrite_rules(false);
            if (get_option('woocommerce_shop_page_id')) {
                $install_complete = true;
            } else {
                $show_page_installer = true;
            }
            // If we havn't just installed, but page installed has not been skipped and shop page does not exist...
        } elseif (!get_option('skip_install_woocommerce_pages') && !get_option('woocommerce_shop_page_id')) {
            $show_page_installer = true;
        }
        if ($show_page_installer) {
            echo '<div id="message" class="updated fade">
    		<p><strong>' . __('Welcome to WooCommerce!', 'woothemes') . '</strong></p>
    		<p>' . __('WooCommerce requires several WordPress pages containing shortcodes in order to work correctly; these include Shop, Cart, Checkout and My Account. To add these pages automatically please click the \'Automatically add pages\' button below, otherwise you can set them up manually. See the \'Pages\' tab in settings for more information.', 'woothemes') . '</p>
    		<p><a href="' . remove_query_arg('installed', add_query_arg('install_woocommerce_pages', 'true')) . '" class="button button-primary">' . __('Automatically add pages', 'woothemes') . '</a> <a href="' . remove_query_arg('installed', add_query_arg('skip_install_woocommerce_pages', 'true')) . '" class="button">' . __('Skip setup', 'woothemes') . '</a></p>
    	</div>';
        } elseif ($install_complete) {
            echo '<div id="message" class="updated fade">
    		<p style="float:right;">' . __('Like WooCommerce? <a href="http://wordpress.org/extend/plugins/woocommerce/">Support us by leaving a rating!</a>', 'woothemes') . '</p>
    		<p><strong>' . __('WooCommerce has been installed and setup. Enjoy :)', 'woothemes') . '</strong></p>
    	</div>';
            flush_rewrite_rules(false);
        }
        ?>
	<div class="wrap woocommerce">
		<form method="post" id="mainform" action="">
			<div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br></div><h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
				<?php 
        $tabs = array('general' => __('General', 'woothemes'), 'pages' => __('Pages', 'woothemes'), 'catalog' => __('Catalog', 'woothemes'), 'inventory' => __('Inventory', 'woothemes'), 'tax' => __('Tax', 'woothemes'), 'shipping' => __('Shipping', 'woothemes'), 'payment_gateways' => __('Payment Gateways', 'woothemes'), 'email' => __('Emails', 'woothemes'), 'integration' => __('Integration', 'woothemes'));
        $tabs = apply_filters('woocommerce_settings_tabs_array', $tabs);
        foreach ($tabs as $name => $label) {
            echo '<a href="' . admin_url('admin.php?page=woocommerce&tab=' . $name) . '" class="nav-tab ';
            if ($current_tab == $name) {
                echo 'nav-tab-active';
            }
            echo '">' . $label . '</a>';
        }
        do_action('woocommerce_settings_tabs');
        ?>
			</h2>
			<?php 
        wp_nonce_field('woocommerce-settings', '_wpnonce', true, true);
        ?>
			<?php 
        switch ($current_tab) {
            case "general":
            case "pages":
            case "catalog":
            case "inventory":
            case "tax":
            case "email":
            case "integration":
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                break;
            case "shipping":
                $links = array('<a href="#shipping-options">' . __('Shipping Options', 'woothemes') . '</a>');
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    $title = isset($method->method_title) && $method->method_title ? ucwords($method->method_title) : ucwords($method->id);
                    $links[] = '<a href="#shipping-' . $method->id . '">' . $title . '</a>';
                }
                echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                // Gateway ordering
                echo '<div class="section" id="shipping-options">';
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                ?>
						<h3><?php 
                _e('Shipping Methods', 'woothemes');
                ?>
</h3>
						<p><?php 
                _e('Your activated shipping methods are listed below. Drag and drop rows to re-order them for display on the frontend.', 'woothemes');
                ?>
</p>
						<table class="wc_shipping widefat" cellspacing="0">
							<thead>
								<tr>
									<th><?php 
                _e('Shipping Method', 'woothemes');
                ?>
</th>
									<th><?php 
                _e('Status', 'woothemes');
                ?>
</th>
								</tr>
							</thead>
							<tbody>
						    	<?php 
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    echo '<tr>
						    			<td>
						    				<p><strong>' . $method->title . '</strong><br/>
						    				<small>' . __('Method ID', 'woothemes') . ': ' . $method->id . '</small></p>
						    				<input type="hidden" name="method_order[]" value="' . $method->id . '" />
						    			</td>
						    			<td>';
                    if ($method->enabled == 'yes') {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success.gif" alt="yes" />';
                    } else {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success-off.gif" alt="no" />';
                    }
                    echo '</td>
						    		</tr>';
                }
                ?>
							</tbody>
						</table>
						<?php 
                echo '</div>';
                // Specific method options
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    echo '<div class="section" id="shipping-' . $method->id . '">';
                    $method->admin_options();
                    echo '</div>';
                }
                echo '</div>';
                break;
            case "payment_gateways":
                $links = array('<a href="#gateway-order">' . __('Payment Gateways', 'woothemes') . '</a>');
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    $title = isset($gateway->method_title) && $gateway->method_title ? ucwords($gateway->method_title) : ucwords($gateway->id);
                    $links[] = '<a href="#gateway-' . $gateway->id . '">' . $title . '</a>';
                }
                echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                // Gateway ordering
                echo '<div class="section" id="gateway-order">';
                ?>
		            	<h3><?php 
                _e('Payment Gateways', 'woothemes');
                ?>
</h3>
		            	<p><?php 
                _e('Your activated payment gateways are listed below. Drag and drop rows to re-order them for display on the checkout.', 'woothemes');
                ?>
</p>
		            	<table class="wc_gateways widefat" cellspacing="0">
		            		<thead>
		            			<tr>
		            				<th width="1%"><?php 
                _e('Default', 'woothemes');
                ?>
</th>
		            				<th><?php 
                _e('Gateway', 'woothemes');
                ?>
</th>
		            				<th><?php 
                _e('Status', 'woothemes');
                ?>
</th>
		            			</tr>
		            		</thead>
		            		<tbody>
				            	<?php 
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    $default_gateway = get_option('woocommerce_default_gateway');
                    echo '<tr>
				            			<td width="1%" class="radio">
				            				<input type="radio" name="default_gateway" value="' . $gateway->id . '" ' . checked($default_gateway, $gateway->id, false) . ' />
				            				<input type="hidden" name="gateway_order[]" value="' . $gateway->id . '" />
				            			</td>
				            			<td>
				            				<p><strong>' . $gateway->title . '</strong><br/>
				            				<small>' . __('Gateway ID', 'woothemes') . ': ' . $gateway->id . '</small></p>
				            			</td>
				            			<td>';
                    if ($gateway->enabled == 'yes') {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success.gif" alt="yes" />';
                    } else {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success-off.gif" alt="no" />';
                    }
                    echo '</td>
				            		</tr>';
                }
                ?>
		            		</tbody>
		            	</table>
		            	<?php 
                echo '</div>';
                // Specific gateway options
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    echo '<div class="section" id="gateway-' . $gateway->id . '">';
                    $gateway->admin_options();
                    echo '</div>';
                }
                echo '</div>';
                break;
            default:
                do_action('woocommerce_settings_tabs_' . $current_tab);
                break;
        }
        ?>
	        <p class="submit">
	        	<input name="save" class="button-primary" type="submit" value="<?php 
        _e('Save changes', 'woothemes');
        ?>
" />
	        	<input type="hidden" name="subtab" id="last_tab" />
	        </p>
		</form>
		
		<script type="text/javascript">
			jQuery(window).load(function(){
			
				// Subsubsub tabs
				jQuery('ul.subsubsub li a:eq(0)').addClass('current');
				jQuery('.subsubsub_section .section:gt(0)').hide();
				
				jQuery('ul.subsubsub li a').click(function(){
					jQuery('a', jQuery(this).closest('ul.subsubsub')).removeClass('current');
					jQuery(this).addClass('current');
					jQuery('.section', jQuery(this).closest('.subsubsub_section')).hide();
					jQuery( jQuery(this).attr('href') ).show();
					jQuery('#last_tab').val( jQuery(this).attr('href') );
					return false;
				});
				
				<?php 
        if (isset($_GET['subtab']) && $_GET['subtab']) {
            echo 'jQuery("ul.subsubsub li a[href=#' . $_GET['subtab'] . ']").click();';
        }
        ?>
				
				// Countries
				jQuery('select#woocommerce_allowed_countries').change(function(){
					if (jQuery(this).val()=="specific") {
						jQuery(this).parent().parent().next('tr').show();
					} else {
						jQuery(this).parent().parent().next('tr').hide();
					}
				}).change();
				
				// Color picker
				jQuery('.colorpick').each(function(){
					jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
					jQuery(this).click(function() {
						if ( jQuery(this).val() == "" ) jQuery(this).val('#');
						jQuery('.colorpickdiv', jQuery(this).parent() ).show();
					});	
				});
				jQuery(document).mousedown(function(){
					jQuery('.colorpickdiv').hide();
				});
				
				// Edit prompt
				jQuery(function(){
					var changed = false;
					
					jQuery('input, textarea, select, checkbox').change(function(){
						changed = true;
					});
					
					jQuery('.woo-nav-tab-wrapper a').click(function(){
						if (changed) {
							window.onbeforeunload = function() {
							    return '<?php 
        echo __('The changes you made will be lost if you navigate away from this page.', 'woothemes');
        ?>
';
							}
						} else {
							window.onbeforeunload = '';
						}
					});
					
					jQuery('.submit input').click(function(){
						window.onbeforeunload = '';
					});
				});
				
				// Sorting
				jQuery('table.wc_gateways tbody, table.wc_shipping tbody').sortable({
					items:'tr',
					cursor:'move',
					axis:'y',
					handle: 'td',
					scrollSensitivity:40,
					helper:function(e,ui){
						ui.children().each(function(){
							jQuery(this).width(jQuery(this).width());
						});
						ui.css('left', '0');
						return ui;
					},
					start:function(event,ui){
						ui.item.css('background-color','#f6f6f6');
					},
					stop:function(event,ui){
						ui.item.removeAttr('style');
					}
				});
				
				// Chosen selects
				jQuery("select.chosen_select").chosen();
				
				jQuery("select.chosen_select_nostd").chosen({
					allow_single_deselect: 'true'
				});
				
			});
		</script>
	</div>
	<?php 
    }
    function woocommerce_settings()
    {
        global $woocommerce, $woocommerce_settings;
        $current_tab = isset($_GET['tab']) ? $_GET['tab'] : 'general';
        // Save settings
        if (isset($_POST) && $_POST) {
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'woocommerce-settings')) {
                die(__('Action failed. Please refresh the page and retry.', 'woocommerce'));
            }
            switch ($current_tab) {
                case "general":
                case "pages":
                case "catalog":
                case "inventory":
                case "shipping":
                case "tax":
                case "email":
                case "integration":
                    woocommerce_update_options($woocommerce_settings[$current_tab]);
                    break;
            }
            do_action('woocommerce_update_options');
            do_action('woocommerce_update_options_' . $current_tab);
            if ($current_tab == 'shipping') {
                do_action('woocommerce_update_options_shipping_methods');
            }
            // Shipping Methods
            flush_rewrite_rules(false);
            wp_redirect(add_query_arg('subtab', esc_attr(str_replace('#', '', $_POST['subtab'])), add_query_arg('saved', 'true', admin_url('admin.php?page=woocommerce&tab=' . $current_tab))));
            exit;
        }
        // Settings saved message
        if (isset($_GET['saved']) && $_GET['saved']) {
            echo '<div id="message" class="updated fade"><p><strong>' . __('Your settings have been saved.', 'woocommerce') . '</strong></p></div>';
            flush_rewrite_rules(false);
            do_action('woocommerce_settings_saved');
        }
        // Hide WC Link
        if (isset($_GET['hide-wc-extensions-message'])) {
            update_option('hide-wc-extensions-message', 1);
        }
        // Install/page installer
        $install_complete = false;
        // Add pages button
        if (isset($_GET['install_woocommerce_pages']) && $_GET['install_woocommerce_pages']) {
            require_once 'woocommerce-admin-install.php';
            woocommerce_create_pages();
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
            // Skip button
        } elseif (isset($_GET['skip_install_woocommerce_pages']) && $_GET['skip_install_woocommerce_pages']) {
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
        }
        if ($install_complete) {
            ?>
    	<div id="message" class="updated woocommerce-message wc-connect">
			<div class="squeezer">
				<h4><?php 
            _e('<strong>Congratulations!</strong> &#8211; WooCommerce has been installed and setup. Enjoy :)', 'woocommerce');
            ?>
</h4>
				<p><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.woothemes.com/woocommerce/" data-text="A open-source (free) #ecommerce plugin for #WordPress that helps you sell anything. Beautifully." data-via="WooThemes" data-size="large" data-hashtags="WooCommerce">Tweet</a>
	<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
			</div>
		</div>
		<?php 
            // Flush rules after install
            flush_rewrite_rules(false);
            // Set installed option
            update_option('woocommerce_installed', 0);
        }
        ?>
	<div class="wrap woocommerce">
		<form method="post" id="mainform" action="">
			<div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br></div><h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
				<?php 
        $tabs = array('general' => __('General', 'woocommerce'), 'pages' => __('Pages', 'woocommerce'), 'catalog' => __('Catalog', 'woocommerce'), 'inventory' => __('Inventory', 'woocommerce'), 'tax' => __('Tax', 'woocommerce'), 'shipping' => __('Shipping', 'woocommerce'), 'payment_gateways' => __('Payment Gateways', 'woocommerce'), 'email' => __('Emails', 'woocommerce'), 'integration' => __('Integration', 'woocommerce'));
        $tabs = apply_filters('woocommerce_settings_tabs_array', $tabs);
        foreach ($tabs as $name => $label) {
            echo '<a href="' . admin_url('admin.php?page=woocommerce&tab=' . $name) . '" class="nav-tab ';
            if ($current_tab == $name) {
                echo 'nav-tab-active';
            }
            echo '">' . $label . '</a>';
        }
        do_action('woocommerce_settings_tabs');
        ?>
			</h2>
			<?php 
        wp_nonce_field('woocommerce-settings', '_wpnonce', true, true);
        ?>
			
			<?php 
        if (!get_option('hide-wc-extensions-message')) {
            ?>
			<div id="woocommerce_extensions"><a href="<?php 
            echo add_query_arg('hide-wc-extensions-message', 'true');
            ?>
" class="hide">&times;</a><?php 
            echo sprintf(__('More functionality and gateway options available via <a href="%s" target="_blank">WC official extensions</a>.', 'woocommerce'), 'http://www.woothemes.com/extensions/woocommerce-extensions/');
            ?>
</div>
			<?php 
        }
        ?>

			<?php 
        switch ($current_tab) {
            case "general":
            case "pages":
            case "catalog":
            case "inventory":
            case "tax":
            case "email":
            case "integration":
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                break;
            case "shipping":
                $links = array('<a href="#shipping-options">' . __('Shipping Options', 'woocommerce') . '</a>');
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    $title = isset($method->method_title) && $method->method_title ? ucwords($method->method_title) : ucwords($method->id);
                    $links[] = '<a href="#shipping-' . $method->id . '">' . $title . '</a>';
                }
                echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                // Gateway ordering
                echo '<div class="section" id="shipping-options">';
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                ?>
						<h3><?php 
                _e('Shipping Methods', 'woocommerce');
                ?>
</h3>
						<p><?php 
                _e('Your activated shipping methods are listed below. Drag and drop rows to re-order them for display on the frontend.', 'woocommerce');
                ?>
</p>
						<table class="wc_shipping widefat" cellspacing="0">
							<thead>
								<tr>
									<th><?php 
                _e('Default', 'woocommerce');
                ?>
</th>
									<th><?php 
                _e('Shipping Method', 'woocommerce');
                ?>
</th>
									<th><?php 
                _e('Status', 'woocommerce');
                ?>
</th>
								</tr>
							</thead>
							<tbody>
						    	<?php 
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    $default_shipping_method = get_option('woocommerce_default_shipping_method');
                    echo '<tr>
						    		<td width="1%" class="radio">
						    			<input type="radio" name="default_shipping_method" value="' . $method->id . '" ' . checked($default_shipping_method, $method->id, false) . ' />
						    			<input type="hidden" name="method_order[]" value="' . $method->id . '" />
						    			<td>
						    				<p><strong>' . $method->title . '</strong><br/>
						    				<small>' . __('Method ID', 'woocommerce') . ': ' . $method->id . '</small></p>
						    			</td>
						    			<td>';
                    if ($method->enabled == 'yes') {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success.png" alt="yes" />';
                    } else {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success-off.png" alt="no" />';
                    }
                    echo '</td>
						    		</tr>';
                }
                ?>
							</tbody>
						</table>
						<?php 
                echo '</div>';
                // Specific method options
                foreach ($woocommerce->shipping->shipping_methods as $method) {
                    echo '<div class="section" id="shipping-' . $method->id . '">';
                    $method->admin_options();
                    echo '</div>';
                }
                echo '</div>';
                break;
            case "payment_gateways":
                $links = array('<a href="#gateway-order">' . __('Payment Gateways', 'woocommerce') . '</a>');
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    $title = isset($gateway->method_title) && $gateway->method_title ? ucwords($gateway->method_title) : ucwords($gateway->id);
                    $links[] = '<a href="#gateway-' . $gateway->id . '">' . $title . '</a>';
                }
                echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                // Gateway ordering
                echo '<div class="section" id="gateway-order">';
                ?>
		            	<h3><?php 
                _e('Payment Gateways', 'woocommerce');
                ?>
</h3>
		            	<p><?php 
                _e('Your activated payment gateways are listed below. Drag and drop rows to re-order them for display on the checkout.', 'woocommerce');
                ?>
</p>
		            	<table class="wc_gateways widefat" cellspacing="0">
		            		<thead>
		            			<tr>
		            				<th width="1%"><?php 
                _e('Default', 'woocommerce');
                ?>
</th>
		            				<th><?php 
                _e('Gateway', 'woocommerce');
                ?>
</th>
		            				<th><?php 
                _e('Status', 'woocommerce');
                ?>
</th>
		            			</tr>
		            		</thead>
		            		<tbody>
				            	<?php 
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    $default_gateway = get_option('woocommerce_default_gateway');
                    echo '<tr>
				            			<td width="1%" class="radio">
				            				<input type="radio" name="default_gateway" value="' . $gateway->id . '" ' . checked($default_gateway, $gateway->id, false) . ' />
				            				<input type="hidden" name="gateway_order[]" value="' . $gateway->id . '" />
				            			</td>
				            			<td>
				            				<p><strong>' . $gateway->title . '</strong><br/>
				            				<small>' . __('Gateway ID', 'woocommerce') . ': ' . $gateway->id . '</small></p>
				            			</td>
				            			<td>';
                    if ($gateway->enabled == 'yes') {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success.png" alt="yes" />';
                    } else {
                        echo '<img src="' . $woocommerce->plugin_url() . '/assets/images/success-off.png" alt="no" />';
                    }
                    echo '</td>
				            		</tr>';
                }
                ?>
		            		</tbody>
		            	</table>
		            	<?php 
                echo '</div>';
                // Specific gateway options
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    echo '<div class="section" id="gateway-' . $gateway->id . '">';
                    $gateway->admin_options();
                    echo '</div>';
                }
                echo '</div>';
                break;
            default:
                do_action('woocommerce_settings_tabs_' . $current_tab);
                break;
        }
        ?>
	        <p class="submit">
	        	<input name="save" class="button-primary" type="submit" value="<?php 
        _e('Save changes', 'woocommerce');
        ?>
" />
	        	<input type="hidden" name="subtab" id="last_tab" />
	        </p>
		</form>
		
		<script type="text/javascript">
			jQuery(window).load(function(){
			
				// Subsubsub tabs
				jQuery('ul.subsubsub li a:eq(0)').addClass('current');
				jQuery('.subsubsub_section .section:gt(0)').hide();
				
				jQuery('ul.subsubsub li a').click(function(){
					jQuery('a', jQuery(this).closest('ul.subsubsub')).removeClass('current');
					jQuery(this).addClass('current');
					jQuery('.section', jQuery(this).closest('.subsubsub_section')).hide();
					jQuery( jQuery(this).attr('href') ).show();
					jQuery('#last_tab').val( jQuery(this).attr('href') );
					return false;
				});
				
				<?php 
        if (isset($_GET['subtab']) && $_GET['subtab']) {
            echo 'jQuery("ul.subsubsub li a[href=#' . $_GET['subtab'] . ']").click();';
        }
        ?>
				
				// Countries
				jQuery('select#woocommerce_allowed_countries').change(function(){
					if (jQuery(this).val()=="specific") {
						jQuery(this).parent().parent().next('tr').show();
					} else {
						jQuery(this).parent().parent().next('tr').hide();
					}
				}).change();
				
				// Color picker
				jQuery('.colorpick').each(function(){
					jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
					jQuery(this).click(function() {
						if ( jQuery(this).val() == "" ) jQuery(this).val('#');
						jQuery('.colorpickdiv', jQuery(this).parent() ).show();
					});	
				});
				jQuery(document).mousedown(function(){
					jQuery('.colorpickdiv').hide();
				});
				
				// Edit prompt
				jQuery(function(){
					var changed = false;
					
					jQuery('input, textarea, select, checkbox').change(function(){
						changed = true;
					});
					
					jQuery('.woo-nav-tab-wrapper a').click(function(){
						if (changed) {
							window.onbeforeunload = function() {
							    return '<?php 
        echo __('The changes you made will be lost if you navigate away from this page.', 'woocommerce');
        ?>
';
							}
						} else {
							window.onbeforeunload = '';
						}
					});
					
					jQuery('.submit input').click(function(){
						window.onbeforeunload = '';
					});
				});
				
				// Sorting
				jQuery('table.wc_gateways tbody, table.wc_shipping tbody').sortable({
					items:'tr',
					cursor:'move',
					axis:'y',
					handle: 'td',
					scrollSensitivity:40,
					helper:function(e,ui){
						ui.children().each(function(){
							jQuery(this).width(jQuery(this).width());
						});
						ui.css('left', '0');
						return ui;
					},
					start:function(event,ui){
						ui.item.css('background-color','#f6f6f6');
					},
					stop:function(event,ui){
						ui.item.removeAttr('style');
					}
				});
				
				// Chosen selects
				jQuery("select.chosen_select").chosen();
				
				jQuery("select.chosen_select_nostd").chosen({
					allow_single_deselect: 'true'
				});
				
			});
		</script>
	</div>
	<?php 
    }
    /**
     * Settings page.
     *
     * Handles the display of the main woocommerce settings page in admin.
     *
     * @access public
     * @return void
     */
    function woocommerce_settings()
    {
        global $woocommerce, $woocommerce_settings;
        // Get current tab/section
        $current_tab = empty($_GET['tab']) ? 'general' : urldecode($_GET['tab']);
        $current_section = empty($_REQUEST['section']) ? '' : urldecode($_REQUEST['section']);
        // Save settings
        if (!empty($_POST)) {
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'woocommerce-settings')) {
                die(__('Action failed. Please refresh the page and retry.', 'woocommerce'));
            }
            if (!$current_section) {
                $old_base_color = get_option('woocommerce_frontend_css_base_color');
                $old_base_color = get_option('woocommerce_frontend_css_base_color');
                include_once 'settings/settings-save.php';
                switch ($current_tab) {
                    case "general":
                    case "pages":
                    case "catalog":
                    case "inventory":
                    case "shipping":
                    case "tax":
                    case "email":
                        woocommerce_update_options($woocommerce_settings[$current_tab]);
                        break;
                }
                do_action('woocommerce_update_options');
                do_action('woocommerce_update_options_' . $current_tab);
                // Handle Colour Settings
                if ($current_tab == 'general' && get_option('woocommerce_frontend_css') == 'yes') {
                    // Save settings
                    $primary = !empty($_POST['woocommerce_frontend_css_primary']) ? woocommerce_format_hex($_POST['woocommerce_frontend_css_primary']) : '';
                    $secondary = !empty($_POST['woocommerce_frontend_css_secondary']) ? woocommerce_format_hex($_POST['woocommerce_frontend_css_secondary']) : '';
                    $highlight = !empty($_POST['woocommerce_frontend_css_highlight']) ? woocommerce_format_hex($_POST['woocommerce_frontend_css_highlight']) : '';
                    $content_bg = !empty($_POST['woocommerce_frontend_css_content_bg']) ? woocommerce_format_hex($_POST['woocommerce_frontend_css_content_bg']) : '';
                    $subtext = !empty($_POST['woocommerce_frontend_css_subtext']) ? woocommerce_format_hex($_POST['woocommerce_frontend_css_subtext']) : '';
                    $colors = array('primary' => $primary, 'secondary' => $secondary, 'highlight' => $highlight, 'content_bg' => $content_bg, 'subtext' => $subtext);
                    $old_colors = get_option('woocommerce_frontend_css_colors');
                    update_option('woocommerce_frontend_css_colors', $colors);
                    if ($old_colors != $colors) {
                        woocommerce_compile_less_styles();
                    }
                }
            } else {
                // If saving a shipping methods options, load 'er up
                if ($current_tab == 'shipping' && class_exists($current_section)) {
                    $current_section_class = new $current_section();
                    do_action('woocommerce_update_options_' . $current_tab . '_' . $current_section_class->id);
                } else {
                    // Save section only
                    do_action('woocommerce_update_options_' . $current_tab . '_' . $current_section);
                }
            }
            // Flush rules and clear any unwanted data
            flush_rewrite_rules(false);
            unset($_SESSION['orderby']);
            $woocommerce->clear_product_transients();
            // Redirect back to the settings page
            $redirect = add_query_arg('saved', 'true');
            if (!empty($_POST['subtab'])) {
                $redirect = add_query_arg('subtab', esc_attr(str_replace('#', '', $_POST['subtab'])), $redirect);
            }
            wp_redirect($redirect);
            exit;
        }
        // Get any returned messages
        $error = empty($_GET['wc_error']) ? '' : urldecode(stripslashes($_GET['wc_error']));
        $message = empty($_GET['wc_message']) ? '' : urldecode(stripslashes($_GET['wc_message']));
        if ($error || $message) {
            if ($error) {
                echo '<div id="message" class="error fade"><p><strong>' . wptexturize($error) . '</strong></p></div>';
            } else {
                echo '<div id="message" class="updated fade"><p><strong>' . wptexturize($message) . '</strong></p></div>';
            }
        } elseif (!empty($_GET['saved'])) {
            echo '<div id="message" class="updated fade"><p><strong>' . __('Your settings have been saved.', 'woocommerce') . '</strong></p></div>';
        }
        // Were the settings saved?
        if (!empty($_GET['saved'])) {
            flush_rewrite_rules(false);
            do_action('woocommerce_settings_saved');
        }
        // Hide WC Link
        if (isset($_GET['hide-wc-extensions-message'])) {
            update_option('hide-wc-extensions-message', 1);
        }
        // Install/page installer
        $install_complete = false;
        // Add pages button
        if (isset($_GET['install_woocommerce_pages']) && $_GET['install_woocommerce_pages']) {
            require_once 'woocommerce-admin-install.php';
            woocommerce_create_pages();
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
            // Skip button
        } elseif (isset($_GET['skip_install_woocommerce_pages']) && $_GET['skip_install_woocommerce_pages']) {
            update_option('skip_install_woocommerce_pages', 1);
            $install_complete = true;
        }
        if ($install_complete) {
            ?>
	    	<div id="message" class="updated woocommerce-message wc-connect">
				<div class="squeezer">
					<h4><?php 
            _e('<strong>Congratulations!</strong> &#8211; WooCommerce has been installed and setup. Enjoy :)', 'woocommerce');
            ?>
</h4>
					<p><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.woothemes.com/woocommerce/" data-text="A open-source (free) #ecommerce plugin for #WordPress that helps you sell anything. Beautifully." data-via="WooThemes" data-size="large" data-hashtags="WooCommerce">Tweet</a>
		<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></p>
				</div>
			</div>
			<?php 
            // Flush rules after install
            flush_rewrite_rules(false);
            // Set installed option
            update_option('woocommerce_installed', 0);
        }
        ?>
		<div class="wrap woocommerce">
			<form method="post" id="mainform" action="" enctype="multipart/form-data">
				<div class="icon32 icon32-woocommerce-settings" id="icon-woocommerce"><br /></div><h2 class="nav-tab-wrapper woo-nav-tab-wrapper">
					<?php 
        $tabs = array('general' => __('General', 'woocommerce'), 'catalog' => __('Catalog', 'woocommerce'), 'pages' => __('Pages', 'woocommerce'), 'inventory' => __('Inventory', 'woocommerce'), 'tax' => __('Tax', 'woocommerce'), 'shipping' => __('Shipping', 'woocommerce'), 'payment_gateways' => __('Payment Gateways', 'woocommerce'), 'email' => __('Emails', 'woocommerce'), 'integration' => __('Integration', 'woocommerce'));
        $tabs = apply_filters('woocommerce_settings_tabs_array', $tabs);
        foreach ($tabs as $name => $label) {
            echo '<a href="' . admin_url('admin.php?page=woocommerce_settings&tab=' . $name) . '" class="nav-tab ';
            if ($current_tab == $name) {
                echo 'nav-tab-active';
            }
            echo '">' . $label . '</a>';
        }
        do_action('woocommerce_settings_tabs');
        ?>
				</h2>
				<?php 
        wp_nonce_field('woocommerce-settings', '_wpnonce', true, true);
        ?>

				<?php 
        if (!get_option('hide-wc-extensions-message')) {
            ?>
					<div id="woocommerce_extensions"><a href="<?php 
            echo add_query_arg('hide-wc-extensions-message', 'true');
            ?>
" class="hide">&times;</a><?php 
            printf(__('More functionality and gateway options available via <a href="%s" target="_blank">WC official extensions</a>.', 'woocommerce'), 'http://www.woothemes.com/extensions/woocommerce-extensions/');
            ?>
</div>
				<?php 
        }
        ?>

				<?php 
        switch ($current_tab) {
            case "general":
                include_once 'settings/settings-frontend-styles.php';
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                break;
            case "tax":
                include 'settings/settings-tax-rates.php';
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                break;
            case "pages":
            case "catalog":
            case "inventory":
            case "email":
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                break;
            case "shipping":
                include 'settings/settings-shipping-methods.php';
                $current = $current_section ? '' : 'class="current"';
                $links = array('<a href="' . admin_url('admin.php?page=woocommerce_settings&tab=shipping') . '" ' . $current . '>' . __('Shipping Options', 'woocommerce') . '</a>');
                // Load shipping methods so we can show any global options they may have
                $shipping_methods = $woocommerce->shipping->load_shipping_methods();
                foreach ($shipping_methods as $method) {
                    if (!$method->has_settings()) {
                        continue;
                    }
                    $title = empty($method->method_title) ? ucwords($method->id) : ucwords($method->method_title);
                    $current = get_class($method) == $current_section ? 'class="current"' : '';
                    $links[] = '<a href="' . add_query_arg('section', get_class($method), admin_url('admin.php?page=woocommerce_settings&tab=shipping')) . '"' . $current . '>' . $title . '</a>';
                }
                echo '<ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                // Specific method options
                if ($current_section) {
                    foreach ($shipping_methods as $method) {
                        if (get_class($method) == $current_section && $method->has_settings()) {
                            $method->admin_options();
                            break;
                        }
                    }
                } else {
                    woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                }
                break;
            case "payment_gateways":
                include 'settings/settings-payment-gateways.php';
                $links = array('<a href="#payment-gateways">' . __('Payment Gateways', 'woocommerce') . '</a>');
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    $title = empty($gateway->method_title) ? ucwords($gateway->id) : ucwords($gateway->method_title);
                    $links[] = '<a href="#gateway-' . $gateway->id . '">' . $title . '</a>';
                }
                echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                echo '<div class="section" id="payment-gateways">';
                woocommerce_admin_fields($woocommerce_settings[$current_tab]);
                echo '</div>';
                // Specific method options
                foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) {
                    echo '<div class="section" id="gateway-' . $gateway->id . '">';
                    $gateway->admin_options();
                    echo '</div>';
                }
                echo '</div>';
                break;
            case "integration":
                $integrations = $woocommerce->integrations->get_integrations();
                $current_section = empty($current_section) ? key($integrations) : $current_section;
                foreach ($integrations as $integration) {
                    $title = empty($integration->method_title) ? ucwords($integration->id) : ucwords($integration->method_title);
                    $current = $integration->id == $current_section ? 'class="current"' : '';
                    $links[] = '<a href="' . add_query_arg('section', $integration->id, admin_url('admin.php?page=woocommerce_settings&tab=integration')) . '"' . $current . '>' . $title . '</a>';
                }
                echo '<ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
                if (isset($integrations[$current_section])) {
                    $integrations[$current_section]->admin_options();
                }
                break;
            default:
                do_action('woocommerce_settings_tabs_' . $current_tab);
                break;
        }
        ?>
		        <p class="submit">
		        	<input name="save" class="button-primary" type="submit" value="<?php 
        _e('Save changes', 'woocommerce');
        ?>
" />
		        	<input type="hidden" name="subtab" id="last_tab" />
		        </p>
			</form>

			<script type="text/javascript">
				jQuery(window).load(function(){
					// Subsubsub tabs
					jQuery('div.subsubsub_section ul.subsubsub li a:eq(0)').addClass('current');
					jQuery('div.subsubsub_section .section:gt(0)').hide();

					jQuery('div.subsubsub_section ul.subsubsub li a').click(function(){
						var $clicked = jQuery(this);
						var $section = $clicked.closest('.subsubsub_section');
						var $target  = $clicked.attr('href');

						$section.find('a').removeClass('current');

						if ( $section.find('.section:visible').size() > 0 ) {
							$section.find('.section:visible').fadeOut( 100, function() {
								$section.find( $target ).fadeIn('fast');
							});
						} else {
							$section.find( $target ).fadeIn('fast');
						}

						$clicked.addClass('current');
						jQuery('#last_tab').val( $target );

						return false;
					});

					<?php 
        if (isset($_GET['subtab']) && $_GET['subtab']) {
            echo 'jQuery("div.subsubsub_section ul.subsubsub li a[href=#' . $_GET['subtab'] . ']").click();';
        }
        ?>

					// Countries
					jQuery('select#woocommerce_allowed_countries').change(function(){
						if (jQuery(this).val()=="specific") {
							jQuery(this).parent().parent().next('tr').show();
						} else {
							jQuery(this).parent().parent().next('tr').hide();
						}
					}).change();

					// Color picker
					jQuery('.colorpick').each(function(){
						jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
						jQuery(this).click(function() {
							if ( jQuery(this).val() == "" ) jQuery(this).val('#');
							jQuery('.colorpickdiv', jQuery(this).parent() ).show();
						});
					});
					jQuery(document).mousedown(function(){
						jQuery('.colorpickdiv').hide();
					});

					// Edit prompt
					jQuery(function(){
						var changed = false;

						jQuery('input, textarea, select, checkbox').change(function(){
							changed = true;
						});

						jQuery('.woo-nav-tab-wrapper a').click(function(){
							if (changed) {
								window.onbeforeunload = function() {
								    return '<?php 
        echo __('The changes you made will be lost if you navigate away from this page.', 'woocommerce');
        ?>
';
								}
							} else {
								window.onbeforeunload = '';
							}
						});

						jQuery('.submit input').click(function(){
							window.onbeforeunload = '';
						});
					});

					// Sorting
					jQuery('table.wc_gateways tbody, table.wc_shipping tbody').sortable({
						items:'tr',
						cursor:'move',
						axis:'y',
						handle: 'td',
						scrollSensitivity:40,
						helper:function(e,ui){
							ui.children().each(function(){
								jQuery(this).width(jQuery(this).width());
							});
							ui.css('left', '0');
							return ui;
						},
						start:function(event,ui){
							ui.item.css('background-color','#f6f6f6');
						},
						stop:function(event,ui){
							ui.item.removeAttr('style');
						}
					});

					// Chosen selects
					jQuery("select.chosen_select").chosen();

					jQuery("select.chosen_select_nostd").chosen({
						allow_single_deselect: 'true'
					});
				});
			</script>
		</div>
		<?php 
    }