<?php

include './core.php';

if(isset($_REQUEST['special']) && $_REQUEST['special'] == 'chargen_preview') {
// BEGINN CHARGERN_PREVIEW
	switch($_REQUEST['action']) {
		case "display_preview":
			$index = $_SESSION['wep_preview']['gumpid_body'].",".$_SESSION['wep_preview']['haar'].",".$_SESSION['wep_preview']['bart'];
			$isgump = "1,1,1";
			$female = "0,0,0";
			$hue = $_SESSION['wep_preview']['hautfarbe'].",".$_SESSION['wep_preview']['haarfarbe'].",".$_SESSION['wep_preview']['haarfarbe'];;
			$indexA = $index;
			$femaleA = $female;
			$hueA = $hue;
			$isgumpA = $isgump;
			$name = $_SESSION['wep_preview']['charname'];
			require_once('./lib/gumpreader.inc.php');
		die();
		case "new":
			switch($_SESSION['wep_preview']['step']) {
				case 1:
					$_SESSION['wep_preview']['charname'] 	= $_REQUEST['charname'];
					if(!eregi("^[a-zA-Z äöüß]{3,16}$",$_SESSION['wep_preview']['charname'])) $form_errors[] = 'Der Name darf nur Buchstaben und Leerzeichen enthalten und muss 3-16 Zeichen lang sein.';				
					//TODO: besser; Sondernamen beachten
					
					// Verbotene Namen überprüfen
					$link = $MySQL->query("SELECT name FROM server.verbotenenamen WHERE name = '".$_SESSION['wep_preview']['charname']."'");
					if($MySQL->num_rows($link)>0) $form_errors[] = 'Dieser Name steht auf der Liste der nicht erlaubten Namen.';
					
					$_SESSION['wep_preview']['geschlecht']	=$_REQUEST['geschlecht'];
					$_SESSION['wep_preview']['volk']	 	= intval($_REQUEST['volk']);
					// Überprüfe Geschlecht und Volk (existiert bzw. gesperrt?)
					if($_SESSION['wep_preview']['geschlecht']=='w') {
						$_SESSION['wep_preview']['gumpid_body'] = intval(@array_pop($MySQL->query_first("SELECT gumpid_female_body FROM server.voelker WHERE id = '".$_SESSION['wep_preview']['volk']."' AND gesperrt = 0")));
					}
					elseif($_SESSION['wep_preview']['geschlecht']=='m') {
						$_SESSION['wep_preview']['gumpid_body'] = intval(@array_pop($MySQL->query_first("SELECT gumpid_male_body FROM server.voelker WHERE id = '".$_SESSION['wep_preview']['volk']."' AND gesperrt = 0")));
					}
					else {
						$form_errors[] = 'Ung&uuml;ltiges Geschlecht ausgew&auml;hlt!';
					}				
					if($_SESSION['wep_preview']['gumpid_body']==0) $form_errors[] = 'Dieses Volk ist zur Zeit gesperrt!';
				
					if( count( $form_errors ) > 0 ) {
						$Smarty->assign( 'form_errors', $form_errors );
					}
					else {
						$_SESSION['wep_preview']['step']=2;
					}
				break;
				case 2:
					$_SESSION['wep_preview']['haar']	 = intval($_REQUEST['haar']);
					$_SESSION['wep_preview']['bart']	 = intval($_REQUEST['bart']);
					$_SESSION['wep_preview']['haarfarbe']= intval($_REQUEST['haarfarbe']);
					$_SESSION['wep_preview']['hautfarbe']= intval($_REQUEST['hautfarbe']);
				
				
					// check values
					$link = $MySQL->query("SELECT id FROM server.haare WHERE gumpid = '".$_SESSION['wep_preview']['haar']."' AND ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1");
					if($MySQL->num_rows($link)==0) {
						$form_errors[] = 'Bitte Haare auswählen';
					}
					$link = $MySQL->query("SELECT id FROM server.baerte WHERE gumpid = '".$_SESSION['wep_preview']['bart']."' AND ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1");
					if($MySQL->num_rows($link)==0 AND $_SESSION['wep_preview']['bart'] != 0 ) {
						$form_errors[] = 'Bitte Bart auswählen';
					}
					$link = $MySQL->query("SELECT id FROM server.haarfarben WHERE hexfarbe = '".$_SESSION['wep_preview']['haarfarbe']."' AND ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1");
					if($MySQL->num_rows($link)==0) {
						$form_errors[] = 'Bitte Haarfarbe auswählen';
					}
					$link = $MySQL->query("SELECT id FROM server.hautfarben WHERE hexfarbe = '".$_SESSION['wep_preview']['hautfarbe']."' AND ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1");
					if($MySQL->num_rows($link)==0) {
						$form_errors[] = 'Bitte Hautfarbe auswählen';
					}
				
					$Smarty->assign('haar', $_SESSION['wep_preview']['haar']);
					$Smarty->assign('bart', $_SESSION['wep_preview']['bart']);
					$Smarty->assign('haarfarbe', $_SESSION['wep_preview']['haarfarbe']);
					$Smarty->assign('hautfarbe', $_SESSION['wep_preview']['hautfarbe']);
				
					if( count( $form_errors ) > 0 ) {
							$Smarty->assign( 'form_errors', $form_errors );
					}
					if($_REQUEST['send_form'] != 'Vorschau') {
						if( count( $form_errors ) > 0 ) {
							$Smarty->assign( 'form_errors', $form_errors );
						}
						else {
							$_SESSION['wep_preview']['step']=3;
						}
					}
				break;
			}
			switch($_SESSION['wep_preview']['step']) {
				case 1:
					// zeige 1. Schritt (Auswahl Name und Volk an)
					$Smarty->assign( 'charname', $_SESSION['wep_preview']['charname'] );
					$ergebnis = $MySQL->fetch_complete("SELECT ID, Bezeichnung, Rasse FROM server.voelker WHERE gesperrt = 0 ORDER BY Rasse, Bezeichnung" );

				for($i=0;$i<count($ergebnis);$i++) {
						$voelker[$i][1] = $ergebnis[$i]['Bezeichnung']." (".$ergebnis[$i]['Rasse'].")";
						$voelker[$i][0] = $ergebnis[$i]['ID'];
					}
 
					$Smarty->assign( 'geschlechter', array("w"=>"weiblich","m"=>"männlich") );
					$Smarty->assign( 'geschlecht', $_SESSION['wep_preview']['geschlecht'] );
					$Smarty->assign( 'volk', $_SESSION['wep_preview']['volk'] );
					$Smarty->assign( 'voelker', $voelker );
					$page['content'] = $Smarty->fetch('wep_chargen_step1.tpl');
				break;
				case 2:
					// Zeige 2. Schritt (Haare, Bart, Haarfarbe, Hautfarbe)
				
					// 1. Hole Haare
					$haare[] = "Bitte w&auml;hlen...";
					$haare = array_merge($haare, $MySQL->fetch_complete("SELECT gumpid, bezeichnung FROM server.haare WHERE ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1 ORDER BY bezeichnung"));
					$Smarty->assign('haare',$haare);
	
					// 2. Hole Bärte
					$baerte[] = array(0 => 0, 'gumpid' => 0, 1 => 'kein Bart', 'bezeichnung' => 'kein Bart');
					$baerte = array_merge($baerte, $MySQL->fetch_complete("SELECT gumpid, bezeichnung FROM server.baerte WHERE ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1 ORDER BY bezeichnung"));
					$Smarty->assign('baerte',$baerte);
				
					// 3. Hole Haar-/Bartfarben
					$haarfarben[] = "Bitte w&auml;hlen...";
					$haarfarben = array_merge($haarfarben, $MySQL->fetch_complete("SELECT hexfarbe, bezeichnung FROM server.haarfarben WHERE ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1 ORDER BY bezeichnung" ));
					$Smarty->assign('haarfarben',$haarfarben);

					// 2. Hole Hautfarben
					$hautfarben[] = "Bitte w&auml;hlen...";
					$hautfarben = array_merge($hautfarben, $MySQL->fetch_complete("SELECT hexfarbe, bezeichnung FROM server.hautfarben WHERE ".$_SESSION['wep_preview']['volk']."_".$_SESSION['wep_preview']['geschlecht']." = 1 ORDER BY bezeichnung" ));
					$Smarty->assign('hautfarben',$hautfarben);

					$page['content'] = $Smarty->fetch('wep_chargen_step2.tpl');
				break;
				case 3:

				break;
			}
		break;
		default:
			// neuen Char anlegen

			$_SESSION['wep_preview'] = NULL;  // BUG in PHP
			unset($_SESSION['wep_preview']);

			$_SESSION['wep_preview']['step']=1;
		
			// zeige 1. Schritt (Auswahl Name und Volk an)
			$ergebnis = $MySQL->fetch_complete("SELECT ID, Bezeichnung, Rasse FROM server.voelker WHERE gesperrt = 0 ORDER BY Rasse, Bezeichnung" );
			for($i=0;$i<count($ergebnis);$i++) {
				$voelker[$i][1] = $ergebnis[$i]['Bezeichnung']." (".$ergebnis[$i]['Rasse'].")";
				$voelker[$i][0] = $ergebnis[$i]['ID'];
			}
 
			$Smarty->assign( 'geschlechter', array("w"=>"weiblich","m"=>"männlich") );
			$Smarty->assign( 'voelker', $voelker );
			$page['content'] = $Smarty->fetch('wep_chargen_step1.tpl');
		break;
	}
	$Smarty->assign( 'page', $page );
	$Smarty->display( 'main.tpl' );
	
die();	

// ENDE Chargen Preview
}

