/**
 * Handler function for displaying custom fields.
 * @param array $form_fields The fields to appear on the attachment form.
 * @param array $post Object representing the post we are saving to.
 * @return array Updated $form_fields with the new fields.
 */
function jwplayer_attachment_fields($form_fields, $post)
{
    $mime_type = substr($post->post_mime_type, 0, 5);
    switch ($mime_type) {
        case "image":
            $form_fields[LONGTAIL_KEY . "duration"] = array("label" => __("Duration"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "duration", true));
            break;
        case "audio":
        case "video":
            $form_fields[LONGTAIL_KEY . 'thumbnail_url'] = array("label" => __("Thumbnail URL"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "thumbnail_url", true));
            $form_fields[LONGTAIL_KEY . "thumbnail"] = array("label" => __("Thumbnail"), "input" => "html", "html" => generateImageSelectorHTML($post->ID));
            $form_fields[LONGTAIL_KEY . "creator"] = array("label" => __("Creator"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "creator", true));
            break;
    }
    $rtmp = get_post_meta($post->ID, LONGTAIL_KEY . "rtmp");
    if ($mime_type == "video" && isset($rtmp) && $rtmp) {
        unset($form_fields["url"]);
        $form_fields[LONGTAIL_KEY . "streamer"] = array("label" => __("Streamer"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "streamer", true));
        $form_fields[LONGTAIL_KEY . "file"] = array("label" => __("File"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "file", true));
        $form_fields[LONGTAIL_KEY . "provider"] = array("label" => __("Provider"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "Provider", true));
    }
    if (isset($_GET["post_id"]) && ($mime_type == "video" || $mime_type == "audio" || $mime_type == "image")) {
        $insert = "<input type='submit' class='button-primary' name='send[{$post->ID}]' value='" . esc_attr__('Insert JW Player') . "' />";
        $form_fields[LONGTAIL_KEY . "player_select"] = array("label" => __("Select Player"), "input" => "html", "html" => generatePlayerSelectorHTML($post->ID));
        $form_fields["jwplayer"] = array("tr" => "\t\t<tr class='submit'><td></td><td class='savesend'>{$insert}</td></tr>\n");
    }
    return $form_fields;
}
/**
 * Handler function for displaying custom fields.
 * @param array $form_fields The fields to appear on the attachment form.
 * @param array $post Object representing the post we are saving to.
 * @return array Updated $form_fields with the new fields.
 */
function jwplayer_attachment_fields($form_fields, $post)
{
    $image_args = array("post_type" => "attachment", "numberposts" => 50, "post_status" => null, "post_mime_type" => "image", "post_parent" => null);
    $image_attachments = get_posts($image_args);
    $video_args = array("post_type" => "attachment", "numberposts" => 25, "post_status" => null, "post_mime_type" => "video", "post_parent" => null);
    $video_attachments = get_posts($video_args);
    $mime_type = substr($post->post_mime_type, 0, 5);
    switch ($mime_type) {
        case "image":
            if (get_option(LONGTAIL_KEY . "image_duration")) {
                $form_fields[LONGTAIL_KEY . "duration"] = array("label" => __("Duration"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "duration", true));
            }
            break;
        case "audio":
        case "video":
            $form_fields[LONGTAIL_KEY . 'thumbnail_url'] = array("label" => __("Thumbnail URL"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "thumbnail_url", true));
            $form_fields[LONGTAIL_KEY . "thumbnail"] = array("label" => __("Thumbnail"), "input" => "html", "html" => generateImageSelectorHTML($post->ID, $image_attachments));
            $form_fields[LONGTAIL_KEY . "creator"] = array("label" => __("Creator"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "creator", true));
            $form_fields[LONGTAIL_KEY . "duration"] = array("label" => __("Duration"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "duration", true));
            $form_fields[LONGTAIL_KEY . 'html5_file'] = array("label" => __("HTML5 file"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "html5_file", true));
            $form_fields[LONGTAIL_KEY . 'html5_file_selector'] = array("label" => __(""), "input" => "html", "html" => generateVideoSelectorHTML($post->ID, "html5_file_selector", $video_attachments));
            $form_fields[LONGTAIL_KEY . 'download_file'] = array("label" => __("Download file"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "download_file", true));
            $form_fields[LONGTAIL_KEY . 'download_file_selector'] = array("label" => __(""), "input" => "html", "html" => generateVideoSelectorHTML($post->ID, "download_file_selector", $video_attachments));
            break;
    }
    $rtmp = get_post_meta($post->ID, LONGTAIL_KEY . "rtmp");
    if ($mime_type == "video" && isset($rtmp) && $rtmp) {
        unset($form_fields["url"]);
        $form_fields[LONGTAIL_KEY . "streamer"] = array("label" => __("Streamer"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "streamer", true));
        $form_fields[LONGTAIL_KEY . "file"] = array("label" => __("File"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "file", true));
        $form_fields[LONGTAIL_KEY . "provider"] = array("label" => __("Provider"), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "provider", true));
    }
    if (isset($_GET["post_id"]) && ($mime_type == "video" || $mime_type == "audio" || $mime_type == "image" && get_option(LONGTAIL_KEY . "image_duration"))) {
        $insert = "<input type='submit' class='button-primary' name='send[{$post->ID}]' value='" . esc_attr__('Insert JW Player') . "' />";
        $form_fields[LONGTAIL_KEY . "player_select"] = array("label" => __("Select Player"), "input" => "html", "html" => generatePlayerSelectorHTML($post->ID));
        $form_fields["jwplayer"] = array("tr" => "\t\t<tr class='submit'><td></td><td class='savesend'>{$insert}</td></tr>\n");
    }
    return $form_fields;
}
/**
 * Handler function for displaying custom fields.
 * @param array $form_fields The fields to appear on the attachment form.
 * @param array $post Object representing the post we are saving to.
 * @return array Updated $form_fields with the new fields.
 */
