function element_addClassName(obj, className) {
  return _element_addClassName(obj, className, ' ');
};


function _element_addClassName(obj,className, offset){
  obj = element_isObject(obj);
  
  if(typeof offset == 'undefined') offset = '';
  obj.className+=offset+className;

  return true;
}

function element_removeClassName(obj,className,offset){
  obj = element_isObject(obj);

  if(typeof offset == 'undefined') offset = '';
    
  obj.className = str_replace(offset+className, '', obj.className);  
  
  return true;

}

function element_replaceClassName(obj,classNameSearch,classNameReplace){
  obj = element_isObject(obj);
  
  obj.className = str_replace(classNameSearch, classNameReplace, obj.className);
  
  
  return true;

}

function element_getZIndex(obj,value){
  obj = element_isObject(obj);
      
  if(obj.style.zIndex != null) {
    return obj.style.zIndex;
  }
  
  return true; 
}

function element_setZIndex(obj,value){
  obj = element_isObject(obj);
      
  if(obj.style.zIndex != null) {
    obj.style.zIndex  = value;
    return true;
  }
  
  return true; 
}

function element_get(type,res){
  var obj;
  
  if(document.getElementById) {
    
    if(type=='id') obj = document.getElementById(res);
    
  } else if(document.all) {

    if(type=='id') obj = document.all[res];
  }
  
  return obj;
}


function element_move(obj,vleft,vtop){
  obj = element_isObject(obj);
  
  var size = element_getSize(obj);

  vleft = element_recalcValue2px(vleft,size.width);
  vtop  = element_recalcValue2px(vtop,size.height);
  vleft = parseInt(vleft);
  vtop  = parseInt(vtop);
  
  obj.style.position = 'absolute';
  obj.style.left  = vleft.toString() + "px";
  obj.style.top = vtop.toString() + "px";

  return true;
}

function element_setVisibility(obj,status){
  obj = element_isObject(obj);

  obj.style.visibility = status;
  
  return true;
}

function element_setDisplay(obj,status){
  obj = element_isObject(obj);

  obj.style.display = status;
  
  return true;

}


function element_getAbsolutePositionX(obj, id){
  
  obj = element_isObject(obj);
  
  if(_ALVINE_NS) return obj.pageX;

  var left = parseInt(obj.offsetLeft);
  
  if(!id) id = "noid"; // dummywert!
  if(obj.id==id) return left;
  
  if(obj.offsetParent!=null && obj.offsetParent.tagName!="HTML") {
    left += element_getPositionX(obj.offsetParent);
  };
  return left;
  
}

function element_getAbsolutePositionY(obj, id){
  
  obj = element_isObject(obj);
  
  if(_ALVINE_NS) return obj.pageY;
   
  var top = parseInt(obj.offsetTop); 
  
  if(!id) id = "noid"; // dummywert!
  if(obj.id==id) return top;
  
  if(obj.offsetParent!=null && obj.offsetParent.tagName!="HTML") {
    top += element_getPositionY(obj.offsetParent);
  };
  
  // to: add padding
  
  return top;
  
}

function element_getPositionX(obj){  
  
  obj = element_isObject(obj);

  if(typeof obj.style.position != 'undefined'){
    if(obj.style.position == 'absolute' && typeof obj.style.left !='undefined'){
      return parseInt(obj.style.left);    
    }
  }
  
  if(typeof obj.offsetLeft =='number'){
    return parseInt(obj.offsetLeft);    
  }
  
  if(typeof obj.clientLeft =='number'){
    return parseInt(obj.clientLeft);
  }

  if(typeof obj.layerX =='number'){
    return parseInt(obj.layerX);
  }
  
  return false;
  
}

function element_getPositionY(obj){
  
  obj = element_isObject(obj);
  
  if(typeof obj.style.position != 'undefined'){
    if(obj.style.position == 'absolute' && typeof obj.style.top != 'undefined'){
      return parseInt(obj.style.top);
    }
  }
  
  if(typeof obj.offsetTop =='number'){
    return parseInt(obj.offsetTop);
  }
  
  if(typeof obj.clientTop =='number'){
    return parseInt(obj.clientTop);
  }

  if(typeof obj.layerY =='number'){
    return parseInt(obj.layerY);
  }
  
  return false;
  
}

function element_getPosition(obj, absolute, id){
  obj = element_isObject(obj); 
  
  if(!absolute){  
    var xpos = element_getPositionX(obj);
    var ypos = element_getPositionY(obj);
  } else {
    var xpos = element_getAbsolutePositionX(obj, id);
    var ypos = element_getAbsolutePositionY(obj, id);
  }
    
  return {x:xpos,y:ypos};
  
}


function element_getSize(obj){
  
  obj = element_isObject(obj);
  
  var ret = new Object();
  ret['width']  = element_getWidth(obj);    
  ret['height'] = element_getHeight(obj);

  return ret;
}



function element_getWidth(obj){  
  obj = element_isObject(obj);
  
  if(typeof obj.style.width != 'undefined' && obj.style.width != '' && obj.style.width!='auto') return parseInt(obj.style.width);  
  
  if(typeof obj.offsetWidth != 'undefined') return parseInt(obj.offsetWidth);
  
  if(typeof obj.clientWidth != 'undefined') return parseInt(obj.clientWidth);
  
  if(typeof obj.width != 'undefined') return parseInt(obj.width);
  
  if(typeof obj.naturalWidth != 'undefined') return parseInt(obj.naturalWidth);
  
  if(typeof document.all != 'undefined'){
    if(typeof document.all[obj.id].offsetWidth != 'undefined' && document.all[obj.id].offsetWidth != ''){
      return parseInt(document.all[obj.id].offsetWidth);
    }
  }
  
  return false;
}

