<?php

/*
 * WordPress Theme
 */

// ==================================================
// Enables theme custom post types, widgets, etc...
// --------------------------------------------------

$aitThemeCustomTypes = array('dir-item-tour' => 32, 'grid-portfolio' => 36);
$aitThemeWidgets = array('post', 'flickr', 'submenu', 'twitter', 'directory-login');
$aitEditorShortcodes = array('custom', 'columns', 'images', 'posts', 'buttons', 'boxesFrames', 'lists', 'notifications', 'modal', 'social', 'video', 'gMaps', 'gChart', 'language', 'tabs', 'gridgallery', 'econtent');
$aitThemeShortcodes = array('boxesFrames' => 2, 'buttons' => 1, 'columns'=> 1, 'custom'=> 1, 'images'=> 1, 'lists'=> 1, 'modal'=> 1, 'notifications'=> 1, 'portfolio'=> 1, 'posts'=> 1, 'sitemap'=> 1, 'social'=> 1, 'video'=> 1, 'language'=> 1, 'gMaps'=> 1, 'gChart'=> 1, 'tabs'=> 1, 'gridgallery'=> 1, 'econtent' => 1, 'directoryRegister' => 1, 'directoryTour' => 1);

// ==================================================
// Loads AIT WordPress Framework
// --------------------------------------------------

require dirname(__FILE__) . '/AIT/ait-bootstrap.php';

// ==================================================
// Loads Theme's text translations
// --------------------------------------------------
load_theme_textdomain('ait', get_template_directory() . '/languages');

// ==================================================
// Metaboxes settings for Posts and Pages
// --------------------------------------------------

$pageOptions = array(
	'header' => new WPAlchemy_MetaBox(array(
		'id' => '_ait_header_options',
		'title' => __('Header', 'ait-admin'),
		'types' => array('page','post'),
		'context' => 'normal',
		'priority' => 'core',
		'config' => dirname(__FILE__) . '/conf/page-header.neon'
	))
);


// ==================================================
// Theme's scripts and styles
// --------------------------------------------------

function aitAdminEnqueueScriptsAndStyles() {
	aitAddStyles(array(
		'ait-select2'     => array('file' => THEME_CSS_URL . "/select2.css")
	));
	aitAddScripts(array(
		'ait-select2' => array('file' => THEME_JS_URL . '/libs/select2.min.js', 'deps' => array('jquery')),
		'ait-googlemaps-api' => array('file' => 'http://maps.google.com/maps/api/js?sensor=false&amp;language=en', 'deps' => array('jquery')),
		'ait-jquery-gmap3'   => array('file' => THEME_JS_URL . '/libs/gmap3.min.js', 'deps' => array('jquery', 'ait-googlemaps-api')),
	));
}
add_action('admin_enqueue_scripts', 'aitAdminEnqueueScriptsAndStyles');


