<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Description of rtgc_edit_link
 *
 * @author Igor Ratsun
 */
class Rtgc_yt_video extends \Abstract_rtgc_callback
{

    const YT_THUMB_LINK = 'https://i.ytimg.com/vi/%s/hqdefault.jpg';

    public function column($value = '')
    {
        return '<img alt="" src="' . sprintf(self::YT_THUMB_LINK, $value) . '" style="width: 100px" />';
    }

    public function field($name = '', $value = NULL)
    {
        $ci = & get_instance();

        $ci->js_css->add_js_string("$(function(){
            $('.yt_input').change(function(){
                code = $(this).val().replace('https://youtu.be/', '');
                link = '" . self::YT_THUMB_LINK . "';
                parent = $(this).parent();
                $(this).val(code);
                parent.find('img').attr('src', link.replace('%s', code));
            });
        });");

        $img = '<img src="' . sprintf(self::YT_THUMB_LINK, $value) . '" style="width: 100px"/><br />';
        $input = '<input class="yt_input" type="text" name="' . $name . '" value="' . $value . '" placeholder="' . $ci->lang->line('Enter youtube share link') . '" />';

        return $img. $input;
    }

}
