// alert('load std_funx');
function catchError(msg,url,line){
/*  var me= msg.constructor==Event ? msg : (this.constructor==Event ? this : (this.event ? this.event : window.event));
  trace( 'ERR: '+ msg +' in L'+line+', '+url+',\nme' + (me ? dump(me) : '') ); 
  if(!me) return true;
  me.stopPropagation();
  me.preventDefault();
  me.cancelBubble = true;
  me.cancelBubble(); // = true;
  me= null;
*/
  return true;
}
// window.onerror= catchError;
/*
if(window.attachEvent) window.attachEvent('onerror',catchError);
else window.addEventListener('error', catchError, false);
*/



/*- _array/object methods -*/

Object.prototype.merge= function(){
  for(var ai=0; ai < arguments.length; ai++ ){
    for(var i in arguments[ai]){
      this[i]= arguments[ai][i];
    }
  }
  return this;
}


function set(){
  for(var ai=0; ai < arguments.length; ai++ ){
    if( typeof arguments[ai] != 'undefined' && typeof arguments[ai] != 'NaN' ){
      return arguments[ai];
    }
  }
  return arguments.pop();
}



Date.fromString= function(str){
  if(!str) return (new Date());
  var d;
  if( d= String(str).match(/^([+-]?)(\d+)$/) ){
    if(d[1] && typeof str=='string'){
// delta t secs 
      return ( new Date( (new Date()).getTime()+ d[0]*1000 ) );
    }
    if(d[2] < 3600*24*365.25*100 ){
// t in secs
       return ( new Date( d[0]*1000 ) );
    }
    return new Date( str );
  }
  if( d= String(str).match(/^(\w+\W+)??((\d{1,2})\W+)?((\d{1,2})\W+)?([1-9]\d)?(\d{2})(-(\d{1,2}))?(-(\d{1,2}))?(\W+(\d{1,2}))?(\W+(\d{1,2}))?(\W+(\d{1,2}))?$/) ){
//                   1:ddd     2 3:d          4 5:m          6:century 7:y    8 9:m       10 11:d       12  13:h       14  15:i       16  17:s
//    trace('nu Dat('+ [ set(d[6],(d[7]<50?20:19))+d[7], set(d[9],d[5],1)-1, set(d[11],d[3],1), set(d[13],0), set(d[15],0), set(d[17],0) ].join(', ') +' )' );
    return ( new Date( set(d[6],(d[7]<50?20:19))+d[7], set(d[9],d[5],1)-1, set(d[11],d[3],1), set(d[13],0), set(d[15],0), set(d[17],0) ) );
  }
  return false;
}
Date.prototype.fromString= function(str){
  this.setTime( Date.fromString(str).getTime() );
  return this; 
}



/*- _string methods -*/


function setStringMethods(){
  String.prototype.repeat= function(times){
    var ret='';
    for(var i=0; i<times; i++){
      ret+=this;
    }
    return ret;
  }
  
  String.prototype.trim = function(){
    return this.replace(/(^\s*|\s*$)/mg,'');
  }
  String.prototype.reverse= function(){
    var ret='';
    for( var i=(this.length-1); i>=0; i-- ){
      ret+= this.substr(i,1);
    }
    return ret;
  }
}

setStringMethods();
if(window.attachEvent) window.attachEvent('onload',setStringMethods);



/*- _debug stuff -*/

function trace(msg,mode,target){
//  return false;
  if(!target){
    var target=document.getElementById('trace');
  }
  msg= (typeof msg=='object' ? dump(msg) : msg).toString().replace(/</g,'&lt;'); // [MSIE] .replace(/\n/g,'<br>\n');
  
  if( typeof target!='object' || target===null ){
//     alert(msg);
    traceMsg.push(msg);
    return false;
  }
  
  if(mode=='overwrite' || mode=='x'){
    target.innerHTML = msg;
  }
  else if(mode=='asc' || mode=='v' || mode=='V'){
    target.innerHTML+= msg + '\n<hr>';
  }
  else{
    target.innerHTML= msg + '\n<hr>' + target.innerHTML;
  }
}
var traceMsg=[];



