wspRoot = function(name)
 {this.name = name; this.wspUrl = '/-/incs/wspro/';};
 
// целочисленное значение
wspRoot.prototype.IntVal = function(mixed_var, base)
{
 var tmp;
 if(typeof(mixed_var) == 'string')
 {
  tmp = parseInt(mixed_var);
  if(isNaN(tmp)) return 0;
   else return tmp.toString(base || 10);
 }
 else if(typeof(mixed_var) == 'number')
  return Math.floor(mixed_var);
 else return 0;
};

// создание AJAX-объекта
wspRoot.prototype.getXmlHttp = function()
{
  var xmlhttp;
  try{
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
   try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }catch(E){xmlhttp = false;};
  };
  if(!xmlhttp && (typeof XMLHttpRequest != 'undefined'))
    xmlhttp = new XMLHttpRequest();
  return xmlhttp;
};

// массив объектов с классом 'clss' дочерних по отношению к 'obj'
wspRoot.prototype.getElementsByClassName = function(clss, obj)
{
 var node = (obj || document);
 if(node.getElementsByClassName)
  return node.getElementsByClassName(clss);

 var list = node.getElementsByTagName('*');
 var length = list.length;
 var classArray = clss.split(/\s+/);
 var classes = classArray.length;
 var result = new Array();
 for(var i=0; i<length; i++)
 {
  for(var j=0; j<classes; j++)
  {
   if(list[i].className.search('\\b' + classArray[j] + '\\b') != -1)
    {result.push(list[i]); break;};
  };
 };
 return result;
};
wspRoot.prototype.getElementsByClass = function(clss, obj) // укороченный алиас
 {return this.getElementsByClassName(clss, obj);}

// определение/назначение ID элементу
wspRoot.prototype.getElementId = function(obj)
{
 var pref = 'wspGlobalObj';
 var id = '';
 try{
  if(obj.id.length > 0)
   id = obj.id;
  else
  {
   var tmp = new Date().getTime();
   while(document.getElementById(pref+tmp)) tmp++;
   id = pref+tmp; obj.id = id;
  };  
 }catch(e){id = null;};
 return id;
};



/* http://phpjs.org/functions/include:433 */
wspRoot.prototype.Modules = {};
wspRoot.prototype.ModuleLoadTimeOut = function(key)
{
 this.Modules[name]['ajax'].abort();
 this.Modules[name]['stat'] = false;
};
wspRoot.prototype.ModuleSet = function(name, stat)
{
 this.Modules[name] = {'stat':stat};
};
wspRoot.prototype.ModuleLoad = function(name, time, async)
{
 this.ModuleLoadCss(name);
 return this.ModuleLoadJs(name, time, async);
};
wspRoot.prototype.ModuleLoadCss = function(name)
{
 name = name.toLowerCase(); var ret = true;
 var filecss = this.wspUrl+'modules/'+name+'/'+name+'.css';
 try
 {
  var d = window.document; var isXML = d.documentElement.nodeName !== 'HTML' || !d.write;
  var css = (d.createElementNS&&isXML ? d.createElementNS('http://www.w3.org/1999/xhtml', 'link') : d.createElement('link'));
   css.setAttribute('type', 'text/css'); css.setAttribute('href', filecss); css.setAttribute('rel', 'stylesheet');
   
  if(d.getElementsByTagNameNS && isXML)
  {
   if(d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0])
    d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(css)
   else
    d.documentElement.insertBefore(css, d.documentElement.firstChild);
  } else d.getElementsByTagName('head')[0].appendChild(css);
 }catch(e){ret = false;};
 
 return ret;
};
wspRoot.prototype.ModuleLoadJs = function(name, time, async)
{
 time = this.IntVal(time);
 time = 1000*(time<1 ? 7 : time);
 async = (async ? false : true);
 name = name.toLowerCase();

 if(this.Modules[name]) return '';

 this.ModuleSet(name, false);
 this.Modules[name]['ajax'] = this.getXmlHttp();
 var mod = null;
 try
 {
  var file = this.wspUrl+'modules/'+name+'/'+name+'.js';
  var stok = (/^(file)|(ftp)/i.test(file) ? 0 : 200);
  var tmr = setTimeout(this.name+'.ModuleLoadTimeOut("'+name+'")', time);
  this.Modules[name]['ajax'].open('GET', file, false);
  this.Modules[name]['ajax'].send(null);
  if(this.Modules[name]['ajax'].status == stok)
  {
   clearTimeout(tmr);
   this.Modules[name]['stat'] = true;
   mod = this.Modules[name]['ajax'].responseText;   
  };
 }catch(e){};
  
 delete this.Modules[name]['ajax'];
 return mod;
};

wsp = new wspRoot('wsp');
