<?php
/* Clases For wordpress countdown*/

class wpdevart_countdown_forntend_main{
	protected static $id_counter=0; // for seting counter
	protected $timer; // timer information
	protected $theme; // design information
	// apply settings to 
	function __construct($timer,$theme){
		$this->timer=$timer;
		$this->theme=$theme;
		self::$id_counter++;
	}
	// get seconds left for repeating
	protected function get_repeat_seconds(){
		return $this->timer['after_countdown_repeat_time']['hour']*3600+$this->timer['after_countdown_repeat_time']['minute']*60;
	}
	protected function get_session_seconds(){
		return $this->timer['timer_seesion_time']['hour']*3600+$this->timer['timer_seesion_time']['minute']*60;
	}
	
	protected function get_texts(){
		if($this->theme["countdown_text_type"]=="standart"){
			return array(
				"week"=>$this->theme["text_for_weeks"],
				"day"=>$this->theme["text_for_day"],
				"hour"=>$this->theme["text_for_hour"],
				"minut"=>$this->theme["text_for_minute"],
				"second"=>$this->theme["text_for_second"],
			);
		}else{
			return array(
				"week"=>__("Weeks","wpdevart_countdown_n"),
				"day"=>__("Days","wpdevart_countdown_n"),
				"hour"=>__("Hours","wpdevart_countdown_n"),
				"minut"=>__("Minutes","wpdevart_countdown_n"),
				"second"=>__("Seconds","wpdevart_countdown_n"),
			);
		}
	}
	private function get_session_fixed_time(){
		$curent_timezone=date_default_timezone_get();
			if(!isset($_SESSION["wpdevart_curent_user_session_time".$this->timer["timer_id"]])){
				$_SESSION["wpdevart_curent_user_session_time".$this->timer["timer_id"]]=mktime ((int)date("H"),(int)date("i"),(int)date("s") ,(int)date("n"), (int)date("j"),(int)date("Y"));
			}			
		date_default_timezone_set($curent_timezone);
		return $_SESSION["wpdevart_curent_user_session_time".$this->timer["timer_id"]];
	}
	// how many seconds left
	
	protected function get_secons_left(){
		// set timezone
		$curent_timezone=date_default_timezone_get();
		date_default_timezone_set($this->timer['timer_timezone']);
		if($this->timer["timer_coundown_type"]=="session_countdown" || $this->timer["timer_coundown_type"]=="session_countup"){
			$end_date=$this->get_session_fixed_time()+$this->get_session_seconds();
		}else{
			$this->timer['timer_end_date'];		
			$exploded_date_time=explode(" ",$this->timer['timer_end_date']);
			$exploded_date=explode("/",$exploded_date_time[0]);			
			if(isset($exploded_date_time[1]))
				$exploded_time=explode(":",$exploded_date_time[1]);
			else
				$exploded_time=array("00","00");
			$year=(int)$exploded_date[2];
			$month=(int)$exploded_date[1];
			$day=(int)$exploded_date[0];		
			$hour=(int)$exploded_time[0];
			$minute=(int)$exploded_time[1];		

			// get end of date
			$end_date=mktime ($hour, $minute, 0, $month, $day, $year);
		}
		// get curent date		
		$curent_date=mktime ((int)date("H"),(int)date("i"),(int)date("s") ,(int)date("n"), (int)date("j"),(int)date("Y"));
		// return default timezone
		date_default_timezone_set($curent_timezone);
		// calculate seconds left
		$seconds_left=$end_date-$curent_date;
		
		if($seconds_left<=0){
			$this->timer['time_is_expired']="1";
			// when coundown enabled repeat method
			if($this->timer['after_countdown_end_type']=="repeat"){
				$seconds_of_timer_repeat=$this->get_repeat_seconds();
				$qunatity_of_repeats=(int)ceil((abs($seconds_left)%$seconds_of_timer_repeat!=0)?abs($seconds_left)/$seconds_of_timer_repeat:(abs($seconds_left)/$seconds_of_timer_repeat+1));
				$seconds_left=$seconds_left+$qunatity_of_repeats*$seconds_of_timer_repeat;
				return $seconds_left;
			}			
			return 0;
		}else{
			return $seconds_left;
		}
	}
	
