<?php

/////////////////////////////
// configure installation: //
/////////////////////////////

define("ROOT", "./");
require_once( ROOT.'lib/autoload.php' );

/** @var \Base $f3 */
$f3 = \Base::instance();
$f3->set('DEBUG',3);
$f3->set('CACHE',FALSE);
$f3->set('AUTOLOAD', ROOT.'app/');
$f3->set('UI', implode(';',array(
    ROOT.'app/views/',
    ROOT.'content/' // content folders can contain .html templates
)));



/////////////////////////
// main configuration: //
/////////////////////////

$f3->config( ROOT.'main.cfg' );
if(!setlocale(LC_TIME, 'de_DE.UTF-8')) {

    //echo "locale not set";
}

///////////////////////////////////////////////
// configuration based on configuration file //
///////////////////////////////////////////////
// set language
$languages = $f3->get('languages');
$f3->set('default_lang', array_shift($languages));
if (in_array(strtolower($f3->get('GET.lang')), $languages)) {
  $f3->set('LANG', strtolower($f3->get('GET.lang')));
} else {
  $f3->set('LANG', $f3->get('default_lang'));
}
// set content dir
if(array_key_exists($f3->get('LANG'), $f3->get('content'))) {
  $content_dir=$f3->get('content.'.$f3->get('LANG'));
} else {
  $content_dir=$f3->get('content.'.$f3->get('default_lang'));
}
$f3->set('CONTENT', $content_dir);

// set menu
if(array_key_exists($f3->get('LANG'), $f3->get('nav.main'))) {
  $menu=$f3->get('nav.main.'.$f3->get('LANG'));
} else {
  $menu=$f3->get('nav.main.'.$f3->get('default_lang'));
}
$f3->set('menuAddHome',$f3->get('nav.main.config.addHome'));
$f3->set('menu', $menu);
// set menu2

if(array_key_exists($f3->get('LANG'), $f3->get('nav.footer'))) {
  $menu=$f3->get('nav.footer.'.$f3->get('LANG'));
} else {
  $menu=$f3->get('nav.footer.'.$f3->get('default_lang'));
}
$f3->set('menu2AddHome',$f3->get('nav.footer.config.addHome'));
$f3->set('menu2', $menu);

if(array_key_exists($f3->get('LANG'), $f3->get('nav.network'))) {
  $menu=$f3->get('nav.network.'.$f3->get('LANG'));
} else {
  $menu=$f3->get('nav.network.'.$f3->get('default_lang'));
}
foreach($menu as $k=>$v) {
    $ex = explode(": ", $menu[$k]);
    $menu[$k] = array(
        'name' => trim($ex[0]),
        'href' => count($ex) > 1 ? $ex[1] : $ex
    );
}
$f3->set('navi.network._config.addHome', $f3->get('nav.network.config.addHome'));
$f3->set('navi.network', $menu);

if(array_key_exists($f3->get('LANG'), $f3->get('nav.languages'))) {
  $menu=$f3->get('nav.languages.'.$f3->get('LANG'));
} else {
  $menu=$f3->get('nav.languages.'.$f3->get('default_lang'));
}
if(is_array($menu)){
    foreach($menu as $k=>$v) {
        $ex = explode(": ", $menu[$k]);
        $menu[$k] = array(
            'name' => trim($ex[0]),
            'href' => count($ex) > 1 ? $ex[1] : $ex
        );
    }
    $f3->set('navi.languages._config.addHome', $f3->get('nav.languages.config.addHome'));
    $f3->set('navi.languages', $menu);
}


// this is needed so the menu can compare the current page with a link to it
// and use this information to determine the active state
$tmp_url = substr($_SERVER['REQUEST_URI'],strlen($f3->get('SITE_URL'))+1);
$url=substr($tmp_url,0,(strpos($tmp_url,'?') === false) ? 999 : strpos($tmp_url,'?'));
$f3->set('url', explode("/", $url));


///////////////////////////////////
// development utility functions //
///////////////////////////////////

function debug($Message=".") {
    if(DEBUG > 0) {
        echo "<br>".$Message;
    } 
}


##############################################################################
// this need to go away from here -----------------------------------------
// just to be more tidy etc.
function is_image($path) {
    $ex = explode('.',$path);
    $ext = array_pop($ex);
    return in_array($ext,array( 'jpg', 'jpeg', 'png' ));
}
function pic_cache($in1,$inwidth=360){
    $f3 = \Base::instance();
    if(is_image($in1)) {
        $info = pathinfo($in1);
        $fn = basename($in1,'.'.$info['extension']);
        $width=$inwidth;
        $name = md5($in1.$width);
        
        $name =  sprintf("%s%s.png",$fn,$name);
        
        $out ='rsc/img_display/s'.$name;
    
        if(!file_exists($in1)) { $in1='rsc/img/default.png'; }
    
        if(!file_exists($out)) {
            $img1 = new Image($in1);
            $img1->resize($width);
            $f3->write($out,$img1->dump('png',9));
        }
    } elseif(!strncmp("locallink:",$in1, strlen("locallink:"))) {
      $key = substr($in1, strlen("locallink:"));
      // localize internal links
      if($f3->get('LANG') != 'DE') {
        $key .= "?lang=".$f3->get('LANG');
      }
      $out = $key;
    } else {
        $out = $in1;
    }
    return $out;
}
//------------------------------------------------------------------------
###############################################################################

if ($f3->get("GET.admin")) {
    $admin = new \Controller\Admin;
    $f3->set('backend',$admin->index());
} else {
    $f3->set('backend',false);
}

// HTML preloading of images
// this could also find some better place
$f3->mset(array(
        'cached_images' => array(\Controller\Page::check_folder_for_backgroundimage('content/'))
    ));



$f3->run();
echo \Template::instance()->render($f3->get('template'));
