Portlet_fileTree=function(action){
  this.action=action;
  
  File=function(id,name,parent,link){
    this.id=id;
    this.name=name;
    this.parent=parent;
    this.link=link;
  }

  Dir=function(id,name,parent){
    this.id=id;
    this.name=name;
    this.parent=parent;
    this.subdirs=new Array();
    this.files=new Array();
    this.isOpened=false;
    
    this.loadContent=function(){
      /*
        returns:
  
        <this>
          <dir id="id1" name="name1" />
          <dir id="id2" name="name2" />
          ...
          <file id="fid1" name="fname1" link="..." />
          ...
        </this>
      */
      
      var t=this;
      jQuery.get('includes/structure1/portlet_fileTree_handler.php?getdir='+this.id,function(data){
        var RGX_DIR=new RegExp('(<dir\\s+id=["])(\\d+)(["]\\s+name=["])(.*?)(["]\\s+/>)');
        var RGX_FILE=new RegExp('(<file\\s+id=["])(\\d+)(["]\\s+name=["])(.*?)(["]\\s+link=["])(.*?)(["]\\s+/>)');
        var RGX_THIS=new RegExp('<this>(.*?)</this>');
                                      
        var a=data.split(RGX_THIS);
        if(a[1].length>1){
          t.subdirs=new Array();
          var d=a[1].split(RGX_DIR);
          for(i=0;i<d.length-1;i+=6){
            t.subdirs.push(new Dir(d[i+2],d[i+4],t));
          }
          d=a[1].split(RGX_FILE);
          for(i=0;i<d.length-1;i+=8){
            t.files.push(new File(d[i+2],d[i+4],t,d[i+6]));
          }
        }
        t.isOpened=true;
        t.updateHTML();
      });
    }
    
    this.getPath=function(){
      if(this.parent!=null){
        var p=this.parent.getPath();
        return p+this.id+'/';
      }
      return '0'+'/';
    }
    
    this.updateHTML=function(){
      var ul=document.createElement('ul');
      var parentPath=this.getPath();
      for(var i=0;i<this.subdirs.length;i++){
        var li=document.createElement('li');
        li.setAttribute('id','portlet_fileTree_'+this.subdirs[i].id);
        var a=document.createElement('a');
        a.setAttribute('href','#');
        a.setAttribute('onclick','portlet_fileTree.root.loadPath(\''+parentPath+this.subdirs[i].id+'/\')');
        var img=document.createElement('img');
        img.setAttribute('src','images/structure1/icons/portlet_editor/plus.png');
        a.appendChild(img);
        img=document.createElement('img');
        img.setAttribute('src','images/structure1/icons/portlet_editor/folder.png');
        a.appendChild(img);
        a.innerHTML+=" "+this.subdirs[i].name;
        li.appendChild(a);
        ul.appendChild(li);
      }
      for(var i=0;i<this.files.length;i++){
        var li=document.createElement('li');
        li.setAttribute('id','portlet_fileTree_'+this.files[i].id);
        var a=document.createElement('a');
        a.setAttribute('href','#');
        a.setAttribute('onclick','portlet_fileTree.close();document.getElementById(\'portlet_editor_editable_textarea\').focus();portlet_editor.'+portlet_fileTree.action+'(\''+this.files[i].link+'\');');
        var img=document.createElement('img');
        img.setAttribute('src','images/structure1/icons/portlet_editor/file.png');
        a.appendChild(img);
        a.innerHTML+=" "+this.files[i].name;
        li.appendChild(a);
        ul.appendChild(li);
      }               
      var e=document.getElementById('portlet_fileTree_'+this.id);
      e.appendChild(ul);
      var a=e.getElementsByTagName('a')[0];   
      if(a!=null){                                 
        a.setAttribute('onclick','portlet_fileTree.root.closeDir(\''+parentPath+'\')');
        var img=a.getElementsByTagName('img')[0];
        if(img!=null){                 
          img.setAttribute('src','images/structure1/icons/portlet_editor/minus.png');
        }
      }
    }
    
    this.loadPath=function(path){
      var first=path.substring(0,path.indexOf("/",0));
      if(first==null || this.id!=first){
        return;
      }              
      if((this.subdirs.length==0 && this.files.length==0) || !this.isOpened){   
        this.loadContent();
      }
      if(first+('/')==path){
        return;
      }
      var rest=path.substring(path.indexOf("/",0)+1,path.length);
      var second=rest.substring(0,rest.indexOf("/",0));
      for(var i=0;i<this.subdirs.length;i++){
        if(this.subdirs[i].id==second){
          this.subdirs[i].loadPath(rest);
        }
      }
    }
    
    this.closeDir=function(path){
      var first=path.substring(0,path.indexOf("/",0));
      if(first==null || this.id!=first){
        return;
      }   
      if(first+('/')==path){
        this.isOpened=false;
        var li=document.getElementById('portlet_fileTree_'+this.id);
        li.removeChild(li.getElementsByTagName('ul')[0]);
        var a=li.getElementsByTagName('a')[0];
        if(a!=null){
          var parentPath=this.getPath();
          a.setAttribute('onclick','portlet_fileTree.root.loadPath(\''+parentPath+this.id+'/\')');
          var img=a.getElementsByTagName('img')[0];
          if(img!=null){
            img.setAttribute('src','images/structure1/icons/portlet_editor/plus.png');
          }
        }
        return;
      }
      var rest=path.substring(path.indexOf("/",0)+1,path.length);
      var second=rest.substring(0,rest.indexOf("/",0));
      for(var i=0;i<this.subdirs.length;i++){     
        if(this.subdirs[i].id==second){
          this.subdirs[i].closeDir(rest);
        }
      }
    }
    
  }
  
  this.close=function(){
    document.getElementById('portlet_editor_editable').removeChild(document.getElementById('portlet_fileTree_wrapper'));
  }
  
  this.root=new Dir(0,"/",null);
  this.html='<button type="button" onclick="portlet_fileTree.close();">Zrušiť</button><br /><div id="portlet_fileTree"><ul><li id="portlet_fileTree_0"><a href="#" onclick="portlet_fileTree.root.loadPath(\'0\')"><img src="images/structure1/icons/portlet_editor/plus.png" /><img src="images/structure1/icons/portlet_editor/folder.png" />/</a></li></ul></div>';

}