function aitEnqueueScriptsAndStyles() {
	// just shortcuts
	$s = THEME_CSS_URL;
	$j = THEME_JS_URL;

	aitAddStyles(array(
		'ait-jquery-colorbox'     => array('file' => "$s/colorbox.css"),
		'ait-jquery-fancybox'     => array('file' => "$s/fancybox/jquery.fancybox-1.3.4.css"),
		'ait-jquery-hover-zoom'   => array('file' => "$s/hoverZoom.css"),
		'ait-jquery-fancycheckbox'=> array('file' => "$s/jquery.fancycheckbox.min.css"),
		'jquery-ui-css'           => array('file' => "http://code.jquery.com/ui/1.10.3/themes/ui-lightness/jquery-ui.css"),
	));

	aitAddScripts(array(
		'jquery-ui-tabs'              => true,
		'jquery-ui-accordion'         => true,
		'jquery-ui-autocomplete'      => true,
		'jquery-ui-slider'            => true,
		'jquery-ui-datepicker'		  => true,
		'ait-jquery-fancycheckbox'    => array('file' => "$j/libs/jquery.fancycheckbox.min.js", 'deps' => array('jquery')),
		'ait-jquery-html5placeholder' => array('file' => "$j/libs/jquery.html5-placeholder-shim.js", 'deps' => array('jquery')),
		'ait-googlemaps-api'          => array('file' => 'http://maps.google.com/maps/api/js?sensor=false&amp;language=en', 'deps' => array('jquery')),
		'ait-jquery-gmap3-label'      => array('file' => "$j/libs/gmap3.infobox.js", 'deps' => array('jquery')),
		'ait-jquery-gmap3'            => array('file' => "$j/libs/gmap3.min.js", 'deps' => array('jquery')),
		'ait-jquery-infieldlabel'     => array('file' => "$j/libs/jquery.infieldlabel.js", 'deps' => array('jquery')),
		'ait-jquery-colorbox'         => array('file' => "$j/libs/jquery.colorbox-min.js", 'deps' => array('jquery')),
		'ait-jquery-fancybox'         => array('file' => "$j/libs/jquery.fancybox-1.3.4.js", 'deps' => array('jquery')),
		'ait-jquery-easing'           => array('file' => "$j/libs/jquery.easing-1.3.min.js", 'deps' => array('jquery')),
		'ait-jquery-nicescroll'       => array('file' => "$j/libs/jquery.nicescroll.min.js", 'deps' => array('jquery')),
		'ait-jquery-quicksand'        => array('file' => "$j/libs/jquery.quicksand.js", 'deps' => array('jquery')),
		'ait-jquery-hover-zoom'       => array('file' => "$j/libs/hover.zoom.js", 'deps' => array('jquery')),
		'ait-jquery-finished-typing'  => array('file' => "$j/libs/jquery.finishedTyping.js", 'deps' => array('jquery')),
		'ait-jquery-jcarousel'        => array('file' => "$j/libs/jquery.jcarousel.min.js", 'deps' => array('jquery')),
		'ait-spin-ajax-loader'        => array('file' => "$j/libs/spin.min.js"),
		'ait-modernizr-touch'         => array('file' => "$j/libs/modernizr.touch.js"),
		'ait-gridgallery'             => array('file' => "$j/gridgallery.js", 'deps' => array('jquery')),
		'ait-rating'                  => array('file' => "$j/rating.js", 'deps' => array('jquery')),
		'ait-reservation'             => array('file' => "$j/reservation.js", 'deps' => array('jquery')),
		'ait-script'                  => array('file' => "$j/script.js", 'deps' => array('jquery'))
	));

	wp_localize_script( 'ait-script', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) , 'ajaxnonce' => wp_create_nonce('ajax-nonce') ) );

}
add_action('wp_enqueue_scripts', 'aitEnqueueScriptsAndStyles');


// ==================================================
// Theme setup
// --------------------------------------------------

function aitThemeSetup() {
	add_editor_style();

	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');

	register_nav_menu('primary-menu', __('Primary Menu', 'ait-admin'));
	register_nav_menu('footer-menu', __('Footer Menu', 'ait-admin'));
}
add_action('after_setup_theme', 'aitThemeSetup');



// ==================================================
// Plugins
// --------------------------------------------------

aitAddPlugins(array(
	array(
		'name'     => 'Contact Form 7',
		'slug'     => 'contact-form-7',
		'required' => false, // only recommended
	),
	array(
		'name'     => 'Revolution Slider',
		'slug'     => 'revslider',
		'required' => false,
		'source'   => dirname(__FILE__) . '/plugins/revslider.zip', // pre-packed
	),
));



function aitWidgetsAreasInit() {
	aitRegisterWidgetAreas(array(
		'sidebar-1'      => array('name' => __('Main Sidebar', 'ait-admin')),
		'sidebar-home'   => array('name' => __('Homepage Sidebar', 'ait-admin')),
		'sidebar-item'   => array('name' => __('Items Sidebar', 'ait-admin')),
		'footer-widgets' => array(
			'name' => __('Footer Widget Area', 'ait-admin'),
			'before_widget' => '<div id="%1$s" class="box widget-container %2$s"><div class="box-wrapper">',
			'after_widget' => "</div></div>",
			'before_title' => '<div class="title-border-bottom"><div class="title-border-top"><div class="title-decoration"></div><h2 class="widget-title">',
			'after_title' => '</h2></div></div>',
		),
	), array(
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget'  => "</aside>",
		'before_title'  => '<h3 class="widget-title"><span>',
		'after_title'   => '</span></h3>',
	));
}
add_action('widgets_init', 'aitWidgetsAreasInit');


// ==================================================
// Some helper functions and filters for theme
// --------------------------------------------------

function default_menu() {
	wp_nav_menu(array('menu' => 'Main Menu', 'fallback_cb' => 'default_page_menu', 'container' => 'nav', 'container_class' => 'mainmenu', 'menu_class' => 'menu clear'));
}

