// PHPSiteFactory Framework
// File Browser
// Requires PRajax Ajax Lib


function SF_FileBrowser(sname, ocontainer, sbpath, sfilters)
{

 this.name = sname;
 this.container=ocontainer;
 this.dirwin = null;
 this.ofilefield = null;
 this.otitle=null;
 
 this.returnType="field";
 this.returnObject=null;
 this.returnFormat='filename'; //relative|absoulute|filename
 this.returnFunction=null;
 this.doFieldEvent=false;
 
 this.width = 380;
 this.height= 250;
 
 this.formwidth=580;
 this.formheight=380;
 
 this.bgcolor="#E9E9E9";
 this.forecolor="#404040";
 this.icon_path="";

 
 this.draggable=false;
 
 this.basepath=sbpath;
 this.limitpath="";
 this.returnpath="";
 
 this.path="";
 this.file="";
 
 this.filters = sfilters;
 
 this.can_render=false;
 this.cdir=false;
 
 this.afiles= new Array();

 this.type=1; // 1 - Select Files, 2 - Select Dirs
 this.orderby = 0; // 0 = standard (dir-files), 1 = filename, 2 = filesize, 3 = date, 4 = filetype;
 this.view = "list"; // list|detailed|big
 
 this.setFile = function(fname)
 {
  this.file=fname;
  if(this.ofilefield)
   this.ofilefield.value=fname;
 }
 
 this.setValueToFileField = function(svalue)
 {
  this.ofilefield.value=svalue;
 }
 
 this.setResult= function(result)
 {
  var r;
  //alert(result.length.toString());
  this.can_render=false;
  this.afiles=null;
  this.afiles=new Array();
  for(r=0;r<result.length;r++)
   this.afiles[r]=result[r];
  this.can_render=true; 
  //this.afiles=result;
  if(this.cdir)
  {
   this.renderDir();
   this.cdir=false;
  }
  //this.otitle.innerHTML=result[0][0];
 }
 
 this.changeDir = function(dirname)
 {
  this.can_render=false;
  this.cdir=true;
  getDir(dirname, this.filters, this.name, getdir_cb);
  this.otitle.innerHTML=dirname;
  this.path=dirname;
  //alert(this.path);
  /*while(!this.can_render)
  {
   var a=0;
  }
  status=''; 
  this.renderDir();*/
 }
 
 this.refresh = function()
 {
  this.changeDir(this.path);
 }
 
 this.addFilter = function(description, extension)
 {
  var opts = document.getElementById(this.name + '_ff').options;
  var newelem = document.createElement("OPTION");
  newelem.text=description;
  newelem.value=extension;
  opts.add(newelem);
 }
 
 this.init = function()
 {
  
  var drag_event ="";
  if(this.draggable)
   drag_event = " onselectstart=\"return false\" onmousedown=\"draggedWin=document.getElementById('" + this.container.id + "'); checkForDiffs(draggedWin, arguments[0]);mouse_down=true;\" onMouseUp=\"mouse_down=false;\"";
  var html_comp = "<table border=0 bgcolor=" + this.bgcolor + " cellpadding=2><tr>";
  html_comp += "<td style=\"-moz-user-select: none;\" align=center colspan=2 " + drag_event + " style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid #909090;\"><span style=\"font-weight:bold; color:" + this.forecolor + ";\" id=\"" + this.name + "_dn\">" + this.path + "</span>&nbsp&nbsp </td></tr>"; 
  html_comp += "<tr><td colspan=2 style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid #909090;\">";
  html_comp += "Tipo visualizzazione: <select onchange=\"" +this.name + ".view=this.value;" +this.name + ".renderDir();\" style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid " + this.forecolor + ";\">";
  html_comp += "<option value=\"list\">Icone piccole</option>";
  html_comp += "<option value=\"big\">Icone grandi</option>";
  html_comp += "<option value=\"detailed\">Lista dettagliata</option></select>";
  html_comp += "<input style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid #909090;\" value=\"Refresh\" type=button onclick=\"" + this.name + ".refresh();\">";
  html_comp += "</td></tr><tr><td valign=top bgcolor=white width=67%>";
  html_comp += "<div id=\"" + this.name + "_dw\"  style=\"width=" + this.width.toString() + ";height=" + this.height.toString() + ";border: 1px solid black; font-size:11px; color:#303030; overflow:scroll;\"></div></td>";
  html_comp += "<td  valign=top style=\"font-size:11px;color:" + this.forecolor + ";\">";
  html_comp += "Anteprima: <br>";
  html_comp += "<img src=\"" + this.icon_path + "spacer.gif\" id=\"" + this.name + "_imgprw\" width=170 height=130><br><br>";
  html_comp += "Formato file: <select style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";\" onchange=\"" + this.name + ".returnFormat=this.value;" + this.name + ".renderDir();\">"; 
  html_comp += "<option value=\"relative\">Percorso Relativo</option>";
  html_comp += "<option value=\"absolute\">Percorso Assoluto</option>";
  html_comp += "<option value=\"filename\" selected>Solo nome file</option>";
  html_comp += "</select>";
  html_comp += "</td></tr>";
  html_comp += "<tr><td valign=top><input style=\"width=" + this.width.toString() + "\" type=text id=\"" + this.name + "_fn\"></td>";
  html_comp += "<td valign=top ><input style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid " + this.forecolor + "; width:170px;\" type=button value=\"Ok\" onclick=\"" + this.name + ".returnFile();\"></td></tr>";
  html_comp += "<tr><td valign=top><select id=\"" + this.name + "_ff\" style=\"width=" + this.width.toString() + "\" onchange=\"" + this.name + ".filters=this.value;" + this.name + ".refresh();\">";
  html_comp += "<option value=\"\" selected>Tutti i file</option></select></td>";
  html_comp += "<td valign=top ><input style=\"background-color:" + this.bgcolor + ";font-size:11px;color:" + this.forecolor + ";border: 1px solid " + this.forecolor + "; width:170px;\" type=button value=\"Chiudi\" onclick=\"" + this.name + ".hide();\"></td></tr>";

  
  html_comp += "</table>";
  //this.container.style.width=this.formwidth;
  //this.container.style.height=this.formheight;
  this.container.style.verticalAlign='top';
  this.container.innerHTML= html_comp;
  this.dirwin=document.getElementById(this.name + '_dw');
  this.otitle=document.getElementById(this.name + '_dn');
  this.ofilefield=document.getElementById(this.name + '_fn');
  this.cdir=true;
  getDir(this.basepath, this.filters, this.name, getdir_cb);
  this.otitle.innerHTML=this.basepath;
 }
 
 this.renderDir = function ()
 {
  var f;
  var dirhtmlcode = "<table width=100% border=0 cellpadding=1>";
  var img_small = "-sm";
  var img_size="16";
  if(this.view=='big')
  {
   img_small = "";
   img_size="32";
   dirhtmlcode += "<tr>";
  }
   
  //alert("primo: " + this.afiles[0]);
  var contat = 1;
  for(f=1; f<this.afiles.length; f++)
  {
   //alert(this.afiles[f]);
   var type_img = '';
   var action="";
   var extrainfo = "";
   if(this.view=="detailed")
    var extrainfo = "</td><td class=fsinfo>" + this.afiles[f][5] + "</td><td class=fsinfo>" + this.afiles[f][6];
   if(this.afiles[f][2] == 'dir')
   {
    //var go_dir=this.afiles[f][0];
    //if(this.afiles[f][0] == '..')
     //go_dir="";
    //action = "getDir('" + this.afiles[f][3] + this.afiles[f][0] + "/', '" + this.afiles[f][4] + "', '" + this.name + "', getdir_cb)";
    action=this.name + ".changeDir('"+ this.afiles[f][3] + this.afiles[f][0] + "/');";
    if(this.afiles[f][0]=='..')
     type_img = '<img border=0 src=' + this.icon_path + 'up.jpg align=absmiddle>';
    else
     type_img = '<img border=0 src=' + this.icon_path + 'folder' + img_small + '.jpg align=absmiddle>';
    var extrainfo = "";
   }
   else
   {
    if(this.returnFormat=="filename")
     action = this.name + ".setValueToFileField('" + this.afiles[f][0] + "');";
	else 
	 action = this.name + ".setValueToFileField('" + this.afiles[f][3] + this.afiles[f][0] + "');";
    if(this.afiles[f][1] == 'gif' || this.afiles[f][1] == 'jpg' || this.afiles[f][1] == 'png' || this.afiles[f][1] == 'bmp' || this.afiles[f][1] == 'jpeg')
    {
      type_img = "<img border=0 src=" + this.afiles[f][3] + this.afiles[f][0] + " align=absmiddle width=" + img_size + " height=" + img_size + ">";
      action += this.name + ".setImagePreview('" + this.afiles[f][3] + this.afiles[f][0] + "');";
    }

    else 
     type_img = '<img border=0 src=' + this.icon_path + 'ascii' + img_small + '.jpg align=absmiddle>';
   }

    if(this.view=='big')
    { 
     if(contat==5)
     {
      dirhtmlcode += "</tr><tr>";
      contat=0;
     }
     dirhtmlcode += "<td align=center><a class=fs href=\"Javascript:" + action + "\">" + type_img + "<br>" + this.afiles[f][0] + "</a>" + extrainfo + "</td>";
    }
    else
     dirhtmlcode += "<tr><td><a class=fs href=\"Javascript:" + action + "\">" + type_img + this.afiles[f][0] + "</a>" + extrainfo + "</td></tr>";
    contat++;
  }
  if(this.view=='big')
   dirhtmlcode += "</tr>";
  dirhtmlcode += "</table>";
   //alert(dirhtmlcode);
   this.dirwin.innerHTML = dirhtmlcode;
 }
 
 this.hide = function(){
  this.container.style.visibility = 'hidden';
 }
 
 this.show = function(){
  this.container.style.visibility = 'visible';
 }
 
 this.setAlpha = function(alphaval)
 {
  this.container.style.filter = 'alpha(opacity=' + alphaval + ')';
 }
 
 this.returnFile = function()
 {
  if(this.returnObject == null && this.returnType!='function')
  {
   alert('No return object defined!');
   return;
  }
  else if(this.returnFunction == null && this.returnType=='function')
  {
   alert('No return function defined!');
   return;
  }
   
  if(this.returnType=="field")
   this.returnObject.value = this.ofilefield.value;
  else if(this.returnType=="text")
   this.returnObject.innerHTML = this.ofilefield.value;
  else if(this.returnType=="image")
   this.returnObject.src = this.ofilefield.value;
  else if(this.returnType=="function")
   this.returnFunction(this.ofilefield.value); 
   
  if(this.doFieldEvent && this.returnType=="field" && this.returnObject.onchange)
   this.returnObject.onchange();
  this.hide();  
 }
 
 this.setImagePreview=function(imgsrc)
 {
  document.getElementById(this.name + '_imgprw').src=imgsrc;
 }
 
 this.setPos=function(x,y)
 {
  this.container.style.top=y;
  this.container.style.left=x;
 }
 
 
 return this;
}

 
 function getdir_cb(ret)
 {
 
  var f;
  var dirhtmlcode = "";
  //alert("primo: " + ret[0]);
  //alert(ret[0][0]);
  var objb = eval(ret[0][0]);
  objb.setResult(ret);
  //objb.renderDir();
 //objb.renderDir();
  
 }
 var brows1=null;

