function DesktopAPI (config) {
    //for (var x in config) alert(x+"-->"+config[x]);
  var _component = null;
  if (!config) config = {};
  if (!config.father) config.father = document.body;
  if (!config.id) config.id = DESKTOP.launchEvent(DESKTOP.GET_UNIQUE_ID);
  config.isLoaded = false;
 // if (!config.config) config.config={};
 
 this.onsave = function(data) {
     config.configId = data;
 }
  
  this.setComponent = function(component, listenerDesktop) {
      _component = component;
      if (listenerDesktop) DESKTOP.launchEvent(DESKTOP.SET_LISTENER_DESKTOP, this)
  }
  
  this.getFather = function() {
      return config.father;
  }
  
  //config = {name: xxx, father:elementDOM, config: {}
  /* Defined in the prototype */
  /*
  this.loadComponent = function(config) {
      DESKTOP.launchEvent(DESKTOP.LOAD_GRAPHIC_UNIT, config)
  }

   */
  this.hadLogged = function() {
      try {
        return DESKTOP.launchEvent(DESKTOP.HAD_LOGGED);
      } catch(ex) {
        return true;
      }
  }

  
  this.getConfig = function(resourceId, categoryName) {

      if (this.hadLogged()&&(!config.isLoaded || resourceId)) {        
        var data = {};
        if(config.config && (typeof(config.config.kboxId) != 'undefined')){
            data = {nombre:config.config.kboxId};
        }else{            
            data = {nombre:config.name};
        }
        if (resourceId) data.idRecurso=resourceId;
        if (categoryName) data.nombreCategoria = categoryName;
        
        configLoad = DESKTOP.launchEvent(DESKTOP.GET_CONFIG, data);
        
        if (!config.isLoaded) {
          for (var x in config.config) {
            configLoad.configuracion[x] = config.config[x];
          }
        }
        
        config.isLoaded = true;        
        config.config = configLoad.configuracion;
        config.configId = configLoad.id;
        config.configOfUser = configLoad.linkUsuario;
        config.configOfResource = configLoad.linkRecurso;        
     }

     return config.config?config.config:{};
  }
  
  this.saveConfig = function(resourceId, categoryName) {
      if (!config.config) this.getConfig();
      var nombre = "";
      if(config.config && (typeof (config.config.kboxId) != 'undefined')){
            nombre = config.config.kboxId;
        }else{
            nombre = config.name;
        }
      if (config.configOfUser || config.configOfResource) {
        DESKTOP.launchEvent(DESKTOP.SAVE_CONFIG, {
          id: config.configId,
          nombre: nombre,
          configuracion: JSON.stringify(config.config)          
        }, this)          
      } else if (resourceId) {          
        DESKTOP.launchEvent(DESKTOP.SAVE_CONFIG, {
          nombre: nombre,
          idRecurso: resourceId,
          nombreCategoria: categoryName,
          configuracion: JSON.stringify(config.config)          
        }, this);                     
      } else {
        config.configOfUser=true;
        DESKTOP.launchEvent(DESKTOP.SAVE_CONFIG, {
          nombre: nombre,
          configuracion: JSON.stringify(config.config),
          user: true
        }, this)
      }
  }
  
  this.getUser = function() {
      return DESKTOP.launchEvent(DESKTOP.GET_INFO_USER)
  }
  this.getUserInfo = function(userId){
      return DESKTOP.launchEvent(DESKTOP.GET_USER_INFO,userId)
  }
  
  this.activeDialog = function(title, type, width, top) {
      return DESKTOP.launchEvent(DESKTOP.ACTIVE_DIALOG, {
          title: title,
          type: type,
          width: width,
          top: top
      })
  }
  
  this.desactiveDialog = function() { 
      DESKTOP.launchEvent(DESKTOP.DESACTIVE_DIALOG)
  }
  //review!!!!!!
  this.hasPermissionRecurso = function(resourceId, resourceCategory, permission) {
      return DESKTOP.launchEvent(DESKTOP.HAS_PERMISSION, {        
            idRecurso: resourceId,
            nombreCategoria: resourceCategory,
            tipo: permission
      });
  }
  
  /* The method return true / false, only use on global permissions!!!
      1* if communityIds and inCompany are empty, return true if the permission exists about a company or commmunity
      2* if communityIds is passed, return true if the permission exist about a community passed
      3* if inCompany (true) is passed, return true if the permission exist about company
      4* passed communiIds and inCompany: do a OR with two before points (case 2 OR case 3)
      
      if "start" parameter is true check if the permission start width this string.
  */  
this.hasPermission = function(permission, communityIds, inCompany, start) {
   if (!communityIds) communityIds = null;
   if (!permission) return false;
   if (typeof permission == "string") {
      if (communityIds && inCompany) {          
          return this.hasPermission(permission, null, inCompany) || this.hasPermission(permission, communityIds);
      }
      
      var userPermissions = this.getUser().permissionsMap;
      var has = false;
  
      for (var x in userPermissions) {          
          if (x==permission || (start && (x.indexOf(permission)==0))) {
              if (communityIds == null && !inCompany) {
                has=true;
                break;
              } else if (inCompany) {                  
                    for (var i=0;i<userPermissions[x].length;i++) {
                        if (userPermissions[x][i].categoryName=="Company") {                    
                            has=true;
                            break;
                        }
                    }   
                 break;
              } else {
                 for (var j=0;j<communityIds.length;j++) {
                    for (var i=0;i<userPermissions[x].length;i++) {
                        if (userPermissions[x][i].categoryName=="Grupo" &&
                            userPermissions[x][i].resourceId==communityIds[j]) {                    
                            has=true;
                            break;
                        }
                    }
                    if (has) break;
                 }
                break;
              }
          }
      }
 
    } else {
        for (var i=0; i<permission.length; i++) {
            has = this.hasPermission(permission[i], communityIds, inCompany)
            if (has) break;
        }
    
    }
    return has;
      
 /*     return DESKTOP.launchEvent(DESKTOP.HAS_PERMISSION, {        
            tipo: permission
      });*/
  }
 
  this.getHasBulletin = function(){
      return DESKTOP.launchEvent(DESKTOP.GET_HAS_BULLETIN, {});
  }

  
  this.setConfig = function (attr, value) {
            alert("Sin implementar");
  }  
  
  this.inbox = function(message) {
      if (_component) _component.onmessage(message);
  }
  
  this.onedit = function() {
      if (_component) _component.onedit();
  }
  
  this.sendMessage = function(key, message) {
      DESKTOP.launchEvent(DESKTOP.SEND_MESSAGE, {key:key, message:message});      
  }
  
  this.authentication = function(name, password) {
      DESKTOP.launchEvent(DESKTOP.SEND_LOGIN, {login:{login:name, password:password}, API:this})
  }
  
  this.resetInfoUser = function() {
      DESKTOP.launchEvent(DESKTOP.RESET_INFO);
  }
  
  this.getComponent = function() {
      return _component;
  }

  this.setLanguage = function(languageKey) {
      DESKTOP.launchEvent(DESKTOP.SET_LANGUAGE, languageKey);
  }

  this.getLanguage = function() {
      return DESKTOP.launchEvent(DESKTOP.GET_LANGUAGE, {});
  }
  this.getLanguageExt = function() {
      return DESKTOP.launchEvent(DESKTOP.GET_LANGUAGE_EXT, {});
  }
  
  this.resetInterface = function() {
      DESKTOP.launchEvent(DESKTOP.RESET_INTERFACE);
  }

  this.exit = function() {
      DESKTOP.launchEvent(DESKTOP.EXIT)
  }
  
  this.saveCookie = function (key, value, expires) {
      if (!expires) expires = 7;
      DESKTOP.launchEvent(DESKTOP.SAVE_COOKIE, {key: key, value:value, expires:expires});
  }
  
  this.removeCookie = function(key) {
      DESKTOP.launchEvent(DESKTOP.REMOVE_COOKIE, key);
  }

  this.recoveryCookie = function(key) {
      return DESKTOP.launchEvent(DESKTOP.RECOVERY_COOKIE, key);
  }
  
  this.loadScript = function(url, key) {             
      $.getScript(url, function() {
          if (_component.onloadscript) _component.onloadscript(key);
      });
  }

  this.initDesktop = function() {
        DESKTOP.launchEvent(DESKTOP.INIT);
    }

  this.getInfoModule = function(key) {      
      if (key =="BulletinBoard") {
        var hasBulletin = this.getHasBulletin();        
        if (((this.getCommunitiesId() == "") || (!hasBulletin))) return false;
      }
      var module = DESKTOP.launchEvent(DESKTOP.GET_INFO_MODULE, key);
      if (module.userPermission && !this.hasPermission(module.userPermission, this.getCommunitiesId(), true)) return false;
      return module;
  }
  this.FindBulletinContent = function(){
      DESKTOP.launchEvent(DESKTOP.FIND_BULLETIN_CONTENT, this);
  }
  //Edited by India Pune team
  /* Defined in the prototype */
   /*
  this.addPage = function (title, template, resourceId, validate) {      
      return DESKTOP.launchEvent(DESKTOP.ADD_PAGE, {title: title, template: template, resourceId: resourceId, validate: validate});
  }
   */
  
  this.closePage = function () {      
      return DESKTOP.launchEvent(DESKTOP.CLOSE_PAGE, {});
  }
  

  this.refreshPage = function() {
      return DESKTOP.launchEvent(DESKTOP.REFRESH_PAGE, {});
  }
  

  /* Defined in the prototype */
  /*
  this.addKbox = function (col, kboxData, kboxCfg) {
      if (!kboxCfg) kboxCfg = {};
      DESKTOP.launchEvent(DESKTOP.ADD_KBOX, {col: col,
                                             kboxData:kboxData, 
                                             kboxCfg: kboxCfg});
  }
   */
  
  this.getCommunities = function() {
            var elements = [];

            var communities = this.getUser().grupos;

            if (communities) {
                for (var i=0; i<communities.length; i++) {
                    elements[i]= {name:communities[i].nombre, key:communities[i].id, type:"Grupo"};
                }
            }
            return elements;
   }    
   
   this.getCommunitiesId = function() {
            var ids = [];

            var communities = this.getCommunities();  

            if (communities) {
                for (var i=0; i<communities.length; i++) {
                    
                    ids[i]= communities[i].key;
                }
            }
            return ids;            
        }     
        
   this.getCompanyAndCommunities = function() {
       var userData = this.getUser();
       var list = this.getCommunities();

       Util.inArray(list, 0, {name:userData.nombreEmpresa, key:userData.idEmpresa, type:"Company"});
       return list;
   }
   
   this.isI18N = function() {
      return DESKTOP.launchEvent(DESKTOP.IS_I18N);       
   }
  
  
  //*********** FUTURE REMOVE, NOT USE **************/
  // remove, temporal to support old Kubbe Blog
  this.createPanel = function() {
      DESKTOP.launchEvent(DESKTOP.CREATE_PANEL, {login:name, password:password})
  }
  //remove, temporal!!!!!
  this.writeInColumn = function() {
      
  }  
  //this code is added to cache the company configuration parameter on client side
  this.getCompanyConfigParam = function(){
      return DESKTOP.launchEvent(DESKTOP.GET_COMPANY_CONFIG_PARAM);
  }
  
  //*********** FUTURE REMOVE, NOT USE **************/  
  this.loadInlineHelp = function(data){
      data.API = this;      
      DESKTOP.launchEvent(DESKTOP.LOAD_IN_LINE_HELP, data);
  }
  this.changeInlineHelp = function(data){
        DESKTOP.launchEvent(DESKTOP.CHANGE_IN_LINE_HELP, data);
  }

  this.removeConfig = function(data){
      DESKTOP.launchEvent(DESKTOP.REMOVE_CONFIG, data);
  }
}

/* Prototype definitions */
if (! DesktopAPI.prototype.addPage) {
    
    DesktopAPI.prototype.addPage = function (title, template, resourceId, validate, helpTitle) {
        return DESKTOP.launchEvent(DESKTOP.ADD_PAGE, {title: title, template: template, resourceId: resourceId, validate: validate, helpTitle: helpTitle});
    };
}

if (! DesktopAPI.prototype.addKbox) {
    DesktopAPI.prototype.addKbox = function (col, kboxData, kboxCfg) {
        if (!kboxCfg) kboxCfg = {};
        DESKTOP.launchEvent(DESKTOP.ADD_KBOX, {col: col,
        kboxData:kboxData, 
        kboxCfg: kboxCfg});
    };
}

if (! DesktopAPI.prototype.loadComponent) {
  DesktopAPI.prototype.loadComponent = function(config) {
      DESKTOP.launchEvent(DESKTOP.LOAD_GRAPHIC_UNIT, config);
  };
}
