function oTable(container, filelist, requestURL, name, aditionalParams)
{
  this.name = name;
  this.additional_params = aditionalParams;
  this.container = container;
  this.filelist = $(filelist);
  this.requestURL = requestURL;
  this.loaded = [];
  this.opened = [];
  
  this.toggleDir('/', this.container, 0);
}

oTable.prototype.dirCheckbox = function(id, checked)
{
  var self = this;
  
  $(this.container).select('tr[class~="' + id + '"] input[type=checkbox]').each(function(item){
    if (checked)
    {
      if (item.checked) item.addClassName('wasChecked');
      self.toggleCheck(item, false);
      
      item.addClassName('wasDisabledBy' + id);
      item.disabled = true;
    }
    else 
    {
      item.removeClassName('wasDisabledBy' + id);
      
      if (item.className.indexOf('wasDisabledBy') === -1)
      {
        item.disabled = false;
      }
      
      if (item.hasClassName('wasChecked') && !item.disabled)
      {
        self.toggleCheck(item, true);
        item.removeClassName('wasChecked');
      }
    }
  });
}

oTable.prototype.setupCheckboxes = function(id)
{
  var self = this;
  var $checkbox;
  
  if (this.container == id) $checkbox = $(id + '_selectall');
  else $checkbox = $(id).select('input[type=checkbox]').first();
  
  $checkbox.observe('click', function(event){
    self.dirCheckbox(id, this.checked);
    if ($checkbox.id == id + '_selectall') self.toggleCheck(this, this.checked);
  });
  
  $$('tr[class~="'+id+'"] input[type=checkbox]').invoke('observe', 'click', function(event){
    self.toggleCheck(this, this.checked);
  });
  
  if ($checkbox.checked) this.dirCheckbox(id, true);
}

oTable.prototype.getData = function(dir, container, depth)
{ 
  var self = this;
  var pars = this.additional_params;
  pars['control'] = 'lssnap';
  pars['path'] = this.unescape(dir);
  
  var req = new Ajax.Request(this.requestURL, {
    method: 'post',
    parameters: pars,
    onComplete: function(transport){
      if (transport.responseText == 'Error')
      {
      	alert('Directory does not exist');
        var icon = $(container.id + '_icon');
        if (icon) icon.src = 'img/snapshot/d.png';
      }
      else
      {
      	var data = transport.responseText.evalJSON(true);
      	
        self.loaded[dir] = 1;
        self.drawData(dir, container, depth, data);
      }
    }
  });
  
  return false;
}

oTable.prototype.toggleDir = function(dir, container, depth)
{
  container = $(container);
  var icon = $(container.id + '_icon');
  
  if (this.opened[container.id])
  {
    if (icon) icon.src = 'img/snapshot/load.gif';
    this.closeDir(dir, container, depth);
    if (icon) icon.src = 'img/snapshot/d.png';
  }
  else
  {
    if (icon) icon.src = 'img/snapshot/load.gif';
    if (this.loaded[dir]) this.openDir(dir, container);
    else this.getData(dir, container, depth);
  }
  
  return false;
}

oTable.prototype.drawData = function(dir, container, depth, data) {
  var content = '';
  var depthVar = depth;
  var prenode = '';
  var icon = $(container.id + '_icon');
  
  while (depth > 1)
  {
    prenode += '<img align="absmiddle" alt="[N]" src="img/snapshot/i-node.png"/>';
    depth--;
  }
  
  for (var i = 0; i < data.length; i++)
  {
    var node = '';
    data[i].name = data[i].name.utf8_decode(); 
    var name = this.escape(data[i].name);
    var filedir = dir + (dir == '/' ? '' : '/') + name;
    
    if (depthVar > 0)
    {
      if (i != data.length - 1)
      {
        node = '<img align="absmiddle" alt="[N]" src="img/snapshot/t-node.png"/>';
      }
      else
      {
        node = '<img align="absmiddle" alt="[N]" src="img/snapshot/l-node.png"/>';
      }
    }
    
    var imgSrc = (data[i].type == 'd' ? 'img/snapshot/d.png' : 'img/' + data[i].icon);
    
    content += ('<tr class="' + container.id + ' ' + container.className + '" id="' + depthVar + '_' + name + '" valign="middle"><td valign="middle">' +
    '<input type="checkbox" name="' + (data[i].type == 'f' ? 'file' : 'dir') + '[]" value="' + filedir + '" />' + prenode + node +
    '<img id="' + depthVar + '_' + name + '_icon" src="' + imgSrc + '" alt="[' + data[i].type + ']" align="absmiddle" /> ' +
    (data[i].type == 'd' ? '<a style="color: #0000EE;" href="#" onclick="return '+this.name+'.toggleDir(\'' + filedir + '\', \'' + depthVar + '_' + name + '\', ' + (depthVar + 1) + ')">' + data[i].name + '</a>' : data[i].name) +
    '</td></tr>');
  }
  
  if (depthVar == 0) container.update(content);
  else container.insert({'after': content});
  
  this.opened[container.id] = 1;
  if (icon) icon.src = 'img/snapshot/do.png';
  
  this.setupCheckboxes(container.id);
}

oTable.prototype.closeDir = function(dir, container) {
  var icon = $(container.id + '_icon');
  
  $(this.container).select('tr[class~="' + container.id + '"]').invoke('hide');
  delete this.opened[container.id];
  
  if (icon) icon.src = 'img/snapshot/d.png';
}

oTable.prototype.openDir = function(dir, container) {
  var icon = $(container.id + '_icon');
  
  $(this.container).select('tr[class^="' + container.id + '"]').invoke('show');
  this.opened[container.id] = 1;
  
  if (icon) icon.src = 'img/snapshot/do.png';
}

oTable.prototype.escape = function(str) {
  return escape(str);
}

oTable.prototype.unescape = function(str) {
  return unescape(str);
}

oTable.prototype.toggleCheck = function(item, flag) {
  item.checked = flag;
  //this.toggleFile(item.value, flag);
}

oTable.prototype.toggleFile = function(file, add) {
  var escaped = file;
  var file = this.unescape(file);
  var $file = $(escaped + '_file');
  
  if (add == true && !$file)
  {
    var content = '<div id="' + escaped + '_file">' + file + '</div>';
    this.filelist.insert({'after': content});
  }
  else if($file)
  {
    $file.remove();
  }
}