function jwplayer_attachment_fields($form_fields, $post)
{
    global $wp_version;
    $image_args = array("post_type" => "attachment", "numberposts" => 50, "post_status" => null, "post_mime_type" => "image", "post_parent" => null);
    $image_attachments = get_posts($image_args);
    $video_args = array("post_type" => "attachment", "numberposts" => 25, "post_status" => null, "post_mime_type" => "video", "post_parent" => null);
    $video_attachments = get_posts($video_args);
    $mime_type = substr($post->post_mime_type, 0, 5);
    switch ($mime_type) {
        case "image":
            if (get_option(LONGTAIL_KEY . "image_duration")) {
                $form_fields[LONGTAIL_KEY . "duration"] = array("label" => __("Duration", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "duration", true));
            }
            break;
        case "audio":
        case "video":
            $form_fields[LONGTAIL_KEY . 'thumbnail_url'] = array("label" => __("Thumb URL", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "thumbnail_url", true));
            $form_fields[LONGTAIL_KEY . "thumbnail"] = array("label" => __("Thumb", 'jw-player-plugin-for-wordpress'), "input" => "html", "html" => generateImageSelectorHTML($post->ID, $image_attachments));
            $form_fields[LONGTAIL_KEY . "creator"] = array("label" => __("Creator", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "creator", true));
            $form_fields[LONGTAIL_KEY . "duration"] = array("label" => __("Duration", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "duration", true));
            $form_fields[LONGTAIL_KEY . 'html5_file'] = array("label" => __("HTML5 file", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "html5_file", true));
            $form_fields[LONGTAIL_KEY . 'html5_file_selector'] = array("label" => __("", 'jw-player-plugin-for-wordpress'), "input" => "html", "html" => generateVideoSelectorHTML($post->ID, "html5_file_selector", $video_attachments));
            $form_fields[LONGTAIL_KEY . 'download_file'] = array("label" => __("Download file", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "download_file", true));
            $form_fields[LONGTAIL_KEY . 'download_file_selector'] = array("label" => __("", 'jw-player-plugin-for-wordpress'), "input" => "html", "html" => generateVideoSelectorHTML($post->ID, "download_file_selector", $video_attachments));
            break;
    }
    $rtmp = get_post_meta($post->ID, LONGTAIL_KEY . "rtmp");
    if ($mime_type == "video" && isset($rtmp) && $rtmp) {
        unset($form_fields["url"]);
        $form_fields[LONGTAIL_KEY . "streamer"] = array("label" => __("Streamer", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "streamer", true));
        $form_fields[LONGTAIL_KEY . "file"] = array("label" => __("File", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "file", true));
        $form_fields[LONGTAIL_KEY . "provider"] = array("label" => __("Provider", 'jw-player-plugin-for-wordpress'), "input" => "text", "value" => get_post_meta($post->ID, LONGTAIL_KEY . "provider", true));
    }
    if (isset($_GET["post_id"]) && ($mime_type == "video" || $mime_type == "audio" || $mime_type == "image" && get_option(LONGTAIL_KEY . "image_duration"))) {
        $insert = "<input type='submit' class='button-primary' name='send[{$post->ID}]' value='" . esc_attr__('Insert JW Player', 'jw-player-plugin-for-wordpress') . "' />";
        $form_fields[LONGTAIL_KEY . "player_select"] = array("label" => __("Select Player", 'jw-player-plugin-for-wordpress'), "input" => "html", "html" => generatePlayerSelectorHTML($post->ID));
        $form_fields["jwplayer"] = array("tr" => "\t\t<tr class='submit'><td></td><td class='savesend'>{$insert}</td></tr>\n");
    } else {
        if (version_compare($wp_version, '3.5', '>=')) {
            if ($mime_type == "video" || $mime_type == "audio" || $mime_type == "image" && get_option(LONGTAIL_KEY . "image_duration")) {
                $insertJS = " <script type='text/javascript'>\n                    function jwplayer_insert(id) {\n                      var selected_player = jQuery('#jwplayermodule_player_select_" . $post->ID . "').val();\n                      wp.media.post('send-attachment-to-editor', {\n                        nonce: wp.media.view.settings.nonce.sendToEditor,\n                        attachment: {\n                          'id': " . $post->ID . ",\n                          '" . LONGTAIL_KEY . "insert_type': 'jwplayer',\n                          '" . LONGTAIL_KEY . "player_select': selected_player\n                        },\n                        html:       '',\n                        post_id: wp.media.view.settings.post.id\n                      }).done(function(response) {\n                        send_to_editor(response);\n                      });\n\t\t\t\t            }\n                  </script>";
                $insert = "<a onclick='jwplayer_insert({$post->ID});' id='jwplayermodule_insert_{$post->ID}' class='button-primary' name='send[{$post->ID}]'>Insert JW Player</a>";
                $form_fields[LONGTAIL_KEY . "player_select"] = array("label" => __("Select Player", 'jw-player-plugin-for-wordpress'), "input" => "html", "html" => generatePlayerSelectorHTML($post->ID));
                $form_fields["jwplayer"] = array("tr" => "\t\t<tr class='submit'><th valign='top' scope='row' class='label'><label><span></span></label></th><td class='savesend'>{$insert}</td></tr>\n{$insertJS}");
            }
        }
    }
    return $form_fields;
}