function dump(me,recursive,depth,noFunx,ownProps){
  if(typeof me == 'false') me=this;
  if(typeof depth != 'number') depth=0;
  if(typeof recursive != 'number') recursive=1; // 0:none, int>0:levels of rec, int<0:all
  var t= (me && me.constructor ? String(me.constructor).replace(/\s*function (\w+)[\S\s]*/,'[$1]') : typeof me);
  var ret= ': '+ t +'= ';

//   trace( 'dump('+ String(me).split('\n').shift() +', '+recursive+', '+depth+', '+noFunx+')' );
  if(typeof me=='function'){ 
    ret+= String(me).split('\n').shift() +'...} \n';
  }
  else if( typeof me=='object' ){
    recuret='';
    if( recursive<0 || depth<recursive ){
      for(var i in me){
        if( (noFunx && typeof me[i]=='function') || (ownProps && !me.hasOwnProperty(i)) ) continue;
        recuret+= '  '.repeat(depth+1) + (me.hasOwnProperty(i) ? '' : '.') + i; // + ' = ('+ (i && me[i] ? String(me[i]).replace(/\s*function (\w+)[\S\s]*/,'[$1]') : typeof me[i]) +') ';
        try{
          recuret+= dump(me[i],recursive,depth+1,noFunx,ownProps)
        }
        catch(e){}
      }
    }
    ret+='Object {'+ (recuret ? '\n'+ recuret + '  '.repeat(depth) : '') +'}\n';
  }
  else{
    try{
      ret+= String(me) + '\n';
    }
    catch(e){}
  }
  return ret;   
}
// Object.prototype.dump = dump;




/*- _DOM stuff -*/
  
function $(str,p){
  if(!p) var p= document;
  switch( str.substr(0,1) ){
    case '#':
      return p.getElementById( str.substr(1) );
    case '.':
      return p.getElementsByClassName( str.substr(1) );
    case '"':
      return p.getElementsByName( str.substr(1) );
    default:
      return p.getElementsByTagName( str );
  }
}



