<?php
$sql = new sql();
final class Main {

	private $sql;

	public function __construct() {
		global $sql;
		$this->sql = &$sql;
	}

	public function removeDia($string) {
		$a = array(
		"ö" => "o", "ű" => "u", "ő" => "o", "ü" => "u", "ł" => "l", "ż" => "z", "ń" => "n", "ć" => "c", "ę" => "e", "ś" => "s", "ŕ" => "r", "á" => "a",
		"ä" => "a", "ĺ" => "l", "č" => "c", "é" => "e", "ě" => "e", "í" => "i", "ď" => "d", "�?" => "n", "ó" => "o", "ô" => "o", "ř" => "r", "ů" => "u",
		"ú" => "u", "š" => "s", "ť" => "t", "ž" => "z", "ľ" => "l", "ý" => "y", "Ŕ" => "R", "�?" => "A", "Ä" => "A", "Ĺ" => "L", "Č" => "C", "É" => "E",
		"Ě" => "E", "Í" => "I", "Ď" => "D", "Ň" => "N", "Ó" => "O", "Ô" => "O", "�?" => "R", "Ů" => "U", "Ú" => "U", "Š" => "S", "Ť" => "T","Ž" => "Z",
		"Ľ" => "L", "Ý" => "Y", "Ä" => "A"
		);
		return strtr($string, $a);

		return false;

	}
	
	public function logVisitor() {
		$query = "
			INSERT INTO fresh_visitors (ipAddress)
			VALUES ('$_SERVER[REMOTE_ADDR]');
		";
		$this->sql->sql_query($query);
	}

	public function getPage(){
		if (!empty($_GET[page])) {
			if ($_SESSION['user'] == 'fr7592') {
				$sql = "
					SELECT include
					FROM fresh_admin_pages
					WHERE id = '".addslashes($_GET[page])."' AND publiced = 1;
				";
			} else {
				$sql = "
					SELECT include
					FROM fresh_pages
					WHERE id = '".addslashes($_GET[page])."' AND publiced = 1;
				";
			}
			$result = $this->sql->sql_query($sql);
			if ($this->sql->sql_num_rows($result) == 1) {
				$data = $this->sql->sql_fetch_array($result);
				return $data['include'];
			}
		}
		if ($_SESSION['user'] == 'fr7592') {
			$sql = "
				SELECT include
				FROM fresh_admin_pages
				WHERE id = 0 AND publiced = 1;
			";
		} else {
			$sql = "
				SELECT include
				FROM fresh_pages
				WHERE id = 0 AND publiced = 1;
			";
		}
		$result = $this->sql->sql_query($sql);
		if ($this->sql->sql_num_rows($result) == 1) {
			$data = $this->sql->sql_fetch_array($result);
			return $data['include'];
		}

		return false;

	}

	public function getMainNavig() {
		if ($_SESSION['user'] == 'fr7592') {
			$query = "
				SELECT id, title
				FROM fresh_admin_pages
				WHERE idParent = 0 AND publiced = 1 AND navig = 1 OR id = 0
				ORDER BY id ASC
			";
		} else {
			$query = "
				SELECT id, title
				FROM fresh_pages
				WHERE idParent = 0 AND publiced = 1 AND navig = 1 OR id = 0
				ORDER BY id ASC
			";
		}
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$return = "<ul>";
			while ($data = $this->sql->sql_fetch_array($result)) {
				if ($data[id] == 0) {
					$return .= "
						<li><a href=\"index.php\">$data[title]</a></li>
					";
				} elseif ($data[id] == $_GET[page]) {
					$return .= "
						<li class=\"selected\"><a href=\"?page=$data[id]\">$data[title]</a></li>
					";
				} else {
					$return .= "
						<li><a href=\"?page=$data[id]\">$data[title]</a></li>
					";	
				}
			}
			$return .= "</ul>";

			return $return;

		}

