$video, 'view' => $view, 'sort' => $sort));
} else {
    $output['url_delete_filter'] = create_url($setting, array('video' => $video, 'view' => $view, 'sort' => $sort, 'filter' => $filter, 'filterid' => $filterid));
}
/* ################
 * # SELECT MEDIA #
 */################
if ($video == 'tvshows') {
    $mysql_table = 'tvshows';
} else {
    $mysql_table = 'movies';
}
$output['select_media'] = '' . mb_strtoupper($lang['i_movies']) . '' . mb_strtoupper($lang['i_tvshows']) . '';
if ($setting['select_media_header'] == 1) {
    $count_movies = mysql_result(mysql_q('SELECT COUNT( * ) FROM movies'), 0);
    $count_tvshows = mysql_result(mysql_q('SELECT COUNT( * ) FROM tvshows'), 0);
    if ($count_movies == 0 or $count_tvshows == 0) {
        $output['select_media'] = '';
    }
}
/* #############
 * # TOP PANEL #
 */#############
$show['panel_top'] = $setting['panel_top'];
if ($setting['panel_top'] == 1) {
    $top_panel_sql = array(
        'top_item_last_added' => 'SELECT id, title, date_added, hide FROM ' . $mysql_table . ' WHERE hide=0 ORDER BY date_added DESC LIMIT ' . $setting['panel_top_limit'],
        'top_item_most_watched' => 'SELECT id, title, hide FROM ' . $mysql_table . ' WHERE hide=0 ORDER BY play_count DESC LIMIT ' . $setting['panel_top_limit'],
        'top_item_last_played' => 'SELECT id, title, last_played, hide FROM ' . $mysql_table . ' WHERE hide=0 ORDER BY last_played DESC LIMIT ' . $setting['panel_top_limit'],
        'top_item_top_rated' => 'SELECT id, title, rating, hide FROM ' . $mysql_table . ' WHERE hide=0 ORDER BY rating DESC LIMIT ' . $setting['panel_top_limit']
    );
    foreach ($top_panel_sql as $name => $item_top_sql) {
        $output[$name] = '';
        $item_top_result = mysql_q($item_top_sql);
        while ($item_top = mysql_fetch_array($item_top_result)) {
            if (file_exists('cache/' . $mysql_table . '_' . $item_top['id'] . '.jpg')) {
                $output[$name].= '
';
            }
        }
    }
}
/* ####################
 * # ARRAYS FOR PANEL #
 */####################
if ($video == 'tvshows') {
    $columns = array('actor', 'genre', 'premiered');
} else {
    $columns = array('actor', 'genre', 'country', 'year', 'director', 'set', 'studio');
}
$panels_array = panels_array($columns, $mysql_table);
$filter_array = array('actor', 'genre', 'country', 'studio', 'director');
if ($filter == '') {
    $mysql_table2 = '';
    $filter_mysql = '';
} else if (in_array($filter, $filter_array)) {
    $mysql_table2 = ', ' . $mysql_table . '_' . $filter;
    $filter_mysql = $mysql_table . '_' . $filter . '.' . $filter . 'id = '. $filterid . ' AND ' . $mysql_table . '.id = ' . $mysql_table . '_' . $filter . '.id AND';
} else {
    $mysql_table2 = '';
    $filter_mysql = $mysql_table . '.' . $filter . ' LIKE "%' . $panels_array[$filter][$filterid] . '%" AND';
}
/* ##############
 * # LEFT PANEL #
 */##############
 
