Limb.namespace('Limb.tabs');
Limb.tabs.containers = [];

Limb.require('Class');
Limb.require('DOM.Utils');
Limb.require('Limb.cp');

Limb.tabs.tab = Class.create();

Limb.tabs.tab.prototype =
{
  initialize: function (container, tab_data)
  {
    this.id = tab_data.id;
    this.container = container;
    this.label = $(tab_data.id);
    this.label.style.cursor = 'pointer'
    this.label.tab = this;
    this.content = $(tab_data.id + '_content');

    if(tab_data.src == '/') tab_data.src = '';
    this.data = tab_data;

    this.prev_height = this.content.style.height;
    this.prev_width = this.content.style.width;
    this.prev_css = this.content.cssText;

    this.onmousedown_handler = tab_data.onmousedown_handler;
    this.activate_handler = tab_data.activate_handler;
    this.deactivate_handler = tab_data.deactivate_handler;

    var hit_obj = Limb.cp.get_obj_by_id(this.label.getElementsByTagName('*') , "tab-label");
    if(!isset(hit_obj)) hit_obj = this.label

    hit_obj.tab = this
    hit_obj.onclick = function()
    {
      if(!this.tab.container.active_tab) return;

      this.tab.activate();

      if(this.tab.onclick)
        this.tab.onclick();
    }
  },

  activate: function()
  {
    if(this.container.active_tab == this)
      return;

    if(this.container.active_tab)
      this.container.active_tab.deactivate();

    this.container.active_tab = this;

    this.label.className = this.container.active_tab_class_name;

    Limb.cookie.set_multi_cookie('TABs', this.container.id + 'active_tab', this.id)

    if(this.activate_handler)
      this.activate_handler(this);
    else
    {
      if(Limb.browser.is_gecko)
      {
        if(this.content.style.visibility == 'hidden')
          this.content.style.visibility = 'visible';

        this.content.style.cssText  = this.prev_css;
        if(this.prev_height && this.prev_width)
        {
          this.content.style.height = this.prev_height;
          this.content.style.width = this.prev_width;
        }
      }
      else
      {
        this.content.style.display = 'block';
      }
    }
  },

  deactivate: function()
  {
    this.label.className = this.container.normal_tab_class_name;

    if(this.deactivate_handler)
      this.deactivate_handler(this);
    else
    {
      if(Limb.browser.is_gecko)
      {
        this.prev_height = this.content.style.height;
        this.prev_width = this.content.style.width;
        this.prev_css = this.content.style.cssText;

        this.content.style.cssText = '';
        this.content.style.height = '0';
        this.content.style.width = '0';
        this.content.style.visibility = 'hidden';
      }
      else
        this.content.style.display = 'none';
    }
  },

  contains_element: function(element_id)
  {
    if(Limb.dom.findChild(this.content, element_id) != null)
      return true;
  },

  get_label_element: function()
  {
    return this.label;
  },

  get_content_element: function()
  {
    return this.content;
  }
}

Limb.tabs.tabs_container = Class.create();

Limb.tabs.tabs_container.prototype =
{
  initialize: function (id, tab_data)
  {
     this.tab_items = []
     this.active_tab = null;
     this.id = id

    if(!tab_data) tab_data = []
    if(typeof(tab_data.active_tab_class_name) == undefined || tab_data.active_tab_class_name == null)
      tab_data.active_tab_class_name = 'tab-active';

    if(typeof(tab_data.normal_tab_class_name) == undefined || tab_data.normal_tab_class_name == null)
      tab_data.normal_tab_class_name = 'tab';

    this.active_tab_class_name = tab_data.active_tab_class_name;
    this.normal_tab_class_name = tab_data.normal_tab_class_name;

    Limb.tabs.containers.push(this);
  },

  register_tab_item: function(tab_data)
  {
    this.tab_items[tab_data.id] = new Limb.tabs.tab(this, tab_data);
  },

  activate: function(tab_id)
  {
    var active_tab_id, first_tab_id;

    for(var id in this.tab_items)
    {
      if(typeof(this.tab_items[id]) != 'object')
        continue;
      if (!first_tab_id)
        first_tab_id = id;

      if(id == tab_id)
      {
        this.tab_items[id].activate();
        active_tab_id = tab_id;
      }
      else
        this.tab_items[id].deactivate();
    }

    if (!active_tab_id)
      this.tab_items[first_tab_id].activate();
  },

  activate_default: function()
  {
    var id = Limb.cookie.get_multi_cookie('TABs', this.id + 'active_tab')
    if (id) this.activate(id);
    else
      this.activate('');
  },

  get_tabs: function()
  {
    return this.tab_items;
  }
}

Limb.tabs.find_element_tab = function(element_id)
{
  for(var i in Limb.tabs.containers)
  {
    tabs = Limb.tabs.containers[i].get_tabs();
    for(var j in tabs)
    {
      if(tabs[j].contains_element(element_id))
        return tabs[j];
    }
  }
  return null;
}

Limb.tabs.highlight_tab = function(container, tab_id)
{
  if(!container.tab_items[tab_id])
    return;

  container.tab_items[tab_id].label.className = container.active_tab_class_name;
  container.active_tab = container.tab_items[tab_id];
}

Limb.tabs.page_tabs = new Limb.tabs.tabs_container('page_tabs');

Limb.tabs.activate_page_tab = function(tab)
{
  var current_path = window.location.pathname + window.location.search;
  if (current_path != tab.data.src)
   if (last_tab_url = Limb.cookie.get_multi_cookie('TABs', tab.id + '_last_url'))
     window.location.href = last_tab_url;
   else
     window.location.href = HTTP_GATEWAY_PATH + tab.data.src;
}