	/* get interval when timer is saved and end date*/
	protected function get_second_from_start(){
		// set timezone
		$curent_timezone=date_default_timezone_get();
		date_default_timezone_set($this->timer['timer_timezone']);
		if($this->timer["timer_coundown_type"]=="session_countdown" || $this->timer["timer_coundown_type"]=="session_countup"){
			$end_date=$this->get_session_fixed_time()+$this->get_session_seconds();			
		}else{
			$this->timer['timer_end_date'];
			$exploded_date_time=explode(" ",$this->timer['timer_end_date']);
			$exploded_date=explode("/",$exploded_date_time[0]);
			$exploded_time=explode(":",$exploded_date_time[1]);
			$year=(int)$exploded_date[2];
			$month=(int)$exploded_date[1];
			$day=(int)$exploded_date[0];		
			$hour=(int)$exploded_time[0];
			$minute=(int)$exploded_time[1];
			// get end of date
			$end_date=mktime ($hour, $minute, 0, $month, $day, $year);		
		}		
		// get curent date		
		$curent_date=mktime ((int)date("H"),(int)date("i"),(int)date("s") ,(int)date("n"), (int)date("j"),(int)date("Y"));
		// return default timezone
		date_default_timezone_set($curent_timezone);
		// calculate seconds left
		$seconds_left=$end_date-$curent_date;
		
		if($seconds_left<=0){
			$this->timer['time_is_expired']="1";
			return $this->get_repeat_seconds();
		}else{
			if($this->timer["timer_coundown_type"]=="session_countdown" || $this->timer["timer_coundown_type"]=="session_countup"){
				return max($seconds_left,$this->get_session_seconds());
			}else{
				return max($seconds_left,($end_date-$this->timer['start_time']));
			}
		}
	}
	protected function get_timer_start_time(){
		
		// set timezone
		$curent_timezone=date_default_timezone_get();
		date_default_timezone_set($this->timer['timer_timezone']);
		if($this->timer["timer_coundown_type"]=="session_countdown" || $this->timer["timer_coundown_type"]=="session_countup"){
			$start_date=$this->get_session_fixed_time();
			// return default timezone
			date_default_timezone_set($curent_timezone);			
		}else{
			$exploded_date_time=explode(" ",$this->timer['timer_start_time']);
			$exploded_date=explode("/",$exploded_date_time[0]);
			$exploded_time=explode(":",$exploded_date_time[1]);
			$year=(int)$exploded_date[2];
			$month=(int)$exploded_date[1];
			$day=(int)$exploded_date[0];		
			$hour=(int)$exploded_time[0];
			$minute=(int)$exploded_time[1];
			// get end of date
			$start_date=mktime ($hour, $minute, 0, $month, $day, $year);
		}
		// get curent date		
		$curent_date=mktime ((int)date("H"),(int)date("i"),(int)date("s") ,(int)date("n"), (int)date("j"),(int)date("Y"));
		// return default timezone
		date_default_timezone_set($curent_timezone);
		// calculate seconds left
		$seconds_left=$start_date-$curent_date;
		return $seconds_left;		
	}
}

// standart_countdown class
class wpdevart_countdown_forntend_stanadart_view extends wpdevart_countdown_forntend_main{