function setDOMMethods(me,recursive){
//  alert('sdm');
  var htmlNode= ( typeof HTMLElement != 'undefined' )
                  ? HTMLElement 
                  : ( ( typeof Element != 'undefined' )
                      ? Element
                      : Object
                    );
  var htmlNodeProto= htmlNode.prototype;

  if(me && me.tagName){
    me.getChildsByAttribute= htmlNodeProto.getChildsByAttribute;
    me.addClass            = htmlNodeProto.addClass;
    me.rmClass             = htmlNodeProto.rmClass;
    me.switchClass         = htmlNodeProto.switchClass;
    me.hover               = htmlNodeProto.hover;
    me.unhover             = htmlNodeProto.unhover;
    me.addListener         = htmlNodeProto.addListener;
    me.rmListener          = htmlNodeProto.rmListener;
  }
  

  getChildsByAttribute= function(name,wert,mode,recursive,d){
    if(typeof d != 'number'){
      d=0;
    }
    if(typeof recursive == 'undefined') recursive=true;
    
    var me=((this) ? this : document); 
    var ret= new Array();
    var msg='';
    var at,hat,cn,hcn,mci;
    
    for(i in me.childNodes){
      cn=me.childNodes[i];
      at={name:false, value:false};
  
      if(cn.tagName != undefined){
        hat=false;
        try{
          if(cn.attributes){
            at= cn.attributes[0];
            if(cn.attributes[name] != undefined){
              at= cn.attributes[name];
              if(wert){
                if(typeof wert=='function'){
                  if(at.value.search(wert)>-1){
                    hat= true;
                  }
                }
                else if(typeof wert=='object'){
                  var wi;
                  for(wi=0; wi<wert.length; wi++){
                    if( at.value.search( new RegExp('\\b'+ wert[wi] +'\\b') ) >-1 ){
                      hat= true;
                    }
                    else if(mode=='and'){
                      hat= false;
                    }
                  }
                }
                else if(at.value == wert){
                  hat= true;
                }    
                else{
                  var vs= at.value.split(' ');
                  for(var vi in vs){
                    if(vs[vi]==wert){
                      hat=true;
                    }
                  }
                }
              }
              else{
                hat= true;
              }
            }
          }
        }
        catch(e){}
        if(hat){
          ret.push(cn);
        }
        if(!at) at={name:false, value:false};
        
        hcn=false;
        try{
          if(cn.hasChildNodes()){
            hcn=true;
          }
        }
        catch(e){}
        if(hcn && recursive){
          ret=ret.concat(cn.getChildsByAttribute(name,wert,mode,(typeof recursive=='number' ? (recursive-1) : true),(d+1)));
        }
      }
    }
  //  alert(msg);
    if(msg) document.getElementById('msg').innerHTML+= msg;
    return ret; 
  }
  htmlNodeProto.getChildsByAttribute= getChildsByAttribute;
  document.getChildsByAttribute= getChildsByAttribute;
  
  
  
  isSiblingOf= function(ancestor,maxDepth){
    if(typeof ancestor != 'object') return false;
    var pa=this;
    var depth=1;
    while( (pa=pa.parentNode) && (!maxDepth || depth<=maxDepth ) ){
      if(pa==ancestor) return depth;
      depth++;
    }
    return false;
  }
  htmlNodeProto.isSiblingOf= isSiblingOf;
  
  
  
  addClass= function(me,add,rm){
    if(!me.tagName){
      var rm= add;
      var add= me;
      var me= this;
    }
    if(rm){
      me.rmClass(rm);
    }
    if(me.className){
      if( !me.className.match( new RegExp('\\b'+ add +'\\b') ) ){
        me.className+= " "+ add;
      }
    }
    else{
      me.className= add;
    }
    return me.className;
  }
  htmlNodeProto.addClass= addClass;
  
  
  
  rmClass= function(me,clas){
  //  alert( 'rmc: ('+me+', '+clas+')' );
    if(!me.tagName){
      clas= me;
      me= this;
    }
    re= new RegExp('\\b'+ (typeof clas=='object' ? '('+ clas.join('|') +')' : clas) +'\\b','g');
    me.className= me.className.replace(re,'');
    return me.className;
  }
  htmlNodeProto.rmClass= rmClass;
  
  
  
  switchClass= function(me,remove,add){
    if(!me.tagName){
      this.rmClass(me);
      this.addClass(remove);
      return this.className;
    }
    rmClass(me,remove);
    addClass(me,add);
    return me.className;
  }
  htmlNodeProto.switchClass= switchClass;
  
  
  
  
  
  hover= function(){
    addClass(this,'hover');
  }
  htmlNodeProto.hover= hover;
  
  unhover= function(){
    rmClass(this,'hover');
  }
  htmlNodeProto.unhover= unhover;
  
  
  setStyle= function(s){
    for(var i in s){
      if(typeof s[i]=='function') continue;
      this.style[i]= s[i];
    }
  }
  htmlNodeProto.setStyle= setStyle;
  
  
  
  
/*- event stuff -*/
  
  addListener= function(evnt,func){
    if( this.addEventListener !== undefined ){
      this.addEventListener(evnt,func,false);
      return true;
    }
    else if( this.attachEvent ){
      this.attachEvent('on'+evnt,func);
      return true;
    }
    return false;
  }
  Object.prototype.addListener= addListener;
  
  rmListener= function(evnt,func){
    if( this.removeEventListener !== undefined ){
      this.removeEventListener(evnt,func,false);
      return true;
    }
    else if( this.attachEvent ){
      this.detachEvent('on'+evnt,func);
      return true;
    }
    return false;
  }
  Object.prototype.rmListener= rmListener;
  
}

setDOMMethods();

if(window.attachEvent){
  setDOMMethods(window);
  window.attachEvent('onload',setDOMMethods);
//  alert(window.addEvent);
}




/*- _window+screen stuff -*/

