/*
  File      : tabstrip.js
  Version   : 0.04 (15-May-2006)
  Purpose   : ym_Web_Tabstrip widget code
  Author    : (c) 2006 Misha Yurasov <mike at managebytes dot com>
*/

var tabstrips = new Array();

//
// Build tabs when document is loaded
//
function tabstrip_create(tabstrip_id)
{
  var func_handler = function () { _tabstrip_create(tabstrip_id); }
  
  if(typeof(window.addEventListener) != 'undefined')
  {
    // mozilla
    window.addEventListener('load', func_handler , false);
  }
  else if(typeof(document.addEventListener) != 'undefined')
  {
    // opera
    document.addEventListener('load', func_handler, false);
  }
  else if(typeof(window.attachEvent) != 'undefined')
  {
    // ie
    window.attachEvent('onload', func_handler);
  }
  else
  {
    _tabstrip_create(tabstrip_id);
  }
}

//
// Build tabs
//
function _tabstrip_create(tabstrip_id)
{
  var ts, pages = new Array(), page, active_tab_i,
    i, c, p = 0, page_max_h = 0, page_max_w = 0,
    new_page_max_h = 0, new_page_max_w = 0,
    onswitch;
  
  // active tab index
  active_tab_i = _tabstrip_read(tabstrip_id);

  // topmost tag
  doc = this.document;
  ts = doc.getElementById(tabstrip_id);
  ts.className = 'tabstrip';
  
  // onswitch callback
  onswitch = ts.getAttribute('onswitch');
  
  // enum pages
  
  c = ts.childNodes;
  
  for (i = 0; i < c.length; i++)
  {
    page = c.item(i);
    //
    if (page.className == 'page')
    {
      pages[p++] = page;
      
      // compute max page dimensions
      page_max_h = Math.max(page.offsetHeight, page_max_h);
      page_max_w = Math.max(page.offsetWidth, page_max_w);
    }
  }
  
  // build tabstrip
  
  if (pages.length > 0)
  {
    var tabs_table, tabs_tb, tabs_tr, tabs = new Array(), tab, sep;
    
    active_tab_i = Math.min(active_tab_i, pages.length - 1);
    
    // <table> (for tabs)
    tabs_table              = doc.createElement('table');
    tabs_table.cellPadding  = '0';
    tabs_table.cellSpacing  = '0';
    tabs_table.border       = '0';
    
    // <tbody>
    tabs_tb = doc.createElement('tbody')
    tabs_table.appendChild(tabs_tb)
    
    // <tr>
    tabs_tr = doc.createElement('tr');
    tabs_tr.className = 'tabs';
    tabs_tb.appendChild(tabs_tr);

    for (i = 0; i < pages.length; i++)
    {
      page = pages[i];
      
      // create tab for the current page

      tab               = doc.createElement('td');
      tab.className     = (i == active_tab_i) ? 'tab active' : 'tab inactive';
      tab.noWrap        = true;
      tab.innerHTML     = page.getAttribute('name');
      tab.unselectable  = true;

      tab.onmouseover   = (i == active_tab_i) ?
        function () { this.className = 'tab active_hover'; } :
        function () { this.className = 'tab inactive_hover'; }

      tab.onmouseout    = (i == active_tab_i) ?
        function () { this.className = 'tab active'; } :
        function () { this.className = 'tab inactive'; }

      tab.setAttribute('tabstrip_id', tabstrip_id);
      tab.setAttribute('page', i);
      tab.setAttribute('onswitch', onswitch);
      
      tab.onmousedown = _tabstrip_switch;
      
      tabs_tr.appendChild(tab);
      tabs[i] = tab;
              
      // create separator
      if (i < pages.length - 1)
      {
        sep           = doc.createElement('td');
        sep.className = 'sep';
        tabs_tr.appendChild(sep);
      }
    }
    
    // display tabs
    ts.insertBefore(tabs_table, pages[0]);

    page_max_w = Math.max(tabs_table.offsetWidth, page_max_w);
    
    for (i = 0; i < pages.length; i++)
    {
      page = pages[i];

      // adjust page dimensions and visibility
      
      page.style.height   = page_max_h;        
      page.style.width    = page_max_w;
      
      if (i == active_tab_i)
      {
        // raise onswitch event
        if (onswitch != null) { eval(onswitch + '(page)'); }
        
        page.style.display = 'block';
      }
      else
      {
        page.style.display = 'none';
      }
      
      // а это для йопнутой css box-model firefox'а
      new_page_max_h = Math.max(page.offsetHeight, new_page_max_h);
      new_page_max_w = Math.max(page.offsetWidth, new_page_max_w);
    }
          
    // adjust tabsheet dimensions
    ts.style.height = new_page_max_h + tabs_table.offsetHeight;      
    ts.style.width = new_page_max_w;      
  }
  
  tabstrips[tabstrip_id] = {pages: pages, tabs: tabs, active: active_tab_i, onswitch: onswitch};
}

//
// Switch tabs
//
function _tabstrip_switch(e)
{
  var ts, active_i, tabstrip_id, cur_tab, cur_page, prev_tab, prev_page;
  
  cur_tab     = e ? e.target : window.event.srcElement; // clicked tab
  cur_i       = parseInt(cur_tab.getAttribute('page')); // clicked tab index
  tabstrip_id = cur_tab.getAttribute('tabstrip_id');    // current tabstrip id
  ts          = tabstrips[tabstrip_id];                 // current tabstrip data
  prev_i      = ts['active'];                           // active page index
  
  if (cur_i != prev_i)
  {
    cur_page    = ts['pages'][cur_i];                   // clicked page
    prev_tab    = ts['tabs'][prev_i];                   // active tab
    prev_page   = ts['pages'][prev_i];                  // active page
    
    // raise onswitch event
    if (ts['onswitch'] != null) { eval(ts['onswitch'] + '(cur_page)'); }
    
    cur_page.style.display  = 'block';
    cur_tab.className       = 'tab active_hover';
    cur_tab.onmouseover     = function () { this.className = 'tab active_hover'; }
    cur_tab.onmouseout      = function () { this.className = 'tab active'; }
    
    prev_page.style.display = 'none';
    prev_tab.className      = 'tab inactive';
    prev_tab.onmouseover    = function () { this.className = 'tab inactive_hover'; }
    prev_tab.onmouseout     = function () { this.className = 'tab inactive'; }
    
    tabstrips[tabstrip_id]['active'] = cur_i;
    
    // save active page in a cookie
    _tabstrip_save(tabstrip_id, cur_i);
  }
}

//
// Save active page index
//
function _tabstrip_save(tabstrip_id, i)
{
  var cookie_exp_date;
  
  // cookie expiration date
  cookie_exp_date = new Date();
  cookie_exp_date.setFullYear(cookie_exp_date.getFullYear() + 1); // now + 1 year
  cookie_exp_date = cookie_exp_date.toGMTString();
  
  // set cookie
  document.cookie = tabstrip_id + '_active_i=' + i + '; expires=' + cookie_exp_date;
}


//
// Read active page index
//
function _tabstrip_read(tabstrip_id)
{
  var cookie_text, cookie_parts, cookie_name, i;
  
  cookie_name = tabstrip_id + '_active_i';
  cookie_text = document.cookie;
  cookie_text = cookie_text.split('; ');
  
  for (i = 0; i < cookie_text.length; i++)
  {
    cookie_parts = cookie_text[i].split('=');
    
    if (cookie_parts[0] == cookie_name)
    {
      return parseInt(cookie_parts[1]);
    }
  }
  
  return 0;
}