<?php
/*
 * #? General diplay processing
*/

class erpDisplayC{

	private $opts;


	function __construct($givenOpts){
		/*  #? We set a low priority in case of other plugins to be rendered before  */
		add_filter('the_content', array($this, 'ConModifier'), 1000 );
		add_action( 'wp_enqueue_scripts', array( $this, 'displayScripts' ) ); 

		$this->opts = get_option($givenOpts);
	}
	
	/**
	 * Loads scripts to display
	 *
	 * @package Wordpress
	 * @since 1.1
	 */
	function displayScripts(){
		wp_enqueue_script( 'jquery' );
		// TODO Change this!!!!!!!!! wp_enqueue_script( 'erpAdminScripts', plugins_url('/inc/imgLiquid.js', __FILE__ ));
	}

	/**
	 * Returns the content to display
	 *
	 * @package Wordpress
	 * @since 1.0
	 * @param string $defcon  Default content of post.
	 * @return string $defcon Modified content if in single post and plugin in use, else unmodified content.
	 */
	function ConModifier($defcon) {

		global $post;

		/* #? Check if we are not inside a post or plugin is deactivated */
		/* TODO Find a way to exclude first pages when in paged post */
		if (!is_single() || !$this->opts['activate_plugin'] )  {
			return $defcon;
		} else {
			return  $defcon . $this->Displayer($this->GetTheArgs($this->opts));
		}
	}

	/**
	 * Returns the arguments to use in post query 
	 *
	 * @package Wordpress
	 * @since 1.0
	 * @param array $argOpt Plugin options
	 * @return array $argu Array to be used in post query (0 on error)
	 */
	function GetTheArgs() {

		global $post;
		$options = extract( $this->opts );
		
		if ( !$options ){
			return 0;
		}

		/* #? Get post standart categories
		 * TODO we must also get custom taxonomies
		*/
		$catAr = array();
		foreach((get_the_category($post->ID)) as $category){
			array_push($catAr,$category->cat_ID);
		}

		$argu = array(
				'post_status'			=> 'publish',
				'post_visibility'		=> 'public',
				'posts_per_page'        => $num_of_p_t_dspl,
				'ignore_sticky_posts' 	=> 1,
				'category__in'			=> $catAr,
				'post__not_in'			=> (array) $post->ID,
				'orderby'				=> 'date',
				'order'					=> 'DESC'
		);
		return $argu;
	}

