// alert('load std_funx');

/*- _string methods -*/
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[i];
  }
  return ret;
}



/*- _debug stuff -*/

function trace(msg,mode,target){
//  return false;
  if(!target){
    var target=document.getElementById('trace');
  }
  if(!target) return false;
  
  if(mode=='overwrite'){
    target.innerHTML = msg;
  }
  else if(mode=='asc'){
    target.innerHTML+= msg + '\n';
  }
  else{
    target.innerHTML= msg + '\n' + target.innerHTML;
  }
}



function dump(me,recursive,depth,noFunx){
  if(typeof me == 'false') me=this;
  if(typeof depth != 'number') depth=0;
  if(typeof recursive != 'number') recursive=0; // 0:all, int>0:levels of rec, int<0:none
  var ret='';
  for(var i in me){
    if(typeof me[i]=='function'){ 
      ret+= noFunx ? '' : '  '.repeat(depth) + i + ' = '+ String(me[i]).split('\n').shift() +'\n';
    }
    else{
      ret+= '  '.repeat(depth) + i + ' = (' + typeof me[i] +') ';
      if(typeof me[i]=='object' && (recursive<0 || depth<recursive)){
        recuret='';
        try{
          recuret= dump(me[i],recursive,depth+1)
        }
        catch(e){}
        ret+='Array {\n' + recuret + '  '.repeat(depth) +'}\n';
      }
      else{
        try{
          ret+= String(me[i]) + '\n';
        }
        catch(e){}
      }
    }
  }
  return ret;   
}
// Object.prototype.dump = dump;




/*- _DOM stuff -*/
var htmlNodeProto= typeof HTMLElement != 'undefined' ? HTMLElement.prototype : Object.prototype;


function getChildsByAttribute(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;






function addClass(me,clas){
  if(!me.tagName){
    clas= me;
    me= this;
  }
  if(me.className){
    if( !me.className.match( new RegExp('\\b'+clas+'\\b') ) ){
      me.className+= " "+ clas;
    }
  }
  else{
    me.className= clas;
  }
  return me.className;
}
htmlNodeProto.addClass= addClass;



function rmClass(me,clas){
//  alert( 'rmc: ('+me+', '+clas+')' );
  if(!me.tagName){
    clas= me;
    me= this;
  }
  re= new RegExp('\\b'+clas+'\\b','g');
  me.className= me.className.replace(re,'');
  return me.className;
}
htmlNodeProto.rmClass= rmClass;





function hover(){
  addClass(this,'hover');
}
htmlNodeProto.hover= hover;

function unhover(){
  rmClass(this,'hover');
}
htmlNodeProto.unhover= unhover;




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



/*- event stuff -*/

function addListener(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;

function rmListener(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;






/*- _so stuff -*/

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,w,h,x,y,appearance){
  var win= me.target ? me.target : "popup";
  var nam= win; // me.name ? me.name : "popup";
  var a = appearance ? appearance : ((me && me.attributes && me.attributes.appearance) ? me.attributes.appearance.value : 'sr' );

  return extWindow(win,nam,me.href,(w ? parseInt(w) : null),(h ? parseInt(h) : null),x,y,a);
//  return false;
}


