<?
class page
{
	function init() {
		global $X;


		if(!empty($_GET['page_key'])) $_GET['page_id']=$X->DB->fast(' SELECT id FROM strony WHERE x_key="'.$_GET['page_key'].'" AND active="1" ');
		$dane=$X->DB->fast(' SELECT * FROM strony WHERE id="'.(int)$_GET['page_id'].'" AND active="1" ');
		if(!empty($dane['id'])) {
			$X->DB->q(' UPDATE strony SET counter=counter+1 WHERE id="'.$dane['id'].'" ');
			$_GET['sel_x_key']=$dane['x_key'];
		} else {
			return $X->T->error_404();
		}

		$R['title']=$X->STR->outt(!empty($dane['seo_t']) ? $dane['seo_t'] : $dane['title']);
		$R['keywords']=$X->STR->outt($dane['seo_k']);
		$R['description']=$X->STR->outt($dane['seo_d']);


		if(!empty($dane['x_static_file'])) {
			if(file_exists(ROOT_PATH.'/'.$dane['x_static_file'])) {
				if($X->FILE->get_ext($dane['x_static_file'])=='php') {
					$R['html']=require_once(ROOT_PATH.'/'.$dane['x_static_file']);
				} else {
					$R['html']=file_get_contents(ROOT_PATH.'/'.$dane['x_static_file']);
				}
			} else {
				$R['html']='404 / Not Found:&nbsp; <b>'.ROOT_PATH.'/'.$dane['x_static_file'].'</b>';
			}
		} else {
			$content=$X->STR->out($dane['content']);
			$content=str_replace('href="#','href="'.URL.'#',$content);

			$R['html']=$content;
		}

		$R['html']=preg_replace_callback('#<a href="mailto:([^"]+)">([^<]*)</a>#is', array($this,"replace_email"), $R['html']);




		$sql_z=$X->DB->q(' SELECT id, file, title FROM strony_galerie WHERE parent_id="'.$dane['id'].'" ORDER BY pozycja,id ');
		if($X->DB->num_rows($sql_z)>0) {
			$g=0;
			while($dane_z=$X->DB->fa($sql_z)) {
				$g++;
				$galeria.='
					<div class="galeria_img">
						<a href="'.USR.'/strony_galerie/'.$dane['id'].'/'.$dane_z['file'].'"><img src="'.USR.'/strony_galerie/'.$dane['id'].'/'.$X->FILE->add_suffix($dane_z['file'],'_').'" alt="" /></a><br />
						'.(!empty($dane_z['title']) ? '<div class="title">'.$X->STR->outt($dane_z['title']).'</div>':'').'
					</div>
					'.($g%2==0 ? '<div class="clearfix visible-sm"></div>':'').'
					'.($g%3==0 ? '<div class="clearfix visible-md"></div>':'').'
					'.($g%4==0 ? '<div class="clearfix visible-lg"></div>':'').'
				';
			}
			$galeria='
				<div class="galeria_images_ovr">
					<div class="galeria_images">
						<div class="gallery">'.$galeria.'<div class="clr"></div></div>
					</div>
				</div>

				<script type="text/javascript">
					$("a.zoom, div.galeria_images a").magnificPopup({ type:"image", gallery:{enabled:true}, mainClass:"mfp-fade", callbacks:{ open:function(){
						if($("div.gallery").hasClass("wipe-active")) return false;
						$("div.gallery").addClass("wipe-active");
						$(".mfp-container").touchwipe({
						     wipeLeft: function(){ $(".mfp-arrow-right").trigger("click"); },
						     wipeRight: function(){ $(".mfp-arrow-left").trigger("click"); },
						});
					}} });
				</script>
			';

			$X->JS[] = JS.'/jquery.magnific/jquery.magnific-popup.min.js';
			$X->CSS[] = JS.'/jquery.magnific/magnific-popup.css';
		}


		if($dane['x_key']=='cennik') {
			$after='<div class="cennik">';

			$sql_c=$X->DB->q(' SELECT id,title FROM cennik WHERE parent_id="0" ORDER BY pozycja,id ');
			while($dane_c=$X->DB->fa($sql_c)) {
				$after.='<h2><span class="glyphicon glyphicon-tint"></span> '.$X->STR->out($dane_c['title']).'</h2>';

				$sql_c2=$X->DB->q(' SELECT id,title,cena FROM cennik WHERE parent_id="'.$dane_c['id'].'" ORDER BY pozycja,id ');
				while($dane_c2=$X->DB->fa($sql_c2)) {
					$after.='
						<div class="cennik_row">
							<div class="cennik_title">'.$X->STR->out($dane_c2['title']).'</div>
							<div class="cennik_value">'.$X->STR->out($dane_c2['cena']).' zł</div>
							<div class="clr"></div>
						</div>
						<div class="cennik_sep"></div>
					';
				}
			}

			$after.='</div>';
		}



		$R['html']='
			<div class="container">
				<div class="page">
					<h1>'.$X->STR->outt($dane['title']).'</h1>
					'.$before.$R['html'].$after.$galeria.'
					<div class="clr"></div>
				</div>
			</div>
		';
		return $R;
	}


	function replace_email($matches) {
		global $X;

		$matches[1]=str_replace('"','&quot;',$matches[1]);
		$matches[2]=str_replace('"','&quot;',$matches[2]);

		$tmp1=preg_split('//u', $matches[1], -1, PREG_SPLIT_NO_EMPTY);
		$tmp2=preg_split('//u', $matches[2], -1, PREG_SPLIT_NO_EMPTY);

		$email1=@implode("'+'",$tmp1);
		$email2=@implode('"+"',$tmp2);
		$time_tag=$X->STR->uniq();


		return '<a href="javascript:void(0)" onclick="window.location.href=\'ma\'+\'ilt\'+\'o:\'+\''.$email1.'\'" id="php_'.$time_tag.'"> </a><script type="text/javascript">document.getElementById("php_'.$time_tag.'").innerHTML="'.$email2.'"</script>';
	}

}
$PAGE=new page();
?>