function element_setWidth(obj,width){
  
  obj = element_isObject(obj);
  obj.style.overflow = 'hidden';
  obj.style.width = (width!='auto')?parseInt(width).toString()+'px':width;
  
  return true;
}


function element_getHeight(obj){
  obj = element_isObject(obj);

  if(typeof obj.style.height != 'undefined' && obj.style.height != '' && obj.style.height!='auto') return parseInt(obj.style.height);

  if(typeof obj.offsetHeight != 'undefined' && obj.offsetHeight != '') return parseInt(obj.offsetHeight);
  
  if(typeof obj.clientHeight != 'undefined' && obj.clientHeight != '') return parseInt(obj.clientHeight);
  
  if(typeof obj.height != 'undefined' && obj.height != '') return parseInt(obj.height);
 
  if(typeof obj.naturalHeight != 'undefined' && obj.naturalHeight != '') return parseInt(obj.naturalHeight);
  
  if(typeof document.all != 'undefined'){
    if(typeof document.all[obj.id].offsetHeight != 'undefined' && document.all[obj.id].offsetHeight != ''){
      return parseInt(document.all[obj.id].offsetHeight);
    }
  }
  
  return false;
}

function element_setHeight(obj,height){
 
  obj = element_isObject(obj);
  obj.style.overflow = 'hidden';
  obj.style.height = (height!='auto')?parseInt(height).toString()+'px':height;
  
  return true;
}
 
function element_setSize(obj,width,height){
  obj = element_isObject(obj);
  
  element_setWidth(obj, width);
  element_setHeight(obj, height);
  
  return true;
}

function element_setBackground(obj,background){

  obj = element_isObject(obj);

  obj.style.background = background;
  
  return true;
}


function element_setOpacity(obj,value){
  obj = element_isObject(obj);
  
  value = parseInt(value);
  value = value>100?100:parseInt(value);
  value = value<0?0:parseInt(value);

  if(typeof obj.style.opacity != 'undefined') {
    obj.style.opacity  = value/100;
    return true;
  }
    
  if(typeof obj.style.MozOpacity != 'undefined') {
    obj.style.MozOpacity = value/100;  
    return true;
  }

  if(typeof obj.filters != 'undefined') { 

    if(obj.style.filter == ''){     
      obj.style.filter = 'alpha(opacity='+value.toString()+')';
    }
    
    obj.filters['alpha']['opacity'] = parseInt(value);

    return true;
  }

  if(typeof obj.style.KhtmlOpacity != 'undefined') {
    obj.style.KhtmlOpacity  = value/100;
    return true;
  } 
  
  return true; 
}

function element_getOpacity(obj){
  obj = element_isObject(obj);

  if(obj.style.opacity != null) return parseInt(obj.style.opacity*100);
  
  if(obj.style.MozOpacity != null) return parseInt(obj.style.MozOpacity*100);  
  
  if(typeof obj.filters != 'undefined') {    
    if(obj.style.filter != ''){
      return parseInt(obj.filters['alpha']['opacity']);
    }
  }
    
  if(obj.style.KhtmlOpacity != 'undefined') return parseInt(obj.style.KhtmlOpacity*100);

  //wird nichts gefunden, wird angenommen, das Element sei komplett durchsichtig
  return 0;
}

var savedUniqueIDs = new Object();
function element_buildUniqueObjectID(object){
  var ret = 0;
  
  if(typeof object.sourceIndex != 'undefined') return object.sourceIndex;  

  //Fierfox doesn't know "sourceIndex"
  var id = null;
  if(typeof object.innerText != 'undefined') ret+= object.innerText;
  if(typeof object.outerText != 'undefined') ret+= object.outerText;
  if(typeof object.innerHTML != 'undefined') ret+= object.innerHTML;
  if(typeof object.childNodes != 'undefined') ret+= object.childNodes.length;
  if(typeof object.tagName != 'undefined') ret+= object.tagName;
  if(typeof object.id != 'undefined') id = object.id;
  var src = null;
  if(typeof object.src != 'undefined') {
    src = object.src;
    ret+= src;
    
    var parts = src.split('/');
    src = parts[parts.length-1];
    src = src.replace(/\./g, '');
  }
  
  ret = id + ret + src;

  var timestamp = time_getMilliTimestamp();
  
  var randomNr = Math.random();
  randomNr *= 1000;
  randomNr = Math.ceil(randomNr);
  
  var uid = 'a'+ret.toString() + randomNr.toString() + timestamp.toString();
  if(typeof savedUniqueIDs[uid] != 'undefined') uid = parseInt(uid)+1;
  
  savedUniqueIDs[uid] = uid.toString(); 
  return uid.toString();
}





function element_isObject(mixed){
  var ret = true;
  
  if (typeof(mixed)!="object"){
    mixed = element_get('id',mixed);
    if (typeof(mixed)=="object") return mixed;
    return false;
  } else {
    return mixed;  
  }
}

/*
 * recalculate px-values and percentage-values
 */
function element_recalcValue2px(value,val100percent){
  if(typeof value == 'undefined') return null;
  if(value == null) return null;

  if(value.toString().substring((value.toString().length-1))=='%'){  //Umrechnung bei %-Angabe
    return parseInt(((val100percent/100) * parseInt(value)));
  }
  
  if(value.toString().substring((value.toString().length-2),2)=='px'){  //Umwandlung bei px-Angabe
    return parseInt(value);
  }

  return parseInt(value);
}
