/**
 * element.lib.js - insert headline here
 *
 * COPYRIGHT: All  title   and  proprietary  rights,  including  trade
 * secrets,   in   the   Software   and   any   copies thereof and the
 * accompanying  written   materials,   are  owned  by   schukai  GmbH
 * and  are  protected  by  German  copyright  laws,  other applicable
 * copyright   laws  and  international  treaty  provisions.
 *
 * @package    alvine
 * @author     schukai GmbH <info@schukai.de>
 * @copyright  Copyright (C) 2002, 2003, 2004, 2005, 2006 schukai GmbH
 * @license    http://www.alvine.de/license/
 * @version    20061114
 * @link       http://www.alvine.de/
 */

//PAGE
function page_onLoad(fn){
  obj = window;

  if(typeof event_add == 'undefined') return false;

  event_add(obj, 'load', fn);
  
  return true;
}


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

//Class


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


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

  return true;
}

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

  if(!obj) return false;
  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;

}

//Position

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);
  
  element_setLeft(obj, vleft);
  element_setTop(obj, vtop);
                          
  return true;
}

function element_center(obj){
  obj = element_isObject(obj);
  
  element_centerX(obj);
  element_centerY(obj);
  
  
  return true;
}

function element_centerX(obj){
  obj = element_isObject(obj);
  
  var viewPort   = alvine_getViewport();

  var size = element_getSize(obj);
  
  var parentCenter = new Object();
  var objectCenter = new Object();
  
  parentCenter.x = parseInt((viewPort.width/2));
  
  objectCenter.x = parseInt((size.width/2));
  
  var newPosition = new Object();
  
  newPosition.x = parentCenter.x - objectCenter.x;
  
  var currentPositionY = element_getPositionY(obj);
  
  element_move(obj, newPosition.x, currentPositionY);
  
  return true;
}

function element_centerY(obj){
  obj = element_isObject(obj);
  
  var viewPort   = alvine_getViewport();

  var size = element_getSize(obj);
  
  var parentCenter = new Object();
  var objectCenter = new Object();
  
  parentCenter.y = parseInt((viewPort.height/2));
  
  objectCenter.y = parseInt((size.height/2));
  
  var newPosition = new Object();
  
  newPosition.y = parentCenter.y - objectCenter.y;
  
  var currentPositionX = element_getPositionX(obj);
  
  element_move(obj, currentPositionX, newPosition.y);
  
  return true;
}

function element_getAbsolutePositionX(obj, id){
  
  obj = element_isObject(obj);
  
  var left = element_getPositionX(obj);

  if(_ALVINE_NS) return left;  
  if(!id) id = "noid"; // dummywert!  
  
  var parentObj = obj.offsetParent;  
  while(parentObj != null && typeof parentObj != 'undefined' && parentObj.tagName!="HTML") {    
    left += element_getPositionX(parentObj);
    if(parentObj.id==id) return left;
    parentObj = parentObj.offsetParent;
  }

  return left;   
  
}

function element_getAbsolutePositionY(obj, id){
  
  obj = element_isObject(obj);
   
  var top = element_getPositionY(obj); 
  if(_ALVINE_NS) return top;
  
  if(!id) id = "noid"; // dummywert!
  
  var parentObj = obj.offsetParent;
  while(parentObj != null && typeof parentObj != 'undefined' && parentObj.tagName!="HTML") {    
    top += element_getPositionY(parentObj);
    if(parentObj.id==id) return top;
    parentObj = parentObj.offsetParent;
  }
  
  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);
  }
  
  if(typeof obj.pageX =='number'){
    return parseInt(obj.pageX);
  }
  
  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);
  }
  
  if(typeof obj.pageY =='number'){
    return parseInt(obj.pageY);
  }
  
  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_setLeft(obj, vleft){
  obj = element_isObject(obj);

  obj.style.position = (obj.style.position!='fixed' && obj.style.position!='absolute')?'absolute':obj.style.position;
  obj.style.left  = vleft + "px";

  return true;
}

