<?php

 	/*	
	*	get background 
	*	---------------------------------------------------------------------
	*/
function meris_get_background($args){
$background = "";
 if (is_array($args)) {
	if (isset($args['image']) && $args['image']!="") {
	$background =  "background:url(".$args['image']. ")  ".$args['repeat']." ".$args['position']." ".$args['attachment'].";";
	}
	else
	{
	if(isset($args['color']) && $args['color'] !=""){
	$background = "background:".$args['color'].";";
	}
	}
	}
return $background;
}

 	/*	
	*	send email
	*	---------------------------------------------------------------------
	*/
function meris_contact(){
	if(trim($_POST['contact-name']) === '') {
		$Error = __('Please enter your name.','meris');
		$name     = "";
		$hasError = true;
	} else {
		$name = trim($_POST['contact-name']);
	}
  
	if(trim($_POST['contact-email']) === '')  {
		$Error = __('Please enter your email address.','meris');
		$email    = "";
		$hasError = true;
	} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['contact-email']))) {
		$Error = __('You entered an invalid email address.','meris');
		$email    = "";
		$hasError = true;
	} else {
		$email = trim($_POST['contact-email']);
	}
	

	if(trim($_POST['contact-msg']) === '') {
		$Error =  __('Please enter a message.','meris');
		$message  = "";
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$message = stripslashes(trim($_POST['contact-msg']));
		} else {
			$message = trim($_POST['contact-msg']);
		}
	}

	if(!isset($hasError)) {
		
		$options = get_option("widget_meris_home_contact");
		$sendto  = $options[2]['contact_email'];
		
	   if (isset($sendto) && preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($sendto))) {
	     $emailTo = $sendto;
	   }
	   else{
	 	 $emailTo = get_option('admin_email');
		}
		 if($emailTo !=""){
		$subject = 'From '.$name;
		$body = "Name: $name \n\nEmail: $email \n\nMessage: $message";
		$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

		wp_mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
		}
		echo json_encode(array("msg"=>__("Your message has been successfully sent!","meris"),"error"=>0));
		
	}
	else
	{
	echo json_encode(array("msg"=>$Error,"error"=>1));
	}
	die() ;
	}
	add_action('wp_ajax_meris_contact', 'meris_contact');
	add_action('wp_ajax_nopriv_meris_contact', 'meris_contact');
	
	// get breadcrumbs
 function meris_get_breadcrumb(){
   global $post,$wp_query;
   $postid = isset($post->ID)?$post->ID:"";
   $show_breadcrumb = "";
   if ( 'page' == get_option( 'show_on_front' ) && ( '' != get_option( 'page_for_posts' ) ) && $wp_query->get_queried_object_id() == get_option( 'page_for_posts' ) ) { 
    $postid = $wp_query->get_queried_object_id();
   }
   if(isset($postid) && is_numeric($postid)){
    $show_breadcrumb = get_post_meta( $postid, '_meris_show_breadcrumb', true );
	}
	if($show_breadcrumb == 'yes' || $show_breadcrumb==""){

     new meris_breadcrumb;

	}
	}
	
	
/*
*  page navigation
*
*/
function meris_native_pagenavi($echo,$wp_query){
    if(!$wp_query){global $wp_query;}
    global $wp_rewrite;      
    $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
    $pagination = array(
    'base' => @add_query_arg('paged','%#%'),
    'format' => '',
    'total' => $wp_query->max_num_pages,
    'current' => $current,
    'prev_text' => '&laquo; ',
    'next_text' => ' &raquo;'
    );
 
    if( $wp_rewrite->using_permalinks() )
        $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg('s',get_pagenum_link(1) ) ) . 'page/%#%/', 'paged');
 
    if( !empty($wp_query->query_vars['s']) )
        $pagination['add_args'] = array('s'=>get_query_var('s'));
    if($echo == "echo"){
    echo '<div class="page_navi">'.paginate_links($pagination).'</div>'; 
	}else
	{
	
	return '<div class="page_navi">'.paginate_links($pagination).'</div>';
	}
}
   
    //// Custom comments list
   
   function meris_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   
   <li  <?php comment_class("comment"); ?> id="comment-<?php comment_ID() ;?>">
                                	<article class="comment-body">
                                    	<div class="comment-avatar"><?php echo get_avatar($comment,'52','' ); ?></div>
                                        <div class="comment-box">
                                            <div class="comment-info"><?php printf(__('%s <span class="says">says:</span>','meris'), get_comment_author_link()) ;?> <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ;?>">
<?php printf(__('%1$s at %2$s','meris'), get_comment_date(), get_comment_time()) ;?></a>  <?php edit_comment_link(__('(Edit)','meris'),'  ','') ;?></div>

 <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.','meris') ;?></em>
         <br />
      <?php endif; ?>
     <div class="comment-content"><?php comment_text() ;?>
      <div class="reply-quote">
             <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ;?>
			</div>
       </div>
    </div></article>

