<?php

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

/**
 * Grid tooltip library
 *
 * @author r.ratsun <r.ratsun@gmail.com>
 */
class rtgrid_tooltip
{

    protected $CI;

    public function __construct()
    {
        $this->CI = & get_instance();
        $this->CI->load->library('gettext_language');
        $this->CI->gettext_language->load(__DIR__ . '/../language/' . $this->CI->config->item('language') . '.po');

        // Load bootstrap
        $this->CI->ciext->load_bootstrap();

        // load js css
        $this->CI->js_css->parse_js('rtgrid/tooltip/tooltip.js');
        $this->CI->js_css->add_css_string($this->CI->parser->parse('rtgrid/tooltip/tooltip.css', array(), TRUE));
        
        $this->CI->js_css->js_reverse('/assets/plugins/jquery-ui-1.10.4.custom/js/jquery-ui-1.10.4.custom.min.js', '/assets/plugins/bootstrap/js/bootstrap.min.js');
    }

    /**
     * Render tooltip
     * 
     * @param string $field
     * @param string $name
     * @param string $html
     * @return string
     */
    public function render_tooltip($field, $name, $html)
    {

        $result = '';

        if (!empty($html)) {
            // prepare key
            $key = 'tooltip_key_' . $field;
            // open tooltip
            $result .= '<div class="grid_tooltip_block">';
            // add icon
            $result .= '<i class="icon-question-sign grid_tooltip" title="' . $this->CI->lang->line('Click to find out') . '" data-key="' . $key . '"></i>';
            // add modal window
            $result .= '<div id="' . $key . '" class="modal hide fade" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3>' . $name . '</h3></div><div class="modal-body">' . $html . '</div></div>';
            // close tooltip
            $result .= '</div>';
        }

        return $result;
    }

}
