<?php
/* ********************
 * Load theme control panel
 * and framework files
 ******************************************************************** */
	if ( !function_exists( 'optionsframework_init' ) ) {
		define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/framework/admin/' );
		require_once dirname( __FILE__ ) . '/framework/admin/options-framework.php'; 
		
		// FRAMEWORK FILES
		get_template_part( 'framework/functions/misc-functions'); // Misc theme functions
		get_template_part( 'framework/functions/breadcrumb'); // Breadcrumb
		get_template_part( 'framework/functions/dashboard-widget'); // Gabfire Themes RSS widget for WP Dashboard
		get_template_part( 'framework/functions/gabfire-media'); // Gabfire Media Module
	}

/* ********************
 * Setup Theme
 ******************************************************************** */
if ( ! function_exists( 'gabfire_setup_theme' ) ) {
	function gabfire_setup_theme() {
		load_theme_textdomain('gabfire', get_template_directory() . '/inc/lang');	

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

		// Enable support for Post Thumbnails, and declare two sizes.
		add_theme_support( 'post-thumbnails' );
		set_post_thumbnail_size( 200, 200, true);
		add_image_size( 'qs-featured', 431, 198, true ); // Featured Big Image
		add_image_size( 'qs-body', 200, 150, true ); // Posts on body section
		add_image_size( 'qs-topstories', 218, 109, true ); // Top stroeis below featured section
		add_image_size( 'qs-belowtabs', 230, 132, true ); // Below tabbed section right and left
		add_image_size( 'qs-loopdefault', 200,150, true ); // Photo gallery slider
		add_image_size( 'qs-innerslide', 920, 613, true ); // Innerpage slider
		add_image_size( 'qs-postbody', 538, 9999 ); // Featured image in post body
		add_image_size( 'ajaxtabs', 30, 30, true ); // Ajaxtabs Widget	

		if ( ! isset( $content_width ) ) $content_width = 538;
		
		add_theme_support('custom-background');
		add_theme_support('menus');
		register_nav_menus( array(
			'primary'   => __( 'Primary', 'gabfire' ),
		) );
	}
}
add_action( 'after_setup_theme', 'gabfire_setup_theme' );

/* ********************
 * Load theme styles and custom scripts
 ******************************************************************** */
	if ( !function_exists( 'gabfire_theme_scripts' ) ) {
	
		function gabfire_theme_scripts() {

			wp_enqueue_style('gabfire-style', get_stylesheet_uri(), false);
					
			$get_sitestyle = array("brown", "default", "green", "gray", "red");
			wp_enqueue_style('color-scheme', get_template_directory_uri() .'/styles/' . $get_sitestyle[of_get_option('of_alt_stylesheet')] . '.css');						
					
			wp_enqueue_style('font-awesome', get_template_directory_uri() .'/framework/font-awesome/css/font-awesome.min.css', array(),'4.0.3' );
			wp_enqueue_style('font-awesome', get_template_directory_uri() .'/framework/font-awesome/css/font-awesome.min.css', array(),'4.0.3');
			
			wp_enqueue_script( 'jquery' );
			wp_enqueue_script('cycle2', get_template_directory_uri() .'/inc/js/jquery.cycle2.min.js',array( 'jquery' ));
			wp_enqueue_script('slides.min', get_template_directory_uri() .'/inc/js/slides.min.jquery.js',array( 'jquery' ));
			wp_enqueue_script('jquerytools', get_template_directory_uri() .'/inc/js/jquery.tools.min.js',array( 'jquery' ));
			wp_enqueue_script('flowplayer', get_template_directory_uri() .'/inc/js/flowplayer/flowplayer-3.2.6.min.js');

			if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
				wp_enqueue_script( 'comment-reply' );	
			}
			
			if(file_exists(get_stylesheet_directory() . '/custom.css')) {
				wp_enqueue_style('custom-style', get_stylesheet_directory_uri() .'/custom.css');
			} elseif(file_exists(get_template_directory_uri() . '/custom.css')) {
				wp_enqueue_style('custom-style', get_template_directory_uri() .'/custom.css');
			}	
		}
		
		add_action( 'wp_enqueue_scripts', 'gabfire_theme_scripts' );
	}