function element_setTop(obj, vtop){
  obj = element_isObject(obj);
  
  obj.style.position = (obj.style.position!='fixed' && obj.style.position!='absolute')?'absolute':obj.style.position;
  obj.style.top  = vtop + "px";

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

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

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

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

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

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

  return (obj.style.visibility=='visible')?true:false;

}

function element_resetOpacity(obj){
  
  obj = element_isObject(obj)
  
  if(isDefined(obj.style.MozOpacity)) {
    obj.style.MozOpacity = 1; 
    return true;
  }

  if(isDefined(obj.style.opacity)) {
    obj.style.opacity  = 1;
    return true;
  }

  if(isDefined(obj.filters)) {
    if(!isDefined(obj.filters.alpha)) {
      obj.style.filter = 'alpha(opacity=100)';
      return true;
    }
    obj.filters.alpha.opacity = 100;
    return true;
  }

  if(isDefined(obj.style.KhtmlOpacity)) {
    obj.style.KhtmlOpacity  = 1;
    return true;
  }  
}

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

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

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

  obj.style.filter = 'alpha(opacity='+value.toString()+')';
    
    if(!isDefined(obj.filters.alpha.opacity)){     
      obj.style.filter = 'alpha(opacity='+value.toString()+')';
    }

    obj.style.overflow = 'hidden'; 
    obj.filters.alpha.opacity = value.toString();
  }

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

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

  if(isDefined(obj.style.MozOpacity) && obj.style.MozOpacity != '') return parseInt(obj.style.MozOpacity*100);	
  
  if(isDefined(obj.style.opacity) && obj.style.opacity != '') return parseInt(obj.style.opacity*100);
	
  if(isDefined(obj.filters) && obj.filters != '') return parseInt(obj.filters.alpha.opacity);
  
  if(isDefined(obj.style.KhtmlOpacity) && obj.style.KhtmlOpacity != '') return parseInt(obj.style.KhtmlOpacity*100);

	return 100;
}

//Size

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(!obj) return null;
   
  switch(obj.tagName){
    case 'TR':
      //ask tbody
      return element_getWidth(obj.parentNode);
    break;
    default:
    break;
  }

  //if(typeof obj.style.posWidth != 'undefined') return parseInt(obj.style.posWidth);

  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);
    }
  }
  
  if(typeof obj.style.maxWidth != 'undefined' && obj.style.maxWidth != '') return parseInt(obj.style.maxWidth);
  if(typeof obj.style.minWidth != 'undefined' && obj.style.minWidth != '') return parseInt(obj.style.minWidth);
  
  return false;
}
function element_getHeight(obj){
  obj = element_isObject(obj);
  
  if(!obj) return null;
  
  switch(obj.tagName){
    case 'TR':
      var tdObjList = obj.getElementsByTagName('TD');
      if(typeof tdObjList[0] == 'undefined') return false;
      var height = 0;
      for(var i in tdObjList){
        if(typeof tdObjList[i].tagName == 'undefined') continue;
        if(tdObjList[i].tagName!='TD') continue;
        
        var tmpHeight = element_getHeight(tdObjList[i]);
        if(tmpHeight<=height) continue;
        
        height = tmpHeight;
      }            
      return height;
    break;
    
    case 'TD':
      var height = 0;

      for(var i in obj.childNodes){
        if(typeof obj.childNodes[i].tagName == 'undefined') continue;
        height+= element_getHeight(obj.childNodes[i]);
      }            
      return height;
    break;
    
  }

  //if(typeof obj.style.posHeight != 'undefined') return parseInt(obj.style.posHeight);

  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);
    }
  }
  
  if(typeof obj.style.maxHeight != 'undefined' && obj.style.maxHeight != '') return parseInt(obj.style.maxHeight);
  if(typeof obj.style.minHeight != 'undefined' && obj.style.minHeight != '') return parseInt(obj.style.minHeight);
  
  return false;
}

