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 aPageParameters = new Array();
      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 aParameters = new Array();
        for(j=0;j<aParametersNodes.length;j++){
          nParameter = aParametersNodes[j];
          parameterName = this.oConnection.getNodeAttribute(nParameter,'name');
          parameterValue = this.oConnection.getNodeValue(nParameter);
          
          aParameters.push([parameterName,parameterValue]);
        }
        aPageParameters.push([blockName,aParameters])
      }
    }
  }catch(err){
  }
  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,aPageParameters);
  }
}

function displayPage(pageName,layoutName,aPageParameters){
  //[[blockName,[[paramName,paramValue],...]],...]
  this.oBlockMng.closeExtraBlocks();
  this.oPage = new page(this,pageName,layoutName,aPageParameters);
  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,aPageParameters){
  this.oPage.storePageParameters(aPageParameters);
  this.oBlockMng.loadExtraBlock(blockName,bFloating);
}

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;
}
