<?php

if(!defined('MOZG'))
        die('Not Found');

if($logged){
        $act = $_GET['act'];
        $metatags['title'] = $lang['friends'];

        if($_GET['page'] > 0) $page = intval($_GET['page']); else $page = 1;
        $gcount = 4;
        $limit_page = ($page-1)*$gcount;
                                
        switch($act){

                //################### Загрузка друзей в окне для выбора СП ###################//
                case "box":
                        NoAjaxQuery();

                        $user_id = $user_info['user_id'];

                        if($_POST['page'] > 0) $page = intval($_POST['page']); else $page = 1;
                        $gcount = 18;
                        $limit_page = ($page-1)*$gcount;

                        if($_POST['user_sex'] == 1)
                                $sql_usSex = 2;
                        elseif($_POST['user_sex'] == 2)
                                $sql_usSex = 1;
                        else
                                $sql_usSex = false;

                        //Все друзья
                        if($sql_usSex){
                                $count = $db->super_query("SELECT COUNT(*) AS cnt FROM `".PREFIX."_friends` tb1, `".PREFIX."_users` tb2 WHERE tb1.user_id = '{$user_id}' AND tb1.friend_id = tb2.user_id AND tb1.subscriptions = 0 AND tb2.user_sex = '{$sql_usSex}'");
                                
                                if($count['cnt']){
                                        $sql_ = $db->super_query("SELECT SQL_CALC_FOUND_ROWS tb1.friend_id, tb2.user_photo, user_search_pref FROM `".PREFIX."_friends` tb1, `".PREFIX."_users` tb2 WHERE tb1.user_id = '{$user_id}' AND tb1.friend_id = tb2.user_id AND tb1.subscriptions = 0 AND tb2.user_sex = '{$sql_usSex}' ORDER by `views` DESC LIMIT {$limit_page}, {$gcount}", 1);
                                        $tpl->load_template('friends/box_friend.html');
                                        foreach($sql_ as $row){
                                                $tpl->set('{user-id}', $row['friend_id']);
                                                $tpl->set('{name}', $row['user_search_pref']);

                                                if($row['user_photo'])
                                                        $tpl->set('{ava}', $config['home_url'].'uploads/users/'.$row['friend_id'].'/50_'.$row['user_photo']);
                                                else
                                                        $tpl->set('{ava}', '/images/no_ava_50.png');

                                                $tpl->compile('content');
                                        }
                                        box_navigation($gcount, $count['cnt'], "''", 'sp.openfriends', '');
                                } else
                                        msgbox('', '<div class="clear" style="margin-top:140px"></div>'.$lang['no_requests'], 'info_2');
                        } else
                                msgbox('', '<div class="clear" style="margin-top:140px"></div>'.$lang['no_requests'], 'info_2');

                        AjaxTpl();
                        
                        die();
                break;
                
                        default:
                                
                //################### Страница заявок в друзья ###################//

                        $user_id = $user_info['user_id'];

                        if($user_info['user_friends_demands'])
                                $user_speedbar = $user_info['user_friends_demands'].' '.gram_record($user_info['user_friends_demands'], 'friends_demands');
                        else
                                $user_speedbar = $lang['no_requests'];
                        //Выводим заявки в друзья если они есть
                        if($user_info['user_friends_demands']){
                                $sql_ = $db->super_query("SELECT SQL_CALC_FOUND_ROWS tb1.from_user_id, demand_date, tb2.user_photo, user_search_pref, user_country_city_name, user_birthday FROM `".PREFIX."_friends_demands` tb1, `".PREFIX."_users` tb2 WHERE tb1.for_user_id = '{$user_id}' AND tb1.from_user_id = tb2.user_id ORDER by `demand_date` DESC LIMIT {$limit_page}, {$gcount}", 1);
                        $tpl->load_template('friends/ajaxrequest.html');
                                foreach($sql_ as $row){
                                        $user_country_city_name = explode('|', $row['user_country_city_name']);
                                        $tpl->set('{country}', $user_country_city_name[0]);
                                        $tpl->set('{city}', ', '.$user_country_city_name[1]);
                                        $tpl->set('{user-id}', $row['from_user_id']);
                                        $tpl->set('{name}', $row['user_search_pref']);
                                        
                                        if($row['user_photo'])
                                                $tpl->set('{ava}', $config['home_url'].'uploads/users/'.$row['from_user_id'].'/100_'.$row['user_photo']);
                                        else
                                                $tpl->set('{ava}', '/images/no_ava_50.png');
                                        
                                        //Возраст юзера
                                        $user_birthday = explode('-', $row['user_birthday']);
                                        $tpl->set('{age}', user_age($user_birthday[0], $user_birthday[1], $user_birthday[2]));
                                        
                                        $tpl->compile('content');
                                }
                                navigation();
                        } else
                                msgbox('', $lang['no_requests'], 'info_2');
                AjaxTpl();
                die();
        }
        $db->free();
        $tpl->clear();
} else {
        $user_speedbar = 'Информация';
        msgbox('', $lang['not_logged'], 'info');
}
?>