<?php
/**
 * OptionTree
 */
add_filter('ot_theme_mode', '__return_true');
add_filter('ot_show_pages', '__return_false');
require('option-tree/ot-loader.php');
require('dropdown-menus.php');
/**
 * Widgets
 */
require('shortcodes.php');
require('widgets/subscribe.php');
require('widgets/tweets.php');
require('widgets/testimonials.php');
require('widgets/work.php');
require('widgets/posts.php');
require('widgets/tabs.php');
require('widgets/accordion.php');
$construct['social_networks'] = array('FaceBook', 'Twitter', 'LinkedIn', 'RSS', 'Dribbble', 'Google');
add_action('init', 'construct_init');
function construct_init() {
	load_theme_textdomain('construct', get_template_directory() . '/languages');
	
	register_nav_menu('top', 'Top Menu');
	
	add_theme_support('post-thumbnails');
	add_theme_support('automatic-feed-links');
	
	add_image_size('full', 950, 342, TRUE);
	add_image_size('half', 442, 264, TRUE);
	add_image_size('third', 279, 170, TRUE);
	add_image_size('fourth', 202, 126, TRUE);
	
	add_image_size('blog-large-no-sidebar', 493, 249, TRUE);
	add_image_size('blog-large', 640, 265, TRUE);
	add_image_size('blog-medium', 327, 211, TRUE);
	
	add_image_size('single', 640, 265, TRUE);
	
	add_image_size('portfolio-long-text', 641, 266, TRUE);
	add_image_size('portfolio-long-text2', 443, 263, TRUE);
	add_image_size('portfolio-long-text3', 279, 170, TRUE);
	add_image_size('portfolio-long-text4', 202, 126, TRUE);
	
	add_image_size('portfolio-sidebar-full', 641, 266, TRUE);
	add_image_size('portfolio-sidebar-half', 299, 182, TRUE);
	add_image_size('portfolio-sidebar-third', 185, 125, TRUE);
	
	add_image_size('client', 160, 100, TRUE);
	add_image_size('featured', 202, 126, TRUE);
	add_image_size('slide', 1000, 428, TRUE);
	add_image_size('nivo', 977, 377, TRUE);
	add_image_size('portfolio', 641, 621, TRUE);
	
	add_image_size('side', 59, 54, TRUE);
	
	register_sidebar(array(
		'name' => 'Blog',
		'id' => 'blog',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span>',
		'after_title' => '</span></h3>'
	));
	register_sidebar(array(
		'name' => 'Page',
		'id' => 'page',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span>',
		'after_title' => '</span></h3>'
	));
	register_sidebar(array(
		'name' => 'Portfolio',
		'id' => 'portfolio',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span>',
		'after_title' => '</span></h3>'
	));
	register_sidebar(array(
		'name' => 'Contact',
		'id' => 'contact',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3><span>',
		'after_title' => '</span></h3>'
	));
	for ($count = 1; $count <= 4; $count++) {
		register_sidebar(array(
			'name' => 'Footer #' . $count,
			'id' => 'footer' . $count,
			'before_widget' => '<li id="%1$s" class="widget %2$s">',
			'after_widget' => '</li>',
			'before_title' => '<h3><span>',
			'after_title' => '</span></h3>'
		));
	}
	$sidebars = ot_get_option('sidebars');
	if ($sidebars) {
		foreach ($sidebars as $sidebar) {
			register_sidebar(array(
				'name' => $sidebar['title'],
				'id' => $sidebar['title'],
				'before_widget' => '<li id="%1$s" class="widget %2$s">',
				'after_widget' => '</li>',
				'before_title' => '<h3><span>',
				'after_title' => '</span></h3>'
			));
		}
	}
	
	register_post_type('portfolio-item', array(
		'labels' => array(
			'name' => __('Portfolio Items', 'construct'),
			'singular_name' => __('Portfolio Item', 'construct'),
			'add_new' => __('Add Item', 'construct'),
			'add_new_item' => __('Add Portfolio Item', 'construct'),
			'edit_item' => __('Edit Portfolio Item', 'construct'),
			'new_item' => __('New Portfolio Item', 'construct'),
			'all_items' => __('Items', 'construct'),
			'view_item' => __('View Portfolio Item', 'construct'),
			'search_items' => __('Search Portfolio Items', 'construct'),
			'not_found' =>  __('No portfolio items found', 'construct'),
			'not_found_in_trash' => __('No portfolio items found in Trash', 'construct'),
			'menu_name' => 'Portfolio'
  	),
		'public' => TRUE,
		'supports' => array('title', 'editor', 'thumbnail')
	));
	register_taxonomy('portfolio-tag', 'portfolio-item', array(
		'labels' => array(
			'label' => __('Portfolio Tag', 'construct'),
			'name' => __('Portfolio Tags', 'construct'),
			'singular_name' => __('Portfolio Tag', 'construct'),
			'search_items' => __('Search Portfolio Tags', 'construct'),
			'all_items' => __('All Portfolio Tags', 'construct'),
			'parent_item' => __('Parent Portfolio Tag', 'construct'),
			'parent_item_colon' => __('Parent Portfolio Tag:', 'construct'),
			'edit_item' => __('Edit Portfolio Tag', 'construct'),
			'update_item' => __('Update Portfolio Tag', 'construct'),
			'add_new_item' => __('Add New Portfolio Tag', 'construct'),
			'new_item_name' => __('New Portfolio Tag Name', 'construct'),
			'menu_name' => __('Tags', 'construct')
		),
		'show_ui' => TRUE
	));
	register_taxonomy('portfolio-category', 'portfolio-item', array(
		'labels' => array(
			'label' => __('Portfolio Category', 'construct'),
			'name' => __('Portfolio Categories', 'construct'),
			'singular_name' => __('Portfolio Category', 'construct'),
			'search_items' => __('Search Portfolio Categories', 'construct'),
			'all_items' => __('All Portfolio Categories', 'construct'),
			'parent_item' => __('Parent Portfolio Category', 'construct'),
			'parent_item_colon' => __('Parent Portfolio Category:', 'construct'),
			'edit_item' => __('Edit Portfolio Category', 'construct'),
			'update_item' => __('Update Portfolio Category', 'construct'),
			'add_new_item' => __('Add New Portfolio Category', 'construct'),
			'new_item_name' => __('New Portfolio Category Name', 'construct'),
			'menu_name' => __('Categories', 'construct')
		),
		'show_ui' => TRUE,
		'hierarchical' => TRUE
	));
	
	register_post_type('faq', array(
		'labels' => array(
			'name' => __('FAQ', 'construct'),
			'singular_name' => __('FAQ', 'construct'),
			'add_new' => __('Add FAQ', 'construct'),
			'add_new_item' => __('Add FAQ', 'construct'),
			'edit_item' => __('Edit FAQ', 'construct'),
			'new_item' => __('New FAQ', 'construct'),
			'all_items' => __('FAQs', 'construct'),
			'view_item' => __('View FAQ', 'construct'),
			'search_items' => __('Search FAQs', 'construct'),
			'not_found' =>  __('No FAQs found', 'construct'),
			'not_found_in_trash' => __('No FAQs found in Trash', 'construct'),
			'menu_name' => __('FAQ', 'construct')
  	),
		'public' => TRUE,
		'supports' => array('title', 'editor', 'thumbnail')
	));
	register_taxonomy('faq-group', 'faq', array(
		'labels' => array(
			'label' => __('FAQ Group', 'construct'),
			'name' => __('FAQ Groups', 'construct'),
			'singular_name' => __('FAQ Group', 'construct'),
			'search_items' => __('Search FAQ Groups', 'construct'),
			'all_items' => __('All FAQ Groups', 'construct'),
			'parent_item' => __('Parent FAQ Group', 'construct'),
			'parent_item_colon' => __('Parent FAQ Group:', 'construct'),
			'edit_item' => __('Edit FAQ Group', 'construct'),
			'update_item' => __('Update FAQ Group', 'construct'),
			'add_new_item' => __('Add New FAQ Group', 'construct'),
			'new_item_name' => __('New FAQ Group Name', 'construct'),
			'menu_name' => __('FAQ Groups', 'construct')
		),
		'show_ui' => TRUE,
		'hierarchical' => TRUE
	));
	
	register_taxonomy('thing', 'portfolio-item', array(
		'labels' => array(
			'label' => __('Thing We Did', 'construct'),
			'name' => __('Things We Did', 'construct'),
			'singular_name' => __('Thing We Did', 'construct'),
			'search_items' => __('Search Things We Did', 'construct'),
			'all_items' => __('All Things We Did', 'construct'),
			'parent_item' => __('Parent Thing We Did', 'construct'),
			'parent_item_colon' => __('Parent Thing We Did:', 'construct'),
			'edit_item' => __('Edit Thing We Did', 'construct'),
			'update_item' => __('Update Thing We Did', 'construct'),
			'add_new_item' => __('Add Thing We Did', 'construct'),
			'new_item_name' => __('New Thing We Did', 'construct'),
			'menu_name' => __('Things We Did', 'construct')
		)
	));
	
	wp_register_script('ie9', get_template_directory_uri() . '/scripts/ie9.js');
	wp_register_script('bxslider', get_template_directory_uri() . '/scripts/jquery.bxSlider.min.js', array('jquery'));
	wp_register_script('simplefaq', get_template_directory_uri() . '/scripts/jquery.simpleFAQ-0.7.min.js', array('jquery'), '0.7');
	wp_register_script('isotope', get_template_directory_uri() . '/scripts/jquery.isotope.js', array('jquery'));
	wp_register_script('prettyphoto', get_template_directory_uri() . '/scripts/jquery.prettyPhoto.js', array('jquery'));
	wp_register_script('onebyone', get_template_directory_uri() . '/scripts/jquery.onebyone.min.js', array('jquery'));
	wp_register_script('flexslider', get_template_directory_uri() . '/scripts/jquery.flexslider-min.js', array('jquery'));
	wp_register_script('moover', get_template_directory_uri() . '/scripts/jquery.id.moover-1.5.min.js', array('jquery'));
	wp_register_script('unleash', get_template_directory_uri() . '/scripts/jquery.unleash.min.js', array('jquery'));
	wp_register_script('nivo', get_template_directory_uri() . '/scripts/jquery.nivo.slider.js', array('jquery'));
	wp_register_script('faq', get_template_directory_uri() . '/scripts/jquery.faq.js', array('jquery'));
	wp_register_script('validate', get_template_directory_uri() . '/scripts/jquery.validate.min.js', array('jquery'));
	
	wp_register_style('prettyphoto', get_template_directory_uri() . '/stylesheets/prettyPhoto.css');
	wp_register_style('onebyone', get_template_directory_uri() . '/stylesheets/jquery.onebyone.css');
	wp_register_style('flexslider', get_template_directory_uri() . '/stylesheets/flexslider.css');
	wp_register_style('moover', get_template_directory_uri() . '/stylesheets/jquery.id.moover-1.5.css');
	wp_register_style('unleash', get_template_directory_uri() . '/stylesheets/unleash.css');
	wp_register_style('nivo', get_template_directory_uri() . '/stylesheets/nivoslider.css');
	
}
add_action('wp_ajax_piecemaker', 'construct_piecemaker');
add_action('wp_ajax_nopriv_piecemaker', 'construct_piecemaker');
function construct_piecemaker() {
	require(dirname(__FILE__) . '/scripts/piecemaker/piecemaker.php');
	exit;
}
function custom_css() {
   echo '<style type="text/css">.option-tree-ui-button{float:none;}</style>';
}
add_action('admin_head', 'custom_css');
add_action('admin_init', 'construct_admin_init');
function construct_admin_init() {
	global $tinymce_shortcodes2;
	global $tinymce_shortcodes3;
	$saved_settings = get_option('option_tree_settings', array());
	$settings = require(get_template_directory() . '/options.php');
	if ($settings != $saved_settings) {
		update_option('option_tree_settings', $settings); 
	}
	
	$tinymce_shortcodes2 = array(
		'tabs', 'tab',
		'horizontal_tabs', 'horizontal_tab',
		'toggle', 'flyouts', 'flyout',
		'intro_slider', 'intro_slider_element', 'client_slider', 'recent_work_slider',
	);
		
	$tinymce_shortcodes3 = array('share',
		'dropcap',
		'map', 'quote', 
		'small_button', 'medium_button', 'large_button', 'button',
		'vimeo', 'youtube',
		'box', 'error_box', 'warning_box', 'success_box',
		'small_icons', 'medium_icons', 'large_icons', 'icon'
	);
	foreach ($construct['social_networks'] as $social_network) {
		$tinymce_shortcodes3[] = 'social' . '_' . strtolower($social_network);
	}
	
	add_filter('mce_external_plugins', 'tinymce_plugins');
	add_filter('mce_buttons_2', 'tinymce_buttons2');
	add_filter('mce_buttons_3', 'tinymce_buttons3');
	
}
function tinymce_plugins($plugins) {
	global $tinymce_shortcodes2;
	global $tinymce_shortcodes3;
	foreach ($tinymce_shortcodes2 as $tinymce_shortcode) {
		$plugins[$tinymce_shortcode] = get_template_directory_uri() . '/tinymce/shortcodes.php';
	}
	foreach ($tinymce_shortcodes3 as $tinymce_shortcode) {
		$plugins[$tinymce_shortcode] = get_template_directory_uri() . '/tinymce/shortcodes.php';
	}
	return $plugins;
}
function tinymce_buttons2($buttons) {
	global $tinymce_shortcodes2;
	foreach ($tinymce_shortcodes2 as $tinymce_shortcode) {
		$buttons[] = $tinymce_shortcode;
	}
	return $buttons;
}
function tinymce_buttons3($buttons) {
	global $tinymce_shortcodes3;
	foreach ($tinymce_shortcodes3 as $tinymce_shortcode) {
		$buttons[] = $tinymce_shortcode;
	}
	return $buttons;
}
add_action('add_meta_boxes', 'construct_meta_boxes');
add_action('save_post', 'construct_save_meta');
function construct_meta_boxes() {
	add_meta_box('options', 'Options', 'meta_box_options', 'page');
	add_meta_box('options', 'Options', 'meta_box_options', 'post');
	add_meta_box('options', 'Options', 'meta_box_options', 'portfolio-item');
	
	add_meta_box('portfolio', 'Portfolio Options', 'meta_box_portfolio', 'portfolio-item');
	
	add_meta_box('info', 'Info', 'meta_box_info', 'page', 'advanced', 'high');
	add_meta_box('info', 'Info', 'meta_box_info', 'post', 'advanced', 'high');
	add_meta_box('info', 'Info', 'meta_box_info', 'portfolio-item', 'advanced', 'high');
	
	add_meta_box('content', 'Content', 'meta_box_content', 'post');
	add_meta_box('content', 'Content', 'meta_box_content', 'portfolio-item');
	
	
//add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
}
function ifsetor(&$var, $default = NULL) {
	if (!isset($var)) {
		return $default;
	}
	return $var;
}
function construct_save_meta($post_id) {
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return;
	}
	$meta['subtitle'] = ifsetor($_POST['construct_subtitle']);
	$meta['layout'] = ifsetor($_POST['construct_layout']);
	$meta['url'] = ifsetor($_POST['construct_url']);
	$meta['url_label'] = ifsetor($_POST['construct_url_label']);
	$meta['video'] = ifsetor($_POST['construct_video']);
	$meta['sidebar'] = ifsetor($_POST['construct_sidebar']);
	foreach ($meta as $key => $value) {
		update_post_meta($post_id, $key, $value);
	}
}
function meta_box_info($post) {
	wp_nonce_field(plugin_basename( __FILE__ ), 'construct_nonce');
	?>
	<table>
		<tr>
			<td>
				<label for="construct_subtitle">SubTitle:</label>
			</td>
			<td>
				<input type="text" name="construct_subtitle" id="construct_subtitle" value="<?php echo get_post_meta(get_the_id(), 'subtitle', TRUE) ?>" size="48" />
			</td>
		</tr>
	</table>
	<?php
}
function meta_box_options($post) {
	global $wp_registered_sidebars;
	wp_nonce_field(plugin_basename( __FILE__ ), 'construct_nonce');
	$default_sidebar = NULL;
	if (isset($post -> page_template)) {
		if ($page_template = $post -> page_template) {
			if (!preg_match('/portfolio_(left|right)_sidebar[1-3]?\.php/', $page_template) && !strcmp($page_template, 'contact.php')) {
				return;
			}
		}
	}
	?>
	<table>
		<tr>
			<td>
				<label for="construct_sidebar">SideBar:</label>
			</td>
			<td>
				<select name="construct_sidebar" id="construct_sidebar">
					<?php
					$current_sidebar = get_post_meta($post -> ID, 'sidebar', TRUE);
					$sidebars = $wp_registered_sidebars;
					foreach ($sidebars as $sidebar) {
						if (!strcmp($sidebar['id'], $current_sidebar)) {
							echo '<option value="' . esc_html($sidebar['id']) . '" selected="selected">' . esc_html($sidebar['name']) . '</option>';
						}
						else {
							echo '<option value="' . esc_html($sidebar['id']) . '">' . esc_html($sidebar['name']) . '</option>';
						}
					}
					?>
				</select>
			</td>
		</tr>
	</table>
	<?php
}
function meta_box_content($post) {
	?>
		<tr>
			<td>
				<label for="construct_video">Embedded Video:</label>
			</td>
			<td>
				<textarea type="text" name="construct_video" id="construct_video" rows="8" cols="96"><?php echo get_post_meta(get_the_id(), 'video', TRUE) ?></textarea>
			</td>
		</tr>
	<?php
}
function meta_box_portfolio($post) {
	wp_nonce_field(plugin_basename( __FILE__ ), 'construct_nonce');
	?>
	<table>
		<tr>
			<td>
				<label for="construct_layout">Layout:</label>
			</td>
			<td>
				<select name="construct_layout" id="construct_layout">
					<?php
					$current_layout = get_post_meta($post -> ID, 'layout', TRUE);
					$layouts = array('full' => 'Full Width', 'left' => 'SideBar on Left', 'right' => 'SideBar on Right');
					foreach ($layouts as $layout => $label) {
						if (!strcmp($layout, $current_layout)) {
							echo '<option value="' . esc_html($layout) . '" selected="selected">' . esc_html($label) . '</option>';
						}
						else {
							echo '<option value="' . esc_html($layout) . '">' . esc_html($label) . '</option>';
						}
					}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				<label for="construct_url">URL:</label>
			</td>
			<td>
				<input type="text" name="construct_url" id="construct_url" value="<?php echo get_post_meta(get_the_id(), 'url', TRUE) ?>" size="48" />
				Any URL with more information about this item.
			</td>
		</tr>
		<tr>
			<td>
				<label for="construct_url">URL Label:</label>
			</td>
			<td>
				<input type="text" name="construct_url_label" id="construct_url_label" value="<?php echo get_post_meta(get_the_id(), 'url_label', TRUE) ?>" size="32" />
				This label will be shown on the link to this item's URL.
			</td>
		</tr>
	</table>
	<?php
}
function the_subtitle() {
	global $post;
	echo get_post_meta($post -> ID, 'subtitle', TRUE);
}
function construct_excerpt($length) {
	Excerpt::length($length);
}
function construct_pagination() {
	global $wp_query;
	echo paginate_links(array('total' => $wp_query -> max_num_pages, 'current' => get_query_var('paged'), 'show_all' => TRUE, 'prev_next' => FALSE, 'type' => 'array', 'base' => @add_query_arg('paged','%#%'), 'format' => '?paged=%#%', 'type' => 'plain'));
	/*
	print_r($pages);
	for ($page = 1; $page <= $wp_query -> max_num_pages; $page++) {
		if ($page == get_query_var('paged')) {
			echo '<span>' . $page . '</span>';
		}
		else {
			echo '<a href="?page=' . $page . '">' . $page . '</a>';
		}
	}*/
}
function construct_next_posts_link() {
	global $paged, $wp_query;
	$max_page = $wp_query -> max_num_pages;
	if (!$paged) {
		$paged = 1;
	}
	$nextpage = intval($paged) + 1;
	$label = __('Next Page »', 'construct');
	$attr = apply_filters('next_posts_link_attributes', '');
	if (!is_single()) {
		if ($nextpage <= $max_page) {
			echo '<a href="' . next_posts($max_page, false) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';
		}
		else {
			echo '<span ' . $attr . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</span>';
		}
	}
}
function construct_previous_posts_link() {
	global $paged;
	$label = __('« Previous Page', 'construct');
	if (!is_single()) {
		if ($paged > 1) {
			$attr = apply_filters('previous_posts_link_attributes', '');
			echo '<a href="' . previous_posts(false) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) . '</a>';
		}
		else {
			echo '<span>' . preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&$1', $label ) . '</span>';
		}
	}
	
}
function construct_thumbnail($url, $width, $height) {
	return get_template_directory_uri() . '/timthumb.php?' . http_build_query(array('src' => $url, 'w' => $width, 'h' => $height));
}
function new_excerpt_length($length) {
    return 40;
}
add_filter('excerpt_length', 'new_excerpt_length');
function the_construct_thumbnail($size = 'large') {
	global $_wp_additional_image_sizes;
	$video = get_post_meta(get_the_id(), 'video', TRUE);
	if ($video) {
		$size = $_wp_additional_image_sizes[$size];
		echo '<div class="embedded" style="width:' . $size['width'] . 'px;height:' . $size['height'] . 'px;">' . $video . '</div>';
	}
	else {
		the_post_thumbnail($size);
	}
}
function has_construct_thumbnail($size = 'large') {
	return has_post_thumbnail(get_the_id()) || get_post_meta(get_the_id(), 'video', TRUE);
}
class Excerpt {
  // Default length (by WordPress)
  public static $length = 55;
  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 25,
      'regular' => 55,
      'long' => 100
    );
  /**
   * Sets the length for the excerpt,
   * then it adds the WP filter
   * And automatically calls the_excerpt();
   *
   * @param string $new_length 
   * @return void
   * @author Baylor Rae'
   */
  public static function length($new_length = 55) {
    Excerpt::$length = $new_length;
    add_filter('excerpt_length', 'Excerpt::new_length');
    Excerpt::output();
  }
  // Tells WP the new length
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
    else
      return Excerpt::$length;
  }
  // Echoes out the excerpt
  public static function output() {
    the_excerpt();
  }
}
function construct_nav() {
	if (function_exists('bcn_display_list')) {
		bcn_display_list();
	}
	else {
		echo 'PLEASE INSTALL THE BREADCRUMB NAV XT PLUGIN!';
	}
}
// An alias to the class
function my_excerpt($length = 55) {
  Excerpt::length($length);
}