	public function create_countdown(){	
		$this->timer['time_is_expired']="0";
		$params_array=array();		
		$params_array["seconds_left"]=$this->get_secons_left();
		$params_array["repeat_seconds"]=$this->get_repeat_seconds();
		$params_array["timer_start_time"]=$this->get_timer_start_time();
		$params_array["time_is_expired"]=$this->timer["time_is_expired"];
		$params_array["after_countdown_end_type"]=$this->timer["after_countdown_end_type"];
		$params_array["after_countdown_text"]=$this->timer["after_countdown_text"];
		$params_array["before_countup_start_type"]=$this->timer["before_countup_start_type"];
		$params_array["before_countup_text"]=$this->timer["before_countup_text"];
		$params_array["coundown_type"]=$this->timer["timer_coundown_type"];		
		$params_array["after_countdown_redirect"]=$this->timer["after_countdown_redirect"];		
		$params_array["display_days"]=$this->theme["countdown_date_display"];
		$params_array["gorup_animation"]=$this->theme["countdown_standart_gorup_animation"];
		$params_array["inline"]=$this->theme["countdown_standart_display_inline"];		
		$params_array["effect"]=($this->theme["countdown_standart_animation_type"]=="random")?wpda_contdown_extend_library::get_randowm_animation():$this->theme["countdown_standart_animation_type"];
		$params_array["display_days_texts"]=$this->get_texts();
		$params_converted_to_js_objec=json_encode($params_array);
		$countdown_html='<div class="wpdevart_countdown_extend_standart" id="wpdevart_countdown_'.self::$id_counter.'"></div>';
		$countdown_script='<script>jQuery("#wpdevart_countdown_'.self::$id_counter.'").wpdevart_countdown_extend_standart('.$params_converted_to_js_objec.')</script>';
		$countdown_style='<style>'.$this->get_css('wpdevart_countdown_'.self::$id_counter).'</style>';
		return $countdown_html.$countdown_script.$countdown_style;
	}	
	
	private function get_css($main_id){
		$main_id="#".$main_id;
		$css="";
		$css.=$main_id."{width:".$this->theme["countdown_global_width"].$this->theme["countdown_global_width_metrick"].";}";
		$css.=$main_id." .wpdevart_countdown_extend_element{min-width:".$this->theme["countdown_standart_elements_width"]."px;}";
		$css.=$main_id." .wpdevart_countdown_extend_element{margin-right:".$this->theme["countdown_standart_elements_distance"]."px;}";
		//$css.=$main_id." .wpdevart_countdown_extend_element:last-child{margin-right:0px;}";
		$css.=$main_id." .time_left_extended{";
		$css.="background-color:".$this->theme["countdown_standart_time_bg_color"].";";
		$css.="font-size:".$this->theme["countdown_standart_time_font_size"]."px;";
		$css.="color:".$this->theme["countdown_standart_time_color"].";";
		$css.="font-family:".$this->theme["countdown_standart_time_font_famely"].";";
		$css.="padding:".$this->theme["countdown_standart_time_padding"]["top"]."px ".$this->theme["countdown_standart_time_padding"]["right"]."px ".$this->theme["countdown_standart_time_padding"]["bottom"]."px ".$this->theme["countdown_standart_time_padding"]["left"]."px;";
		$css.="margin:".$this->theme["countdown_standart_time_margin"]["top"]."px ".$this->theme["countdown_standart_time_margin"]["right"]."px ".$this->theme["countdown_standart_time_margin"]["bottom"]."px ".$this->theme["countdown_standart_time_margin"]["left"]."px;";
		$css.="border-width:".$this->theme["countdown_standart_time_border_width"]."px;";
		$css.="border-radius:".$this->theme["countdown_standart_time_border_radius"]."px;";
		$css.="border-color:".$this->theme["countdown_standart_time_border_color"].";";
		$css.="}";
		$css.=$main_id." .time_text{";
		$css.="background-color:".$this->theme["countdown_standart_time_text_bg_color"].";";
		$css.="font-size:".$this->theme["countdown_standart_time_text_font_size"]."px;";
		$css.="color:".$this->theme["countdown_standart_time_text_color"].";";	
		$css.="font-family:".$this->theme["countdown_standart_time_text_font_famely"].";";
		$css.="padding:".$this->theme["countdown_standart_time_text_padding"]["top"]."px ".$this->theme["countdown_standart_time_text_padding"]["right"]."px ".$this->theme["countdown_standart_time_text_padding"]["bottom"]."px ".$this->theme["countdown_standart_time_text_padding"]["left"]."px;";
		$css.="margin:".$this->theme["countdown_standart_time_text_margin"]["top"]."px ".$this->theme["countdown_standart_time_text_margin"]["right"]."px ".$this->theme["countdown_standart_time_text_margin"]["bottom"]."px ".$this->theme["countdown_standart_time_text_margin"]["left"]."px;";
		$css.="border-width:".$this->theme["countdown_standart_time_text_border_width"]."px;";
		$css.="border-radius:".$this->theme["countdown_standart_time_text_border_radius"]."px;";
		$css.="border-color:".$this->theme["countdown_standart_time_text_border_color"].";";
		$css.="}";
		if($this->theme["countdown_standart_animation_type"]!="none"){
			if($this->theme["countdown_standart_gorup_animation"]=="group"){
				$css.=$main_id."{";
				$css.="visibility: hidden;";
				$css.="}";
			}else{
				$css.=$main_id." .wpdevart_countdown_extend_element{";
				$css.="visibility: hidden;";
				$css.="}";

			}
		}
		
		return $css;
	}
}

