<?php
/**
 * Initialize the options before anything else.
 */
add_action( 'admin_init', 'custom_theme_options', 1 );

/**
 * Build the custom settings & update OptionTree.
 */
function custom_theme_options() {

	/**
	 * Get a copy of the saved settings array.
	 */
	$saved_settings = get_option( 'option_tree_settings', array() );


	function etheme_theme_settings_defaults() {
		$defaults = array();
		return apply_filters('etheme_theme_settings_defaults', $defaults);
	}

	/**
	 * Custom settings array that will eventually be
	 * passes to the OptionTree Settings API Class.
	 */

	$sections = array(
		array(
			'id'       => 'general',
			'title'    => 'General',
			'icon'     => 'icon-cog'
		),
		array(
			'id'       => 'bg_colors',
			'title'    => 'Background Colors',
			'icon'     => 'icon-picture'
		),
		array(
			'id'       => 'typography',
			'title'    => 'Fonts and Icons color',
			'icon'     => 'icon-text-height'
		),
		array(
			'id'       => 'header',
			'title'    => 'Header',
			'icon'     => 'icon-cogs'
		),
		array(
			'id'       => 'footer',
			'title'    => 'Footer',
			'icon'     => 'icon-cogs'
		),
		array(
			'id'       => 'shop',
			'title'    => 'Shop',
			'icon'     => 'icon-shopping-cart'
		),
		array(
			'id'       => 'quick_view',
			'title'    => 'Quick View',
			'icon'     => 'icon-shopping-cart'
		),
		array(
			'id'       => 'blog_page',
			'title'    => 'Blog Layout',
			'icon'     => 'icon-indent-right'
		),
		array(
			'id'       => 'backup',
			'title'    => 'Import/Export',
			'icon'     => 'icon-cog'
		)

	);

	$settings = array(
		array(
			'id'          => 'responsive',
			'label'       => 'Enable Responsive Markup',
			'default'     => array(
				0 => 1
			),
			'type'        => 'checkbox',
			'section'     => 'general',
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				)
			)
		),
		array(
			'id'          => 'main_layout',
			'label'       => 'Site Layout',
			'default'     => 'wide',
			'type'        => 'select',
			'section'     => 'general',
			'choices'     => array(
				array(
					'value' => 'wide',
					'label' => 'Wide'
				),
				array(
					'value' => 'boxed',
					'label' => 'Boxed'
				)
			)
		),
		array(
			'id'          => 'to_top',
			'label'       => '"Back To Top" button',
			'default'     => array(
				0 => 1
			),
			'type'        => 'checkbox',
			'section'     => 'general',
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				)
			)
		),
		array(
			'id'          => 'search_autocomplete',
			'label'       => 'Enable Search Autocomplete',
			'default'     => array(
				0 => 1
			),
			'type'        => 'checkbox',
			'section'     => 'general',
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				)
			)
		),
		array(
			'id'          => 'wp_admin_bar',
			'label'       => 'Enable WordPress admin top bar for users',
			'default'     => array(
				0 => 1
			),
			'type'        => 'checkbox',
			'section'     => 'general',
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				)
			)
		),
		// Background Colors
		array(
			'id'          => 'background_img',
			'label'       => 'Site Background',
			'desc'        => '',
			'default'     => '',
			'type'        => 'background',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'top_bar_bg',
			'label'       => 'Top Bar',
			'default'     => '#333333',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'top_bar_menu_bg',
			'label'       => 'Top Bar Menu',
			'default'     => '#4A4A4A',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'top_bar_menu_popup_bg',
			'label'       => 'Top Bar Menu Popup',
			'default'     => '#4A4A4A',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'header_bg',
			'label'       => 'Header',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'content_bg',
			'label'       => 'Main Content',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'box_bg',
			'label'       => 'Boxed Blocks',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'buttons_bg',
			'label'       => 'Buttons and Icons',
			'default'     => '#9D3BC6',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'buttons_hover_bg',
			'label'       => 'Hovered Buttons and Icons',
			'default'     => '#6A2886',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'tabs_bg',
			'label'       => 'Tabs',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'tabs_active_bg',
			'label'       => 'Active Tabs',
			'default'     => '#9D3BC6',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'input_focus_color',
			'label'       => 'Input Boxes Focus Border Color',
			'default'     => '#C68DDE',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'footer_bg',
			'label'       => 'Footer',
			'default'     => '#000000',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),
		array(
			'id'          => 'bottom_bar_bg',
			'label'       => 'Bottom Bar',
			'default'     => '#2B2B2B',
			'type'        => 'colorpicker',
			'section'     => 'bg_colors',
		),

		// TYPOGRAPHY (FONTS)
		array(
			'id'          => 'top_bar_font',
			'label'       => 'Top Bar Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'top_bar_menu_font',
			'label'       => 'Top Bar Menu Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'top_bar_menu_popup_font',
			'label'       => 'Top Bar Menu Popup Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'header1_font',
			'label'       => 'Header Font 1',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'header2_font',
			'label'       => 'Header Font 2',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'content_font',
			'label'       => 'Content Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'box_font',
			'label'       => 'Box Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'links_font',
			'label'       => 'Links Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'links_hover_font',
			'label'       => 'Hovered Links Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'price_font',
			'label'       => 'Price Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'special_price_font',
			'label'       => 'Special Price Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'buttons_font',
			'label'       => 'Buttons Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'buttons_hover_text_color',
			'label'       => 'Hovered Buttons Text Color',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'tabs_font',
			'label'       => 'Tabs Font',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'tabs_hover_text_color',
			'label'       => 'Hovered Tabs Text Color',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'icons_text_color',
			'label'       => 'Icons Text Color',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'icons_hover_text_color',
			'label'       => 'Hovered Icons Text Color',
			'default'     => '#FFFFFF',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'add_to_cart_color',
			'label'       => 'Add To Cart Color',
			'default'     => '#9D3BC6',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'add_to_cart_hover_color',
			'label'       => 'Add To Cart Hover Color',
			'default'     => '#9D3BC6',
			'type'        => 'colorpicker',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h1',
			'label'       => 'H1',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h2',
			'label'       => 'H2',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h3',
			'label'       => 'H3',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h4',
			'label'       => 'H4',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h5',
			'label'       => 'H5',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'h6',
			'label'       => 'H6',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'footer1_font',
			'label'       => 'Footer Font 1',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),
		array(
			'id'          => 'footer2_font',
			'label'       => 'Footer Font 2',
			'default'     => '',
			'type'        => 'typography',
			'section'     => 'typography',
		),

		// HEADER
		array(
			'id'          => 'logo',
			'label'       => 'Logo image',
			'default'     => '[template_url]/img/logo.png',
			'desc'        => 'Upload image: png, jpg or gif file',
			'type'        => 'upload',
			'section'     => 'header'
		),
		array(
			'id'          => 'turn_logo',
			'label'       => 'Turned Logo image',
			'default'     => '[template_url]/img/logo_small.png',
			'desc'        => 'Upload image: png, jpg or gif file',
			'type'        => 'upload',
			'section'     => 'header'
		),
		array(
			'id'          => 'favicon',
			'label'       => 'Favicon',
			'default'     => '[template_url]/img/favicon.ico',
			'desc'        => 'Upload image: png, jpg or gif file',
			'type'        => 'upload',
			'section'     => 'header'
		),
		array(
			'id'          => 'header_type',
			'label'       => 'Header Type',
			'default'     => 1,
			'type'        => 'radio',
			'section'     => 'header',
			'class'       => '',
			'choices'     => array(
				array(
					'value'   => 1,
					'label'   => 'Dynamic',
				),
				array(
					'value'   => 2,
					'label'   => 'Preset',
				)
			)
		),
		// FOOTER
		array(
			'id'          => 'footer_type',
			'label'       => 'Footer Type',
			'default'     => 1,
			'type'        => 'radio',
			'section'     => 'footer',
			'class'       => '',
			'choices'     => array(
				array(
					'value'   => 1,
					'label'   => 'Default',
				),
				array(
					'value'   => 2,
					'label'   => 'Variant 2',
				),
			)
		),
		// SHOP

		array(
			'id'          => 'product_hover_preview',
			'label'       => 'Enable Product Preview On Hover',
			'type'        => 'checkbox',
			'section'     => 'shop',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'new_icon',
			'label'       => 'Enable "NEW" icon',
			'type'        => 'checkbox',
			'section'     => 'shop',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'sale_icon',
			'label'       => 'Enable "Sale" icon',
			'type'        => 'checkbox',
			'section'     => 'shop',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'discount_label',
			'label'       => 'Enable "Discount" label',
			'type'        => 'checkbox',
			'section'     => 'shop',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),

		array(
			'id'          => 'posts_per_page',
			'label'       => 'Posts per page',
			'type'        => 'text',
			'default'     => 6,
			'section'     => 'shop'
		),
		array(
			'id'          => 'isotope_animation',
			'label'       => 'Enable isotope animation',
			'section'     => 'shop',
			'type'        => 'checkbox',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'products_per_page',
			'label'       => 'Products per page',
			'type'        => 'text',
			'default'     => 9,
			'section'     => 'shop'
		),
		array(
			'id'          => 'ajax_addtocart',
			'label'       => 'Ajax "Add To Cart"',
			'default'     => array(
				0 => 1
			),
			'type'        => 'checkbox',
			'section'     => 'shop',
			'class'       => '',
			'choices'     => array(
				array(
					'value'   => 1,
					'label'   => ''
				)
			)
		),
		array(
			'id'          => 'single_product_thumb_width',
			'label'       => 'Product Thumbnails Width',
			'default'     => 63,
			'type'        => 'text',
			'section'     => 'shop'
		),
		array(
			'id'          => 'single_product_thumb_height',
			'label'       => 'Product Thumbnails Height',
			'default'     => 90,
			'type'        => 'text',
			'section'     => 'shop'
		),
		array(
			'id'          => 'product_custom_block',
			'label'       => 'Custom Block for Product Details Page',
			'default'     => '',
			'type'        => 'staticblocks-select',
			'section'     => 'shop'
		),

		// BLOG
		array(
			'id'          => 'blog_layout',
			'label'       => 'Blog Layout',
			'type'        => 'select',
			'section'     => 'blog_page',
			'default'     => 'default',
			'choices'     => array(
				array(
					'value' => 'default',
					'label' => 'Default'
				),
				array(
					'value' => 'grid',
					'label' => 'Grid'
				),
			)
		),
		array(
			'id'          => 'enable_filter',
			'label'       => 'Enable Filter Panel (only for Grid view)',
			'type'        => 'checkbox',
			'section'     => 'blog_page',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'blog_sidebar',
			'label'       => 'Sidebar position',
			'default'     => 'right',
			'type'        => 'select',
			'section'     => 'blog_page',
			'class'       => '',
			'choices'     => array(
				array(
					'value'   => 'no_sidebar',
					'label'   => 'Without Sidebar'
				),
				array(
					'value'   => 'left',
					'label'   => 'Left Sidebar'
				),
				array(
					'value'   => 'right',
					'label'   => 'Right Sidebar'
				)
			)
		),
		array(
			'id'          => 'blog_sidebar_responsive',
			'label'       => 'Sidebar position for responsive layout',
			'default'     => 'bottom',
			'type'        => 'select',
			'section'     => 'blog_page',
			'class'       => '',
			'choices'     => array(
				array(
					'value'   => 'top',
					'label'   => 'Top'
				),
				array(
					'value'   => 'bottom',
					'label'   => 'Bottom'
				)
			)
		),
		array(
			'id'          => 'import_export',
			'label'       => 'Import or Export your theme configuration',
			'default'     => '',
			'desc'        => '',
			'type'        => 'backup',
			'section'     => 'backup'
		),
		// QUICK VIEW
		array(
			'id'          => 'quick_view',
			'label'       => 'Enable Quick View',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'quick_product_name',
			'label'       => 'Product name',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'quick_descr',
			'label'       => 'Short description',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'quick_price',
			'label'       => 'Price',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		array(
			'id'          => 'quick_add_to_cart',
			'label'       => 'Add to cart',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		/*
		array(
			'id'          => 'quick_share',
			'label'       => 'Share icons',
			'type'        => 'checkbox',
			'section'     => 'quick_view',
			'default'     => array(
				0 => 1
			),
			'choices'     => array(
				array(
					'value' => 1,
					'label' => ''
				),
			)
		),
		*/

	);

	$custom_settings = array(
		'contextual_help' => array(
			'content'       => array(
				array(
					'id'        => 'general_help',
					'title'     => 'General',
					'content'   => ''
				)
			),
			'sidebar'       => '',
		),
		'sections'        => $sections,
		'settings'        => $settings
	);

	if(is_array($settings)){
		foreach($settings as $key => $value){
			$defaults[$value['id']] = $value['default'];
		}
	}

	add_option( 'option_tree', $defaults ); // update_option  add_option


	/* settings are not the same update the DB */
	if ( $saved_settings !== $custom_settings ) {
		update_option( 'option_tree_settings', $custom_settings );
	}

}