/* ********************
 * Initialize theme scripts
 ******************************************************************** */
	if ( !function_exists( 'gabfire_initialize_scripts' ) ) {
	
		function gabfire_initialize_scripts() {	?>
		<script type='text/javascript'>	
			(function($) {
				$(document).ready(function() { 
					$(".children").parent("li").addClass("has-child-menu");
					$(".sub-menu").parent("li").addClass("has-child-menu");
					$('.mainnav li ul').hide().removeClass('fallback');
					$('.mainnav > li').hover(
						function () {
							$('ul', this).stop().slideDown(250);
						},
						function () {
							$('ul', this).stop().slideUp(250);
						}
					);
					
					$('#slides').slides({
					<?php if(of_get_option('of_cd_inner_rotate') == 1) { ?>
					play: <?php if ( of_get_option('of_cd_inner_pause', 5) <> "" ) { echo of_get_option('of_cd_inner_pause', 5).'000'; } else { echo '5000'; } ?>,
					pause: 2500,
					hoverPause: true,
					<?php } ?>
					autoHeight: true
					});	
					$("ul.tabs").tabs("div.panes > div");
					$("ul.sc_tabs").tabs("div.sc_tabs-content > div");	
					$('a[href=#top]').click(function(){	$('html, body').animate({scrollTop:0}, 'slow');	return false;});
				});
			})(jQuery);
		</script>
		<?php }
		
		add_action("wp_head", "gabfire_initialize_scripts"); 
		
	}
		
/* ********************
 * Theme comment style
 ******************************************************************** */
	if ( ! function_exists( 'gabfire_comment' ) ) {

		function gabfire_comment( $comment, $args, $depth ) {
			$GLOBALS['comment'] = $comment;
			switch ( $comment->comment_type ) :
				case '' :
			?>
			<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">

				<div class="comment-inner" id="comment-<?php comment_ID(); ?>">
				
					<div class="comment-top">
						<div class="comment-avatar">
							<?php echo get_avatar( $comment, 50 ); ?>
						</div> 
						<span class="comment-author">
							<i class="icon-user"></i> 
							<?php printf( __( '%s ', 'gabfire'), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
						</span>
						<span class="comment-date-link">
							<i class="icon-calendar-empty"></i>&nbsp;<?php printf(esc_attr__('%1$s at %2$s','gabfire'), get_comment_date(), get_comment_time()); edit_comment_link( __( 'Edit', 'gabfire'), ' ' , ''); ?>
						</span>
					</div>				
					
					<?php if ( $comment->comment_approved == '0' ) : ?>
						<p class="waiting_approval"><?php _e( 'Your comment is awaiting moderation.', 'gabfire' ); ?></p>
					<?php endif; ?>
					
					<?php comment_text(); ?>
					
					<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
					
				</div><!-- comment-inner  -->

			<?php
					break;
				case 'pingback'  :
				case 'trackback' :
			?>
			<li class="post pingback">
				<p><?php _e( 'Pingback:', 'gabfire' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'gabfire' ), ' ' ); ?></p>
			<?php
					break;
			endswitch;
		}
	}