// vertical_countdown class
class wpdevart_countdown_forntend_vertical_view extends wpdevart_countdown_forntend_main{
	// generete html
	public function create_countdown(){
		$params_array=array();
		
		
		
		$this->timer['time_is_expired']="0";
		$params_array=array();		
		$params_array["seconds_left"]=$this->get_secons_left();
		$params_array["repeat_seconds"]=$this->get_repeat_seconds();
		$params_array["timer_start_time"]=$this->get_timer_start_time();
		$params_array["time_is_expired"]=$this->timer["time_is_expired"];
		$params_array["after_countdown_end_type"]=$this->timer["after_countdown_end_type"];
		$params_array["after_countdown_text"]=$this->timer["after_countdown_text"];
		$params_array["before_countup_start_type"]=$this->timer["before_countup_start_type"];
		$params_array["before_countup_text"]=$this->timer["before_countup_text"];
		$params_array["coundown_type"]=$this->timer["timer_coundown_type"];		
		$params_array["after_countdown_redirect"]=$this->timer["after_countdown_redirect"];		
		$params_array["display_days"]=$this->theme["countdown_date_display"];
		$params_array["gorup_animation"]=$this->theme["countdown_vertical_gorup_animation"];
		$params_array["inline"]=$this->theme["countdown_vertical_display_inline"];		
		$params_array["effect"]=($this->theme["countdown_vertical_animation_type"]=="random")?wpda_contdown_extend_library::get_randowm_animation():$this->theme["countdown_vertical_animation_type"];
		$params_array["display_days_texts"]=$this->get_texts();
		$params_converted_to_js_objec=json_encode($params_array);
		
		$countdown_html='<div class="wpdevart_countdown_extend_vertical" id="wpdevart_countdown_'.self::$id_counter.'"></div>';
		$countdown_script='<script>jQuery(document).ready(function(){jQuery("#wpdevart_countdown_'.self::$id_counter.'").wpdevart_countdown_extend_vertical('.$params_converted_to_js_objec.');})</script>';
		$countdown_style='<style>'.$this->get_css('wpdevart_countdown_'.self::$id_counter).'</style>';
		return $countdown_html.$countdown_script.$countdown_style;
	}	
	// generete css
	public function get_css($main_id){
		$main_id="#".$main_id;
		$css="";
		$css.=$main_id." .wpdevart_countdown_extend_element{margin-right:".$this->theme["countdown_vertical_elements_distance"]."px;}";
		$css.=$main_id." .time_left_extended > span{";
		$css.="		
			background: ".$this->theme["countdown_vertical_background_color"]."; /* Old browsers */
			background: -moz-linear-gradient(top, ".$this->theme["countdown_vertical_background_color"]." 38%, ".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")." 100%); /* FF3.6+ */
			background: -webkit-gradient(linear, left top, left bottom, color-stop(38%,".$this->theme["countdown_vertical_background_color"]."), color-stop(100%,".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20").")); /* Chrome,Safari4+ */
			background: -webkit-linear-gradient(top, ".$this->theme["countdown_vertical_background_color"]." 38%,".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")." 100%); /* Chrome10+,Safari5.1+ */
			background: -o-linear-gradient(top, ".$this->theme["countdown_vertical_background_color"]." 38%,".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")." 100%); /* Opera 11.10+ */
			background: -ms-linear-gradient(top, ".$this->theme["countdown_vertical_background_color"]." 38%,".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")." 100%); /* IE10+ */
			background: linear-gradient(to bottom, ".$this->theme["countdown_vertical_background_color"]." 38%,".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")." 100%); /* W3C */
			filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='".$this->theme["countdown_vertical_background_color"]."', endColorstr='".wpda_contdown_extend_library::darkest_color($this->theme["countdown_vertical_background_color"],"20")."',GradientType=0 ); /* IE6-9 */";
		$css.="border-width:".$this->theme["countdown_vertical_time_border_width"]."px;";
		$css.="border-color:".$this->theme["countdown_vertical_time_border_color"].";";

		$css.=$main_id."}";
		$css.=$main_id." .time_left_extended li{";
		$css.="font-size:".$this->theme["countdown_vertical_time_font_size"]."px;";
		$css.="color:".$this->theme["countdown_vertical_time_color"].";";
		$css.="font-family:".$this->theme["countdown_vertical_time_font_famely"].";";
	//	$css.="margin:".$this->theme["countdown_vertical_time_margin"]["top"]."px ".$this->theme["countdown_circle_time_margin"]["right"]."px ".$this->theme["countdown_circle_time_margin"]["bottom"]."px ".$this->theme["countdown_circle_time_margin"]["left"]."px;";
		$css.="}";
		$css.=$main_id." .time_text{";
		$css.="background-color:".$this->theme["countdown_vertical_time_text_bg_color"].";";
		$css.="font-size:".$this->theme["countdown_vertical_time_text_font_size"]."px;";
		$css.="color:".$this->theme["countdown_vertical_time_text_color"].";";	
		$css.="font-family:".$this->theme["countdown_vertical_time_text_font_famely"].";";
		$css.="padding:".$this->theme["countdown_vertical_time_text_padding"]["top"]."px ".$this->theme["countdown_vertical_time_text_padding"]["right"]."px ".$this->theme["countdown_vertical_time_text_padding"]["bottom"]."px ".$this->theme["countdown_vertical_time_text_padding"]["left"]."px;";
		$css.="margin:".$this->theme["countdown_vertical_time_text_margin"]["top"]."px ".$this->theme["countdown_vertical_time_text_margin"]["right"]."px ".$this->theme["countdown_vertical_time_text_margin"]["bottom"]."px ".$this->theme["countdown_vertical_time_text_margin"]["left"]."px;";
		$css.="border-width:".$this->theme["countdown_vertical_time_text_border_width"]."px;";
		$css.="border-radius:".$this->theme["countdown_vertical_time_text_border_radius"]."px;";
		$css.="border-color:".$this->theme["countdown_vertical_time_text_border_color"].";";
		$css.="}";
		if($this->theme["countdown_vertical_animation_type"]!="none"){
			if($this->theme["countdown_vertical_gorup_animation"]=="group"){
				$css.=$main_id."{";
				$css.="visibility: hidden;";
				$css.="}";
			}else{
				$css.=$main_id." .wpdevart_countdown_extend_element{";
				$css.="visibility: hidden;";
				$css.="}";

			}
		}
		return $css;
		
	}	
}
// circle_countdown class
class wpdevart_countdown_forntend_circle_view extends wpdevart_countdown_forntend_main{
	// generete html
	public function create_countdown(){
		
		$this->timer['time_is_expired']="0";
		$params_array=array();		
		$params_array["seconds_left"]=$this->get_secons_left();
		$params_array["repeat_seconds"]=$this->get_repeat_seconds();
		$params_array["start_time"]=$this->get_second_from_start();	
		$params_array["timer_start_time"]=$this->get_timer_start_time();
		$params_array["bg_color"]=$this->theme["countdown_circle_border_color_outside"];
		$params_array["fg_color"]=$this->theme["countdown_circle_border_color_inside"];
		$params_array["thickness"]=$this->theme["countdown_circle_width_parcents"]/100;
		$params_array["linecap"]=$this->theme["countdown_circle_type_of_rounding"];
		$params_array["direction"]=$this->theme["countdown_circle_border_direction"];
		$params_array["time_is_expired"]=$this->timer["time_is_expired"];
		$params_array["after_countdown_end_type"]=$this->timer["after_countdown_end_type"];
		$params_array["after_countdown_text"]=$this->timer["after_countdown_text"];
		$params_array["before_countup_start_type"]=$this->timer["before_countup_start_type"];
		$params_array["before_countup_text"]=$this->timer["before_countup_text"];
		$params_array["coundown_type"]=$this->timer["timer_coundown_type"];		
		$params_array["after_countdown_redirect"]=$this->timer["after_countdown_redirect"];		
		$params_array["display_days"]=$this->theme["countdown_date_display"];
		$params_array["gorup_animation"]=$this->theme["countdown_circle_gorup_animation"];
		$params_array["inline"]=$this->theme["countdown_circle_display_inline"];		
		$params_array["effect"]=($this->theme["countdown_circle_animation_type"]=="random")?wpda_contdown_extend_library::get_randowm_animation():$this->theme["countdown_circle_animation_type"];
		$params_array["display_days_texts"]=$this->get_texts();
		$params_converted_to_js_objec=json_encode($params_array);
		$countdown_html='<div class="wpdevart_countdown_extend_circle" id="wpdevart_countdown_'.self::$id_counter.'"></div>';
		$countdown_script='<script>jQuery(document).ready(function(){jQuery("#wpdevart_countdown_'.self::$id_counter.'").wpdevart_countdown_extend_circle('.$params_converted_to_js_objec.')});</script>';
		$countdown_style='<style>'.$this->get_css('wpdevart_countdown_'.self::$id_counter).'</style>';
		return $countdown_html.$countdown_script.$countdown_style;
	}	
	// generete css
	public function get_css($main_id){
		$main_id="#".$main_id;
		$css="";
		$css.=$main_id."{width:".$this->theme["countdown_global_width"].$this->theme["countdown_global_width_metrick"].";}";
		$css.=$main_id." .wpdevart_countdown_extend_element{width:".$this->theme["countdown_circle_elements_width_height"]."px;height:".$this->theme["countdown_circle_elements_width_height"]."px;}";
		$css.=$main_id." .wpdevart_countdown_extend_element{margin-right:".$this->theme["countdown_circle_elements_distance"]."px;}";
		$css.=$main_id." .wpdevart_countdown_extend_element div.wpdevart_countdown_background{background-color:".wpda_contdown_extend_library::hex2rgba($this->theme["countdown_circle_background_color"], $this->theme["countdown_circle_background_color_opacity"]/100).";}";
		$css.=$main_id." .time_left_extended{";
		$css.="font-size:".$this->theme["countdown_circle_time_font_size"]."px;";
		$css.="color:".$this->theme["countdown_circle_time_color"].";";
		$css.="font-family:".$this->theme["countdown_circle_time_font_famely"].";";
		$css.="margin:".$this->theme["countdown_circle_time_margin"]["top"]."px 0px ".$this->theme["countdown_circle_time_margin"]["bottom"]."px 0px;";
		$css.="}";
		$css.=$main_id." .time_text{";
		$css.="font-size:".$this->theme["countdown_circle_time_text_font_size"]."px;";
		$css.="color:".$this->theme["countdown_circle_time_text_color"].";";	
		$css.="font-family:".$this->theme["countdown_circle_time_text_font_famely"].";";
		$css.="padding:".$this->theme["countdown_circle_time_text_padding"]["top"]."px ".$this->theme["countdown_circle_time_text_padding"]["right"]."px ".$this->theme["countdown_circle_time_text_padding"]["bottom"]."px ".$this->theme["countdown_circle_time_text_padding"]["left"]."px;";
		$css.="margin:".$this->theme["countdown_circle_time_text_margin"]["top"]."px 0px ".$this->theme["countdown_circle_time_text_margin"]["bottom"]."px 0px;";
		$css.="}";
		if($this->theme["countdown_circle_animation_type"]!="none"){
			if($this->theme["countdown_circle_gorup_animation"]=="group"){
				$css.=$main_id."{";
				$css.="visibility: hidden;";
				$css.="}";
			}else{
				$css.=$main_id." .wpdevart_countdown_extend_element{";
				$css.="visibility: hidden;";
				$css.="}";

			}
		}
		return $css;
		
	}	
}




?>