<?php if( ! defined( 'ABSPATH' ) ) exit;

/**
 * Enqueue scripts and styles marquee.
 */
function live_news_marquee_scripts() {	
	wp_enqueue_style( 'live-news-marquee-css', get_template_directory_uri() . '/include/marquee/marquee.css' );
	wp_enqueue_script( 'live-news-jquery-marquee-js', get_template_directory_uri() . '/include/marquee/jquery.marquee.js', array(), '', true);
}
add_action( 'wp_enqueue_scripts', 'live_news_marquee_scripts' );

function live_news_marquee () { 
    $the_query = new WP_Query( array(  'ignore_sticky_posts' => 1 ) );
?>
<div class='marquee-cont'>
	<span class="marquee-title"><?php echo esc_html( get_theme_mod( 'live_news_marquee_title', 'Latest Post') ); ?><span class="triangle-right"></span><span class="triangle-right-1"></span></span>

    <div class='marquee-with-options'>
	
	<?php
	if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
    $the_query->the_post(); 
	?>
		<a href="<?php echo esc_url(get_permalink()); ?>"> <span class="dashicons dashicons-welcome-view-site"></span> <?php echo esc_html( get_the_title() ); ?> </a>
	<?php
		}
	}
	/* Restore original Post Data */
	wp_reset_postdata();
	?>
	</div>
</div>	 
	 <?php
}

function live_news_marquee_vert () {
?>
<div class='marquee-vert' data-direction='down' data-pauseOnHover='true' data-pauseOnCycle='true'>
	
	<?php
	   $the_query = new WP_Query( array(  'ignore_sticky_posts' => 1 ) );
	if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
    $the_query->the_post(); 
	?>
		<div><a href="<?php echo esc_url( get_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?> </a></div>
	<?php
		}
	}
	/* Restore original Post Data */
	wp_reset_postdata();
	?>
	
</div>
	<?php
}

/**
 * Marquee customize.
 */

add_action( 'customize_register', 'live_news_customize_register_marquee' );

function live_news_customize_register_marquee( $wp_customize ) {
		
		$wp_customize->add_section( 'seos_marquee_section' , array(
			'title'       => __( 'Latest Post', 'live-news' ),
			'priority'    => 2,	
			//'description' => __( 'Social media buttons', 'seos-white' ),
		) );

 		$wp_customize->add_setting( 'show_marquee', array (
            'default' => '',		
			'sanitize_callback' => 'live_news_sanitize_checkbox',
		) );
				
		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'show_marquee', array(
			'label'    => __( 'Deactivate on Homepage', 'live-news' ),
			'section'  => 'seos_marquee_section',
			'priority'    => 1,				
			'settings' => 'show_marquee',
			'type' => 'checkbox',
		) ) );
		
 		$wp_customize->add_setting( 'show_marquee_all', array (
            'default' => '',		
			'sanitize_callback' => 'live_news_sanitize_checkbox',
		) );
				
		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'show_marquee_all', array(
			'label'    => __( 'Deactivate on All Pages', 'live-news' ),
			'section'  => 'seos_marquee_section',
			'priority'    => 1,				
			'settings' => 'show_marquee_all',
			'type' => 'checkbox',
		) ) );	
		
		$wp_customize->add_setting( 'live_news_marquee_title', array (
		    'default' => 'Latest Post',
			'sanitize_callback' => 'sanitize_text_field',
		) );
		
		$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'live_news_marquee_title', array(
			'label'    => __( 'Section Title', 'live-news' ),		
			'section'  => 'seos_marquee_section',
			'settings' => 'live_news_marquee_title',
			'type'     =>  'text'				
		) ) );		

 		$wp_customize->add_setting( 'live_news_marquee_speed', array (
            'default' => 0,		
			'sanitize_callback' => 'absint',
		) );
				
		 $wp_customize->add_control( 'live_news_marquee_speed', array(
		    'priority'    => 3,	
		    'type' => 'range',
		    'section' => 'seos_marquee_section',
		    'settings' => 'live_news_marquee_speed',
		    'label' => __( 'Slider Speed', 'live-news' ),
		    'input_attrs' => array(
			  'min' => 10,
			  'max' => 1000,
			  'step' => 1,
		    ),
		) );		
				
} 


function live_news_script_marquee () { ?>
	<script>
		jQuery(function(jQuery){
		  var $mwo = jQuery('.marquee-with-options');
		  jQuery('.marquee').marquee();
		  jQuery('.marquee-with-options').marquee({
			//speed in milliseconds of the marquee
			speed: <?php if ( get_theme_mod( 'live_news_marquee_speed') ) { echo esc_html( get_theme_mod( 'live_news_marquee_speed') ); } else echo "70"; ?>,
			//gap in pixels between the tickers
			gap: 10,
			//gap in pixels between the tickers
			delayBeforeStart: 0,
			//'left' or 'right'
			direction: 'left',
			//true or false - should the marquee be duplicated to show an effect of continues flow
			duplicated: true,
			//on hover pause the marquee - using jQuery plugin https://github.com/tobia/Pause
			pauseOnHover: true
		  });

		  //Direction upward
		  jQuery('.marquee-vert').marquee({
			direction: 'up',
			speed: <?php if ( get_theme_mod( 'live_news_sticky_speed') ) { echo esc_html ( get_theme_mod( 'live_news_sticky_speed') ); } else echo "200"; ?>,
			delayBeforeStart: 0,
			startVisible: false
		  });

		  //pause and resume links
		  jQuery('.pause').on("click",function(e){
			e.preventDefault();
			$mwo.trigger('pause');
		  });
		  jQuery('.resume').on("click",function(e){
			e.preventDefault();
			$mwo.trigger('resume');
		  });
		  //toggle
		  jQuery('.toggle').on("hover",function(e){
			$mwo.trigger('pause');
		  },function(){
			$mwo.trigger('resume');
		  })
		  .on("click",function(e){
			e.preventDefault();
		  })
		});
	</script>
<?php }

add_action('wp_head','live_news_script_marquee');