<?

require_once "smarty/Smarty.class.php";
//require_once "config.php";

define ('DEFAULT_ID', '1');
define ('MAIN_ID', '1');
define ('NAMAI_ID', 'namai');
define ('GYVENTOJAI_ID', 'zmones');
define ('GYVUNAI_ID', 'gyvunai');
define ('MES_ID', 'mes');

define ('SHOW_ID', '3');
define ('SHOWA_ID', '2');
define ('AUTORIAI_ID', '4');
define ('DEFAULT_V', '1');

define ('MAXF', '12');
define ('MAXC', '5');
define ('MAXR', '4');



class page {
	var $lang;
	var $id;
	// Gathered
	var $data;
	var $title;
	var $text;
	var $menu;
	var $desc;
    var $mmenu, $msize;

	function page() {
		$this->get_data();		
		$this->set();
	}

	function get_data() {
		$this->lang = "lt";
		$this->data = $_GET;
		// Get now
		$pi = $_SERVER["argv"][0];
		if (isset ($_GET["id"])) $this->id = $_GET["id"]; else $this->id = DEFAULT_ID;
	}

	
	function get_value ($wh, $def) {
		if (isset ($_GET[$wh])) return $_GET[$wh]; else return $def;	
	}
	
	function add_menu ($where, $title, $link, $parent) {
		 $this->mmenu [$where][1] = $title;
		 $this->mmenu [$where][2] = $parent;	
		 $this->mmenu [$where][3] = $link;			 
		 if ($where > $this->msize) $this->msize=$where;	 
	}
		
	function create_menu (){
		$msize = 0;
		$this->add_menu (1,"Pradzia","index.php?id=".MAIN_ID,0);
		$this->add_menu (2,"Namai","index.php?id=".SHOWA_ID."&cat=".NAMAI_ID,1);		
		$this->add_menu (3,"Gyventojai","index.php?id=".SHOWA_ID."&cat=".GYVENTOJAI_ID,1);		
		$this->add_menu (4,"Gyvunai","index.php?id=".SHOWA_ID."&cat=".GYVUNAI_ID,1);
		$this->add_menu (5,"Mes","index.php?id=".SHOWA_ID."&cat=".MES_ID,1);	
		$this->add_menu (6,"Autoriai","index.php?id=".AUTORIAI_ID."&cat=0",1);

		// Autoriai
		$this->add_menu (7,"Vilius","index.php?id=".AUTORIAI_ID."&cat=1",AUTORIAI_ID);
		$this->add_menu (8,"Kristine","index.php?id=".AUTORIAI_ID."&cat=2",AUTORIAI_ID);
		$this->add_menu (9,"Jindria","index.php?id=".AUTORIAI_ID."&cat=3",AUTORIAI_ID);
		$this->add_menu (10,"Jerika","index.php?id=".AUTORIAI_ID."&cat=4",AUTORIAI_ID);	
		$this->add_menu (11,"Jovita","index.php?id=".AUTORIAI_ID."&cat=5",AUTORIAI_ID);								
	}
	
	function menu_link($nr) {
	  return '<a href="' . $this->mmenu[$nr][3] . '">' . $this->mmenu[$nr][1] . '</a>'; 
	}
	
	function gmenu(){
     $this->create_menu();     
     $textas = $this->menu_link(1);	  
     $textas .= " : ";   
     for ($ind=1; $ind<=$this->msize; $ind++) {
     	if ($this->mmenu[$ind][2] == $this->id) {
     		  $textas .= $this->menu_link($ind) . ' :: ';
     	}
     }
	 $this->menu = $textas; 
	}
	
	function get_page($tfile) {
      if (file_exists($tfile)){		
      $cont = file($tfile);    
      $text = "";
      foreach($cont as $r) {
        $text .= $r;
      }
      return $text;
      }
      else return "";
	}
	