if( isset( $_REQUEST['imgID'] ) )
{
    $wep = $MySQL->query_first( "SELECT * FROM weekend_pictures WHERE wepID = ".intval( $_REQUEST['imgID'] ) );
    if( $wep['starttime'] > time() )
    	access_error();
    	
    $Smarty->assign( 'wep', $wep );

    $imgurl = combine_path( $settings['wep_dir'], 'image'.$wep['wepID'].'.jpg' );
    $Smarty->assign( 'imgurl', $imgurl );

    $Smarty->display( 'wep_item.tpl' );

    die();
}

$ppr = 3;
$Smarty->assign( 'ppr', $ppr );
$ppp = 6;

// start page navigation

if( !isset( $_REQUEST['page_num'] ) || $_REQUEST['page_num'] < 1 )
	$page_num = 1;
else
	$page_num = intval( $_REQUEST['page_num'] );

$Smarty->assign( 'page_num', $page_num );

list( $screenshot_count ) = $MySQL->query_first( "SELECT COUNT(*) FROM weekend_pictures WHERE starttime <= ".time() );
$pages = ceil( $screenshot_count / intval( $ppp ) );

$Smarty->assign( 'pages', $pages );

$page_navigation = make_page_navigation( "wep.php?&amp;".SID, $page_num, $pages, 2 );
$Smarty->assign( 'page_navigation', $page_navigation );

// end page navigation

$weps_res = $MySQL->query( "SELECT * FROM weekend_pictures WHERE starttime <= ".time()." ORDER BY `starttime` DESC LIMIT ".( intval( $ppp ) * ( $page_num - 1 ) ).", ".$ppp );
$weps = array();
while( $row = $MySQL->fetch_array( $weps_res ) )
{
    $row['thumburl'] = combine_path( $settings['wep_dir'], 'thumb'.$row['wepID'].'.gif' );
    $row['imgurl'] = combine_path( $settings['wep_dir'], 'image'.$row['wepID'].'.jpg' );
    $row['imgsize'] = @getimagesize( $row['imgurl'] );
    $row['thumbsize'] = @getimagesize( $row['thumburl'] );

    $weps[] = $row;
}
$Smarty->assign( 'weps', $weps );

$page['content'] = $Smarty->fetch( 'wep.tpl' );

$Smarty->assign( 'page', $page );
$Smarty->display( 'main.tpl' );

?>