// overall panel
$show['panel_overall'] = $setting['panel_overall'];
if ($setting['panel_overall'] > 0) {
    $overall_sql = 'SELECT play_count, hide FROM ' . $mysql_table . ' WHERE hide=0';
    $overall_result = mysql_q($overall_sql);
    $overall_all = mysql_num_rows($overall_result);
    $overall_watched = 0;
    while($overall = mysql_fetch_array($overall_result)) {
        if ($overall['play_count'] > 0) {
            $overall_watched++;
        }
    }
    $output['overall_all'] = $overall_all;
    $output['overall_watched'] = $overall_watched;
    $output['overall_unwatched'] = $overall_all - $overall_watched;
}
// menu panel
$menu_array = array('genre', 'year', 'country', 'set', 'studio');
foreach ($menu_array as $menu_name) {
    $output['panel_' . $menu_name] = '';
    if ($setting['panel_' . $menu_name] <> 0 && isset($panels_array[$menu_name]) && count($panels_array[$menu_name]) > 0) {
        $show['panel_' . $menu_name] = 1;
        foreach ($panels_array[$menu_name] as $key => $val) {
            if ($filter == $menu_name && $filterid == $key) {
                $output['panel_' . $menu_name].= '
';
    }
    
    // play count
    if ($setting['show_playcount'] == 1 && $list['play_count'] > 0) {
        $playcount_array = str_split($list['play_count']);
        $output_desc['playcount_img'] = '
';
        }
        $output_desc['playcount_img'].= '
 ';
            } else if ($r >= 0.5) {
                $output_desc['rating_star'].= '
 '; 
            } else {
                $output_desc['rating_star'].= '
 ';
            }
            $r--;
        }
    }
    
    // actors
    $output_actor_array = array();
    $actor_sql = 'SELECT actor.id, actor.actor FROM actor, ' . $video . '_actor WHERE ' . $video . '_actor.id = "' . $list['id'] . '" AND actor.id = ' . $video . '_actor.actorid ORDER BY ' . $video . '_actor.order';
    $actor_res = mysql_q($actor_sql);
    
    while ($val = mysql_fetch_assoc($actor_res)) {
        if ($val['actor'] !== '') {
            if (file_exists('cache/actors/' . substr(md5($val['actor']), 0, 10) . '.jpg')) {
                $actor_thumb = '
';
            } else {
                $actor_thumb = '';
            }
            $output_actor_array[] = '' . $actor_thumb . $val['actor'] . '';
        }
    }
    if (count($output_actor_array) > 0) {
        $show_desc['actor'] = 1;
        $output_desc['actor'] = implode(' / ', $output_actor_array);
    }
    
    // plot
    if ($list['plot'] !== '') {
        $show_desc['plot'] = 1;
        $output_desc['plot'] = $list['plot'];
    }
    
    // only movies
    if ($video == 'movies') {
    
        // year
        if ($list['year'] !== '') {
            $show_desc['year'] = 1;
            $output_desc['year'] = '' . $list['year'] . '';
        }
        
        // country
        $output_country_array = array();
        $country_sql = 'SELECT country.id, country.country FROM country, ' . $video . '_country WHERE ' . $video . '_country.id = "' . $list['id'] . '" AND country.id = ' . $video . '_country.countryid';
        $country_res = mysql_q($country_sql);
        
        while ($val =  mysql_fetch_assoc($country_res)) {
            $output_country_array[] = '' . $val['country'] . '';
        }
        if (count($output_country_array) > 0) {
            $show_desc['country'] = 1;
            $output_desc['country'] = implode(' / ', $output_country_array);
        }
        
        // runtime
        if ($list['runtime'] !== NULL) {
            $show_desc['runtime'] = 1;
            $output_desc['runtime'] = $list['runtime'];
        }
        
        // director
        $director_sql = 'SELECT director.id, director.director FROM director, ' . $video . '_director WHERE ' . $video . '_director.id = "' . $list['id'] . '" AND director.id = ' . $video . '_director.directorid';
        $director_res = mysql_q($director_sql);
        $val =  mysql_fetch_assoc($director_res);
        if (isset($val['director'])) {
            $show_desc['director'] = 1;
            $output_desc['director'] = '' . $val['director'] . '';
        }
        
        // set
        if ($list['set'] !== '') {
            $show_desc['set'] = 1;
            $output_desc['set'] = '' . $list['set'] . '';
        }
        
        // imdb id
        if ($list['imdbid'] !== '' && $list['imdbid']{0} == 't') {
            $output_desc['imdb_url'] = '
';
        }
        
        // studio
        $studio_sql = 'SELECT studio.id, studio.studio FROM studio, ' . $video . '_studio WHERE ' . $video . '_studio.id = "' . $list['id'] . '" AND studio.id = ' . $video . '_studio.studioid';
        $studio_res = mysql_q($studio_sql);
        $val =  mysql_fetch_assoc($studio_res);
        if (isset($val['studio'])) {
            $show_desc['studio'] = 1;
            $output_desc['studio'] = '' . $val['studio'] . '';
            if (file_exists('templates/' . $setting['theme'] . '/img/studios/' . $val['studio'] . '.png')) {
                $show_desc['studio_art'] = 1;
                $output_desc['studio_art'] = '
';
            }
        }
        
        // ribbon new
        if (substr($list['date_added'], 0, 10) == $new_date) {
            $show_desc['ribbon_new'] = 1;
            $output_desc['ribbon_new'] = '
';
        }
        
        // streams
        $stream_sql = 'SELECT * FROM `movies_stream` WHERE id = "' . $list['id'] . '"';
        $stream_res = mysql_q($stream_sql);
        
        if (mysql_num_rows($stream_res) > 0) {
            $str = array('v' => array(), 'a' => array(), 's' => array());
            while ($stream = mysql_fetch_assoc($stream_res)) {
                $str[$stream['type']][] = $stream;
            }
        }
        
        $img_flag_vres = '';
        $img_flag_vtype = '';
        $img_flag_vq = '';
        if (isset($str['v'])) {
            foreach ($str['v'] as $s) {
                // video resolution
                foreach ($vres_assoc as $key => $val) {
                    if (is_numeric($s['v_width']) && $s['v_width'] >= $key) {
                        $img_flag_vres = '
';
                    }
                }
                
                // video codec
                foreach ($vtype_assoc as $key => $val) {
                    if (in_array($s['v_codec'], $vtype_assoc[$key])) {
                        $img_flag_vtype = '
';
                    }
                }
                
                // video sd, hd or uhd
                $img_flag_vq = '
';
                if (is_numeric($s['v_width']) && $s['v_width'] >= 1280) {
                    $img_flag_vq = '
';
                }
                if (is_numeric($s['v_width']) && $s['v_width'] >= 3000) {
                    $img_flag_vq = '
';
                }
                
                $output_desc['img_flag_v'].= '
';
                    }
                }
                
                // audio channel
                foreach ($achan_assoc as $val) {
                    if (is_numeric($s['a_chan']) && $s['a_chan'] >= $val) {
                        $img_flag_achan = '
';
                    }
                }
                
                // audio language
                if (file_exists('templates/' . $setting['theme'] . '/img/flags/l_' . check_flag($s['a_lang'], $iso_lang) . '.png')) {
                    $img_flag_alang = '
';
                } else {
                    $img_flag_alang = $s['a_lang'];
                }
                $output_desc['img_flag_a'].= '
';
                } else {
                    $img_flag_slang = $s['s_lang'];
                }
                $output_desc['img_flag_s'].= '