/**
 * Initialize the meta boxes for pages.
 */
add_action( 'admin_init', 'page_meta_boxes' );


function page_meta_boxes() {
	global $wpdb;
	$page_options = array(
		array(
			'id'          => 'sidebar_state',
			'label'       => 'Sidebar Position',
			'type'        => 'select',
			'choices'     => array(
				array(
					'value' => '',
					'label' => 'Default'
				),
				array(
					'value' => 'no_sidebar',
					'label' => 'Without Sidebar'
				),
				array(
					'value' => 'left',
					'label' => 'Left Sidebar'
				),
				array(
					'value' => 'right',
					'label' => 'Right Sidebar'
				)
			)
		),
		array(
			'id'          => 'widget_area',
			'label'       => 'Widget Area',
			'type'        => 'sidebar_select'
		),
		array(
			'id'          => 'page_heading',
			'label'       => 'Show Page Title',
			'type'        => 'select',
			'choices'     => array(
				array(
					'value' => 'disable',
					'label' => 'Disable'
				),
				array(
					'value' => 'enable',
					'label' => 'Enable'
				)
			)
		)

	);

	$sliders = array(
		array(
			'value' => 'no_slider',
			'label' => 'No Slider'
		),
		array(
			'value' => 'zoom_slider',
			'label' => 'Zoom Slider'
		)
	);

	if(class_exists('RevSliderAdmin')) {

		$rs = $wpdb->get_results(
			"
				SELECT id, title, alias
				FROM ".$wpdb->prefix."revslider_sliders
				ORDER BY id ASC LIMIT 100
			"
		);

		if ($rs) {
			$_ri = 2;
			foreach ( $rs as $slider ) {
				$sliders[$_ri]['value'] = $slider->alias;
				$sliders[$_ri]['label'] = $slider->title;
				$_ri++;
			}
		} else {
			$sliders["No sliders found"] = 0;
		}
	}

	if(count($sliders)>0 ){
		array_push($page_options, array(
			'id'          => 'page_slider',
			'label'       => 'Show slider',
			'type'        => 'select',
			'choices'     => $sliders
		));
	}

	array_push($page_options, array(
		'id'          => 'zoom_slider',
		'label'       => 'Zoom slider code',
		'type'        => 'textarea',
	));

	$my_meta_box = array(
		'id'        => 'page_layout',
		'title'     => 'Page Layout',
		'desc'      => '',
		'pages'     => array( 'page', 'post' ),
		'context'   => 'side',//side normal
		'priority'  => 'low',
		'fields'    => $page_options
	);

	ot_register_meta_box( $my_meta_box );


}


/**
 * Initialize the meta boxes for products.
 */
add_action( 'admin_init', 'products_meta_boxes' );


function products_meta_boxes() {
	$page_options = array(
		array(
			'id'          => 'product_new',
			'label'       => 'Mark as New product',
			'type'        => 'select',
			'choices'     => array(
				array(
					'value' => 'disable',
					'label' => 'No'
				),
				array(
					'value' => 'enable',
					'label' => 'Yes'
				)
			)
		)
	);

	$my_meta_box = array(
		'id'        => 'product_options',
		'title'     => 'Theme options',
		'desc'      => '',
		'pages'     => array( 'product' ),
		'context'   => 'normal',//side normal
		'priority'  => 'low',
		'fields'    => $page_options
	);

	ot_register_meta_box( $my_meta_box );

}

?>