/* ********************
 * Widgetize theme
 ******************************************************************** */
	 if ( ! function_exists( 'gabfire_register_sidebar' ) ) {
		function gabfire_register_sidebar($args) {
			$common = array(
				'before_widget' => '<aside id="%1$s" class="widget %2$s"><div class="widgetinner">',
				'after_widget'  => '</div></aside>',
				'before_title'  => '<h3 class="widgettitle">',
				'after_title'   => '</h3>'
			);

			$args = wp_parse_args($args, $common);
			return register_sidebar($args);
		}
		
		gabfire_register_sidebar( array(
			'name'          => __( 'Sidebar1', 'gabfire' ),
			'id'            => 'Sidebar1',
			'description'   => __( 'Sidebar above 125px sidebar ads', 'gabfire' ),
		) );	
		gabfire_register_sidebar( array(
			'name'          => __( 'Sidebar2', 'gabfire' ),
			'id'            => 'Sidebar2',
			'description'   => __( 'Sidebar below 125px sidebar ads', 'gabfire' ),
		) );			
	}

	 if ( ! function_exists( 'gabfire_register_sidebar2' ) ) {
		function gabfire_register_sidebar2($args) {
			$common = array(
				'before_widget' => '<div id="%1$s" class="widget %2$s">',
				'after_widget'  => '</div>',
				'before_title'  => '<h3 class="widgettitle">',
				'after_title'   => '</h3>'
			);
			$args = wp_parse_args($args, $common);
			return register_sidebar($args);
		}

		gabfire_register_sidebar2( array(
			'name'          => __( 'FooterLeft', 'gabfire' ),
			'id'            => 'FooterLeft',
			'description'   => __( 'Footer left widgetized column', 'gabfire' ),
		) );	
		gabfire_register_sidebar2( array(
			'name'          => __( 'FooterMid', 'gabfire' ),
			'id'            => 'FooterMid',
			'description'   => __( 'Footer mid widgetized column', 'gabfire' ),
		) );	
		gabfire_register_sidebar2( array(
			'name'          => __( 'FooterRight', 'gabfire' ),
			'id'            => 'FooterRight',
			'description'   => __( 'Footer right widgetized column', 'gabfire' ),
		) );	
		gabfire_register_sidebar2( array(
			'name'          => __( 'PostWidget', 'gabfire' ),
			'id'            => 'PostWidget',
			'description'   => __( 'Single post page, between entry and comments', 'gabfire' ),
		) );	
		gabfire_register_sidebar2( array(
			'name'          => __( 'PageWidget', 'gabfire' ),
			'id'            => 'PageWidget',
			'description'   => __( 'Below entry - Static Page-', 'gabfire' ),
		) );	
	}

/* ********************
 * Custom field panels below text editor
 ******************************************************************** */		