function element_setWidth(obj,width,fixOverflow){
  
  obj = element_isObject(obj);
  
  if(!obj) return null;
  
  fixOverflow = (fixOverflow===false)?false:true;
  
  var inputWidth = width;
  width = (width!='auto')?parseInt(width).toString()+'px':width;
  if(inputWidth.toString().substr(-1)=='%') width = inputWidth;
  
  switch(obj.tagName){
    case 'TR':
      var tdList = obj.getElementsByTagName('TD');
      if(tdList.length==0) return false;
      for(var i in tdList){
        element_setWidth(tdList[i], inputWidth);
      }
      
      return true;
    break;    
  }
  
  if(fixOverflow) obj.style.overflow = 'hidden';
  obj.style.width = width;
  
  return true;
}

function element_setHeight(obj,height,fixOverflow){

  obj = element_isObject(obj);
  
  if(!obj) return null;
  
  fixOverflow = (fixOverflow===false)?false:true;
  
  var inputHeight = height;
  height = (height!='auto')?parseInt(height).toString()+'px':height;
  if(inputHeight.toString().substr(-1)=='%') height = inputHeight;
  
  switch(obj.tagName){
    case 'TR':
      var tdList = obj.getElementsByTagName('TD');
      if(tdList.length==0) return false;
      for(var i in tdList){
        element_setHeight(tdList[i], inputHeight);
      }
      
      return true;
    break;
  }

  if(fixOverflow) obj.style.overflow = 'hidden';
  obj.style.height = height;
  
  return true;
}

function element_setSize(obj,width,height,fixOverflow){
  obj = element_isObject(obj);
  
  fixOverflow = (fixOverflow===false)?false:true;
  
  element_setWidth(obj, width, fixOverflow);
  element_setHeight(obj, height, fixOverflow);
  
  return true;
}

function element_resetSize(obj){
  obj = element_isObject(obj);
  
  
  element_resetWidth(obj);
  element_resetHeight(obj);
  
  return true;
}

function element_resetWidth(obj){
  obj = element_isObject(obj);
  
  element_setWidth(obj, 'auto');
  
  return true;
}

function element_resetHeight(obj){
  obj = element_isObject(obj);
  
  element_setHeight(obj, 'auto');
  
  return true;
}


//Layout
function element_setBackground(obj,background){

  obj = element_isObject(obj);

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

//Content
function element_setText(obj, newString){
  obj = element_isObject(obj);
  
  if(!obj) return false;
  
  if(isDefined(obj.text))        obj.text        = newString;
  if(isDefined(obj.innerText))   obj.innerText   = newString;
  if(isDefined(obj.textContent)) obj.textContent = newString;
  if(isDefined(obj.innerHTML))   obj.innerHTML   = newString;
  
  return true;

}


function element_getText(obj){
  obj = element_isObject(obj);
  
  if(!obj) return false;
  
  if(isDefined(obj.text))        return obj.text;
  if(isDefined(obj.innerText))   return obj.innerText;
  if(isDefined(obj.textContent)) return obj.textContent;
  if(isDefined(obj.innerHTML))   return obj.innerHTML;
  
  return true;

}

//Children
function element_removeChildNodes(obj, childNode){
  obj = element_isObject(obj);
  
  var removeAll = (!isDefined(childNode) || isNull(childNode))?true:false;
  
  if(!removeAll){
  obj.removeChildNode(childNode);
  } else {
    for(var i=0; i<obj.childNodes.length; i++) {
      var removeChild = obj.childNodes[i]
      obj.removeChild(removeChild);
    }
  }
  
  return true;
}

//calc
function element_calcDifference(value1,value2){

  value1 = parseInt(value1);
  value2 = parseInt(value2);

	var retdiff = value1 - value2;

	//consider which direction to go
	var retdirection = 1;
	if (value1 < value2) retdirection = -1;
	
  retdiff = retdirection * parseInt(retdiff);
  
  return {difference:retdiff,direction:retdirection};
}

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


//Check

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

function element_isNumber(value){
  if(parseInt(value) != value) return false;
  
  return true;
}

function isNull(val){
  return(val==null);
}

function isDefined(mixed){
  if(typeof mixed == 'undefined') return false;

  return true;
}

