<?php

// Exit if accessed directly
if ( !defined('ABSPATH')) exit;

add_action( 'after_setup_theme', 'phoenix_setup' );

if ( ! function_exists( 'phoenix_setup' ) ):
    function phoenix_setup() {
        // Navigations
        register_nav_menus(array(
            'main-menu' => __('Main menu', 'phoenix')
        ));
        
        add_image_size( 'slide', 130, 100, true );

        /**
         * Sets up the content width value based on the theme's design and stylesheet.
         */
        if ( ! isset( $content_width ) )
            $content_width = 960;
            
        // This theme styles the visual editor with editor-style.css to match the theme style.
        add_editor_style();

        // Adds RSS feed links to <head> for posts and comments.
        add_theme_support( 'automatic-feed-links' );

        // Add posts thumbs
        add_theme_support( 'post-thumbnails' );	
        
        /*
         * This theme supports custom background color and image, and here
         * we also set up the default background color.
         */
        add_theme_support( 'custom-background', array(
            'default-color' => 'fff',
        ) );
        
        /*
         * Makes Phoenix available for translation.
         *
         * Translations can be added to the /languages/ directory.
         */
        load_theme_textdomain( 'phoenix', get_template_directory() . '/languages' );
    }
endif;

function phoenix_widgets_init() {
    register_sidebar(array(
        'name' => __('Sidebar', 'phoenix'),
        'description' => __('Main right area', 'phoenix'),
        'id' => 'footer-sidebar',
        'before_title' => '<div class="widget-title">',
        'after_title' => '</div>
              <div class="widget-content">',
        'before_widget' => '<div id="%1$s" class="widget columns small-12 large-3 %2$s">',
        'after_widget' => '</div>
            </div>'
    ));

    register_sidebar(array(
        'name' => __('Footer Sidebar', 'phoenix'),
        'description' => __('Footer area', 'phoenix'),
        'id' => 'footer-sidebar',
        'before_title' => '<div class="widget-title">',
        'after_title' => '</div>
              <div class="widget-content">',
        'before_widget' => '<div id="%1$s" class="widget columns small-12 large-3 %2$s">',
        'after_widget' => '</div>
            </div>'
    ));

    register_sidebar(array(
        'name' => __('Single page sidebar', 'phoenix'),
        'description' => __('Sidebar for your widgets on single pages', 'phoenix'),
        'id' => 'side',
        'before_title' => '<div class="side-widget-title">',
        'after_title' => '</div><div class="side-widget-content">',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div></div>'
    ));
}
add_action( 'widgets_init', 'phoenix_widgets_init' );

/**
 * A safe way of adding JavaScripts to a WordPress generated page.
 */
if (!is_admin())
    add_action('wp_enqueue_scripts', 'phoenix_js');

if (!function_exists('phoenix_js')) {

    function phoenix_js() {
        // JS at the bottom for fast page loading. 
        // except for Modernizr which enables HTML5 elements & feature detects.
        wp_enqueue_script('jquery');
        wp_enqueue_script('modernizr', get_template_directory_uri() . '/includes/foundation/js/vendor/custom.modernizr.js', array('jquery'), '2.6.2', false);
        wp_enqueue_script('zepto', get_template_directory_uri() . '/includes/foundation/js/vendor/zepto.js', array('jquery'), '1.0', false);
        wp_enqueue_script('foundation', get_template_directory_uri() . '/includes/foundation/js/foundation.min.js', array('jquery'), '4.3.1', false);
        wp_enqueue_script('main', get_template_directory_uri() . '/js/main.js', array('jquery'), '1.0', false);
        if ( is_singular() ) wp_enqueue_script( "comment-reply" );
        
        // stylesheet
        wp_enqueue_style('foundation-style', get_template_directory_uri() . '/includes/foundation/css/foundation.min.css', false, '4.1.6');
        wp_enqueue_style('phoenix-style', get_stylesheet_uri(), false, '1');
    }

}

// Comments
function phoenix_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
		// Display trackbacks differently than normal comments.
	?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
		<p><?php _e( 'Pingback:', 'phoenix' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'phoenix' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
			break;
		default :
		// Proceed with normal comments.
		global $post;
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<article id="comment-<?php comment_ID(); ?>" class="comment row">
            <div class="large-2 small-2 columns">
                <?php echo get_avatar( $comment, 64 ); ?>
            </div>
            <div class="large-10 small-10 columns">
                <header class="comment-meta comment-author">
                    <?php
                        printf( '<cite class="fn">%1$s</cite>', get_comment_author_link() );
                    ?>
                    <div class="reply">
                        &#8226; <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'phoenix' ), 'after' => '', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
                    </div><!-- .reply -->
                </header><!-- .comment-meta -->

                <?php if ( '0' == $comment->comment_approved ) : ?>
                    <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'phoenix' ); ?></p>
                <?php endif; ?>

                <section class="comment-content comment">
                    <?php comment_text(); ?>
                </section><!-- .comment-content -->
            </div>
		</article><!-- #comment-## -->
        <div class="clear"></div>
	<?php
		break;
	endswitch; // end comment_type check
}