		return false;

	}
	
	public function pagging($pageCount) {
		$res = '';
    $pg = 0;
    if (!empty($_GET['tag'])) {
      $link_tag = '&tag='.$_GET['tag'];
    } else {
      $link_tag = '';
    }
    if (!empty($_GET['pg'])) $pg = abs(intval($_GET['pg']));
    if ($pg > $pageCount) $pg = 0;
   
    $spat = '&lt;&lt; Späť';
    if ($pg > 0) {
        $spat = '<a href="?page='.$_GET[page].'&pg='.($pg - 1).$link_tag.'" title="Späť">'.$spat.'</a>';
    }
    $res .= $spat.' | ';
    
    for ($i = 0; $i <= $pageCount; $i++) {
        if ((($i > $pg - 5) && ($i < $pg + 5)) || ($i == 0) || ($i == $pageCount)) {
            $bodky_l = '';
            $bodky_p = '';
            if (($i == 0) && ($pg > 5)) $bodky_l = '.. ';
            if (($i == $pageCount) && ($pg < $pageCount - 5)) $bodky_p = '.. ';
    
            if ($i == $pg) {
                $res .= '<span style="font-size: 1.4em; color: red;">'.($i + 1).'</span> '; // aktualnu stranku zvyrazni inou farbou
            } else {
                $res .= $bodky_p.'<a href="?page='.$_GET[page].'&pg='.$i.$link_tag.'" title="Strana '.($i + 1).'">'.($i + 1).'</a> '.$bodky_l;
            }
        }
    }

    $dalsie = 'Ďalšie &gt;&gt;';
    if ($pg < $pageCount) {
        $dalsie = '<a href="?page='.$_GET[page].'&pg='.($pg + 1).$link_tag.'" title="Ďalšie">'.$dalsie.'</a>';
    }
    $res .= '| '.$dalsie;
    
    return $res;
    
	}

	/* HOME: START*/

	public function getStreet() {
		$query = "
			SELECT id, name, text, imgPath
			FROM fresh_streetstyle_articles
			WHERE publiced = 1
			ORDER BY RAND()
			LIMIT 2;
		";
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$res = "
				<div class=\"block-type-mainpeople\">
					<h2>AKTUÁLNE Z ULICE</h2>
					<div class=\"blockDiv\">
					<div class=\"innerDiv\">
			";

			$data = $this->sql->sql_fetch_array($result);
			$path = "content/images/$data[imgPath]";
			$files = scandir($path);
			$imgName = "";
			$i = 0;
			while ((trim($imgName) == '') AND ($i < sizeof($files))) {
				if ((strpos($files[$i], 'humb_') == false) AND ($files[$i] != '.') AND ($files[$i] != '..')) {
					$imgName = $files[$i];
				}
				$i++;
			}
			$imgPath = "content/images/$data[imgPath]/$imgName";
			//echo $data[name];
			//echo $imgPath;

			//$text = strip_tags(stripslashes($data[text]));
			//$text = mb_substr($text, 0, 20, "UTF-8").'...';
			$res .= "
				<div class=\"img1\">
					<div class=\"image\">
						<a href=\"?page=1&article=$data[id]\"><img src=\"$imgPath\" title=\"$text\" /></a>
					</div>
				<div class=\"trans-background\">&nbsp;</div>
				<div class=\"attribute-link\">
					<a href=\"?page=1&article=$data[id]\">$data[name]</a>  
				</div>    
				</div>
			";

			$data = $this->sql->sql_fetch_array($result);
			$path = "content/images/$data[imgPath]";
			$files = scandir($path);
			$imgName = "";
			$i = 0;
			while ((trim($imgName) == '') AND ($count < sizeof($files))) {
				if ((strpos($files[$i], 'humb_') == false) AND ($files[$i] != '.') AND ($files[$i] != '..')) {
					$imgName = $files[$i];
				}
				$i++;
			}
			$imgPath = "content/images/$data[imgPath]/$imgName";
			$res .= "
				<div class=\"img2\">
					<div class=\"image\">
						<a href=\"?page=1&article=$data[id]\"><img src=\"$imgPath\" title=\"$text\" /></a>
					</div>
				<div class=\"trans-background\">&nbsp;</div>
				<div class=\"attribute-link\">
					<a href=\"?page=1&article=$data[id]\">$data[name]</a>
				</div>
				</div>
			";

			$res .= "
					</div>
					</div>
				</div>	
			";

			return $res;

		}

		return -1;

	}

	public function getVideo() {
		$query = "
			SELECT code FROM fresh_home_video ORDER BY created DESC LIMIT 1;
		";
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$data = $this->sql->sql_fetch_array($result);
			$videoCode = stripslashes($data['code']);
			return $videoCode;
		}

		return false;

	}

	public function getFreshNews() {
		$query = "
			SELECT title, text, created, imgPath
			FROM fresh_flash_news
			ORDER BY created DESC
			LIMIT 4;
		";
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$res = '<div class=\"news-body\">';
			while ($data = $this->sql->sql_fetch_array($result)) {
				$text = stripslashes($data['text']);
				$date = $this->formatDateFromDb($data['created']);
				$res .= "
					<!-- BLOCK CONTENT: START -->
					<div class=\"class-article float-break\">
						<div>
						<div> 
						<span style=\"font-size: 10px; padding-top: -10px;\">$date</span>
							<h2 style=\"font-weight: bold; color: #000000;\">$data[title]</h2>
							<img src=\"$data[imgPath]\" width=\"50\" height=\"76\" style=\"float:left; margin-right: 5px;\" />
							<p>$text</p>
						</div>
						</div>
					</div>
					<!-- BLOCK CONTENT: END -->
				";
			}
			$res .= '</div>';

			return $res;

		}

		return -1;
	}

	public function getEvents() {

		$query = "
			SELECT id, place, shortTitle, date, imgPath
			FROM fresh_events_articles
			WHERE publiced = 1 AND main = 1
			ORDER BY date DESC
			LIMIT 4;
		";

		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$res = "
				<div class=\"block-type-4items5\">
					<div class=\"news-header\">	 
						<h2> Treba zažiť </h2>
					</div>
					<div class=\"news-body\">
			";
			$count = 0;
			while ($data = $this->sql->sql_fetch_array($result)) {
				switch ($count) {
					case 0: $color = '#F26F21';
					break;
					case 1: $color = '#B18A67';
					break;
					case 2: $color = '#599EA5';
					break;
					case 3: $color = '#00ADEE';
					break;
				}
				$shortTitle = stripslashes($data['shortTitle']);
				$place = stripslashes($data['place']);
				$count++;
				$imgPath = "content/images/$data[imgPath]/logo_small.jpg";
				$date = $this->formatDateFromDb($data['date']);
				$res .= "
					<div class=\"class-article float-break\">
						<div>
						<div> 
							<h2 style=\"background-color:$color;\"><a href=\"?page=6&article=$data[id]\">$shortTitle</a></h2>
							<a href=\"?page=6&article=$data[id]\">
								<img src=\"$imgPath\" style=\"float:left;margin:5px;\" width=\"100px\" height=\"90px\" alt=\"\"/>
							</a>
							<p>$date</p>
							<p>$place</p>
						</div>
						</div>
					</div>
				";
			}

			$res .= "
					</div>
				</div>
				<div class=\"block-separator\"></div>
			";

			return $res;

		}

		return -1;


	}
	
	public function getAdPartners() {
		$res = "
			<div style=\"width: 196px; border: 1px solid #000000; margin-top: 550px; background: #FFFFFF;\">
				<a href=\"\" style=\"float: left;\"><img src=\"design/partners/ad_partners/got2be.jpg\" /></a>
				<a href=\"\" style=\"float: left; margin-left: 5px;\"><img src=\"design/partners/ad_partners/basys.jpg\" /></a>
				<a href=\"\" style=\"float: left;\"><img src=\"design/partners/ad_partners/fa.jpg\" /></a>
				
			</div>
		";
		
		return $res;
		
	}

	/* HOME: END */

	public function getPath() {
		if ($_GET[page] < 1) {
			return false;
		}
	}

	public function getImages($path, $type) {
		if (strlen(trim($path)) < 5) {
			return -1;
		}

		$path = "content/images/$path";
		if (is_dir($path)) {
			$imgs = scandir($path);
			if (sizeof($imgs) > 0) {
				$images = array();
				for ($i=0;$i<sizeof($imgs);$i++) {
					if ($type == 'thumbs') {
						if (($imgs[$i] != '.') AND ($imgs[$i] != '..') AND (substr_count($imgs[$i], 'thumb_') > 0)) {
							$images[sizeof($images)] = $imgs[$i];
						}
					} elseif ($type == 'images') {
						if (($imgs[$i] != '.') AND ($imgs[$i] != '..') AND (substr_count($imgs[$i], 'thumb_') == 0)) {
							$images[sizeof($images)] = $imgs[$i];
						}
					}
				}
				if (sizeof($images) > 0) {
					return $images;
				}
			}
		}

		return -1;

	}

	public function getArticle($idArticle) {
		if (!is_numeric($idArticle)) {
			return false;
		}


		switch ($_GET['page']) {
			case 1:
				$return = array();
				$query = "
					SELECT publiced, name, sign, text, created, imgPath
					FROM fresh_streetstyle_articles
					WHERE id = $idArticle;
				";
				$result = $this->sql->sql_query($query);
				if ($this->sql->sql_num_rows($result) == 1) {
					$data = $this->sql->sql_fetch_array($result);
					$return['name'] = $data['name'];
					$return['sign'] = $data['sign'];
					$return['text'] = $data['text'];
					$return['created'] = $data['created'];

				}

				break;
			case 2:
				$return = array();
				$query = "
					SELECT publiced, main, shortTitle, title, shortText, text, author, created, keywords
					FROM fresh_styles_articles
					WHERE id = $idArticle;
				";
				$result = $this->sql->sql_query($query);
				if ($this->sql->sql_num_rows($result) == 1) {
					$data = $this->sql->sql_fetch_array($result);
					$return['shortTitle'] = $data['shortTitle'];
					$return['title'] = $data['title'];
					$return['shortText'] = $data['shortText'];
					$return['text'] = $data['text'];
					$return['author'] = $data['author'];
					$return['created'] = $data['created'];
					$return['keywords'] = $data['keywords'];

				}

				break;
			case 4:
				$return = array();
				$query = "
					SELECT publiced, shortTitle, title, shortText, text, author, keywords
					FROM fresh_products_articles
					WHERE id = $idArticle;
				";
				$result = $this->sql->sql_query($query);
				if ($this->sql->sql_num_rows($result) == 1) {
					$data = $this->sql->sql_fetch_array($result);
					$return['shortTitle'] = $data['shortTitle'];
					$return['title'] = $data['title'];
					$return['shortText'] = $data['shortText'];
					$return['text'] = $data['text'];
					$return['author'] = $data['author'];
					$return['keywords'] = $data['keywords'];

				}

				break;
		}

		if (sizeof($return) > 0) {
			return $return;
		}

		return false;

	}

	public function printArticlesShort($keyword, $startDate, $endDate, $countOnPage, $mainArticle, $showAll, $paging){
	  if (!empty($_GET['tag']) AND (($_GET['page'] == 2) OR ($_GET['page'] == 0) OR (empty($_GET['page'])) OR ($_GET['page'] == 1003) OR ($_GET['page'] == 4))) {
	    $TagName = $this->get_tag_name($_GET['tag']);
	    if ($TagName !== false) {
	      $db_tag = " AND (keywords LIKE '$TagName%' OR keywords LIKE '%$TagName' OR keywords LIKE '%$TagName%' OR keywords='$TagName')";
	    }
	  } else {
	    $db_tag = '';
	  }
		if (($_GET['page'] == 2) OR ($_GET['page'] == 0) OR (empty($_GET['page'])) OR ($_GET['page'] == 1003)) {
			$tableName = 'fresh_styles_articles';
			$page = 2;
		} elseif ($_GET['page'] == 4) {
			$tableName = 'fresh_products_articles';
			$page = 4;
		} else {
			$tableName = '';
		}
		if (strlen(trim($tableName)) < 5) {
			return false;
		}
		if (!empty($_GET['offset'])) {
			$offset = $_GET['offset'];
		} else {
			$offset = 0;
		}
		
		$pg = 0;
    if (isset($_GET['pg'])) $pg = abs(intval($_GET['pg']));
		
		/*if (($offset == 0) AND ($mainArticle == 1)) {
		$limit--;
		}*/
		if ($mainArticle == 1) {
			if (($_GET['page'] == 2) OR ($_GET['page'] == 0) OR (empty($_GET['page'])) OR ($_GET['page'] == 1003)) {
				if ($showAll == 0) {
					$show = ' AND publiced = 1';
				} else {
					$show = '';
				}
				$queryCount = "
					SELECT ceil(COUNT(*) / $countOnPage) AS count
					FROM $tableName
					WHERE
					main <> 1
					$show
				  $db_tag
				";
				
				$resultCount = $this->sql->sql_query($queryCount);
				$rowsCount = $this->sql->sql_fetch_array($resultCount);
				$rowsCount = $rowsCount['count']-1;
				if ($pg > $rowsCount) $pg = 0;
				$offset = $pg * $countOnPage;
				
				$query = "
					SELECT id, shortTitle, shortText, author, modified, keywords, imgPath
					FROM $tableName
					WHERE
					main <> 1
					$show
				  $db_tag
					ORDER BY created DESC
					LIMIT $countOnPage
					OFFSET $offset
				";
			} elseif ($_GET['page'] == 4) {
				if ($showAll == 0) {
					$show = 'WHERE publiced = 1';
				} else {
					$show = '';
				}
				$queryCount = "
					SELECT ceil(COUNT(*) / $countOnPage)
					FROM $tableName
					$show
				  $db_tag
				";
				
				$resultCount = $this->sql->sql_query($queryCount);
				$rowsCount = $this->sql->sql_fetch_array($resultCount);
				$rowsCount = $rowsCount['count']-1;
				if ($pg > $pocetstran) $pg = 0;
				$offset = $pg * $countOnPage;
				
				$query = "
					SELECT id, shortTitle, shortText, author, modified, keywords, imgPath
					FROM $tableName
					$show
				  $db_tag
					ORDER BY created DESC	
					LIMIT $countOnPage
					OFFSET $offset
				";
			}
		} else {
			$queryCount = "
				SELECT ceil(COUNT(*) / $countOnPage)
				FROM $tableName
				WHERE 1
				$show
			  $db_tag
			";
			
			$resultCount = $this->sql->sql_query($queryCount);
			$rowsCount = $this->sql->sql_fetch_array($resultCount);
			$rowsCount = $rowsCount['count']-1;
			if ($pg > $pocetstran) $pg = 0;
			$offset = $pg * $countOnPage;
			
			$query = "
				SELECT id, shortTitle, shortText, author, modified, keywords, imgPath
				FROM $tableName
				WHERE 1
				$show
			  $db_tag
				ORDER BY created DESC	
				LIMIT $countOnPage
				OFFSET $offset
			";
		}
		/*
		if (($keyword != 0) AND ($startDate != 0) AND ($startDate != 0)) {
		$query = "
		SELECT id, shortTitle, shortText, author, modified, keywords
		FROM $tableName
		WHERE (keywords = '$keyword'
		OR keywords LIKE '%$keyword%'
		OR keywords LIKE '%$keyword'
		OR keywords LIKE '$keyword%')
		AND modified >= '$startDate'
		AND modified <= '$endDate'
		AND show = $show
		ORDER BY created DESC
		LIMIT $limit;
		";
		}
		elseif ($keyword != 0) {
		$query = "
		SELECT id, shortTitle, shortText, author, modified, keywords
		FROM $tableName
		WHERE (keywords = '$keyword'
		OR keywords LIKE '%$keyword%'
		OR keywords LIKE '%$keyword'
		OR keywords LIKE '$keyword%')
		AND show = $show
		ORDER BY created DESC
		LIMIT $limit;
		";
		}elseif (($startDate != 0) AND ($startDate != 0)){
		$query = "
		SELECT id, shortTitle, shortText, author, modified, keywords
		FROM $tableName
		WHERE modified >= '$startDate'
		AND modified <= '$endDate'
		AND show = $show
		ORDER BY created DESC
		LIMIT $limit;
		";
		}else {
		$query = "
		SELECT id, shortTitle, shortText, author, modified, keywords, imgPath
		FROM $tableName
		WHERE show = $show
		ORDER BY created DESC
		LIMIT $limit;
		";
		}*/
		$sqlResult = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($sqlResult) > 0) {
			$result = '';
			if (($mainArticle == 1) AND ($offset == 0)) {
				$query1 = "
					SELECT id, shortTitle, shortText, author, modified, keywords, imgPath
					FROM $tableName
					WHERE main = 1
					$show
				  $db_tag
					ORDER BY created DESC
					LIMIT 0, 1;
				";
				$res = $this->sql->sql_query($query1);
				if ($this->sql->sql_num_rows($res) > 0) {
					$data = $this->sql->sql_fetch_array($res);
					$data['shortTitle'] = stripslashes($data['shortTitle']);
					$data['shortText'] = stripslashes($data['shortText']);
					$data['author'] = stripslashes($data['author']);
					$data['keywords'] = stripslashes($data['keywords']);
					$date = $this->formatDateFromDb($data[modified]);
					$result1 = "
						<!-- ARTICLE: START -->
						<div class=\"block-type-mainstory\">
						<div class=\"innerDiv\">
						    <div class=\"image\">
						 		<a href=\"index.php?page=$page&article=$data[id]\"><img src=\"content/images/$data[imgPath]/thumb.jpg\" /></a>
							</div>
						    <span class=\"infoBarDate\">$date </span>|<span class=\"infoBar\"> $data[keywords]</span>
						    <h1><a href=\"index.php?page=$page&article=$data[id]\">$data[shortTitle]</a></h1>
						    <p>$data[shortText]</p>
						</div>
						</div>
						<div class=\"block-separator\"></div>
						<!-- ARTICLE: END -->
					";
					$result = $result.$result1;
				}
			}
			/*if (($resultCount > $limit) AND ($paging == 1)) {
				$resPaging = "<div align=\"center\" style=\"font-size: 1.2em; font-weight: bold;\">";
				if ($offset > 0) {
					if ($offset < $limit) {
						$offst = '';
					} else {
						$offst = $offset - $limit;
					}
					$resPaging .= "
						<a href=\"?page=$_GET[page]\" style=\"padding-right: 10px;\">|&lt</a>
						<a href=\"?page=$_GET[page]&offset=$offst\" style=\"padding-right: 10px;\">&lt&lt</a>
					";
				}

				$pageId = $this->div($offset, $limit);
				
				$resPaging .= "$pageId";

				if (($limit + $offset) < $rowsCount) {
					if (($offset + $limit) < $rowsCount) {
						$offst = $offset + $limit;
					} else {
						$offst = $rowsCount - $offset + $limit;
					}
					$endOffset = $this->div($rowsCount, $limit) * $limit;
					$resPaging .= "
						<a href=\"?page=$_GET[page]&offset=$offst\" style=\"padding-left: 10px;\">&gt&gt</a>
						<a href=\"?page=$_GET[page]&offset=$endOffset\" style=\"padding-left: 10px;\">&gt|</a>
					";
				}
				$resPaging .= "</div>";
			}*/
			$result = $result."
				<div class=\"block-articles\">
			";
			while ($data = $this->sql->sql_fetch_array($sqlResult)) {
				$data['shortTitle'] = stripslashes($data['shortTitle']);
				$data['shortText'] = stripslashes($data['shortText']);
				$data['author'] = stripslashes($data['author']);
				$data['keywords'] = stripslashes($data['keywords']);
				$date = $this->formatDateFromDb($data[modified]);
				$result1 = "
					<!-- ARTICLE: START -->
					<div class=\"innerDiv\">
					<div class=\"class-article\">
						<div class=\"imageleft\">
							<a href=\"index.php?page=$page&article=$data[id]\"><img src=\"content/images/$data[imgPath]/thumb.jpg\" /></a>
						</div>
					    <span class=\"infoBarDate\">$date </span>|<span class=\"infoBar\"> $data[keywords]</span>
						<h2><a href=\"index.php?page=$page&article=$data[id]\">$data[shortTitle]</a></h2>
						<p>$data[shortText]</p>
					</div>
					</div>
					<!-- ARTICLE: END -->
				";
				$result = $result.$result1;
			}
			$resPaging .= "
				<div align=\"center\" style=\"font-size: 1.2em;\">
			";
			$resPaging .= $this->pagging($rowsCount);
			$resPaging .= "
				</div>
			";
			if (($_GET['page'] == 0) OR empty($_GET['page'])) {
				$resPaging = '';
			}
			$result = "$result.$resPaging</div>";
			return $result;
		}

		return false;

	}

	/* STREETSTYLE: START */
	public function printStreetShort($limit) {
		if (!empty($_GET['offset'])) {
			$offset = $_GET['offset'];
		} else {
			$offset = 0;
		}
		$query = "
			SELECT id, name, sign, text, imgPath
			FROM fresh_streetstyle_articles
			ORDER BY created DESC
			LIMIT $limit
			OFFSET $offset;
		";
		$queryCount = "
			SELECT COUNT(*) as count
			FROM fresh_streetstyle_articles
			ORDER BY created DESC
		";
		$resultCount = $this->sql->sql_query($queryCount);
		$rowsCount = $this->sql->sql_fetch_array($resultCount);
		$rowsCount = $rowsCount['count'];
		if ($resultCount > $limit) {
			$resPaging = "<div align=\"center\" style=\"font-size: 1.2em; font-weight: bold;\">";
			if ($offset > 0) {
				if ($offset < $limit) {
					$offst = '';
				} else {
					$offst = $offset - $limit;
				}
				$resPaging .= "
					<a href=\"?page=$_GET[page]\" style=\"padding-right: 10px;\">|&lt</a>
					<a href=\"?page=$_GET[page]&offset=$offst\" style=\"padding-right: 10px;\">&lt&lt</a>
				";
			}
			$pageId = $this->div($offset, $limit);
			$resPaging .= "$pageId";
			if (($limit + $offset) < $rowsCount) {
				if (($offset + $limit) < $rowsCount) {
					$offst = $offset + $limit;
				} else {
					$offst = $rowsCount - $offset + $limit;
				}
				$endOffset = $this->div($rowsCount, $limit) * $limit;
				$resPaging .= "
					<a href=\"?page=$_GET[page]&offset=$offst\" style=\"padding-left: 10px;\">&gt&gt</a>
					<a href=\"?page=$_GET[page]&offset=$endOffset\" style=\"padding-left: 10px;\">&gt|</a>
				";
			}
			$resPaging .= "</div>";
		}		
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$res = "<div class=\"block-articles\">$resPaging";
			while ($data = $this->sql->sql_fetch_array($result)) {
				$dirPath = 'content/images/' . $data['imgPath'];
				$files = scandir($dirPath);
				$fileName = "";
				$i = 0;
				while (($fileName == '') AND ($i <= sizeof($files))) {
					if (substr_count($files[$i], 'humb_') > 0) {
						$fileName = $files[$i];
					}
					$i++;
				}
				$file = "content/images/$data[imgPath]/$fileName";
				$res = $res . "
					<!-- ARTICLE: START -->
					<div class=\"innerDiv\">
					<div class=\"class-article\">
						<div class=\"imageleft\">
							<img src=\"$file\" />
						</div>
						<h2><a href=\"index.php?page=1&article=$data[id]\">$data[name]</a></h2>
						<p>$data[text]</p>
					</div>
					</div>
					<!-- ARTICLE: END -->
				";
			}
			$res = $res."$resPaging</div>";

			return $res;

		}

		return -1;

	}

	/* STREETSTYLE: END */
	
	/* PANELS: START */
	
	public function getArticlesPanel() {
		$query = "
			SELECT id, shortTitle
			FROM fresh_styles_articles
			WHERE publiced = 1
			ORDER BY created DESC
			LIMIT 10;
		";
		$result = $this->sql->sql_query($query);
		if ($this->sql->sql_num_rows($result) > 0) {
			$res = "
				<div style=\"background-color: #FFFFFF; font-size: 0.9em; padding: 5px;\">
				<h1 style=\"text-decoration: underline;\">Najnovšie články</h1><br />
			";	
			while ($data = $this->sql->sql_fetch_array($result)) {
				$res .= "
					<a href=\"?page=2&article=$data[id]\" style=\"color: $\">$data[shortTitle]</a><br /><br />
				";
			}
			
			$res .= "
				</div>
			";
		}
		
		return $res;
		
		return false;
		
	}
	
	public function panel_news() {
		$query = "
			SELECT id, name
			FROM fresh_tags
			ORDER BY id ASC;
		";
		$query1 = "
			SELECT id, shortTitle
			FROM fresh_styles_articles
			WHERE publiced = 1
			ORDER BY created DESC
			LIMIT 10;
		";
		
		$result = $this->sql->sql_query($query);
		$res_count = $this->sql->sql_num_rows($result);
		$result1 = $this->sql->sql_query($query1);
		$res = "
				<div id=\"accordion\" style=\"background-color: #FFFFFF; font-size: 0.9em; padding: 5px;\">
			";	
		if ($this->sql->sql_num_rows($result) > 0) {
			$res .= "
				<h2 class=\"toggler\" style=\"text-decoration: underline; font-weight: bold; background-color: #D2E0E6; cursor: pointer; padding: 2px;\">Témy článkov</h2>
				<div class=\"element float-break\">
				<div style=\"float: left;\">
			";	
			//while ($data = $this->sql->sql_fetch_array($result)) {
			for ($i=0;$i<(round($res_count/2, 0));$i++) {
			  $data = $this->sql->sql_fetch_array($result);
				$res .= "
					<a href=\"?page=$_GET[page]&tag=$data[id]\" style=\"margin: 5px; padding: 0px; display: block;\">&gt $data[name]</a>
				";
			}
			
			$res .= "
			  </div>
			  <div style=\"float: left;\">
			";
			
			for ($i=0;$i<($res_count-round($res_count/2, 0));$i++) {
			  $data = $this->sql->sql_fetch_array($result);
				$res .= "
					<a href=\"?page=$_GET[page]&tag=$data[id]\" style=\"margin: 5px; padding: 0px; display: block;\">&gt $data[name]</a>
				";
			}
			
			$res .= "
			  </div>
			  </div>
			";
		}
		if ($this->sql->sql_num_rows($result1) > 0) {
			$res .= "
				<h2 class=\"toggler\" style=\"text-decoration: underline; font-weight: bold; background-color: #D2E0E6; cursor: pointer; padding: 2px; margin-top: 10px;\">Najnovšie články</h2>
				<div class=\"element\">
			";	
			while ($data1 = $this->sql->sql_fetch_array($result1)) {
				$res .= "
					<a href=\"?page=$_GET[page]&article=$data1[id]\" style=\"margin: 5px; padding: 0px; display: block;\">&gt $data1[shortTitle]</a>
				";
			}
			
			$res .= "
				</div>
			";
		}
		
		$res .= "
			</div>
		";
		
		return $res;
		
		return false;
		
	}
	
	/* PANELS: END */
	
	/* GALLERIES: START */
	
	public function printGallery() {
		if (is_numeric($_GET['page']) AND is_numeric($_GET['article'])) {
			switch ($_GET['page']) {
				case 2:
					$dbTable = 'fresh_styles_articles';
					break;
				default:
					$dbTable = null;
					break;
			}
			$query = "
				SELECT galleryPath
				FROM $dbTable
				WHERE id = $_GET[article] AND galleryPath <> 'NULL' AND galleryPath <> '';
			";
			$result = $this->sql->sql_query($query);
			if ($this->sql->sql_num_rows($result) > 0) {
				$data = $this->sql->sql_fetch_array($result);
				$imgDir = "content/images/$data[galleryPath]/";
				$files = scandir($imgDir);
				if (sizeof($files) > 0) {
					$res = "<div class=\"float-break\">";
					foreach ($files as $key => $val) {
						if ((strpos($val, 'thumb_') !== 0) AND ($val != '.') AND ($val != '..')) {
							$imgLink = $imgDir.$val;
							$thumbLink = $imgDir."thumb_".$val;
							//echo $imgLink."<br />";
							//echo $thumbLink."<br /><br />";
							$res .= "
								<div style=\"float: left; margin-left: 20px;\">
								<a href=\"$imgLink\" rel=\"lightbox-atomium\"><img src=\"$thumbLink\" /></a>
								</div>
							";
						}
					}
					
					$res .= "</div>";
					return $res;
				}
			}
		}
		
		return false;
		
	}
	
	/* GALLERIES: END */
	
	/* MESSAGE BOARD: Start */
	
	public function message_board($page, $id_article) {
	  if (!empty($_POST['nadpis']) AND !empty($_POST['reakcia']) AND is_numeric($_POST['react_id']) AND is_numeric($_GET['article']) AND !empty($_SESSION[idUser])) {
	    if (is_numeric($_POST['react_id'])) $idParent = $_POST['react_id']; else $idParent = 0;
	    $query = "
	      INSERT INTO fresh_messages (id_parent, id_user, id_page, id_article, publiced, header, text, created)
	      VALUES ($idParent, $_SESSION[idUser], $_GET[page], $_GET[article], 'y', '$_POST[nadpis]', '$_POST[reakcia]', NOW());
	    ";
	    $result = $this->sql->sql_query($query);
	    
	  }
	  
	  if (!is_numeric($page) OR !is_numeric($id_article)) {
	    return false;
	  }
	  $query = "
	    SELECT * FROM fresh_messages WHERE id_page = $page AND id_article = $id_article AND publiced = 'y' AND id_parent=0;
	  ";
	  
	  $result = $this->sql->sql_query($query);
	  
	  
	  if ( $this->sql->sql_num_rows($result) > 0 )
    {
        $res = '
          <div style="margin-top: 10px;">
          <h2>Diskusia k článku</h2>      
        ';
        while ( $row_main = $this->sql->sql_fetch_array($result) )
        {
            $this->margin = 0;
            $res .= '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid red; border: 1px solid #BBBBBB; background-color: #EEEEEE; color: #000;">';
            $res .= '<div>';
            $res .=   '<div style="float: left; width: 50%;"><b>Autor:</b> '.$this->getUserName($row_main['id_user']).'</div>';
            $res .= 	'<div style="font-size: 11px; float: left; width: 50%; text-align: right;"><span style="font-weight: bold;">Pridané: </span>' . $row_main['created'] . '</div>';
            $res .= '</div>';
            $res .= '<div style="padding-top: 5px; border-top: 1px solid #FFF; clear: both; color: #000; position: relative;">';
            $res .= 	$row_main['text'];
            $res .= '</div>';
            $res .= 	'<div style="font-size: 11px; float: left; margin-top: 5px;"><a href="?page=' . $_GET['page'] . '&amp;article=' . $_GET['article'] . '&amp;react=' . $row_main['id'] . '#react" title="">Reagovať</a></div>';
            $res .= 	'<div style="clear: both;"></div>';
            if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_main['id'] == $_GET['react']) )
            {
                $res .= '<div id="react" style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
                $res .= 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
                $res .=		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
                $res .= 		'<div>Nadpis: <br /><input type="text" name="nadpis" size="20" maxlength="50" /></div>';
                $res .= 		'<div>Text: <br /><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
                $res .= 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
                $res .= 	'</form>';
                $res .= '</div>';
            }
            $res .= '</div>';
            $res .= $this->sub_forum($row_main['id']);
        }
        $res .= '</div>';
        return $res;
    }
	  
	  return false;
	  
	}
	
	public function sub_forum($id_parent)
  {
  $sql_sub = 'SELECT * FROM fresh_messages WHERE id_parent=' . $id_parent . ' ORDER BY id ASC';
  if ( $result_sub = mysql_query($sql_sub) )
  {
      $res = '';
      while ( $row_sub = mysql_fetch_assoc($result_sub) )
      {
          $this->margin++;
          $res .= '<div style="margin-bottom: 15px; padding: 7px; border: 1px solid #BBBBBB; background-color: #EEEEEE; color: #000; margin-left: ' . ($this->margin*20) . 'px;">';
          $res .= '<div>';
          $res .=   '<div style="float: left; width: 50%;"><b>Autor:</b> '.$this->getUserName($row_sub['id_user']).'</div>';
          $res .= 	'<div style="font-size: 11px; float: left; width: 50%; text-align: right;"><span style="font-weight: bold;">Pridané: </span>' . $row_sub['created'] . '</div>';
          $res .= '</div>';
          $res .= '<div style="padding-top: 5px; border-top: 1px solid #FFF; clear: both; color: #000; position: relative;">';
          $res .= 	$row_sub['text'];
          $res .= '</div>';
          $res .= 	'<div style="font-size: 11px; float: left; margin-top: 5px;"><a href="?page=' . $_GET['page'] . '&amp;article=' . $_GET['article'] . '&amp;react=' . $row_sub['id'] . '#react" title="">Reagovať</a></div>';
          $res .= 	'<div style="clear: both;"></div>';
          if ( isset($_GET['react']) && ($_GET['react'] != '') && ($row_sub['id'] == $_GET['react']) )
          {
              $res .= '<div id="react" style="border-top: 1px solid #00377E; margin-top: 10px; padding-top: 10px;">';
              $res .= 	'<form method="post" action="' . str_replace('&', '&amp;', substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '&react')) ) . '">';
              $res .=		'<input type="hidden" name="react_id" value="' . $_GET['react'] . '" />';
              $res .= 		'<div>Nadpis: <br /><input type="text" name="nadpis" size="20" maxlength="50" /></div>';
                $res .= 		'<div>Text: <br /><textarea name="reakcia" rows="3" cols="10" style="width: 100%;"></textarea></div>';
              $res .= 		'<div><input style="font-size: 11px;" value="Reaguj" type="submit" /></div>';
              $res .= 	'</form>';
              $res .= '</div>';
          }
          $res .= '</div>';
          $res .= $this->sub_forum($row_sub['id']);
          $this->margin--;
      }
      return $res;
  } 
  
  return false;
  
  }
	
	/* MESSAGE BOARD: End */

	public function getUserName($userId) {  // vrati meno uzivatela, alebo false
		  if (!is_numeric($userId)) {
		    return false;
		  }
			$name = addslashes(trim($name));
			$query = "
				SELECT name
				FROM fresh_user_data
				WHERE id = '".$userId."';
			";
			$result = $this->sql->sql_query($query);
			if ($result) {
				$data = $this->sql->sql_fetch_array($result);
				return $data['name'];
			}
			
			return false;
			
		}
	
	/* HELP FUNCTIONS */
	
	public function get_tag_name($TagID) {
	  if (!is_numeric($TagID)) {
	    return false;
	  }
	  
	  $query = "
	    SELECT name
	    FROM fresh_tags
	    WHERE id=$TagID;
	  ";
	  $result = $this->sql->sql_query($query);
	  if ($this->sql->sql_num_rows($result) > 0) {
	    $data = $this->sql->sql_fetch_array($result);
	    return $data['name'];
	  }
	  
	  return false;
	  
	}

	public function formatDateFromDb($date) {
		$date = trim($date);
		if (strlen($date) == 19) {
			$result = substr($date, 8, 2).'.'.substr($date, 5, 2).'.'.substr($date, 0, 4);
			return $result;
		}

		return false;

	}

	public function formatTimeFromDb($date) {
		$date = trim($date);
		if (strlen($date) == 19) {
			$result = substr($date, 11, 8);
			return $result;
		}

		return false;

	}

	public function div($divident, $divider) {
		if (!is_numeric($divident) OR !is_numeric($divider)) {
			return -1;
		}

		$result = $divident / $divider;

		if (strpos($result, '.') == false) {
			return $result;
		} else {
			$result = substr($result, 0, strpos($result, '.'));
			return $result;
		}

		return -1;

	}

}

?>