/**
 * Класс для работы с закладками.
 * @param String container_id идетификатор контейнера, в котором создаются закдаки
 * @param String name имя компонета
 * @param String width ширина компонента
 */
var cTabs = function( container_id, name, width)
{
    

    this.container_id = container_id;
    this.name = name;
    this.items_container_id = name + '_items';

     // Проверяем а есть ли уже такой контейнер
  
    var div_elem = document.getElementById(this.items_container_id);
   
    if (div_elem != null)
    {
        this.created = true;
        return true;
    }


    this.items = new Array();

    // Создание элемента ul.
    this.container_elem = document.getElementById( this.container_id );

    if (!this.container_elem)
    {
        return false;
    }

    this.ul_elem = document.createElement('ul');
    if (!this.ul_elem)
    {
        return false;
    }

    this.ul_elem.setAttribute('class', 'tab');
    this.container_elem.appendChild(this.ul_elem);

    this.created = false;

    this.current_tab_index = 0;
    

    // Создание контейнера вкладок.
    div_elem = document.createElement('div');
    div_elem.style.width = width;
    div_elem.style.border = '1px solid black';
    div_elem.setAttribute('id', this.items_container_id);
    this.container_elem.appendChild(div_elem);
}


/**
 * Добавляет закладку
 * @param String tab_caption кепшен закладки.
 * @param String link ссылка, на которую будет вести закладка.
 * @param String plugin_name имя плагина, который будет запускаться при нажатии
 * @param Array plugin_params праметры плагина, запускаемого во вкладке.
 * на данную закладку.
 */
cTabs.prototype.add = function( tab_caption, link, plugin_name, plugin_params, js_code )
{
    if (this.created==true) {
        return false;
    }

    var item = new Array();
    item['caption'] = tab_caption;
    item['plugin_name'] = plugin_name;
    if (plugin_params != undefined)
    {
        item['plugin_params'] = plugin_params;
    }
    if (js_code != undefined)
    {
        item['js_code'] = js_code;
    }

    this.items.push( item );

    var index = this.items.length - 1;

    var li_elem = document.createElement('li');
    this.ul_elem.appendChild(li_elem);

    var a_elem = document.createElement('a');
    if (!link)
    {
        link = 'javascript:' + this.name + '.open_tab('+index+')';
    }

    a_elem.setAttribute('href', link);
    li_elem.appendChild(a_elem);

    var span_elem = document.createElement('span');
    span_elem.textContent = tab_caption;
    a_elem.appendChild(span_elem);
}

/**
 * Возвращает имя элемента таба по его индексу.
 *
 * @param int tab_index индекс таба
 * @return string
 */
cTabs.prototype.get_tab_name = function( tab_index )
{
    return this.name + '[items][' + tab_index + ']';
}

/**
 *  Проверяет существует ли физически такая закладка.
 */
cTabs.prototype.tab_exists = function( tab_index )
{
    var tab_name = this.get_tab_name( tab_index );
    var elem = document.getElementById(tab_name);
    if (!elem)
    {
        return false;
    }

    return elem;
}

cTabs.prototype.create_tab = function( tab_index )
{
    //debugger;
    var items_container = document.getElementById(this.items_container_id);
    if (!items_container)
    {
        return false;
    }

    var item = document.createElement('div');
    item.setAttribute('id', this.get_tab_name(tab_index));

    items_container.appendChild(item);

    this.load_tab(tab_index);
}

cTabs.prototype.load_tab = function( tab_index )
{
    
    var js_code = this.items[ tab_index ]['js_code'];
    if (js_code != undefined)
    {
        eval(js_code);
    }

    var plugin_name = this.items[ tab_index ]['plugin_name'];
    if (plugin_name)
    {
        var plugin_params = this.items[ tab_index ]['plugin_params'];
        if (!plugin_params)
        {
            plugin_params = new Array();
        }
        var item_container_id = this.get_tab_name(tab_index);
        call_ajax(plugin_name, item_container_id, plugin_params);
    }
    
}

cTabs.prototype.hide_tab = function( tab_index )
{
    var tab = this.tab_exists(tab_index);
    if (tab)
    {
        tab.style.display = 'none';
    }
}

cTabs.prototype.show_tab = function( tab_index )
{
    var tab = this.tab_exists(tab_index);
    if (tab)
    {
        tab.style.display = 'block';
    }
}

cTabs.prototype.hide_all_tabs = function()
{
    for (i=0; i<this.items.length; i++)
    {
        this.hide_tab(i);
    }
}

/**
 *  Открывает заклаку по ее индексу.
 */
cTabs.prototype.open_tab = function( tab_index )
{
    this.current_tab_index = tab_index;

    this.hide_all_tabs();
    if (this.tab_exists(tab_index))
    { 
        this.show_tab(tab_index);
    }
    else 
    {
        this.create_tab(tab_index);
    }
}