// Responsive slider
function phoenix_responsive_slider(){
    $query = new WP_Query('post_type=slides&posts_per_page=-1');
?>
    <ul data-orbit data-options="bullets:true">
<?php while($query->have_posts()){ $query->the_post(); ?>
        <li>
            <?php if (get_post_meta(get_the_id(), 'custom_link', 1)): ?>
            <a href="<?php echo get_post_meta(get_the_id(), 'custom_link', 1); ?>">
            <?php endif; ?>
                 <?php echo get_the_post_thumbnail(get_the_id(), 'large', array('alt' => get_the_title())); ?>
            <?php if (get_post_meta(get_the_id(), 'custom_link', 1)): ?>
            </a>
            <?php endif; ?>
            
            <?php  
                $content_post = get_post(get_the_id());
                $content = $content_post->post_content;
                $content = apply_filters('the_content', $content);
                $content = str_replace(']]>', ']]&gt;', $content);
                if ($content):
            ?>            
            <div class="orbit-caption"><?php the_content(); ?></div>
            <?php endif; ?>
        </li>
<?php }?>
    </ul>
<?php 
    wp_reset_postdata();
} // end Responsive Slider

add_filter( 'wp_title', 'phoenix_title', 10, 3 );
function phoenix_title( $title, $sep, $seplocation )
{
    global $page, $paged;
    // Don't affect in feeds.
    if ( is_feed() )
        return $title;
    // Add the blog name
    if ( 'right' == $seplocation )
        $title .= get_bloginfo( 'name' );
    else
        $title = get_bloginfo( 'name' ) . $title;
    // Add the blog description for the home/front page.
    $site_description = get_bloginfo( 'description', 'display' );
    if ( $site_description && ( is_home() || is_front_page() ) )
        $title .= " {$sep} {$site_description}";
    // Add a page number if necessary:
    if ( $paged >= 2 || $page >= 2 )
        $title .= " {$sep} " . sprintf( __( 'Page %s', 'phoenix' ), max( $paged, $page ) );
    return $title;
}

function phoenix_footer_script(){ ?>
    <script type="text/javascript">
                jQuery(document).foundation();
    </script>
<?php } 
add_action('wp_footer', 'phoenix_footer_script');

// The register_post_type() function is not to be used before the 'init'.
add_action( 'init', 'phoenix_custom_init' );

/* Here's how to create your customized labels */
function phoenix_custom_init() {
	$labels = array(
		'name' => _x( 'Slides', 'phoenix' ), 
		'singular_name' => _x( 'slides', 'phoenix' ),
		'add_new' => _x( 'Add new', 'phoenix' ),
		'add_new_item' => __( 'Add new slide', 'phoenix' ),
		'edit_item' => __( 'Edit', 'phoenix' ),
		'new_item' => __( 'New slide', 'phoenix' ),
		'view_item' => __( 'View', 'phoenix' ),
		'search_items' => __( 'Search slide', 'phoenix' ),
		'not_found' =>  __( 'No any slides was found', 'phoenix' ),
		'not_found_in_trash' => __( 'No any slides was found in trash', 'phoenix' ),
		'parent_item_colon' => ''
	);

	// Create an array for the $args
	$args = array( 'labels' => $labels, /* NOTICE: the $labels variable is used here... */
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
        'menu_icon' => get_bloginfo('template_directory').'/images/slides.png',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array( 'title', 'editor', 'thumbnail' )
	); 

	register_post_type( 'slides', $args ); /* Register it and move on */
}

// Meta fields for pages
add_action('admin_init', 'phoenix_slide_extra_fields', 1);

function phoenix_slide_extra_fields() {
    add_meta_box( 'extra_fields', 'Page Settings', 'phoenix_extra_fields_box_func', 'slides', 'normal', 'high'  );
}

function phoenix_extra_fields_box_func( $post ){
?>  
    <style type="text/css">
        .slide_options input {width:100%;}
    </style>
    
    <div class="slide_options">
        <label><?php _e('Custom link for slide image', 'phoenix'); ?>: <input type="text" name="extra[custom_link]" value="<?php echo get_post_meta($post->ID, 'custom_link', 1); ?>" /></label>
    </div>
	<input type="hidden" name="extra_fields_nonce" value="<?php echo wp_create_nonce(__FILE__); ?>" />

<?php
}

add_action('save_post', 'page_extra_fields_update', 0);

function page_extra_fields_update( $post_id ){
    if ( !wp_verify_nonce($_POST['extra_fields_nonce'], __FILE__) ) return false; 
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE  ) return false; 
	if ( !current_user_can('edit_post', $post_id) ) return false; 

	if( !isset($_POST['extra']) ) return false;	

	$_POST['extra'] = array_map('trim', $_POST['extra']);
	foreach( $_POST['extra'] as $key=>$value ){
		if( empty($value) ) {
			delete_post_meta($post_id, $key);
		} else {
			update_post_meta($post_id, $key, $value); 
		}
	}
	return $post_id;
}

?>