/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

function ServerDir(path, loadingIcon) {
  this.path = path;
  this.loadingIcon = loadingIcon;
  this.widgetAddedQueue = new Array();
  this.widgetAddedSending = false;
}

ServerDir.prototype.getCategoryList = function(callback) {
  var request = createXMLHttpRequest();
        
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }  
  request.open("GET", this.path + "?command=categories");
  var self = this;
  request.onreadystatechange = function(e) {
    if (request.readyState == 4) {
      if (window.netscape) {
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
      }
      var response = request.responseText;
      if (response == null || (request.status != 200)) {
        callback(null);
        return;
      }
      var result = null;
      if (response.indexOf("new Array(") == 0) {
        result = eval(response);
      }
      else {
        /*
        result = new Array();
        var index = 0;
        while (index >= 0) {
          var index2 = response.indexOf("\n", index);
          if (index2 >=0 ) {
            result[result.length] = response.substring(index, index2);
            index = index2 + 1;
          }
          else {
            index = -1;
          }
        }
        */
        result = new Array();
      }  
      self.loadingIcon.style.visibility = "hidden";
      callback(result);
    }
  }
  this.loadingIcon.style.visibility = "visible";
  request.send(null);    
}
  
ServerDir.prototype.getPathList = function(callback, category) {
  var request = createXMLHttpRequest();
        
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }
  if (category) {
    var url = this.path + "?command=widgets&category=" + escape(category);
    addError(url);
    request.open("GET", url);
  }
  else {
    request.open("GET", this.path + "?all");
  }
  var self = this;  
  request.onreadystatechange = function(e) {
    if (request.readyState == 4) {
      if (window.netscape) {
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
      }
      var response = request.responseText;
      addError(response);
      if (response == null || request.status != 200) {
        return(null);
      }
      var result = null;
      if (response.indexOf("new Array(") == 0) {
        result = eval(response);
      }
      else {
        result = new Array(0);
        var index = 0;
        while (index >= 0) {
          var index2 = response.indexOf("\n", index);
          if (index2 >=0 ) {
            var i = result.length;
            result[i] = new Object();
            result[i].path = response.substring(index, index2);
            result[i].title = result[i].path;
            index = index2 + 1;
          }
          else {
            index = -1;
          }
        }
      }  
      self.loadingIcon.style.visibility = "hidden";
      callback(result);
    }
  }
  this.loadingIcon.style.visibility = "visible";
  request.send(null);    
}
  
ServerDir.prototype.getWidgetsCount = function(callback) {
  var request = createXMLHttpRequest();
        
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }
  var url = this.path + "?command=widgetsCount";
  request.open("GET", url);
  request.onreadystatechange = function(e) {
    if (request.readyState == 4) {
      if (window.netscape) {
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
      }
      var response = request.responseText;
      if (response == null || request.status != 200) {
        callback("Many");
      }
      callback(response);
    }
  }
  request.send(null);      
}

ServerDir.prototype.getWidgetInfo = function(callback, path) {
  var request = createXMLHttpRequest();
        
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }
  var url = this.path + "?command=widget&path=" + escape(path);
  request.open("GET", url);
  var self = this;
  request.onreadystatechange = function(e) {    
    if (request.readyState == 4) {
      if (window.netscape) {
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
      }
      var response = request.responseText;
      addError(response);
      if (response == null || request.status != 200) {
        return(null);
      }
      var result = null;
      if (response.indexOf("new Object({") == 0) {
        result = eval(response);
      }
      else {
        result = Object();
        result.path = path;
        result.title = path;
        result.description = "No description";
        result.summary = "No summary";
        result.version = "1.0";
        result.size = "unknown";
      }  
      self.loadingIcon.style.visibility = "hidden";
      callback(result);
    }
  }
  this.loadingIcon.style.visibility = "visible";
  request.send(null);    
}

ServerDir.prototype.widgetAdded = function(path) {
  if (this.widgetAddedSending) {
    this.widgetAddedQueue.push(path);
  }
  else {
    this.sendWidgetAdded(path);
  }
}  

ServerDir.prototype.sendWidgetAdded = function(path) {
  var request = createXMLHttpRequest();
        
  var url = this.path + "?command=widgetAdded&path=" + escape(path);
  request.open("GET", url, true);
  var _self = this;
  request.onreadystatechange = function(e) {
    if (request.readyState == 4) {
      if (_self.widgetAddedQueue.length > 0) {
        var path = _self.widgetAddedQueue.shift();
        _self.sendWidgetAdded(path);
        //alert("Send queued " + path);
      }
      else {
        this.widgetAddedSending = false;        
      }
    }
  }
  this.widgetAddedSending = true;
  request.send(null);      
}  

ServerDir.prototype.favouriteRemoved = function(path) {
  var request = createXMLHttpRequest();
        
  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  }
  var url = this.path + "?command=favouriteRemoved&path=" + escape(path);
  request.open("GET", url, true);
  var self = this;
  request.send(null);      
}  
