/*--------------------------------------------------------------------------*
 *
 * showhide div
 *
 * extra layer on top of ajax_callrpc
 *
 *--------------------------------------------------------------------------*/
var _needs_hide;
var _use_jquery = 1;
var _speed_show = "fast";
var _speed_hide = "fast";
var _debug_console = 0;

var p_fragment;
var _cur_tab;

var _inc_1 = 0;
var _inc_2 = 0;
var _inc_3 = 0;
var _inc_4 = 0;
var _inc_5 = 0;
var _inc_6 = 0;

var _lst   = new Array();
var _count = 0;

var _resultdiv = false;


function register_resultdiv(divid)
{
  _resultdiv = divid;
}

function attach_calendar(id)
{
  $('#' + id).attachDatepicker();
  showDatepicker();
}

function activate_all_tabs(nb_tabs)
{
  var i;
  if (_debug_console == 1)
  {
    window.open('debug','debug console','width=440,height=440,scrollbars=yes');
  }

  for (i = 1; i <= nb_tabs; i++)
    {
      $('#diagtabs').tabs();//{ fxSlide: true, fxSlide: false, fxSpeed: 'slow' });
      $('#diagtabs').tabsDisable(i);
      $('#subtab'+i).tabs();
    }
  
  for (i = 0; i < 2; i++)
    {
      $('#toptabs').tabs();
      //{fxFade: true, fxFade: true, fxSpeed: 'false' });
    }
  /*
    $('#client_info').draggable({ axis:'y', distance:100 } );
    $('#choices').draggable({ axis:'y', distance:100 } );
    $('#subtabs').draggable({ axis:'y', distance:100 } );
  */
  $('#toptabs').tabsClick(1);
  //$('#toptabs').tabs();

  ajax_set_infodivid('statuswait');
  ajax_callrpc('status_neufbox', 'index.php?task=statusBox');
}

function showhide(id)
{
  if(document.getElementById(id).style.display == 'block') 
  {
    hide_div(id);
  } 
  else 
  {
    show_div(id);
  }
}

function jq_ajax_callrpc(title, id, url)
{
  var desttab;
  var container;
  var tabid;
  var elem;
  var destroy = 0;
  var subtab_div;

  if (_resultdiv != false)
    {
      if (document.getElementById(_resultdiv).style.display == 'none')
	{
	  show_div(_resultdiv);
	}
    }

  ajax_set_infodivid('infobox');

  container = '#subtab' +id;
  url = "index.php?task=result&" + url;
  
  show_div('subtabs');
  
  subtab_div = document.getElementById('subtab'+id);

  /* avoid js errors if the div does not exisit ! */
  if (subtab_div)
    {
      if (document.getElementById('subtab'+id).style.display == 'none')
	{
	  document.getElementById('subtab'+id).style.display = 'block';
	}
    }
  
  elem = find_tab(url);
  
  if (elem == null)
    {
      switch (id)
	{
	case 1:
	  _inc_1++;
	  desttab = _inc_1;
	  break;
	  
	case 2:
	  _inc_2++;
	  desttab = _inc_2;
	  break;
	  
	case 3:
	  _inc_3++;
	  desttab = _inc_3;
	  break;
	  
	case 4:
	  _inc_4++;
	  desttab = _inc_4;
	  break;
	  
	case 5:
	  _inc_5++;
	  desttab = _inc_5;
	  break;
	  
	case 6:
	  _inc_6++;
	  desttab = _inc_6;
	  break;
	}

      tabid     = 'new-tab-' + desttab + '-' + id;
      $(container).tabsAdd('#' + tabid, title);

      /* important: remove the 1st element if any */
      if (desttab == 1)
	{
	  $(container).tabsRemove(1);
	}

      elem = new Array(url, tabid, desttab, id, title);
      register_tab(elem);
    }
  else
    {
      tabid   = elem[1];
      desttab = elem[2];
    }
  $('#diagtabs').tabsEnable(id);

  show_tab(id);
  $(container).tabsClick(desttab);
 
  //url += "&args[ndi]=" + getCookie('ndi');
  
  url += "&divid=" + id + "&desttab=" + desttab +  "&tabid=" + tabid + "&container=" + 'subtab' + "&title=" + encodeURI (title)
  ajax_callrpc(tabid, url);
  //$(container).tabsLoad(desttab, url);
}