	/**
	 * Frontend diplayer (For the main content area)
	 *
	 * @package Wordpress
	 * @since 1.0
	 * @param array $argum Arguments to be used in query
	 * @return string $print Content to be diplayed
	 */
	function Displayer($argum) {
		$q = new WP_Query($argum);
		$options = extract( $this->opts);
		
		if ( $q->have_posts() ) {
				
			$print = '';
			if ($display_layout == 'pop_up_block') {
				// #? Div metro is only used to count the width of main content area (js bellow)
				$print .= '<div id="metroW" style="width=100%; display:hidden; height:0px;"></div>';
			}
				
			$print .= '<div id="erp-wraper"><h2 class="erp_h2">' . $titletd . '</h2>';
			if ($display_layout == '1_post_per_row') {
				// Display posts in unordered list layout
				$print .= '<ul class="erp_ul_elem">';
			}
		
			// Cycle through all items retrieved
			while ( $q->have_posts() ) {
				$q->the_post();
				
				// TODO Change margins in css stylesheet to be aplpied only to last child and if javascript is enabled adjust this dynamicaly
				if ($display_layout == 'define_bellow' || $display_layout == 'pop_up_block') {
					$print .= '<div class="erp_container" style="width:' . ((100/$num_of_p_p_row) -2) . '%;">';
				}
				
				$conTtl = '<h3 class="erp_con_ttl"  style="font-size:' . ($ttl_sz ? $ttl_sz . "px" : "100%") . ';">' . get_the_title( get_the_ID() ) . '</h3>';
		
				$conExc = $erpcontent == 'post_title' ? '' : '<span class="erp_con_exc"  style="font-size:' . ($exc_sz ? $exc_sz . "px" : "") . ';">' . wp_trim_words(get_the_content(), $exc_len , '<span style="font-size:80%;"><em>' . $more_txt . '</em></span>') . '</span>';
				/* #? Get the thumb, medium size should be ok for most of themes */
				if ($thumbnail_height && $crop_thumbnail && has_post_thumbnail(get_the_ID()) && $display_thumbnail) {
					$th = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),"large");
					$src = substr($th[0], strlen(get_bloginfo('wpurl')));
					$th = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),"large");
					$thumb_display = '<img class="erp_img" src="' . plugins_url( basename( dirname( __FILE__ ) ) ) . '/inc/tim/tb.php?src=' . $src . '&h=' . ($thumbnail_height>$th[2] ? $th[2] : $thumbnail_height) . '&w=' . ($thumbnail_width ? $thumbnail_width : $th[1]) . '" />';
				} else if (has_post_thumbnail(get_the_ID()) && $display_thumbnail ){
					$thumb_display = get_the_post_thumbnail(get_the_ID(), 'medium' ) ;
				} else {
					$thumb_display = 0;
				}
				
				if ($display_layout == '1_post_per_row') {
					$print .= '<li class="erp_li_elem" style="margin-top:0px; list-style-type: none;">';
				}
				
				$print .= '<a class="erp_a_elem" style="text-decoration:none;" href="' . get_permalink() . '">' . ($thumb_display ? $thumb_display : "") . $conTtl  . $conExc . '</a>' ;
				
				if ($display_layout == '1_post_per_row') {
					$print .= '</li><hr class="erpHorRule">';
				}
		
				if ($display_layout == 'define_bellow' || $display_layout == 'pop_up_block') {
					$print .= '</div>';
				}
			}
			if ($display_layout == '1_post_per_row') {
				$print .= '</ul>';
			}
			$print .= '</div>';
			if ($display_layout == 'pop_up_block'){
				$print .= '<script type="text/javascript">
					jQuery(document).ready(function($){
											// modify wraper
											var w = $("#metroW").width();
											$("#erp-wraper").css({"width": w+"px", "position":"fixed", "z-index":100, "bottom":"0px", "display":"none", "background": "rgb(200,200,200)", "background": "rgba(150, 150, 150, 0.6)", "border-radius":"8px 8px 0 0" });
											$(".erp_container").css({ "margin": "0px 1% 20px 1%"});
		
											var closeButton = $("#erp-wraper .erp_h2").before("<div id=\"erp_cont_close\" type=\"button\" style=\"background-image: url(' . plugins_url( basename( dirname( __FILE__ ) ) ) . '/inc/img/close.gif);  background-repeat: no-repeat no-repeat; float:right; height: 13px; width: 13px; margin:3px 3px 0 0; \"></div>");
											var openButton = $("#erp-wraper .erp_h2").before("<div id=\"erp_cont_open\"  style=\"background-image: url(' . plugins_url( basename( dirname( __FILE__ ) ) ) . '/inc/img/open.gif);  background-repeat: no-repeat no-repeat; float:right; height: 13px; width: 13px; margin:3px 3px 0 0; display:none;\"></div>");
						
											$("#erp-wraper .erp_container").wrapAll("<div id=\"containerWraper\"></div>");
											$("#erp_cont_close").click(function(){ $("#containerWraper").slideToggle("fast", function(){ $("#erp_cont_close").hide(1); $("#erp_cont_open").show(1); });});
											$("#erp_cont_open").click(function(){ $("#containerWraper").slideToggle("fast", function(){  $("#erp_cont_open").hide(1);  $("#erp_cont_close").show(1); });});
						
											$("#erp-wraper .erp_h2").css("margin","0 0 7px 1%");
											$(window).scroll(function(){
												// get the height of body
												var h = $("#metroW").parent().height();
												var y = $(window).scrollTop();
												if( y > (h*.95) ){
													$("#erp-wraper").slideDown("slow");
												} else {
													$("#erp-wraper").slideUp("slow");
												}
											});
					});
					</script>';
			}
		}
		wp_reset_postdata();
		
		return $print;
		
	}
} // class erpDisplayC

?>