/**
 * Output the relative file path to the avatar to crop.
 *
 * @since 1.1.0
 */
function bp_avatar_to_crop_src()
{
    echo bp_get_avatar_to_crop_src();
}
function tv_upload_file()
{
    global $bp, $wpdb;
    if (!isset($bp->signup->step)) {
        $bp->signup = new stdClass();
    }
    if (!isset($bp->avatar_admin)) {
        $bp->avatar_admin = new stdClass();
    }
    $bp->signup->step = 'completed-confirmation';
    $next_user_id = get_last_id_auto_increment($wpdb->prefix . 'users');
    $bp->signup->avatar_dir = wp_hash($next_user_id);
    $bp->displayed_user->id = $next_user_id;
    //$bp->avatar_admin->image->url  = $_FILES['file']['name'];
    $avatar = tv_bp_core_avatar_handle_upload($_FILES, 'xprofile_avatar_upload_dir');
    if ($avatar) {
        $bp->avatar_admin->step = 'crop-image';
        ob_start();
        //tv_add_jquery_cropper();
        // Bail if no image was uploaded
        $image = apply_filters('bp_inline_cropper_image', getimagesize(bp_core_avatar_upload_path() . buddypress()->avatar_admin->image->dir));
        if (empty($image)) {
            //
            $full_height = bp_core_avatar_full_height();
        }
        $full_width = bp_core_avatar_full_width();
        // Calculate Aspect Ratio
        if (!empty($full_height) && $full_width != $full_height) {
            $aspect_ratio = $full_width / $full_height;
        } else {
            $aspect_ratio = 1;
        }
        // Default cropper coordinates
        $crop_left = round($image[0] / 4);
        $crop_top = round($image[1] / 4);
        $crop_right = $image[0] - $crop_left;
        $crop_bottom = $image[1] - $crop_top;
        $html = '<li class="image-wrap thumbnail" style="width: 100%">';
        //$html .= '<h5>'. _e( 'Crop Your New Avatar', 'buddypress' ).'</h5>';
        $html = '<img src="' . bp_get_avatar_to_crop() . '" id="avatar-to-crop" class="avatar" alt="Image to crop" />';
        $html .= '  <div id="avatar-crop-pane">';
        $html .= '      <img src="' . bp_get_avatar_to_crop() . '" id="avatar-crop-preview" class="avatar" alt="Crop Preview" />';
        $html .= '  </div>';
        $html .= '  <input type="button" name="avatar_crop_submit" id="avatar_crop_submit" value="Crop image" />';
        $html .= '  <img class="waiting" src="' . esc_url(admin_url('images/wpspin_light.gif')) . '" alt="" style="display:none" />';
        $html .= '<input type="hidden" name="image_src" id="image_src" value="' . bp_get_avatar_to_crop_src() . '" />';
        $html .= '<input type="hidden" name="id_user" id="id_user" value="' . $next_user_id . '" />';
        $html .= '<input type="hidden" id="x" name="x" />';
        $html .= '<input type="hidden" id="y" name="y" />';
        $html .= '<input type="hidden" id="w" name="w" />';
        $html .= '<input type="hidden" id="h" name="h" />';
        $html .= '<script type="text/javascript">';
        $html .= '
            jQuery("#avatar-to-crop").Jcrop({
                onChange: showPreview,
                onSelect: showPreview,
                onSelect: updateCoords,
                keySupport:false,
                aspectRatio: ' . $aspect_ratio . ',
                setSelect: [' . $crop_left . ', ' . $crop_top . ', ' . $crop_right . ', ' . $crop_bottom . ' ]
            });
            updateCoords({x:' . $crop_left . ', y: ' . $crop_top . ', w: ' . $crop_right . ', h: ' . $crop_bottom . '});

        function updateCoords(c) {
            jQuery("#x").val(c.x);
            jQuery("#y").val(c.y);
            jQuery("#w").val(c.w);
            jQuery("#h").val(c.h);
        }

        function showPreview(coords) {

            if ( parseInt(coords.w) > 0 ) {
                var fw = 150;
                var fh = 150;
                var rx = fw / coords.w;
                var ry = fh / coords.h;

                jQuery( "#avatar-crop-preview" ).css({
                    width: Math.round(rx * ' . $image[0] . ') + "px",
                    height: Math.round(ry * ' . $image[1] . ') + "px",
                    marginLeft: "-" + Math.round(rx * coords.x) + "px",
                    marginTop: "-" + Math.round(ry * coords.y) + "px"
                });
            }
        }

        ';
        $html .= '</script>';
        $html .= '
        <style type="text/css">
        .jcrop-holder { float: left; margin: 0 20px 20px 0; text-align: left; }
        #avatar-crop-pane { width: 150px; height: 150px; overflow: hidden; }
        #avatar-crop-submit { margin: 20px 0; }
        .jcrop-holder img,
        #avatar-crop-pane img,
        #avatar-upload-form img,
        #create-group-form img,
        #group-settings-form img { border: none !important; max-width: none !important; }
    </style>    ';
        echo $html;
    } else {
        echo 'Error upload';
    }
    exit;
}