function register_tab(elem)
{
  _lst[_count] = elem;
  _count++;
}

function find_tab(title)
{
  var i;
  for (i= 0 ; i < _lst.length ; i++)
    {
      if (_lst[i][0] == title)
	{
	  return _lst[i];
	}
    }
  return null;
}

function jq_ajax_callrpc_old(divid, url)
{
	ajax_show('infobox');

	ajax_callrpc(divid, url);
  
	ajax_hide('infobox');
}

function show_contact(choice)
{
	/* On cache tous les choix */
	hide_div_fast('Section1');
	hide_div_fast('Section2');
	hide_div_fast('Section3');
	hide_div_fast('Section4');
	hide_div_fast('Section5');
	
	/* Suppression du premier onglet selectionner */
	var sfEls = document.getElementById("contact_li").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].className = "";
	}
	
	/* Afficher le contenu du choix */
	$('#Section'+choice).slideUp(400);
	$('#Section'+choice).slideDown();

	ajax_show('Section'+choice);
}

function show_tab(id)
{
  var selected;
  selected = $('#diagtabs').tabsSelected();
  //$('#diagtabs').tabsEnable(id);
  if (selected != id)
    {
      if (!$('#diagtabs').tabsClick(id))
	{
	  $('#diagtabs').tabsClick(id);
	}
    }
}

function make_dialog(id, subdivid, _title)
{  
  var _width, _height;
  var mydiv;
  var subdiv;
  
  mydiv   = document.getElementById(id);
  _width  = mydiv.style.width  + 10;
  _height = mydiv.style.height + 10;
  
  subdiv  = document.getElementById(subdivid);

  if (subdiv)
    {
      if (subdiv.style.display != 'block')
	subdiv.style.display = 'block';
    }
  
  $("#" + id).show();
  $("#" + id).dialog({title:_title, width:_width, height:_height});
}

function show_dialog(id)
{
  show_div(id);
  
  $("#" + id).dialog();$(this).hide().next().show();
}

function show_dialog_size(id, _width, _height)
{
  show_div(id);
  $("#" + id).dialog({width: _width, height: _height});$(this).hide().next().show();
}

function show_div_fast(id)
{
  var el;
  
  el = document.getElementById(id);
  if (el)
    {
      el.style.display = "block";
    }
}

function hide_div_fast(id)
{
  var el;
  
  el = document.getElementById(id);
  if (el)
    {
      el.style.display = "none";
    }
}

function show_div(id)
{
  var el;
  
  el = document.getElementById(id);
  if (_needs_hide == 1)
    _needs_hide = 0;

  if (_use_jquery == 1)
    {
      $('#'+id).show(_speed_show);
    }
  else
    {
      el.style.display = 'block';
    }
}

function hide_div(id)
{
  var el;
  
  el = document.getElementById(id);
  
  if (_use_jquery == 1)
    {
      $('#'+id).slideUp(_speed_hide);
    }
  else
    {
      el.style.display = 'none';
    }
}

function remove_div(id)
{
  var el;
  
  el = document.getElementById(id);
  
  if (_use_jquery == 1)
    {
      $('#'+id).fadeOut("slow");
    }
  else
    {
      el.style.display = 'none';
    }
}

function _divid_status(divid)
{
  var el;
  
  el = document.getElementById(divid);
  return el.style.display;
}

function motion_show_child(divid)
{
  var status;
  
  status = _divid_status(divid);

  if (status == 'block')
    {
      _needs_hide = 0;
    }
  else
    {
      show_div(divid);
      _needs_hide = 1;
    }
}

function motion_hide_child(divid)
{
  if (_needs_hide == 1)
    {
      hide_div(divid);
      _needs_hide = 0;
    }
}