<?php
        }
		
 function meris_get_default_slider(){
	
	$controller   = '';
	$slideContent = '';
	
	$slide_time       = meris_options_array("slide_time");
	$slide_height     = meris_options_array("slide_height");
	$slide_height     = $slide_height==""?"":"height:".$slide_height.";";
	$slide_time       = is_numeric($slide_time)?$slide_time:"5000";
		  
	$return = '<section class="homepage-slider"><div id="carousel-meris-generic" style="'.$slide_height.'" class="carousel slide" data-interval="'.$slide_time.'" data-ride="carousel">';
	 for($i=1;$i<=5;$i++){
	$active = '';
	// $title = meris_options_array('meris_slide_title_'.$i);
	 $text  = meris_options_array('meris_slide_text_'.$i);
	 $image = meris_options_array('meris_slide_image_'.$i);
	 $link  = meris_options_array('meris_slide_link_'.$i);
	
	 if($i==1) $active     = 'active';

	 
	 if(isset($image) && strlen($image)>10){
		 $controller   .= '<li data-target="#carousel-meris-generic" data-slide-to="'.($i-1).'" class="'.$active.'"></li>';
		 
		 $slideContent .= '<div class="item '.$active.'">';
		 
		 if(trim($link) == ""){
         $slideContent .= '<img src="'.$image.'" alt="" />';
		 }else{
		$slideContent .= '<a href="'.esc_url($link).'" target="_blank"><img src="'.$image.'" alt="" /></a>';
			 }
         $slideContent .= '<div class="carousel-caption">'.$text.'</div></div>';
		 
		 }
		
	}
	     $return .= '<ol class="carousel-indicators">'. $controller .'</ol>';
		 $return .= '<div class="carousel-inner">'. $slideContent .'</div>';
		 
		 $return .= '<a class="left carousel-control" href="#carousel-meris-generic" data-slide="prev">
						<span class="fa fa-angle-left"></span>
					</a>
					<a class="right carousel-control" href="#carousel-meris-generic" data-slide="next">
						<span class="fa fa-angle-right"></span>
					</a>';
		$return .= '</div></section>';

        return $return;
   }
   
   	// get sidebar
	
 function meris_get_sidebar($sidebar,$default = true){

	 if($default){
	 if ( is_active_sidebar($sidebar) ){
	 dynamic_sidebar($sidebar);
	 }
	 else{
	 dynamic_sidebar('displayed_everywhere');
	 }
	 }else{
	 if ( is_active_sidebar($sidebar) ){
	 dynamic_sidebar($sidebar);
	 }
	 }

 }
		// fix shortcode

 function meris_fix_shortcodes($content){   
			$replace_tags_from_to = array (
				'<p>[' => '[', 
				']</p>' => ']', 
				']<br />' => ']',
				']<br>' => ']',
				']\r\n' => ']',
				']\n' => ']',
				']\r' => ']',
				'\r\n[' => '[',
			);

			return strtr( $content, $replace_tags_from_to );
		}

 function meris_the_content_filter($content) {
	  $content = meris_fix_shortcodes($content);
	  return $content;
	}
	
 add_filter( 'the_content', 'meris_the_content_filter' );
 
  function meris_enqueue_less_styles($tag, $handle) {
		global $wp_styles;
		$match_pattern = '/\.less$/U';
		if ( preg_match( $match_pattern, $wp_styles->registered[$handle]->src ) ) {
			$handle = $wp_styles->registered[$handle]->handle;
			$media = $wp_styles->registered[$handle]->args;
			$href = $wp_styles->registered[$handle]->src . '?ver=' . $wp_styles->registered[$handle]->ver;
			$rel = isset($wp_styles->registered[$handle]->extra['alt']) && $wp_styles->registered[$handle]->extra['alt'] ? 'alternate stylesheet' : 'stylesheet';
			$title = isset($wp_styles->registered[$handle]->extra['title']) ? "title='" . esc_attr( $wp_styles->registered[$handle]->extra['title'] ) . "'" : '';
	
			$tag = "<link rel='stylesheet' id='$handle' $title href='$href' type='text/less' media='$media' />";
		}
		return $tag;
	}
	add_filter( 'style_loader_tag', 'meris_enqueue_less_styles', 5, 2);
	
	
add_action( 'optionsframework_sidebar','meris_options_panel_sidebar' );

/**
 * Meris admin sidebar
 */
function meris_options_panel_sidebar() { ?>
	<div id="optionsframework-sidebar">
		<div class="metabox-holder">
	    	<div class="postbox">
	    		<h3><?php esc_attr_e( 'Quick Links', 'meris' ); ?></h3>
      			<div class="inside"> 
		          <ul>
                   <li><a href="<?php echo esc_url( 'http://www.mageewp.com/meris-theme.html' ); ?>" target="_blank">Upgrade to Pro</a></li>
                  <li><a href="<?php echo esc_url( 'http://www.mageewp.com/themes/' ); ?>" target="_blank">MageeWP Themes</a></li>
                  <li><a href="<?php echo esc_url( 'http://www.mageewp.com/documents/tutorials' ); ?>" target="_blank">Tutorials</a></li>
                  <li><a href="<?php echo esc_url( 'http://www.mageewp.com/documents/faq/' ); ?>" target="_blank">FAQ</a></li>
                  <li><a href="<?php echo esc_url( 'http://www.mageewp.com/knowledges/' ); ?>" target="_blank">Knowledge</a></li>
                  <li><a href="<?php echo esc_url( 'http://www.mageewp.com/forums/meris-theme/' ); ?>" target="_blank">Support Forums</a></li>
                  </ul>
      			</div>
	    	</div>
	  	</div>
	</div>
    <div class="clear"></div>
<?php
} 