function getViewportSize(){
  if(typeof window.innerHeight == 'number'){
    return {w:window.innerWidth, h:window.innerHeight};
  }
  else if(typeof document.body.clientHeight == 'number'){
    return {w:document.body.clientWidth, h:document.body.clientHeight};
  }
  else if(typeof document.documentElement.clientHeight == 'number'){
    return {w:document.documentElement.clientWidth, h:document.documentElement.clientHeight};
  }
  return false;  
}



function extWindow(win,name,href,w,h,x,y,appearance){
  if(typeof win == 'object' && win!=null && win.tagName && win.tagName=='A'){
// bad construction for multipurpose use... ':-\
// called from within <a href onClick=extWindow(this)> => grab values from anchor properties
    var me = win;
    win= undefined;
    var retval=false;
  }
  else{
    var retval=true;
    var me = {};
  }

  if(!name) var name=me.target ? me.target : '_blank';

  if(typeof win !='object' || win==null){
    if(typeof extWindows != 'object' ){
      extWindows={};
    }
    if(!extWindows[name]){
      extWindows[name]= {closed:true};
    }
    var win= extWindows[name];
  }
  if(!href) var href= me.href ? me.href : '';
  w= Math.round( typeof w == 'number' ? w : screen.availWidth /1.6 );
  h= Math.round( typeof h == 'number' ? h : screen.availHeight/1.6 );
  x= Math.round( typeof x == 'number' ? x : (screen.availWidth -w)/1.6 );
  y= Math.round( typeof y == 'number' ? y : (screen.availHeight-h)/1.6 );
  
  var a = parseParamString( appearance ? appearance : ((me && me.attributes && me.attributes.appearance) ? me.attributes.appearance.value : 'sr' ) );
  
  if(!win || win.closed){
    var params= ''+
      'location='   +(a.l ? 'yes' : 'no') + ', ' +
      'toolbar='    +(a.t ? 'yes' : 'no') + ', ' +
      'menubar='    +(a.m ? 'yes' : 'no') + ', ' +
      'personalbar='+(a.p ? 'yes' : 'no') + ', ' +
      'resizable='  +(a.r ? 'yes' : 'no') + ', ' +
      'scrollbars=' +(a.c ? 'yes' : 'no') + ', ' +
      'status='     +(a.s ? 'yes' : 'no') + ', ' +
      'dependent='  +(a.d ? 'yes' : 'no') + ', ' +
      'width='+w+', height='+h+', left='+x+', top='+y;
    win = window.open(href,name,params);
  }          
  else{      
    if(href) win.location.href=href;
  } 
  win.focus();
  extWindows[name]= win;

  return retval;
}



function parseParamString(str){
  var ret={};
  if(!str) return ret;
  
  for(var i=0; i<str.length; i++) {
    ret[str[i]]=true;
  }
  return ret; 
}



// function popup(me,href,w ,h ,x ,y ,appearance){
function popup(p1,p2,p3,p4,p5,p6,p7){
  if(typeof p1=='object' && p1.href){
    var win= p1.target ? p1.target : "popup";
    var href= p1.href;
    var w= p2;
    var h= p3;
    var x= p4;
    var y= p5;
    var appearance= p6 ? p6 : (p1.attributes.appearance ? p1.attributes.appearance.value : 'sr');
    
  }
  else{
    var win= p1 ? p1 : "popup";
    var href= p2;
    var w= p3;
    var h= p4;
    var x= p5;
    var y= p6;
    var appearance= p7 ? p7 : 'sr';
  }
  var nam= win; // me.name ? me.name : "popup";
  return extWindow(win,nam,href,w,h,x,y,appearance);
}





/*- _runtime -*/

function dl(url, callback){
  if(url===true){
    var ret= [];
    var scripts= document.getElementsByTagName('script');
    for(var i=0; i<scripts.length; i++){
      if(scripts[i].src) ret.push( dl( scripts[i].src +'?'+ (new Date()).getTime() ) );
    }
    return ret;
  }
  if(url || (url=prompt('load script file:')) ){
    var f= document.createElement('script');
    f.setAttribute('type','text/javascript');
    if(callback){
      f.onload= callback;
    }
    f.setAttribute('src',url);
    return document.getElementsByTagName('head')[0].appendChild(f);
  }
  return false;  
}