	function set() {
		$this->gmenu();	
		$this->title = "Neveronys :: " .  "Kaimas Sirvintu rajone";
	
		switch ($this->id) {
			case SHOWA_ID: $this->text = $this->fotos(); break;
			case SHOW_ID: $this->text = $this->foto(); break;
			case MAIN_ID: $this->text = $this->get_page("menu" . $this->id . ".txt"); break;
			case AUTORIAI_ID: $this->text = $this->get_page("images/authors/a" . $this->get_value("cat",1) . ".txt"); break;
		}	
			
	}

	
	function img_link ($nr, $cat) {
		return 'index.php?id='.SHOW_ID.'&nr='.$nr.'&cat='.$cat;
	}
		
	function img_src ($nr, $cat) {
		$kelias = 'images/' . $cat . '/foto' . $nr . '.JPG';

        if (file_exists($kelias))
          return '<img src="'.$kelias.'">';
          else return "";
	}
	
	function img_src2 ($nr, $cat) {
		$kelias = 'images/' . $cat . '/f' . $nr . '.JPG';
        if (file_exists($kelias))
          return '<img src="'.$kelias.'">';
          else return "";
	}	
			
	function fotos () {
		$start = $this->get_value("start",1);
		$cat = $this->get_value("cat","namai");	
		$mode = $this->get_value("mode",1);	
		
		$fname = "docs/" . $cat . ".txt";	 // komentsai
	    if (file_exists ($fname)) $t .= $this->get_page($fname);
	    
	    
	    
		if ($mode == 1) { // lenteleje
		$t .= "<table>";
		$now = $start;
		$now2 = 1;
		$end = $start + MAXF - 1;

		
		while ($now <= $end) {
		  if ($now2 % MAXR == 1) $t .= "<tr>";
		  $t .= "<td>";
		  $t .= '<a href="' . $this->img_link($now, $cat) . '">' . $this->img_src($now, $cat) . '</a>';
		  $t .= "</td>";	
		  if ($now2 % MAXR == 0) $t .= "</tr>";			  
		  $now++;
		  $now2++;	
		}		
		$t .= "</table>";

		}
		else {  // viename stulpelyje
	    $t .= "<table>";
		$now = $start;
		$now2 = 1;
		$end = $start + MAXC - 1;

		while ($now <= $end) {
		  $t .= "<tr><td>";
	      $t .= $this->img_src2($now, $cat) . '<br>';		  
	      $t .= "</td></tr>";		  	  
		  $now++;
		  $now2++;	
		}		
		$t .= "</table>";		
		
		}
		$t .= "<br>";
		$t .= '<a href="index.php?mode='.$mode.'&id='. SHOWA_ID.'&start=1&cat='.$cat.'">Pradzia</a> ';
		$t .= '<a href="index.php?mode='.$mode.'&id='. SHOWA_ID.'&start='.($start+1).'&cat='.$cat.'">Toliau</a>';	
		
        if ($mode == 1) 
	       $t .= ' <a href="index.php?id='.$this->id.'&cat='.$cat.'&mode=2">Ziureti dideles</a>'; else 
	       $t .= ' <a href="index.php?id='.$this->id.'&cat='.$cat.'&mode=1">Ziureti mazas</a>'; 		
		return $t;
	}
	
	function foto() {
		$nr = $this->get_value("nr",1);
		$cat = $this->get_value("cat","namai");	
		$t = $this->img_src2($nr,$cat);
		$t .= '<br><br>[ <a href="index.php?id='.SHOWA_ID.'&cat='.$cat.'">Atgal</a> ]';
		return $t;
	}
	
	function show() {
		$smarty =& new Smarty;
		//
		$smarty->assign('pagem', $this->menu);
		$smarty->assign('path_img', PATH_IMG);
		//
		$smarty->assign('id', $this->id);
		//
		$smarty->assign('title', $this->title);
		$smarty->assign('text', $this->text);

		$smarty->display('site.tpl');
	}
}

$smarty =& new Smarty;
$page =& new page();
$page->show();

?>