/**
 * event.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/
 */

/*usage:
 * addEvent(object,'mousemove',function);
 * removeEvent(object,'mousemove',function);
 * 
 * important: Function must be given as reference(without quotes!)
*/

function event_add( obj, type, fn, useCapture){
	obj = element_isObject(obj);
  if(!obj) return false;

  //if fn is a string, wrap around a FunctionObject
  if(typeof fn == 'string'){
    var str = fn;
    fn = function(){
      eval(str);
    };
  }

  var ietype = type;
	if(type.indexOf('on')>-1) {
		type = type.substring(2);
	} else{
	 	ietype = 'on'+type;
	}
  
  if (obj.addEventListener) {
    useCapture = (useCapture===true)?true:false;
    obj.addEventListener( type, fn, useCapture );
  } else if (obj.attachEvent) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function() { obj['e'+type+fn]( window.event ); }
    obj.attachEvent( ietype, obj[type+fn] );
  }
  
  return true;
  
}

function event_remove( obj, type, fn ){
  obj = element_isObject(obj);
  if(!obj) return false;
  
  //if fn is a string, wrap around a FunctionObject
  if(typeof fn == 'string'){
    var str = fn;
    fn = function(){
      eval(str);
    };
  }
  
  var ietype = type;
	if(type.indexOf('on')>-1) {
	 	type = type.substring(2);
	} else{
	 	ietype = 'on'+type;
	}
	
  if (obj.removeEventListener) {
    obj.removeEventListener( type, fn, false );
  } else if (obj.detachEvent) {
    obj.detachEvent( ietype, obj[type+fn] );
    obj[type+fn] = null;
    obj['e'+type+fn] = null;
  }
  
  return true;
}