if ( !function_exists( 'gabfire_meta_box_add' ) ) {

	add_action( 'add_meta_boxes', 'gabfire_meta_box_add' );

	function gabfire_meta_box_add()
	{
		add_meta_box( '', 'Gabfire Custom Fields', 'gabfire_meta_box', 'post', 'normal', 'high' );
		add_meta_box( '', 'Gabfire Custom Fields', 'gabfire_meta_box', 'page', 'normal', 'high' );
	}

	function gabfire_meta_box( $post )
	{
		$values = get_post_custom( $post->ID );
		$video = isset( $values['iframe'] ) ? esc_attr( $values['iframe'][0] ) : '';
		$feapost = isset( $values['featured'] ) ? esc_attr( $values['featured'][0] ) : '';  
		$disable_feaimage = isset( $values['post_feaimage'] ) ? esc_attr( $values['post_feaimage'][0] ) : '';  
		$post_template = isset( $values['gabfire_post_template'] ) ? esc_attr( $values['gabfire_post_template'][0] ) : '';  
		wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
		?>		
			
		<?php if ( gabfire_get_current_post_type() == 'post' ) { ?>
			<p class="gab_caption"><?php _e('Post Layout','gabfire'); ?></p>
			<p>
				<div class="gab_fieldname"><label for="gabfire_post_template"><?php _e('Select a Post layout</br><strong>Note:</strong> Select Big Picture only if you have uploaded more than 1 photo','gabfire'); ?></label></div>
				<div class="gab_fieldinput">
					<select name="gabfire_post_template" id="gabfire_post_template">
						<option value="default" <?php selected( $post_template, '2col' ); ?>><?php _e('Default','gabfire'); ?></option>
						<option value="bigpicture" <?php selected( $post_template, 'bigpicture' ); ?>><?php _e('Big Picture','gabfire'); ?></option>
						<option value="leftsidebar" <?php selected( $post_template, 'leftsidebar' ); ?>><?php _e('Left Sidebar','gabfire'); ?></option>
						<option value="fullwidth" <?php selected( $post_template, 'fullwidth' ); ?>><?php _e('No Sidebar','gabfire'); ?></option>
					</select>
				</div>
				<div class="clear"></div>
			</p>
			
			<hr class="gab_hr">
		<?php } ?>			
			
		<p class="gab_caption"><?php _e('Video URL','gabfire'); ?></p>
		<div class="gab_fieldrow">
			<div class="gab_fieldname"><label for="iframe"><?php _e('You can add any Youtube, Vimeo, Dailymotion or Screenr video url into this box. (Do NOT use short URL)','gabfire'); ?></label></div>
			<div class="gab_fieldinput"><input type="text" class="gab_textfield" name="iframe" class="regular-text" id="iframe" value="<?php echo $video; ?>" /></div>
			<div class="clear"></div>
		</div>
		
		<p class="gab_caption"><?php _e('Is Featured?','gabfire'); ?></p>
		<div class="gab_fieldrow">
			<div class="gab_fieldname"><label for="featured"><label for="featured"><?php _e('Check this box to display this post on featured section of front page.</br><strong>Note:</strong> This will only work if Custom Field option is enabled within Theme Options','gabfire'); ?></label></div>
			<div class="gab_fieldinput"><input type="checkbox" id="featured" name="featured" <?php checked( $feapost, 'true' ); ?> />  </div>
			<div class="clear"></div>
		</div>			
		
		<hr class="gab_hr">
		
		<p class="gab_caption"><?php _e('Disable Featured Image on Post page?','gabfire'); ?></p>
		<div class="gab_fieldrow">
			<div class="gab_fieldname"><label for="post_feaimage"><label for="post_feaimage"><?php _e('If you have enabled featured post on single post page option, the featured image shows at top of every post automatically. You may check this box to disable that image for certain posts.','gabfire'); ?></label></div>
			<div class="gab_fieldinput"><input type="checkbox" id="post_feaimage" name="post_feaimage" <?php checked( $disable_feaimage, 'true' ); ?> />  </div>
			<div class="clear"></div>
		</div>				
		
		<?php	
	}

	add_action( 'save_post', 'gabfire_meta_box_save' );
	function gabfire_meta_box_save( $post_id )
	{
		// Bail if we're doing an auto save
		if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
		
		// if our nonce isn't there, or we can't verify it, bail
		if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
		
		// if our current user can't edit this post, bail
		if( !current_user_can( 'edit_post' ) ) return;
		
		// now we can actually save the data
		$allowed = array( 
			'a' => array( // on allow a tags
				'href' => array() // and those anchords can only have href attribute
			)
		);
		
		// Probably a good idea to make sure your data is set
		if( isset( $_POST['iframe'] ) && !empty( $_POST['iframe'] ) )
			update_post_meta( $post_id, 'iframe', wp_kses( $_POST['iframe'], $allowed ) );

		$chk = isset( $_POST['featured'] ) && $_POST['featured'] ? 'true' : 'false';  
			update_post_meta( $post_id, 'featured', $chk );  
			
		if( isset( $_POST['gabfire_post_template'] ) && !empty( $_POST['gabfire_post_template'] ) )	
			update_post_meta( $post_id, 'gabfire_post_template', esc_attr( $_POST['gabfire_post_template'] ) );  				
			
		$chk2 = isset( $_POST['post_feaimage'] ) && $_POST['post_feaimage'] ? 'true' : 'false';  
			update_post_meta( $post_id, 'post_feaimage', $chk2 );  				
	}
	
	function gabfire_custom_fields_css() {
		wp_enqueue_style('gabfire-customfields-style', get_template_directory_uri() .'/framework/functions/css/custom-fields.css' );
	}
	add_action('admin_head-post.php', 'gabfire_custom_fields_css');
	add_action('admin_head-post-new.php', 'gabfire_custom_fields_css');
	
}