function default_page_menu() {
	echo '<nav class="mainmenu">';
	wp_page_menu(array('menu_class' => 'menu clear'));
	echo '</nav>';
}

function default_footer_menu() {
	wp_nav_menu(array('menu' => 'Main Menu', 'container' => 'nav', 'container_class' => 'footer-menu', 'menu_class' => 'menu clear', 'depth' => 1));
}

remove_action('wp_head', 'wp_generator'); // do not show generator meta element

add_filter('widget_title', 'do_shortcode');
add_filter('widget_text', 'do_shortcode'); // do shortcode in text widget

if(!isset($content_width)) $content_width = 1000;


// ==================================================
// Custom styling of admin interface of Revolution slider
// --------------------------------------------------

if(isset($revSliderVersion)) {
	// Some custom styles for slides in Revolution Slider admin
	function aitRevSliderAdminStyles(){ wp_enqueue_style('ait-revolution-slider-admin-css', THEME_URL . '/design/admin-plugins/revslider.css'); }
	function aitRevSliderAdminScripts(){ wp_enqueue_script('ait-revolution-slider-admin-js', THEME_URL . '/design/admin-plugins/revslider.js'); }

	add_action('admin_print_styles', 'aitRevSliderAdminStyles');
	add_action('admin_print_scripts', 'aitRevSliderAdminScripts');
}

// ==================================================
// Tour Operator Widget for sidebar
// --------------------------------------------------

class Tour_Search_Widget extends WP_Widget {

	/**
	 * Constructor
	 *
	 * @return void
	 **/
	function Tour_Search_Widget() {
		$widget_ops = array( 'classname' => 'tour-search-widget', 'description' => 'Tour Operator Search Block' );
		$this->WP_Widget( 'tour-search-widget', 'Theme &rarr; Tour search', $widget_ops );
	}

	/**
	 * Outputs the HTML for this widget.
	 *
	 * @param array  An array of standard parameters for widgets in this theme
	 * @param array  An array of settings for this widget instance
	 * @return void Echoes it's output
	 **/
	function widget( $args, $instance ) {

		extract( $args, EXTR_SKIP );
		echo $before_widget;
		// echo $before_title;
		// echo $after_title;

		global $aitThemeOptions;
		if (isset($aitThemeOptions->search->searchPosition) && $aitThemeOptions->search->searchPosition == 'inMap') {

			echo '<div class="search-bad-location">';
			if (!empty($aitThemeOptions->search->searchTitle)) {
				echo $before_title;
				echo $aitThemeOptions->search->searchTitle;
				echo $after_title;
			}
			_e( "<span>Search form is located in map. Please set location of search form to sidebar.</span>", "ait" );
			echo '</div>';

		} else {

			// print tour search form template
			$template = new NFileTemplate(dirname(__FILE__) . '/Templates/snippets/tour-search.php');

			$template->registerHelperLoader('NTemplateHelpers::loader');
			$template->registerHelper("printf", "sprintf");
			$template->onPrepareFilters[] = create_function('$template',
				'
				$engine = new NLatteFilter();
				WpLatteMacros::install($engine->parser);
				$template->registerFilter($engine);
				'
			);
			$params = $GLOBALS['latteParams'];
			$params['site'] = WpLatteSiteEntity::getInstance();
			$params['isWidget'] = true;
			$template->setParams($params);

			$template->render();

		}

		echo $after_widget;
	}

	/**
	 * Deals with the settings when they are saved by the admin. Here is
	 * where any validation should be dealt with.
	 *
	 * @param array  An array of new settings as submitted by the admin
	 * @param array  An array of the previous settings
	 * @return array The validated and (if necessary) amended settings
	 **/
	function update( $new_instance, $old_instance ) {

		// update logic goes here
		$updated_instance = $new_instance;
		return $updated_instance;
	}

	/**
	 * Displays the form for this widget on the Widgets page of the WP Admin area.
	 *
	 * @param array  An array of the current settings for this widget
	 * @return void Echoes it's output
	 **/
	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array(  ) );

	}
}

add_action( 'widgets_init', create_function( '', "register_widget( 'Tour_Search_Widget' );" ) );

// ==================================================
// Loads Theme's specific functions
// --------------------------------------------------

require_once dirname(__FILE__) . '/functions/load.php';