function system(bDebug){
  //objects
  this.oDebug = new debug();
  this.oConnection = new connection(this);
  this.oLayoutMng = new layoutMng(this);
  this.oBlockMng = new blockMng(this);
  this.oHtmlMng = new htmlMng(this);
  //properties
  this.browserCode;
  this.browserVersion
  this.systemName;
  this.defaultPage;
  this.oPage;
  this.login;
  this.userId;
  //methods
  this.checkBrowser = checkBrowser;
  this.inArray = inArray;
  this.getSystemName = getSystemName;
  this.loadSystem = loadSystem;
  this.parseSystemConfig = parseSystemConfig;
  this.displayPage = displayPage;
  this.sortBlock = sortBlock;
  this.closeBlock = closeBlock;
  this.openExtraBlock = openExtraBlock;
  this.focusFloatingBlock = focusFloatingBlock;
  this.closeFloatingBlock = closeFloatingBlock;
  //init
  if(bDebug) this.oDebug.debugOn();
  this.loadSystem();
}

function getSystemName(){
  var sPath = new String(window.location.pathname);
  var lastSlash = sPath.lastIndexOf('/') + 1;
  var lastDot = sPath.lastIndexOf('.');
  this.systemName = sPath.substring(lastSlash,lastDot);
}

function loadSystem(){
  this.oHtmlMng.loadCss('./_libs/css/common.css');
  this.checkBrowser();
  this.getSystemName();
  this.oConnection.loadFile('./systems/'+this.systemName+'/s_'+this.systemName+'.xml','oSystem.parseSystemConfig');
}

function parseSystemConfig(xCode){
  var oXml = this.oConnection.generateXDoc('xml',xCode);
  var oNode = oXml.getElementsByTagName('defaultLayout')[0];
  if(!oNode){
    this.oDebug.de('default layout for system '+this.systemName+' not defined');
    return;
  }
  this.oLayoutMng.defaultLayout = this.oConnection.getNodeValue(oNode);
  oNode = oXml.getElementsByTagName('defaultPage')[0];
  if(!oNode){
    this.oDebug.de('default page for layout '+layoutName+' not defined');
    return;
  }
  this.defaultPage = this.oConnection.getNodeValue(oNode);
  try{
    oNode = oXml.getElementsByTagName('defaultPage_parameters')[0];
    if(oNode){
      var oPageParameters = new Object();
      var aBlocks = oNode.getElementsByTagName('block');
      var nBlock;
      var blockName;
      var aParametersNodes;
      var nParameter;
      var parameterName;
      var parameterValue;
      for(i=0;i<aBlocks.length;i++){
        nBlock = aBlocks[i];
        blockName = this.oConnection.getNodeAttribute(nBlock,'name');
        aParametersNodes = nBlock.getElementsByTagName('parameter');
        var oParameters = new Object();
        for(j=0;j<aParametersNodes.length;j++){
          nParameter = aParametersNodes[j];
          parameterName = this.oConnection.getNodeAttribute(nParameter,'name');
          parameterValue = this.oConnection.getNodeValue(nParameter);
          
          oParameters[parameterName] = parameterValue;
        }
        oPageParameters[blockName] = oParameters;
      }
    }
  }catch(err){
  }
  
  var aJsClasses = oXml.getElementsByTagName('js_class');
  var nClass;
  var className;
  var sClassName;
  var objectName;
  for(j=0;j<aJsClasses.length;j++){
    nClass = aJsClasses.item(j);
    className = this.oConnection.getNodeValue(nClass);
    sClassName = new String(className);
    objectName = 'o'+sClassName.charAt(0).toUpperCase()+sClassName.substring(1,sClassName.length);
    this.oHtmlMng.loadJs('./systems/'+this.systemName+'/js/cj_'+className+'.js');
  }
  
  var aExternals = oXml.getElementsByTagName('external_component');
  var nExternal;
  var externalName;
  var aFiles;
  var nFile;
  var fileType;
  var fileName;
  for(j=0;j<aExternals.length;j++){
    nExternal = aExternals.item(j);
    externalName = this.oConnection.getNodeAttribute(nExternal,'name');
    var aFiles = nExternal.getElementsByTagName('file');
    for(k=0;k<aFiles.length;k++){
      nFile = aFiles.item(k);
      fileType = this.oConnection.getNodeAttribute(nFile,'type');
      fileName = this.oConnection.getNodeValue(nFile);
      if(fileType == 'js') this.oHtmlMng.loadJs('./systems/'+this.systemName+'/external_components/'+externalName+'/'+fileName+'.js');
      if(fileType == 'css') this.oHtmlMng.loadCss('./systems/'+this.systemName+'/external_components/'+externalName+'/'+fileName+'.css');
    }
  }
  
  this.oHtmlMng.loadCss('./systems/'+this.systemName+'/css/'+this.systemName+'.css');
  var nLogin = oXml.getElementsByTagName('login')[0];
  var bLogin = this.oConnection.getNodeValue(nLogin)
  this.login = bLogin == 'false' || bLogin == '0' ? false : true;
  if(this.login){
    this.displayPage('login','login');
  }else{
    this.displayPage(this.defaultPage,this.oLayoutMng.defaultLayout,oPageParameters);
  }
}

function displayPage(pageName,layoutName,oPageParameters){
  this.oBlockMng.closeExtraBlocks();
  this.oPage = new page(this,pageName,layoutName,oPageParameters);
  this.oPage.loadPage();
}

function sortBlock(blockName,fieldName,sortDataType,sortOrder,bExtraBlock){
  bExtraBlock = bExtraBlock ? bExtraBlock : false;
  this.oBlockMng[blockName].sort(fieldName,sortDataType,sortOrder,bExtraBlock);
  
}

function openExtraBlock(blockName,bFloating,oPageParameters,bAddClose){
  bFloating = bFloating == 'true' ? new Boolean(true) : new Boolean(false); 
  bAddClose = bAddClose == true ? Boolean(true) : Boolean(false); 
  this.oPage.storePageParameters(oPageParameters);
  this.oBlockMng.loadExtraBlock(blockName,bFloating,bAddClose);
}

function focusFloatingBlock(e,blockName){
  this.oHtmlMng.bringFloatingBlockToFront(blockName);
  this.oHtmlMng.dragFloatingBlock(e,blockName);
}

function closeBlock(blockName){
  this.oBlockMng[blockName].close();
}

function closeFloatingBlock(blockName){
  this.oBlockMng[blockName].closeExtra();
}
//tools
function checkBrowser(){
  if(navigator.appName == 'Netscape'){
    this.browserCode = 'm';
  }else if(navigator.appName == 'Microsoft Internet Explorer'){
    this.browserCode = 'e';
    strVersion = new String(navigator.appVersion);
    var startVersion = strVersion.indexOf('MSIE') + 5;
    var stopVersion = strVersion.indexOf(';',startVersion);
    this.browserVersion = strVersion.substring(startVersion,stopVersion);
  }
  //this.oConnection.browserCode = this.browserCode;
}
function inArray(array,toFind){
	return ('_|_'+array.join('_|_')+'_|_').indexOf('_|_'+toFind+'_|_') > -1;
}
