<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 3 2008-08-25 18:35 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

$face_offset=25;
if($in_ajax) {

    require_once MUDDER_CACHEDIR.'./cache_faces.php';
    if($op=="browse") {
        $caption = '选择头像';
        if($ucenter_enable) {
            echo '<div id="chagesfacetbl">'.uc_avatar($user->uid).'</div>';
        } else {
            $offset = $face_offset;
            $total = count($_faces);
            $max_page = ceil($total / $offset);
            $page = $page && $page <= $max_page ? $page : 1;
            $min = ($page - 1) * $offset;
            $max = $page * $offset - 1;
            $faces = array();
            $i = 0;
            foreach($_faces as $key => $val) {
                if($i >= $min && $i <=$max) {
                    $tmp = str_replace(array('.gif','jpg','.png','jpeg'),'',strtolower($val));
                    $faces[$tmp] = get_face($val);
                    unset($tmp);
                }
                $i++;
            }
            foreach($faces as $key => $val){
                echo "<li><img src=\"$val\" style=\"border:1px solid #ccc;padding:1px\" /><br /><input type=\"radio\" name=\"faceid\" value=\"$key\" />$key</li>";
            }
        }
        output();
    } elseif($op=="change") {
        $caption = '更改头像';
        $faceid = isset($faceid) ? trim($faceid) : '';
        if(empty($faceid) && $faceid < 0) dialog($caption, '未选择头像。');
        $result = FALSE;
        foreach($_faces as $key => $face) {
            $tmp = str_replace(array('.gif','.jpg','.png','.jpeg'),'',strtolower($face));
            if($tmp == $faceid) {
                $newface = $key;
                $result = TRUE;
            }
        }
        if(!$result) {
            dialog($caption, '无效的头像。'.$faceid);
        } else {
            $new_face = $_faces[$newface];
            $db->update("UPDATE {$dbpre}members SET face='$new_face' WHERE uid='{$user->uid}'");
            echo get_face($new_face);
            output();
        }
    } else {
        dialog('系统提示','未定义的操作。');
    }

} elseif(!check_submit('mysubmit')) {

    $max_face_page = ceil(count($_faces) / $face_offset);
    $page = $page && $page <= $max_page ? $page : 1;
    $min = ($page - 1) * $face_offset;
    $max = $page * $face_offset - 1;
    $faces = array();
    $i = 0;
    foreach($_faces as $key => $val) {
        if($i >= $min && $i <=$max) {
            $tmp = str_replace(array('.gif', 'jpg', '.png', 'jpeg'), '', strtolower($val));
            $faces[$tmp] = get_face($val);
        }
        $i++;
    }
    if($ucenter_enable) {
        $HTML_face = '<div id="chagesfacetbl">'.uc_avatar($user->uid).'</div>';
    }

} else {
    redirect('未定义操作。');
}
?>