<?php

// Cache
require_once('dis_cache.php');

$strecke_id = $_GET['strecke_id'];
$template_name = 'strecke';
require_once('dis_common.php');

// cache some not!
/*if(
//	$STRECKE['strecke_id'] == 1215313 || // vor
//	$STRECKE['strecke_id'] == 1215314 || // sta
	$STRECKE['strecke_id'] == 1215315 || // oerl
//	$STRECKE['strecke_id'] == 1215316 || // laemm
//	$STRECKE['strecke_id'] == 1215317 || // ziel
//	$STRECKE['strecke_id'] == 1215318 || // mini
	false
){
	$GLOBALS['cache']['is_running'] = false;
}*/

// werbung
extract_werbung($STRECKE['match']);

// page nr.
if($_GET['page'] == ''){
	$PAGE = 0;
}else{
	$PAGE = ((int) $_GET['page']) - 1;
	if($PAGE < 0){
		$ERROR404 = 'negative page';
		require_once('dis_404.php');	
	}
}

$PAGES = ceil($STRECKE['fotos'] / $PicsPerPage);
if($PAGE >= $PAGES){
	$ERROR404 = 'page too high';
	require_once('dis_404.php');	
}

// check url
$canonical_url = $STRECKE['url_base'].$STRECKE['strecke_id'].'s'.($PAGE > 0 ? ($PAGE+1) : '').'.html';
if($_SERVER['REQUEST_URI'] != $canonical_url){
	header('HTTP/1.1 301 Moved Permanently');
	header('Location: '.$canonical_url);
	die();
}

/*
** if no index: display first foto
*/
if(!$STRECKE['index']){
	header('Location: '.$STRECKE['foto_ids'][0].'.html');
	die();
}

header('X-Pictools-Log: '.$MANDANT[$_SERVER['SERVER_NAME']][0].';strecke;'.$STRECKE['strecke_id']);

// generate all thumbnails
$FOTOS = array();
for($i=$PAGE*$PicsPerPage; $i<($PAGE+1)*$PicsPerPage; $i++){
	if(isset($STRECKE['foto_ids'][$i])){
		$FOTOS[] = '<a href="'.$STRECKE['foto_ids'][$i].'.html"><img src="'.                                                     $STRECKE['foto_ids'][$i].'t.jpeg" width="180" height="125" border="0"></a>';
//		$FOTOS[] = '<a href="'.$STRECKE['foto_ids'][$i].'.html"><img src="http://bilder.nw-news.de/'.dirname($canonical_url).'/'.$STRECKE['foto_ids'][$i].'t.jpeg" width="180" height="125" border="0"></a>';
	}
}

// insert werbung
if($WERBUNG['index'] != ''){
	array_splice($FOTOS, mt_rand(0, count($FOTOS)), 0, '<table border="0" cellspacing="0" cellpadding="0" width="180" height="125"><tr><td width="28"><img src="'.$TEMPLATE['TBASE'].'index.png"><td witdh="125" height="125">'.$WERBUNG['index'].'</td><td width="27"></td></tr></table>');
}

// add empty elements
while(count($FOTOS) < 16){
	$FOTOS[] = '';
}

$out = '';
$out .= '<table border="0" cellspacing="0" cellpadding="0">';
for($j=0; $j<4; $j++){
	$out .= '<tr>';
	for($i=0; $i<4; $i++){
		$out .= '<td width="180" height="125">'.$FOTOS[$j*4+$i].'</td>';
		if($i<3){
			$out .= '<td width="10" height="125"></td>';
		}
	}
	$out .= '</tr>';
	if($j<3){
		$out .= '<tr><td colspan="7" height="10"></td></tr>';
	}
}

$out .= display_timeline(7, $PAGE/ceil($STRECKE['fotos']/$PicsPerPage-1));

if($STRECKE['timeline']){
	$REPLACE['%%TITEL%%'] .= ' — '.$STRECKE['timeline_frames'][$PAGE*$PicsPerPage].' - '.$STRECKE['timeline_frames'][$PAGE*$PicsPerPage+$PicsPerPage-1];
}

$out .= '<tr><td colspan="7" height="30"></td></tr>';

if($PAGES > 1){
	if($STRECKE['timeline']){
	}else{
		$out .= '<tr><td colspan="7" class="navi" align="center">Seiten: ';
		if($PAGES <= 15){
			for($i=1; $i<=$PAGES; $i++){
				if($i-1 == $PAGE){
					$out .= '<span class="current_page">'.$i.'</span> ';
				}else{
					$out .= '<a href="'.$STRECKE['strecke_id'].'s'.$i.'.html">'.$i.'</a> ';
				}
			}
		}else{
			$pg = array_fill(1, $PAGES, false);
			$pg[1] = 'k';
			$pg[$PAGES] = 'k';
			for($i=10; $i<$PAGES; $i+=10){
				$pg[$i] = 'k';
			}
			for($j=-3; $j<=3; $j++){
				if($PAGE+1+$j >= 1 && $PAGE+1+$j <= $PAGES){
					$pg[$PAGE+1+$j] = true;
				}
			}
			foreach($pg AS $k => $v){
				if($v && $k > 1 && $pg[$k-1] == false){
					if($k <= 2 || $pg[$k-2] == true){
						$out .= '.';
					}else{
						$out .= '..';
					}
				}else if($v){
					$out .= ' ';
				}
				if($k-1 == $PAGE){
					$out .= '<span class="current_page">'.$k.'</span>';
				}else if($v){
					if($v === 'k'){
						$out .= '<span style="font-size: 80%">';
					}
					$out .= '<a href="'.$STRECKE['strecke_id'].'s'.$k.'.html">'.$k.'</a>';
					if($v === 'k'){
						$out .= '</span>';
					}
				}
			}
		}
		$out .= '</td></tr>';
	}
	$out .= '<tr><td colspan="7" height="10"></td></tr>';
}

$out .= '<td colspan="7" class="bz_cy_tg">Tags: <b>%%TAGS%%</b></td>';



$REPLACE['%%LIST%%'] = $out;

$REPLACE['%%PIC-NUM%%'] = $PAGE+1;
$REPLACE['%%PIC-COUNT%%'] = $PAGES;

$REPLACE['%%LINK-PREV%%'] = $STRECKE['strecke_id'].'s'.($PAGE == 0 ? ($PAGES) : $PAGE).'.html';
$REPLACE['%%LINK-NEXT%%'] = $STRECKE['strecke_id'].'s'.($PAGE+1 == $PAGES ? '1' : ($PAGE+2)).'.html';

$REPLACE['%%TAGS%%'] = implode(', ', $tags_out);



output_page();

// debug
//echo '<pre>'.htmlspecialchars(var_export(array($PAGE,$WERBUNG,$STRECKE,$_SERVER['SERVER_NAME']),true));

?>