<?php
/* Get the theme options
================================================== */
global $tw_options; // This is your opt_name ?>

<div class="top-bar">

		<div class="container">

			<div class="topbar-left">

			<?php
			/**
			 * If most popular posts is installed
			 */
			if (function_exists('wmp_get_popular') && $tw_options['left-side'] == 1) : ?>

				<?php

					/**
					 * Sanitize timeline
					 */
					switch($tw_options['breaking-timeline']) {
						case 2:
							$range = 'monthly';
							break;
						case 3:
							$range = 'weekly';
							break;
						case 4:
							$range = 'daily';
							break;
						default:
							$range = 'all_time';
							break;
					}

					switch($tw_options['breaking-animation']) {
						case 2:
							$animation = 'fade';
							break;
						case 3:
							$animation = 'vertical';
							break;
						default:
							$animation = 'scroll';
							break;
					}

				$delay 		= ($tw_options['breaking-delay']) ? $tw_options['breaking-delay'] : '5';
				$num_posts 	= ($tw_options['breaking-number']) ? $tw_options['breaking-number'] : '5';

				?>

					<!-- Breaking News -->
					<div class="breaking range-<?php echo esc_attr( $range ); ?> tw-<?php echo esc_attr( $animation ); ?>">

						<!-- Breaking News Title -->
						<div class="breaking-title">
							<?php
								if ($tw_options['breaking-text-switch'] == 0) {
									_e('Breaking', 'themewich');
								} else {
									echo str_replace(' ', '&nbsp;', $tw_options['breaking-text']);
								}
							?>
						</div>
						<!-- End Breaking News Title -->

						<div class="breaking-ticker">
							<ul class="news-ticker tw-<?php echo esc_attr( $animation ); ?>" data-delay="<?php echo esc_attr( $tw_options['breaking-delay'] );?>" data-animation="<?php echo esc_attr( $animation ); ?>">

							<?php
								$popposts = wmp_get_popular( array(
									'limit' 	=> $num_posts,
									'post_type' => 'post',
									'range' 	=> $range
								) );
								global $post;
								if ( count( $popposts ) > 0 ) :
									foreach ( $popposts as $post ):
										setup_postdata( $post );

										$title = get_the_title();

										if (strlen($title) >= 46) {
											$title = mb_substr($title, 0, 46) . "... ";
										} ?>

										<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php echo sanitize_text_field($title); ?></a></li>

									<?php
									endforeach;
								else :

									// Regular posts
									$the_query = new WP_Query( array(
										'showposts' => 6,
										'post_type' => 'post' // get only posts
									) );

									if ( $the_query->have_posts() ) :
										while ( $the_query->have_posts() ) : $the_query->the_post();

											// get the title
											$title = get_the_title();

											// shorten title so it fits
											if (strlen($title) >= 46) {
												$title = mb_substr( $title, 0, 46 ) . "... ";
											} ?>

											<li>
												<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
													<?php echo esc_html( $title ); ?>
												</a>
											</li>

										<?php endwhile;
									endif;
								endif;

								// reset post data
								wp_reset_postdata(); ?>

							</ul>
						</div>
					</div>

					<script>
						jQuery(document).ready(function() {
							(function($){
								var newsticker = $('.news-ticker'),
									$delay 		= parseInt(newsticker.attr('data-delay') + '000'),
									$animation 	= newsticker.attr('data-animation');

								if ($animation == 'scroll') {
									$ticker = newsticker.bxSlider({
										useCSS: false,
										pager: false,
										ticker: true,
										tickerHover: true,
										autoHover: true,
										speed: $delay * 4
									});
								} else {
									$ticker = newsticker.bxSlider({
										useCSS: false,
										autoHover: true,
										mode: $animation,
										auto: true,
										pager: false,
										pause: $delay
									});
								}
							})(jQuery);
						});
					</script>

				<?php else : ?>
					<?php echo wp_kses_post( $tw_options['left-text'] ); ?>
				<?php endif; ?>

			</div>

			<div class="topbar-right">
				<?php
					if ( $tw_options['right-side'] == 1 ) :
							foreach ( $tw_options['social-order'] as $key => $social ) {
								$url = isset($tw_options['social-urls'][$key]) ? addhttp($tw_options['social-urls'][$key]) : '';
								if ( filter_var($url, FILTER_VALIDATE_URL) && $social ) {
									echo '<a href="'.$url.'" class="" target="_blank">';
									echo '<i class="fa fa-'.$key.'"></i>';
									echo '</a>';
								}
							}
					else :
						echo wp_kses_post( $tw_options['right-text'] );
					endif;
				?>
			</div>
		</div>
	</div>