';
        }
        if ($list['trailer'] !== '' && $setting['show_trailer'] == 1 && $id <> 0) {
            $show_desc['trailer'] = 1;
            if (substr($list['trailer'], 0, 18) == 'http://www.youtube') {
                $output_desc['trailer'].= '
                    
                ';
            } else {
                $ext = substr($list['trailer'], strrpos($list['trailer'], '.')+1, strlen($list['trailer']));
                foreach ($mimetype_assoc as $key => $val) {
                    if(in_array($ext, $val)) {
                        $mimetype = $key;
                        break;
                    } else {
                        $mimetype = '';
                    }
                }
                if ($ext == 'mov') {
                    $output_desc['trailer'].= '
                        ';
                } else {
                    $output_desc['trailer'].= '
                    ';
                }
            }
        }
    }
    
    // only tvshows
    if ($video == 'tvshows') {
    
        // premiered
        if ($list['premiered'] !== '') {
            $show_desc['premiered'] = 1;
            $output_desc['premiered'] = '' . $list['premiered'] . '';
        }
    
        // seasons
        $season_array = array();
        $seasons_sql = 'SELECT season FROM episodes WHERE tvshow = "' . $list['id'] . '" ORDER BY season';
        $seasons_result = mysql_q($seasons_sql);
        while ($seasons = mysql_fetch_array($seasons_result)) {
            if (!array_key_exists($seasons['season'], $season_array)) {
                $season_array[$seasons['season']] = '' . $lang['i_season'] . ' ' . $seasons['season'] . '';
            }
        }
        if (count($season_array) <> 0) {
            $show_desc['seasons'] = 1;
            $output_desc['seasons'] = implode(' / ', $season_array);
        }
        
        // episodes
        if ($id <> 0) {
            // get date for last added episode
            $new_episode_sql = 'SELECT date_added FROM episodes ORDER BY date_added DESC LIMIT 0, 1';
            $new_episode_result = mysql_q($new_episode_sql);
            $new_episode_date = mysql_fetch_assoc($new_episode_result);
            $new_episode_date = substr($new_episode_date['date_added'], 0, 10);
        
            $show_desc['episodes'] = 1;
            $episodes_sql = 'SELECT id, title, episode, season, plot, firstaired, file, play_count, date_added, last_played FROM episodes WHERE tvshow = "' . $list['id'] . '" ORDER BY season, episode ASC';
            $episodes_result = mysql_q($episodes_sql);
            $i = -1;
            
            $output_epiosde_list = '';
            while ($episodes = mysql_fetch_assoc($episodes_result)) {
            
                // output and show episodes arrays
                $output_episode = array();
                $show_episode = array();    
                foreach ($item_episode as $val) {
                    $output_episode[$val] = '';
                    $show_episode[$val] = 0;
                }
                
                if (isset($_SESSION['logged_admin']) && $_SESSION['logged_admin'] == true && $video == 'tvshows') {
                    $show_episode['xbmc'] = 1;
                }
                
                $output_episode['episode'] = $episodes['id'];
                $output_episode['season'] = $episodes['season'];
                $output_episode['plot'] = $episodes['plot'];
                $output_episode['aired'] = $episodes['firstaired'];
                                
                // title
                $output_episode['title'] = $episodes['episode'] . '. ' . ($episodes['title'] == '' ? $lang['i_episode'] . ' ' . $episodes['episode'] : $episodes['title']);
                
                // file
                $output_episode['file'] = 'http://' . $setting['xbmc_login'] . ':' . $setting['xbmc_pass'] . '@' . $setting['xbmc_host'] . ':' . $setting['xbmc_port'] . '/vfs/' . urlencode($episodes['file']);
                
                // thumbnail
                if (file_exists('cache/episodes_' . $episodes['id'] . '.jpg')) {
                    $output_episode['thumbnail'] = '
';
                } else {
                    $output_episode['thumbnail'] = '
';
                }
                
                // episode ribbon new
                if (substr($episodes['date_added'], 0, 10) == $new_episode_date) {
                    $output_episode['ribbon_new'] = '
';
                }
        
                // episode streams
                $stream_sql = 'SELECT * FROM `episodes_stream` WHERE id = "' . $episodes['id'] . '"';
                $stream_res = mysql_q($stream_sql);
                
                if (mysql_num_rows($stream_res) > 0) {
                    $str = array('v' => array(), 'a' => array(), 's' => array());
                    while ($stream = mysql_fetch_assoc($stream_res)) {
                        $str[$stream['type']][] = $stream;
                    }
                }
                
                $img_flag_vres = '';
                $img_flag_vtype = '';
                $img_flag_vq = '';
                if (isset($str['v'])) {
                    foreach ($str['v'] as $s) {
                        // episode video resolution
                        foreach ($vres_assoc as $key => $val) {
                            if (is_numeric($s['v_width']) && $s['v_width'] >= $key) {
                                $img_flag_vres = '
';
                            }
                        }
                        // episode video codec
                        foreach ($vtype_assoc as $key => $val) {
                            if (in_array($s['v_codec'], $vtype_assoc[$key])) {
                                $img_flag_vtype = '
';
                            }
                        }
                        // episode video sd, hd or uhd
                        $img_flag_vq = '
';
                        if (is_numeric($s['v_width']) && $s['v_width'] >= 1280) {
                            $img_flag_vq = '
';
                        }
                        if (is_numeric($s['v_width']) && $s['v_width'] >= 3000) {
                            $img_flag_vq = '
';
                        }
                        $output_episode['img_flag_v'].= $img_flag_vres . $img_flag_vtype . $img_flag_vq;
                    }
                }
                if (isset($str['a'])) {
                    foreach ($str['a'] as $s) {
                        // episode audio codec
                        foreach ($atype_assoc as $key => $val) {
                            if(in_array($s['a_codec'], $atype_assoc[$key])) {
                                $img_flag_atype = '
';
                            }
                        }
                        // episode audio channel
                        foreach ($achan_assoc as $val) {
                            if (is_numeric($s['a_chan']) && $s['a_chan'] >= $val) {
                                $img_flag_achan = '
';
                            }
                        }
                        // episode audio language
                        if (file_exists('templates/' . $setting['theme'] . '/img/flags/l_' . check_flag($s['a_lang'], $iso_lang) . '.png')) {
                            $img_flag_alang = '
';
                        } else {
                            $img_flag_alang = $s['a_lang'];
                        }
                        $output_episode['img_flag_a'].= $img_flag_atype . $img_flag_achan . $img_flag_alang;
                    }
                }
                if (isset($str['s'])) {
                    foreach ($str['s'] as $s) {
                        // episode subtitles
                        if (file_exists('templates/' . $setting['theme'] . '/img/flags/l_' . check_flag($s['s_lang'], $iso_lang) . '.png')) {
                            $img_flag_slang = '
';
                        } else {
                            $img_flag_slang = $s['s_lang'];
                        }
                        $output_episode['img_flag_s'].= $img_flag_slang ;
                    }
                }
                if ($episodes['season'] <> $i) {
                    $show_episode['season_title'] = 1;
                }
                $i = $episodes['season'];
            
                $episode_list = new Teamplate('episodes.tpl', $setting, $lang);
                foreach ($output_episode as $key => $val) {
                    $episode_list->tpl($key, $val);
                }
                foreach ($show_episode as $key => $val) {
                    $episode_list->show($key, $val);
                }
                $output_epiosde_list.= $episode_list->init();
                $output_desc['episodes'] = $output_epiosde_list;
            }
        }
    }
    
    // facebook meta data
    if ($setting['show_facebook'] == 1) {
        $show_desc['facebook_button'] = 1;
    }
    $url = 'http://' . $_SERVER['SERVER_NAME'] . implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), 0, -1)) . '/';
    $output_desc['fb_url'] = $url . 'index.php?video=' . $video . '&fb_link=' . urlencode($list['title']) . ($video == 'movies' ? $list['year'] : '');
    if ($id <> 0) {
        $output['meta_img'] = (file_exists('cache/' . $mysql_table . '_' . $id . '.jpg') ? $url . 'cache/' . $mysql_table . '_' . $id . '.jpg' : 'templates/' . $setting['theme'] . '/img/d_poster.jpg');
        $output['meta_title'] = htmlspecialchars($list['title']);
        $output['meta_desc'] = htmlspecialchars($list['plot']);
        $output['meta_url'] = $output_desc['fb_url'];
        $output['meta_type'] = ($video == 'tvshows' ? 'video.tv_show' : 'video.movie');
    }
    
    // panel movie
    $panel_list = new Teamplate($views[$include_view] . '.tpl', $setting, $lang);
    foreach ($output_desc as $key => $val) {
        $panel_list->tpl($key, $val);
    }
    foreach ($show_desc as $key => $val) {
        $panel_list->show($key, $val);
    }
    $output_panel_list.= $panel_list->init();
}
$output['panel_list'] = $output_panel_list;
$output['sort'] = $sort;
// faccebok meta data
if ($id == 0) {
    $url = 'http://' . $_SERVER['SERVER_NAME'] . implode('/', array_slice(explode('/', $_SERVER['REQUEST_URI']), 0, -1)) . '/';
    $output['meta_title'] = $setting['site_name'];
    $output['meta_url'] = $url . 'index.php';
    $output['meta_img'] = $url . ('templates/' . $setting['theme'] . '/img/logo.jpg');
    $output['meta_type'] = 'website';
    $output['meta_desc'] = 'Page whereby using XBMC can present your library of movies and TV series.';
}
if ($setting['show_facebook'] == 1) {
    $show['facebook'] = 1;
    if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        $get_lang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
    } else {
        $get_lang = 'en';
    }
    if (array_key_exists ($get_lang , $lang_fb_assoc)) {
        $lang_fb = $lang_fb_assoc[$get_lang];
    } else {
        $lang_fb = $lang_fb_assoc['en'];
    }
    $output['facebook'] = '
                            ';
}
// create page
$index = new Teamplate('index.tpl', $setting, $lang);
foreach ($output as $key => $val) {
    $index->tpl($key, $val);
}
foreach ($show as $key => $val) {
    $index->show($key, $val);
}
$site = $index->init();
print $site;
?>